exports.id = 'showdb';
exports.title = 'Show db data';
exports.group = 'Worksys';
exports.color = '#888600';
exports.version = '1.0.2';
exports.icon = 'sign-out';
exports.input = 8;
exports.output = 1;

const { exec } = require('child_process');

exports.install = function(instance) {

  instance.on("0", _ => {
    instance.send(0, FLOW.GLOBALS.settings);
  })
  instance.on("1", _ => {
    instance.send(0, FLOW.GLOBALS.relaysData);
  })
  instance.on("2", _ => {
    instance.send(0, FLOW.GLOBALS.nodesData);
  })
  instance.on("3", _ => {
    instance.send(0, FLOW.GLOBALS.pinsData);
  })
  instance.on("4", _ => {
    instance.send(0, { rpcSwitchOffLine, rpcSetNodeDimming, rpcLineProfile, rpcNodeProfile, sunCalcExample, dataFromTerminalBroadcast })
  })
  instance.on("5", _ => {
    exec("sudo tail -n 25 monitor.txt", (err, stdout, stderr) => {
      if (err || stderr) instance.send(0, { err, stderr });
      else instance.send(0, stdout);
    })
  })
  instance.on("6", _ => {
    exec("sudo tail -n 25 err.txt", (err, stdout, stderr) => {
      if (err || stderr) instance.send(0, { err, stderr });
      else instance.send(0, stdout);
    })
  })
  instance.on("7", _ => {
    instance.send(0, FLOW.deviceStatus);
  })
};


const rpcSwitchOffLine =
{
  "topic": "v1/gateway/rpc",
  "content": {
    "device": "jbN4q7JPZmexgdnz2yKbWGDYAWwO0Q3BMX6ERLoV",
    "data": {
      "id": 8,
      "method": "set_command",
      "params": {
        "entities": [
          {
            "entity_type": "edb_line",
            "tb_name": "MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O"
          }
        ],
        "command": "switch",
        "payload": {
          "value": false
        }
      }
    }
  }
}

const rpcSetNodeDimming =
{
  "topic": "v1/gateway/rpc",
  "content": {
    "device": "jbN4q7JPZmexgdnz2yKbWGDYAWwO0Q3BMX6ERLoV",
    "data": {
      "id": 10,
      "method": "set_command",
      "params": {
        "entities": [
          {
            "entity_type": "street_luminaire",
            "tb_name": "jbN4q7JPZmexgdnz2yKbWdDYAWwO0Q3BMX6ERLoV"
          }
        ],
        "command": "dimming",
        "payload": {
          "value": 5
        }
      }
    }
  }
}

const rpcLineProfile =
{
  "topic": "v1/gateway/rpc",
  "content": {
    "device": "jbN4q7JPZmexgdnz2yKbWGDYAWwO0Q3BMX6ERLoV",
    "data": {
      "id": 9,
      "method": "set_profile",
      "params": {
        "entities": [
          {
            "entity_type": "edb_line",
            "tb_name": "MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O"
          }
        ],
        "payload": {
          "intervals": [
            {
              "value": 0,
              "end_time": "20:00",
              "start_time": "13:00"
            },
            {
              "value": 1,
              "end_time": "05:30",
              "start_time": "20:00"
            },
            {
              "value": 0,
              "end_time": "13:00",
              "start_time": "05:30"
            }
          ],
          "astro_clock": true,
          "dawn_lux_sensor": false,
          "dusk_lux_sensor": false,
          "dawn_lux_sensor_value": 5,
          "dusk_lux_sensor_value": 5,
          "dawn_astro_clock_offset": 0,
          "dusk_astro_clock_offset": 0,
          "dawn_lux_sensor_time_window": 30,
          "dusk_lux_sensor_time_window": 30,
          "dawn_astro_clock_time_window": 60,
          "dusk_astro_clock_time_window": 60
        }
      }
    }
  }
}


const rpcNodeProfile =
{
  "topic": "v1/gateway/rpc",
  "content": {
    "device": "jbN4q7JPZmexgdnz2yKbWGDYAWwO0Q3BMX6ERLoV",
    "data": {
      "id": 11,
      "method": "set_profile",
      "params": {
        "entities": [
          {
            "entity_type": "street_luminaire",
            "tb_name": "jbN4q7JPZmexgdnz2yKbWdDYAWwO0Q3BMX6ERLoV"
          }
        ],
        "payload": {
          "intervals": [
            {
              "cct": 3000,
              "value": 0,
              "end_time": "17:50",
              "start_time": "13:00"
            },
            {
              "cct": 3000,
              "value": 100,
              "end_time": "21:30",
              "start_time": "17:50"
            },
            {
              "cct": 3000,
              "value": 0,
              "end_time": "13:00",
              "start_time": "07:10"
            },
            {
              "cct": 3000,
              "value": 50,
              "end_time": "00:00",
              "start_time": "21:30"
            },
            {
              "cct": 3000,
              "value": 10,
              "end_time": "04:30",
              "start_time": "00:00"
            },
            {
              "cct": 3000,
              "value": 100,
              "end_time": "07:10",
              "start_time": "04:30"
            }
          ],
          "astro_clock": true,
          "dawn_lux_sensor": false,
          "dusk_lux_sensor": false,
          "dawn_lux_sensor_value": 5,
          "dusk_lux_sensor_value": 5,
          "dawn_astro_clock_offset": 30,
          "dusk_astro_clock_offset": 20,
          "dawn_lux_sensor_time_window": 30,
          "dusk_lux_sensor_time_window": 30,
          "dawn_astro_clock_time_window": 60,
          "dusk_astro_clock_time_window": 60
        }
      }
    }
  }
}

const sunCalcExample = {
  dusk_no_offset: '20:18',
  dawn_no_offset: '05:19',
  dusk: '20:18',
  dusk_hours: 20,
  dusk_minutes: 18,
  dawn: '05:19',
  dawn_hours: 5,
  dawn_minutes: 19,
  dusk_time: 1715278688962,
  dawn_time: 1715224744357,
  dusk_astro_clock_offset: 0,
  dawn_astro_clock_offset: 0
}


const dataFromTerminalBroadcast = {
  address: 4294967295,
  byte1: 0,
  byte2: 0,
  byte3: 0,
  byte4: 96,
  name: "Time Schedule settings",
  recipient: 2,
  register: 8,
  rw: 1
}
