[
  {
    "id": "c584fd3b0f836a63",
    "type": "tab",
    "label": "Ingest Live Data from an External MQTT Broker",
    "disabled": false,
    "info": "",
    "env": []
  },
  {
    "id": "4890b9a0481f1f0f",
    "type": "mqtt in",
    "z": "c584fd3b0f836a63",
    "name": "",
    "topic": "source/gateway/fr-guide-gw-01",
    "qos": "1",
    "datatype": "auto-detect",
    "broker": "3f515ef451c125d1",
    "nl": false,
    "rap": true,
    "rh": 0,
    "inputs": 0,
    "x": 150,
    "y": 140,
    "wires": [
      [
        "2f923a09d52120c0"
      ]
    ]
  },
  {
    "id": "2f923a09d52120c0",
    "type": "function",
    "z": "c584fd3b0f836a63",
    "name": "Process data",
    "func": "let topic = msg.topic || \"\";\nlet parts = topic.split(\"/\");\n\n/*\n MQTT-IN can subscribe to either:\n source/+/+\n $share/fr-guide/source/+/+\n\n Publishers still publish to normal topics such as:\n source/gateway/fr-guide-gw-01\n source/hvacs/fr-guide-hvac-01\n\n Most brokers keep msg.topic as the original publish topic.\n This fallback handles brokers/nodes that expose $share in msg.topic.\n*/\nif (parts[0] === \"$share\") {\n    parts = parts.slice(2);\n}\n\nif (parts.length !== 3) {\n    node.warn(\"Ignoring unsupported topic: \" + msg.topic);\n    return null;\n}\n\nconst prefix = parts[0];\nconst deviceType = parts[1];\nconst externalId = parts[2];\n\nif (prefix !== \"source\") {\n    return null;\n}\n\nif (deviceType !== \"gateway\" && deviceType !== \"hvacs\") {\n    node.warn(\"Unsupported device type: \" + deviceType);\n    return null;\n}\n\nif (!externalId) {\n    return null;\n}\n\nif (typeof msg.payload === \"string\") {\n    msg.payload = JSON.parse(msg.payload);\n}\n\nmsg.payload = msg.payload || {};\nmsg.payload.time = msg.payload.time || Date.now() * 1000;\n\nif (deviceType === \"gateway\") {\n    msg.topic = \"data\";\n    msg.payload.cpuUtil = Number(msg.payload.cpuUtil || 30);\n    msg.payload.memUtil = Number(msg.payload.memUtil || 25);\n    msg.payload.diskUtil = Number(msg.payload.diskUtil || 20);\n    msg.payload.firmware = msg.payload.firmware || \"Oracle Linux 9.1\";\n} else {\n    msg.topic = \"hvacs/\" + externalId;\n    msg.payload.temperature = Number(msg.payload.temperature || 72);\n    msg.payload.humidity = Number(msg.payload.humidity || 45);\n    msg.payload.pressure = Number(msg.payload.pressure || 101.2);\n    msg.payload.mode = msg.payload.mode || \"cool\";\n}\n\nmsg.payload = JSON.stringify(msg.payload);\nreturn msg;",
    "outputs": 1,
    "timeout": 0,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 490,
    "y": 280,
    "wires": [
      [
        "dd983787c0f7a8d5",
        "0acc3212d4fb7b70"
      ]
    ]
  },
  {
    "id": "dd983787c0f7a8d5",
    "type": "mqtt out",
    "z": "c584fd3b0f836a63",
    "name": "MQTT out to gateway",
    "topic": "",
    "qos": "1",
    "retain": "",
    "respTopic": "",
    "contentType": "application/json",
    "userProps": "",
    "correl": "",
    "expiry": "",
    "broker": "78336d711de5e360",
    "x": 780,
    "y": 240,
    "wires": []
  },
  {
    "id": "0acc3212d4fb7b70",
    "type": "debug",
    "z": "c584fd3b0f836a63",
    "name": "MQTT Input debug",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "true",
    "targetType": "full",
    "statusVal": "",
    "statusType": "auto",
    "x": 690,
    "y": 400,
    "wires": []
  },
  {
    "id": "5a15b611183a05d2",
    "type": "mqtt in",
    "z": "c584fd3b0f836a63",
    "name": "",
    "topic": "source/hvacs/fr-guide-hvac-01",
    "qos": "1",
    "datatype": "auto-detect",
    "broker": "3f515ef451c125d1",
    "nl": false,
    "rap": true,
    "rh": 0,
    "inputs": 0,
    "x": 190,
    "y": 260,
    "wires": [
      [
        "2f923a09d52120c0"
      ]
    ]
  },
  {
    "id": "93a57e3dfd04ccdb",
    "type": "mqtt in",
    "z": "c584fd3b0f836a63",
    "name": "",
    "topic": "source/hvacs/fr-guide-hvac-02",
    "qos": "1",
    "datatype": "auto-detect",
    "broker": "3f515ef451c125d1",
    "nl": false,
    "rap": true,
    "rh": 0,
    "inputs": 0,
    "x": 190,
    "y": 420,
    "wires": [
      [
        "2f923a09d52120c0"
      ]
    ]
  },
  {
    "id": "3f515ef451c125d1",
    "type": "mqtt-broker",
    "name": "External MQTT Broker",
    "broker": "<external-broker-host>",
    "port": "8883",
    "tls": "",
    "clientid": "<unique-client-id>",
    "autoConnect": true,
    "usetls": true,
    "protocolVersion": "5",
    "keepalive": 60,
    "cleansession": true,
    "autoUnsubscribe": true,
    "birthTopic": "",
    "birthQos": "0",
    "birthRetain": "false",
    "birthPayload": "",
    "birthMsg": {},
    "closeTopic": "",
    "closeQos": "0",
    "closeRetain": "false",
    "closePayload": "",
    "closeMsg": {},
    "willTopic": "",
    "willQos": "0",
    "willRetain": "false",
    "willPayload": "",
    "willMsg": {},
    "userProps": "",
    "sessionExpiry": ""
  },
  {
    "id": "78336d711de5e360",
    "type": "mqtt-broker",
    "name": "IoT Device Host",
    "broker": "<device-host>",
    "port": "8883",
    "tls": "",
    "clientid": "",
    "autoConnect": true,
    "usetls": true,
    "protocolVersion": "5",
    "keepalive": 60,
    "cleansession": true,
    "autoUnsubscribe": true,
    "birthTopic": "",
    "birthQos": "0",
    "birthRetain": "false",
    "birthPayload": "",
    "birthMsg": {},
    "closeTopic": "",
    "closeQos": "0",
    "closeRetain": "false",
    "closePayload": "",
    "closeMsg": {},
    "willTopic": "",
    "willQos": "0",
    "willRetain": "false",
    "willPayload": "",
    "willMsg": {},
    "userProps": "",
    "sessionExpiry": ""
  }
]
