[
  {
    "id": "acb0000000000001",
    "type": "tab",
    "label": "Algae Bloom Labs LoRaWAN",
    "disabled": false,
    "info": "DFRobot DFR1093-915 local SIoT to the existing sensor workbook."
  },
  {
    "id": "acb0000000000010",
    "type": "mqtt-broker",
    "name": "Gateway SIoT",
    "broker": "127.0.0.1",
    "port": "1883",
    "clientid": "",
    "autoConnect": true,
    "autoUnsubscribe": true,
    "usetls": false,
    "protocolVersion": "4",
    "keepalive": "60",
    "cleansession": true
  },
  {
    "id": "acb0000000000002",
    "type": "mqtt in",
    "z": "acb0000000000001",
    "name": "DFRobot raw uplinks",
    "topic": "siot/lora/+/raw",
    "qos": "1",
    "datatype": "auto",
    "broker": "acb0000000000010",
    "nl": false,
    "rap": true,
    "rh": 0,
    "inputs": 0,
    "x": 150,
    "y": 120,
    "wires": [
      [
        "acb0000000000003"
      ]
    ]
  },
  {
    "id": "acb0000000000003",
    "type": "change",
    "z": "acb0000000000001",
    "name": "Gateway settings",
    "rules": [
      {
        "t": "set",
        "p": "devEui",
        "pt": "msg",
        "to": "YOUR_16_DIGIT_DEVEUI",
        "tot": "str"
      },
      {
        "t": "set",
        "p": "gatewayConnection",
        "pt": "msg",
        "to": "YOUR_PRIVATE_GATEWAY_CONNECTION",
        "tot": "str"
      }
    ],
    "x": 370,
    "y": 120,
    "wires": [
      [
        "acb0000000000004"
      ]
    ]
  },
  {
    "id": "acb0000000000004",
    "type": "function",
    "z": "acb0000000000001",
    "name": "Prepare private upload",
    "func": "if (msg.retain) return null;\nconst topic = /^siot\\/lora\\/([a-f0-9]{16})\\/raw$/i.exec(msg.topic || \"\");\nif (!topic) return null;\nconst device = topic[1].toLowerCase();\nconst expected = String(msg.devEui || \"\").trim().toLowerCase();\nconst connection = String(msg.gatewayConnection || \"\").trim();\nconst match = /^(https:\\/\\/script\\.google\\.com\\/macros\\/s\\/[\\w-]+\\/exec)#([a-f0-9]{64})$/i.exec(connection);\nif (expected !== device || !match) {\n    if (!/^[a-f0-9]{16}$/.test(expected) || !match) {\n        node.status({fill:\"red\",shape:\"ring\",text:\"Set the DevEUI and workbook connection\"});\n    }\n    return null;\n}\nlet packet = msg.payload;\nfor (let depth = 0; depth < 3; depth++) {\n    if (Buffer.isBuffer(packet)) {\n        packet = packet.length === 30 && packet[0] === 0xa1 ? packet.toString(\"hex\") : packet.toString(\"utf8\");\n        continue;\n    }\n    if (typeof packet === \"object\" && packet) {\n        packet = packet.raw ?? packet.value ?? packet.msg ?? packet.message ?? packet.payload ?? packet.data;\n        continue;\n    }\n    if (typeof packet === \"string\") {\n        packet = packet.trim();\n        if (/^a1[a-f0-9]{58}$/i.test(packet)) break;\n        try { packet = JSON.parse(packet); } catch { break; }\n        continue;\n    }\n    break;\n}\nif (typeof packet !== \"string\" || !/^a1[a-f0-9]{58}$/i.test(packet)) {\n    node.status({fill:\"yellow\",shape:\"ring\",text:\"Waiting for a dashboard radio packet\"});\n    return null;\n}\nnode.status({fill:\"blue\",shape:\"dot\",text:\"Sending LoRaWAN reading\"});\nreturn {\n    url: match[1], headers: {\"Content-Type\":\"text/plain;charset=UTF-8\"},\n    payload: JSON.stringify({action:\"appendLoRaWAN\",key:match[2],device,packet,time:Date.now()})\n};",
    "outputs": 1,
    "x": 620,
    "y": 120,
    "wires": [
      [
        "acb0000000000005"
      ]
    ]
  },
  {
    "id": "acb0000000000005",
    "type": "http request",
    "z": "acb0000000000001",
    "name": "Send to Google Sheets",
    "method": "POST",
    "ret": "txt",
    "paytoqs": "ignore",
    "url": "",
    "persist": false,
    "senderr": false,
    "x": 190,
    "y": 210,
    "wires": [
      [
        "acb0000000000006"
      ]
    ]
  },
  {
    "id": "acb0000000000006",
    "type": "function",
    "z": "acb0000000000001",
    "name": "Show upload status",
    "func": "let result = msg.payload;\nif (typeof result === \"string\") {\n    try { result = JSON.parse(result); } catch { result = null; }\n}\nif (result?.ok === true && /^[a-f0-9]{16}$/i.test(result.accepted || \"\")) {\n    node.status({fill:\"green\",shape:\"dot\",text:\"Saved \" + new Date().toLocaleTimeString()});\n} else {\n    node.status({fill:\"red\",shape:\"ring\",text:\"Workbook rejected the upload. Check connection and authorization.\"});\n}\nreturn null;",
    "outputs": 1,
    "x": 445,
    "y": 210,
    "wires": [
      []
    ]
  }
]
