[
  {
    "id": "tulip_tab",
    "type": "tab",
    "label": "Tulip integration",
    "disabled": false,
    "info": "# Tulip <-> OV camera template\n\nEndpoints this flow adds (camera IP = the camera's address):\n\n| Call | What it does |\n| --- | --- |\n| POST http://CAMERA_IP/node-red/tulip/trigger | Fire an inspection. Optional body {\"serial\":\"...\",\"part\":\"...\"} is saved with the capture |\n| GET  http://CAMERA_IP/node-red/tulip/result | The latest inspection result, flattened for Tulip |\n| POST http://CAMERA_IP/node-red/tulip/recipe | Switch recipe. Body {\"id\": 3} |\n| POST http://CAMERA_IP/node-red/tulip/production | Production (live) mode on/off. Body {\"enabled\": true} |\n| GET  http://CAMERA_IP/node-red/tulip/status | Active recipe and production mode |\n| GET  http://CAMERA_IP/node-red/tulip/raw | Everything the camera reported for the last inspection (to find more fields) |\n\nMQTT (optional, section 5): every result is also published to\nov/<camera>/result. Configure the broker on the MQTT node, then deploy.\n\nRequirements\n- The recipe's trigger mode must be **Node-RED** (Recipe Editor > Imaging /\n  Triggering) or the Trigger node does nothing.\n- Tulip Cloud cannot reach a camera on the plant network by IP. Use a Tulip\n  Edge device or an On-Premise Connector Host on the same network.\n"
  },
  {
    "id": "c_trig",
    "type": "comment",
    "name": "1. TRIGGER  POST /node-red/tulip/trigger",
    "info": "Fires one inspection with the camera's Trigger node.\n\nOptional JSON body: any string or number fields (for example serial, part, operator). They are saved as Capture Metadata, so they show in the Library with the image and come back in /tulip/result.\n\nReplies 202 straight away; read the result with GET /tulip/result (or MQTT).\n\nThe recipe's trigger mode must be Node-RED.",
    "x": 200,
    "y": 60,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "trig_in",
    "type": "http in",
    "name": "",
    "url": "/tulip/trigger",
    "method": "post",
    "upload": false,
    "swaggerDoc": "",
    "x": 150,
    "y": 120,
    "wires": [
      [
        "trig_meta_fn",
        "trig_resp_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "trig_meta_fn",
    "type": "function",
    "name": "Body -> capture metadata",
    "func": "// Remember the address Tulip used, for image links in results\nif (msg.req && msg.req.headers && msg.req.headers.host) flow.set('cameraHost', msg.req.headers.host);\n// Keep only flat string/number fields: that is what Capture Metadata accepts\nconst body = (msg.payload && typeof msg.payload === 'object') ? msg.payload : {};\nconst meta = {};\nfor (const [k, v] of Object.entries(body)) {\n  if (typeof v === 'string' || typeof v === 'number') meta[k] = v;\n}\nflow.set('lastTriggerMeta', meta);\nflow.set('lastTriggerAt', new Date().toISOString());\nmsg.payload = meta;\nreturn msg;",
    "outputs": 1,
    "x": 400,
    "y": 120,
    "wires": [
      [
        "trig_meta",
        "trig_fire"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "trig_meta",
    "type": "capture-metadata",
    "name": "Capture Metadata",
    "color": "yellow",
    "x": 650,
    "y": 100,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "trig_fire",
    "type": "capture",
    "name": "Trigger",
    "x": 620,
    "y": 140,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "trig_link_in",
    "type": "link in",
    "name": "trigger from MQTT",
    "links": [
      "mq_trig_link_out"
    ],
    "x": 255,
    "y": 160,
    "wires": [
      [
        "trig_meta_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "trig_resp_fn",
    "type": "function",
    "name": "202 accepted",
    "func": "msg.statusCode = 202;\nmsg.payload = { accepted: true, at: new Date().toISOString(), result_url: '/node-red/tulip/result' };\nreturn msg;",
    "outputs": 1,
    "x": 400,
    "y": 180,
    "wires": [
      [
        "trig_resp"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "trig_resp",
    "type": "http response",
    "name": "",
    "statusCode": "",
    "headers": {},
    "x": 610,
    "y": 180,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "c_res",
    "type": "comment",
    "name": "2. RESULT  GET /node-red/tulip/result",
    "info": "All Block Outputs fires after every inspection. The final pass/fail is set by this recipe's Inspection Pass/Fail logic and saved with the capture, so the flow waits a moment and reads it back from the Library (isPass), together with the recipe name and the capture's metadata.\n\nThe flattened result is kept in flow context and served at GET /tulip/result, and published over MQTT if section 5 is enabled. GET /tulip/raw returns everything All Block Outputs reported, to find more fields to add.\n\nimage_data is a small (640 px) JPEG of the capture inside the result itself (data:image/jpeg;base64,...). Tulip's Image widget can show it without reaching the camera, and it works on https pages. image_url links to the full-size image on the camera, built from the address each caller used; it only opens where that address is reachable over plain http.",
    "x": 190,
    "y": 260,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "res_in",
    "type": "overview-unified-pipeline-input",
    "name": "All Block Outputs",
    "x": 140,
    "y": 320,
    "wires": [
      [
        "res_keep"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_keep",
    "type": "function",
    "name": "Keep raw, look up verdict",
    "func": "// The verdict is not in All Block Outputs: read it from the saved capture\nconst p = msg.payload || {};\nflow.set('lastRaw', p);\nmsg.raw = p;\nmsg.tries = (msg.tries || 0) + 1;\nmsg.url = 'http://127.0.0.1/edge/v2/capture/library?captureIds=' + encodeURIComponent(p.capture_id);\nreturn msg;",
    "outputs": 1,
    "x": 370,
    "y": 320,
    "wires": [
      [
        "res_wait"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_wait",
    "type": "delay",
    "name": "",
    "pauseType": "delay",
    "timeout": "700",
    "timeoutUnits": "milliseconds",
    "rate": "1",
    "nbRateUnits": "1",
    "rateUnits": "second",
    "randomFirst": "1",
    "randomLast": "5",
    "randomUnits": "seconds",
    "drop": false,
    "allowrate": false,
    "outputs": 1,
    "x": 560,
    "y": 320,
    "wires": [
      [
        "res_lookup"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_lookup",
    "type": "http request",
    "name": "Read capture",
    "method": "GET",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 720,
    "y": 320,
    "wires": [
      [
        "res_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_fn",
    "type": "function",
    "name": "Flatten for Tulip",
    "func": "const raw = msg.raw || {};\nconst cap = (msg.payload && msg.payload.captures && msg.payload.captures[0]) || null;\n// Verdict not written yet: try once more\nif ((!cap || typeof cap.isPass !== 'boolean') && msg.tries < 3) {\n  msg.payload = raw;\n  return [null, msg];\n}\n// Keep only the image path: the camera reports an internal address\nconst m = (raw.image_url || '').match(/^https?:\\/\\/[^/]+(\\/.*)$/);\nconst imagePath = m ? m[1] : null;\nconst result = {\n  pass: cap ? cap.isPass : null,\n  judgment: cap ? (cap.isPass ? 'pass' : 'fail') : 'unknown',\n  capture_id: raw.capture_id ?? null,\n  inspection_id: raw.inspection_id ?? null,\n  recipe_id: raw.recipe_id ?? null,\n  recipe_name: (cap && cap.recipe && cap.recipe.name) || null,\n  inspection_time: raw.inspection_time || null,\n  image_path: imagePath,\n  image_data: null,\n  trigger_data: flow.get('lastTriggerMeta') || {},\n  metadata: (cap && cap.userMetadata) || {},\n};\nmsg.result = result;\nif (!imagePath) { msg.payload = null; return [null, null, msg]; }\n// Fetch a 640 px thumbnail to embed in the result\nmsg.url = 'http://127.0.0.1' + imagePath + '?width=640';\ndelete msg.payload;\nreturn [msg, null, null];",
    "outputs": 3,
    "x": 910,
    "y": 320,
    "wires": [
      [
        "res_thumb"
      ],
      [
        "res_keep_retry"
      ],
      [
        "res_attach"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_thumb",
    "type": "http request",
    "name": "Read thumbnail",
    "method": "GET",
    "ret": "bin",
    "paytoqs": "ignore",
    "url": "",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 1100,
    "y": 320,
    "wires": [
      [
        "res_attach"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_attach",
    "type": "function",
    "name": "Attach thumbnail",
    "func": "const result = msg.result;\nif (msg.statusCode === 200 && Buffer.isBuffer(msg.payload)) {\n  result.image_data = 'data:image/jpeg;base64,' + msg.payload.toString('base64');\n}\nflow.set('lastResult', result);\n// image_url for the debug sidebar and MQTT, from the last address Tulip used\nconst host = flow.get('cameraHost');\nmsg.payload = Object.assign({}, result, { image_url: (host && result.image_path) ? 'http://' + host + result.image_path : null });\ndelete msg.payload.image_path;\nreturn msg;",
    "outputs": 1,
    "x": 1290,
    "y": 320,
    "wires": [
      [
        "res_debug",
        "mqtt_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_keep_retry",
    "type": "change",
    "name": "retry",
    "rules": [
      {
        "t": "set",
        "p": "payload",
        "pt": "msg",
        "to": "raw",
        "tot": "msg"
      }
    ],
    "x": 910,
    "y": 380,
    "wires": [
      [
        "res_keep"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_debug",
    "type": "debug",
    "name": "Last result",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": true,
    "complete": "payload",
    "targetType": "msg",
    "statusVal": "payload.judgment",
    "statusType": "msg",
    "x": 1480,
    "y": 300,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "res_get_in",
    "type": "http in",
    "name": "",
    "url": "/tulip/result",
    "method": "get",
    "upload": false,
    "swaggerDoc": "",
    "x": 150,
    "y": 400,
    "wires": [
      [
        "res_get_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_get_fn",
    "type": "function",
    "name": "Latest result",
    "func": "const r = flow.get('lastResult');\nif (!r) { msg.statusCode = 404; msg.payload = { error: 'no inspection yet' }; return msg; }\n// Link to the full image on the address this caller used\nconst host = msg.req && msg.req.headers && msg.req.headers.host;\nif (host) flow.set('cameraHost', host);\nmsg.payload = Object.assign({}, r, { image_url: (host && r.image_path) ? 'http://' + host + r.image_path : null });\ndelete msg.payload.image_path;\nreturn msg;",
    "outputs": 1,
    "x": 370,
    "y": 400,
    "wires": [
      [
        "res_get_resp"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_get_resp",
    "type": "http response",
    "name": "",
    "statusCode": "",
    "headers": {},
    "x": 570,
    "y": 400,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "raw_in",
    "type": "http in",
    "name": "",
    "url": "/tulip/raw",
    "method": "get",
    "upload": false,
    "swaggerDoc": "",
    "x": 150,
    "y": 440,
    "wires": [
      [
        "raw_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "raw_fn",
    "type": "function",
    "name": "Raw All Block Outputs",
    "func": "// Everything All Block Outputs sent for the last inspection: use it to find\n// fields to add to 'Flatten for Tulip'\nmsg.payload = flow.get('lastRaw') || { error: 'no inspection yet' };\nreturn msg;",
    "outputs": 1,
    "x": 380,
    "y": 440,
    "wires": [
      [
        "res_get_resp"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "c_rec",
    "type": "comment",
    "name": "3. RECIPE  POST /node-red/tulip/recipe  {\"id\": 3}",
    "info": "Activates the recipe with that ID (the number in the recipe's URL, /recipes/<id>).\n\nActivating a recipe reloads Node-RED with THAT recipe's flow, so this endpoint replies 202 first and switches half a second later. Confirm the switch with GET /tulip/status a few seconds later.\n\nEvery recipe Tulip switches to needs this same flow imported (and the Node-RED trigger mode if Tulip triggers it), or the /tulip endpoints disappear after the switch.",
    "x": 220,
    "y": 480,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "rec_in",
    "type": "http in",
    "name": "",
    "url": "/tulip/recipe",
    "method": "post",
    "upload": false,
    "swaggerDoc": "",
    "x": 150,
    "y": 540,
    "wires": [
      [
        "rec_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "rec_fn",
    "type": "function",
    "name": "Validate id",
    "func": "const id = Number(msg.payload && msg.payload.id);\nif (!Number.isInteger(id) || id < 1) {\n  msg.statusCode = 400;\n  msg.payload = { error: 'send {\"id\": <recipe id>}' };\n  return [null, msg];\n}\n// Reply first: activating the recipe reloads Node-RED and drops this request\nconst reply = RED.util.cloneMessage(msg);\nreply.statusCode = 202;\nreply.payload = { switching_to: id, confirm_with: '/node-red/tulip/status' };\nmsg.url = 'http://127.0.0.1/edge/recipe/activate';\nmsg.method = 'POST';\nmsg.headers = { 'Content-Type': 'application/json' };\nmsg.payload = { id };\ndelete msg.res; delete msg.req;\nreturn [msg, reply];",
    "outputs": 2,
    "x": 360,
    "y": 540,
    "wires": [
      [
        "rec_wait"
      ],
      [
        "rec_resp"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "rec_wait",
    "type": "delay",
    "name": "",
    "pauseType": "delay",
    "timeout": "500",
    "timeoutUnits": "milliseconds",
    "rate": "1",
    "nbRateUnits": "1",
    "rateUnits": "second",
    "randomFirst": "1",
    "randomLast": "5",
    "randomUnits": "seconds",
    "drop": false,
    "allowrate": false,
    "outputs": 1,
    "x": 560,
    "y": 520,
    "wires": [
      [
        "rec_call"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "rec_call",
    "type": "http request",
    "name": "Activate recipe",
    "method": "use",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 750,
    "y": 520,
    "wires": [],
    "z": "tulip_tab"
  },
  {
    "id": "rec_resp",
    "type": "http response",
    "name": "",
    "statusCode": "",
    "headers": {},
    "x": 570,
    "y": 580,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "c_mode",
    "type": "comment",
    "name": "4. LIVE MODE  POST /node-red/tulip/production  {\"enabled\": true}",
    "info": "Turns Production Mode on (true) or off (false), the same switch as the top bar of the camera UI. GET /tulip/status returns the active recipe and whether Production Mode is on.",
    "x": 250,
    "y": 620,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "mode_in",
    "type": "http in",
    "name": "",
    "url": "/tulip/production",
    "method": "post",
    "upload": false,
    "swaggerDoc": "",
    "x": 160,
    "y": 680,
    "wires": [
      [
        "mode_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mode_fn",
    "type": "function",
    "name": "Validate enabled",
    "func": "const e = msg.payload && msg.payload.enabled;\nif (typeof e !== 'boolean') {\n  msg.statusCode = 400;\n  msg.payload = { error: 'send {\"enabled\": true} or {\"enabled\": false}' };\n  return [null, msg];\n}\nmsg.url = 'http://127.0.0.1/edge/flow/production_mode';\nmsg.method = 'POST';\nmsg.headers = { 'Content-Type': 'application/json' };\nmsg.payload = { enabled: e };\nreturn [msg, null];",
    "outputs": 2,
    "x": 380,
    "y": 680,
    "wires": [
      [
        "mode_call"
      ],
      [
        "mode_resp"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mode_call",
    "type": "http request",
    "name": "Set production mode",
    "method": "use",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 600,
    "y": 660,
    "wires": [
      [
        "status_mode"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "status_in",
    "type": "http in",
    "name": "",
    "url": "/tulip/status",
    "method": "get",
    "upload": false,
    "swaggerDoc": "",
    "x": 150,
    "y": 740,
    "wires": [
      [
        "status_host"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "status_host",
    "type": "function",
    "name": "Remember host",
    "func": "if (msg.req && msg.req.headers && msg.req.headers.host) flow.set('cameraHost', msg.req.headers.host);\nreturn msg;",
    "outputs": 1,
    "x": 330,
    "y": 740,
    "wires": [
      [
        "status_mode"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "status_mode",
    "type": "http request",
    "name": "Read production mode",
    "method": "GET",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "http://127.0.0.1/edge/flow/production_mode",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 830,
    "y": 700,
    "wires": [
      [
        "status_keep"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "status_keep",
    "type": "change",
    "name": "keep mode",
    "rules": [
      {
        "t": "set",
        "p": "production",
        "pt": "msg",
        "to": "payload.enabled",
        "tot": "msg"
      }
    ],
    "x": 1020,
    "y": 700,
    "wires": [
      [
        "status_recipe"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "status_recipe",
    "type": "http request",
    "name": "Read active recipe",
    "method": "GET",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "http://127.0.0.1/edge/recipe/active",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 1200,
    "y": 700,
    "wires": [
      [
        "status_out"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "status_out",
    "type": "function",
    "name": "Status",
    "func": "const r = msg.payload || {};\nmsg.statusCode = 200;\nmsg.payload = {\n  production_mode: msg.production === true,\n  active_recipe_id: r.id ?? null,\n  active_recipe_name: r.name ?? null,\n  last_result: null,\n};\nconst last = flow.get('lastResult');\nconst host = msg.req && msg.req.headers && msg.req.headers.host;\nif (last) {\n  msg.payload.last_result = Object.assign({}, last, { image_url: (host && last.image_path) ? 'http://' + host + last.image_path : null });\n  delete msg.payload.last_result.image_path;\n}\nreturn msg;",
    "outputs": 1,
    "x": 1380,
    "y": 700,
    "wires": [
      [
        "mode_resp"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mode_resp",
    "type": "http response",
    "name": "",
    "statusCode": "",
    "headers": {},
    "x": 1540,
    "y": 740,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "c_mqtt",
    "type": "comment",
    "name": "5. MQTT (optional)  ov/<serial>/result \u00b7 ov/<serial>/status \u00b7 ov/<serial>/cmd",
    "info": "Two-way MQTT for Tulip's MQTT connector. <serial> is the camera serial from the top bar of the camera UI.\n\n- ov/<serial>/result: every inspection result (the same JSON as /tulip/result), as soon as it is ready\n- ov/<serial>/status: active recipe and production mode, retained, published whenever this flow starts (so also after every recipe switch)\n- ov/<serial>/cmd: commands in. {\"action\":\"trigger\",\"serial\":\"...\"}, {\"action\":\"recipe\",\"id\":5}, {\"action\":\"production\",\"enabled\":true}\n\nTo use it: open 'Plant broker (edit me)', set your broker's host, port, TLS and login, enable the three MQTT nodes (select them and press Ctrl/Cmd+E, or untick Disabled), then Deploy.",
    "x": 260,
    "y": 840,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "mq_start",
    "type": "inject",
    "name": "On start",
    "props": [
      {
        "p": "payload"
      }
    ],
    "repeat": "",
    "crontab": "",
    "once": true,
    "onceDelay": "2",
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 150,
    "y": 900,
    "wires": [
      [
        "mq_serial"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mq_serial",
    "type": "http request",
    "name": "Read serial",
    "method": "GET",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "http://127.0.0.1/postgrest/device_info?select=serial_number&order=id.asc&limit=1",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 330,
    "y": 900,
    "wires": [
      [
        "mq_serial_keep"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mq_serial_keep",
    "type": "function",
    "name": "Remember serial",
    "func": "const s = Array.isArray(msg.payload) && msg.payload[0] ? msg.payload[0].serial_number : null;\nflow.set('serial', s || 'camera');\nnode.status({ text: 'ov/' + (s || 'camera') + '/...' });\nreturn msg;",
    "outputs": 1,
    "x": 520,
    "y": 900,
    "wires": [
      [
        "mq_status_mode"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mq_status_mode",
    "type": "http request",
    "name": "Read production mode",
    "method": "GET",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "http://127.0.0.1/edge/flow/production_mode",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 720,
    "y": 900,
    "wires": [
      [
        "mq_status_keep"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mq_status_keep",
    "type": "change",
    "name": "keep mode",
    "rules": [
      {
        "t": "set",
        "p": "production",
        "pt": "msg",
        "to": "payload.enabled",
        "tot": "msg"
      }
    ],
    "x": 900,
    "y": 900,
    "wires": [
      [
        "mq_status_recipe"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mq_status_recipe",
    "type": "http request",
    "name": "Read active recipe",
    "method": "GET",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "http://127.0.0.1/edge/recipe/active",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 1080,
    "y": 900,
    "wires": [
      [
        "mq_status_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mq_status_fn",
    "type": "function",
    "name": "Status message",
    "func": "const r = msg.payload || {};\nmsg.topic = 'ov/' + (flow.get('serial') || 'camera') + '/status';\nmsg.payload = JSON.stringify({ production_mode: msg.production === true, active_recipe_id: r.id ?? null, active_recipe_name: r.name ?? null, at: new Date().toISOString() });\nmsg.qos = 1; msg.retain = true;\nreturn msg;",
    "outputs": 1,
    "x": 1270,
    "y": 900,
    "wires": [
      [
        "mqtt_out"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mqtt_fn",
    "type": "function",
    "name": "Result topic",
    "func": "msg.topic = 'ov/' + (flow.get('serial') || 'camera') + '/result';\nmsg.payload = JSON.stringify(msg.payload);\nmsg.qos = 1;\nmsg.retain = false;\nreturn msg;",
    "outputs": 1,
    "x": 1480,
    "y": 340,
    "wires": [
      [
        "res_link_out"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "res_link_out",
    "type": "link out",
    "name": "to MQTT publish",
    "mode": "link",
    "links": [
      "pub_link_in"
    ],
    "x": 1615,
    "y": 340,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "mqtt_broker",
    "type": "mqtt-broker",
    "name": "Plant broker (edit me)",
    "broker": "broker.example.local",
    "port": "1883",
    "clientid": "",
    "autoConnect": true,
    "usetls": false,
    "protocolVersion": "4",
    "keepalive": "60",
    "cleansession": true,
    "autoUnsubscribe": true,
    "birthTopic": "",
    "birthQos": "0",
    "birthPayload": "",
    "closeTopic": "",
    "closePayload": "",
    "willTopic": "",
    "willQos": "0",
    "willPayload": ""
  },
  {
    "id": "pub_link_in",
    "type": "link in",
    "name": "results to publish",
    "links": [
      "res_link_out"
    ],
    "x": 1335,
    "y": 840,
    "wires": [
      [
        "mqtt_out"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mqtt_out",
    "type": "mqtt out",
    "name": "Publish",
    "topic": "",
    "qos": "",
    "retain": "",
    "respTopic": "",
    "contentType": "",
    "userProps": "",
    "correl": "",
    "expiry": "",
    "broker": "mqtt_broker",
    "d": true,
    "x": 1450,
    "y": 880,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "mq_cmd_in",
    "type": "mqtt in",
    "name": "Commands",
    "topic": "ov/+/cmd",
    "qos": "1",
    "datatype": "json",
    "broker": "mqtt_broker",
    "nl": false,
    "rap": true,
    "rh": 0,
    "inputs": 0,
    "d": true,
    "x": 140,
    "y": 980,
    "wires": [
      [
        "mq_cmd_fn"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mq_cmd_fn",
    "type": "function",
    "name": "Route command",
    "func": "// Only act on commands for this camera: ov/<serial>/cmd\nconst serial = flow.get('serial') || 'camera';\nif (msg.topic.split('/')[1] !== serial) return null;\nconst c = (msg.payload && typeof msg.payload === 'object') ? msg.payload : {};\nif (c.action === 'trigger') {\n  const body = Object.assign({}, c); delete body.action;\n  return [{ payload: body }, null, null];\n}\nif (c.action === 'recipe' && Number.isInteger(Number(c.id))) {\n  return [null, { url: 'http://127.0.0.1/edge/recipe/activate', method: 'POST', headers: { 'Content-Type': 'application/json' }, payload: { id: Number(c.id) } }, null];\n}\nif (c.action === 'production' && typeof c.enabled === 'boolean') {\n  return [null, null, { url: 'http://127.0.0.1/edge/flow/production_mode', method: 'POST', headers: { 'Content-Type': 'application/json' }, payload: { enabled: c.enabled } }];\n}\nnode.warn('Unknown command: ' + JSON.stringify(c));\nreturn null;",
    "outputs": 3,
    "x": 340,
    "y": 980,
    "wires": [
      [
        "mq_trig_link_out"
      ],
      [
        "mq_cmd_recipe"
      ],
      [
        "mq_cmd_mode"
      ]
    ],
    "z": "tulip_tab"
  },
  {
    "id": "mq_trig_link_out",
    "type": "link out",
    "name": "to trigger",
    "mode": "link",
    "links": [
      "trig_link_in"
    ],
    "x": 495,
    "y": 940,
    "z": "tulip_tab",
    "wires": []
  },
  {
    "id": "mq_cmd_recipe",
    "type": "http request",
    "name": "Activate recipe",
    "method": "use",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 560,
    "y": 980,
    "wires": [],
    "z": "tulip_tab"
  },
  {
    "id": "mq_cmd_mode",
    "type": "http request",
    "name": "Set production mode",
    "method": "use",
    "ret": "obj",
    "paytoqs": "ignore",
    "url": "",
    "tls": "",
    "persist": false,
    "proxy": "",
    "insecureHTTPParser": false,
    "authType": "",
    "senderr": false,
    "headers": [],
    "x": 570,
    "y": 1020,
    "wires": [
      [
        "mq_status_mode"
      ]
    ],
    "z": "tulip_tab"
  }
]