Action/Analysis to delete data by group id

Action/Analysis to delete data by group id

I have a sensor that reboots if it doesn't receive a response back from the network.  When it does reboot it sends all zeros for the data on fPort4.  When this happens it falsly triggers alerts.   I tried to write the payload parser to ignore data on fPort4 but for some reason I can't figure out it still parses the data.  My next idea was to create an action that when the fPort=4 it would trigger an analysis to just delete the data but I can't get that to work either.  If someone could take a look at the code and see what the issue is I would greatly appreciate it.
  1. const { Analysis, Device } = require("@tago-io/sdk");
    const { Utils } = require("@tago-io/sdk");

    async function monitorAndDelete(context) {
      // Load environment variables
      const envVars = Utils.envToJson(context.environment);
      context.log("Environment variables loaded:", JSON.stringify(envVars));

      const deviceToken = envVars.device_token;

      // Check if device token is set
      if (!deviceToken) {
        context.log("Error: Device token not found in environment variables.");
        return;
      }

      const device = new Device({ token: deviceToken });

      // Check if data is available in the context
      if (!context.data) {
        context.log("No data received in context.");
        return;
      }

      if (!Array.isArray(context.data)) {
        context.log("Data received but not in array format.");
        return;
      }

      // Iterate through all incoming data
      const groupsToDelete = new Set();
      for (const dataPoint of context.data) {
        if (!dataPoint.value || !dataPoint.group) {
          context.log(`Skipping data point without value or group: ${JSON.stringify(dataPoint)}`);
          continue;
        }

        try {
          const buffer = Buffer.from(dataPoint.value, "hex");
          const fPort = buffer[1]; // Adjust based on your payload structure

          if (fPort === 4) {
            context.log(`Marking group for deletion with Group ID: ${dataPoint.group}`);
            groupsToDelete.add(dataPoint.group);
          } else {
            context.log(`Data received on fPort ${fPort}, no action taken.`);
          }
        } catch (error) {
          context.log(`Error processing data point: ${error.message}`);
        }
      }

      // Delete all data points for marked groups
      if (groupsToDelete.size > 0) {
        try {
          for (const group of groupsToDelete) {
            context.log(`Deleting all data for Group ID: ${group}`);
            await device.deleteData({ groups: [group] });
            context.log(`Successfully deleted data for Group ID: ${group}`);
          }
        } catch (error) {
          context.log(`Error deleting data for groups: ${error.message}`);
        }
      } else {
        context.log("No groups marked for deletion.");
      }
    }

    module.exports = new Analysis(monitorAndDelete);