Access device name, config parameters and tags in a form

Hi everyone, I’m trying to create a form where I can view the Device name, inspect the config parameters and tags. Is this possible? And if so, could you direct me as to how. Thanks!

Hi Sevils,
For an Input Form, it doesn’t seem like a possibility.

If you want to extract this information to a Dynamic Table widget, you can do it using an Analysis.
You can get the TagoIO snippet “Get Device List” when creating a new analysis, that should be a good starting point for you.

What you want in this script is to get more fields from your device list.

const devices = await account.devices.list({
    page: 1,
    fields: ["id", "tags", "name"],
    // filter,
    amount: 100,
  });

Then you want to transform this information to a TagoIO data and insert in a device, which you can use in your Dynamic table widget.

const data_to_tago = devices.map((device) => {
        return [
            { variable: "dev_list_name", value: device.name, serie: device.id },
            { variable: "dev_list_tags", value: JSON.stringify(device.tags), serie: device.id }
        ];
}).reduce((final, dev_variables) => final.concat(dev_variables), []);

And for last, insert this information to a device for your widget. Usually you want to delete all the previous data stored for the variables you’re adding.

const device = new Device({ token: 'Your device-token' });
await device.deleteData({ variables: ["dev_list_name", "dev_list_tags"], qty: 10000 });
device.sendData(data_to_tago).then(context.log).catch(context.log)

Now all you need is to create a Dynamic Table widget, select this device and enter new columns with the variables dev_list_name and dev_list_tags.

Thanks for the update Vitor, that will allow the name etc is able to be viewed in the widget, but I need the data to be able to be modified and written back to the original device. Is this on the roadmap?
An example is a client who may relocate a device (or many in some cases), He would need to be able to change the name, and perhaps an offset parameter to get it calibrated correctly. Having to receive a phone call every time this is required becomes very time consuming.

Hi @sevils,
The intructions I sent you will only copy the devices you want to a Dynamic table.

If you want them to be able to change, for example, the name of the device, you will also need to write an analysis for that.

You can enable the user to edit the field through the Dynamic table, and it will trigger the analysis when this action is taken by the user.

In your analysis, you will need to check what was changed and apply to the device. Since you’re using the serie of the data as the device ID, it should be an easy analysis to make:

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

async function startAnalysis(context, scope) {
  if (!scope.length) return context.log('Analysis must be triggered by an widget');

  const env_vars = Utils.envToJson(context.environment);
  if (!env_vars.account_token) {
    return context.log('Missing account_token environment variable');
  }

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

  // get variable that was edited
  const device_name = scope.find(data => data.variable === 'device_name');

  // make sure we received the device_name variable and not other column.
  // when some data is edited through an widget, the analysis will receive the _widget_exec paramater in the environment variable.
  if (device_name && env_vars._widget_exec === 'edit') {
    // we're asssuming the serie is the device ID
    const device_id = device_name.serie;

    await account.devices.edit(device_id, { name: device_name.value });
  }

  // if you enable delete, you can delete devices as that:
  if (device_name && env_vars._widget_exec === 'delete') {
    // we're asssuming the serie is the device ID
    const device_id = device_name.serie;

    await account.devices.delete(device_id);
  }
}

module.exports = new Analysis(startAnalysis);

Ahh I see, that’s perfect, thanks for the explanation. I’ll work on this and hopefully get it working. Many thanks!