API request limitations

API request limitations

@Julihermes Melo

Hi.

I am doing a analysis and I have to use the function “Utils.getTokenByName” to get a device token. Worked fine some times, but stop works, after some days, worked fine some times and again stop works.

This API requests have any limitation or I missing something?

PS. No error messages appears, only not continue the script, I will put the code and prints to clarify.

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

async function copyToDummyDevice(account = new Account(), dummy_device = new Device(), device_id = '') {
    // Get the device token and instance device class.
    const device_token = await Utils.getTokenByName(account, device_id);
    const device = new Device({ token: device_token });

    // Fetch the last location on the device, result is an array, so [last_location] automatically access index 0 of the array.
    // variable we're trying to fetch is location.
    const [last_location] = await device.getData({ variables: ['location'], qty: 1 });
    if (!last_location) { // stop if no variable location was found in the device.
        return context.log("Missing variable location for this device");
    }

    // Delete any previous value for the variable location, with serie of the device_id
    await dummy_device.deleteData({ variables: 'location', series: [device_id] });

    // Insert the data location, with the serie being the device_id.
    dummy_device.sendData({
        variable: 'location',
        value: "test",
        location: last_location.location,
        serie: device_id,
    }); 
}

async function myAnalysis(context) {
  
  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 });

  // Fetch the list of devices filtered by tag key "device_type" and value "customer"
  const customer_devices = await account.devices.list({ fields: ['id', 'name', 'tags'], filter: { tags: [{ key: 'device_type', value: 'map' }] } });

  for (const customer_dummy_info of customer_devices) {
    // Check if there is any device with tag key "customer_id" with the value of the tag on the customer device.
    const devices_to_copy =  await account.devices.list({ fields: ['id', 'name', 'tags'], filter: { tags: [{ key: 'device_type', value: "pin" }] } });
    if (!devices_to_copy.length) {
      context.log(`No pin device found for customer ""`);
      continue;
    }

    // Get the dummy device token and instance device class.
    const dummy_device_token = await Utils.getTokenByName(account, customer_dummy_info.id);
    const dummy_device = new Device({ token: dummy_device_token });

    // Send each device to the function copyToDummyDevice.
    devices_to_copy.forEach((device) => {
      copyToDummyDevice(account, dummy_device, device.id);
    });
  }
}

module.exports = new Analysis(myAnalysis);