Edit Tags for a Specific Device

Edit Tags for a Specific Device

In the following code I am creating a device from a form on blueprint devices with their corresponding labels. But now what I would like is to be able to edit that data from the device labels.



const { Analysis, Account, Utils, Device } = require("@tago-io/sdk");

async function startAnalysis(context, scope) {
  if (!scope[0]) {
    return context.log("The analysis must be triggered by a widget.");
  }

  context.log("Creating your device");
  const env = Utils.envToJson(context.environment);
  if (!env.account_token) return context.log('Missing "account_token" environment variable');
  else if (env.account_token.length !== 36) return context.log('Invalid "account_token" in the environment variable');

  const account = new Account({ token: env.account_token });

  const dashboard_dev_token = await Utils.getTokenByName(account, scope[0].device);
  const dashboard_device = new Device({ token: dashboard_dev_token });

  const network_id = scope.find((x) => x.variable === "device_network");
  const connector_id = scope.find((x) => x.variable === "device_connector");
  const device_name = scope.find((x) => x.variable === "id_tienda");
  const nombre = scope.find((x) => x.variable === "nombre");
  const device_eui = scope.find((x) => x.variable === "id_tienda");
  const region = scope.find((x) => x.variable === "region");
  const zona = scope.find((x) => x.variable === "zona");
  const tamano = scope.find((x) => x.variable === "tamano");
  const status = scope.find((x) => x.variable === "status_operativo");
  const direccion = scope.find((x) => x.variable === "direccion");
  const email = scope.find((x) => x.variable === "email");
  const tienda_user = scope.find((x) => x.variable === "tienda_user");
  const solar_activa = scope.find((x) => x.variable === "solar_activa");
  const check_eolica = scope.find((x) => x.variable === "check_eolica");
  const entidad = scope.find((x) => x.variable === "entidad");
  const cp = scope.find((x) => x.variable === "cp");
  const gerente = scope.find((x) => x.variable === "gerente");
  const id_cont = scope.find((x) => x.variable === "id_cont");
  const carga = scope.find((x) => x.variable === "carga");
  const demanda = scope.find((x) => x.variable === "demanda");
  const no_medidor = scope.find((x) => x.variable === "no_medidor");
  const tarifa = scope.find((x) => x.variable === "tarifa");
  const multiplicador = scope.find((x) => x.variable === "multiplicador");
  const facturacion = scope.find((x) => x.variable === "facturacion");



  if (!connector_id || !connector_id.value) {
    return context.log('Missing "device_connector" in the data scope.');
  } else if (!network_id || !network_id.value) {
    return context.log('Missing "device_network" in the data scope.');
  } else if (!device_eui || !device_eui.value) {
    return context.log('Missing "device_eui" in the data scope.');
  }

  const result = await account.devices
    .create({
      name: device_name.value,
      serie_number: device_eui.value,
      tags: [
        { key: "CUENTA", value: "RETAIL" },
        { key: "TIENDA", value: nombre.value },
        { key: "device_eui", value: device_eui.value },
        { key: "REGION", value: region.value },
        { key: "ZONA", value: zona.value },
        { key: "TAMAÑO", value: tamano.value },
        { key: "ESTADO", value: status.value },
        { key: "DIRECCION", value: direccion.value },
        { key: "SOLAR", value: solar_activa ? solar_activa.value.toString() : '' },
        { key: "EOLICA", value: check_eolica ? check_eolica.value.toString() : '' },
      ],
      connector: connector_id.value,
      network: network_id.value,
      active: true,
      type: "mutable",
      chunk_period: "month",
      chunk_retention: 13,
    })
    .catch((error) => {
      dashboard_device.sendData({ variable: "validation", value: `Error when creating the device ${error}`, metadata: { color: "red" } });
      throw error;
    });