[
    {
        "id": "ef7ed1565e0a7ef0",
        "type": "subflow",
        "name": "Get measurements from MIP",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 40,
                "y": 260,
                "wires": [
                    {
                        "id": "108202ccd244fdf8"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1080,
                "y": 260,
                "wires": [
                    {
                        "id": "076cd0e938ce4b8e",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "46eda8b884c10357",
        "type": "http request",
        "z": "ef7ed1565e0a7ef0",
        "name": "service call to MIP",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": true,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 390,
        "y": 260,
        "wires": [
            [
                "408eb7a7b8d6e3fc"
            ]
        ]
    },
    {
        "id": "108202ccd244fdf8",
        "type": "function",
        "z": "ef7ed1565e0a7ef0",
        "name": "build request",
        "func": "let mip_params = { \n    \"params\": [{\n        \"acronym\": \"resource.id\",\n        \"value\": [], \n        \"operator\": \"IN\" \n    }], \n    \"columns\": [\n        \"resource.id\", \n        \"resource.act.yield.primary\", \n        \"resource.act.scrap.primary\", \n        \"resource.act.status\", \n        \"resource.act.status.text\"], \n    \"languageKey\": msg.languageKey\n    }\n\nObject.keys(msg.mapping).forEach(function (machine) {\n   mip_params.params[0].value.push(machine)\n});\nmsg.payload = mip_params\n\nmsg.url = msg.baseUrl + \"/data/BOResource/list?X-Access-Id=\" + msg.xAccessId\n\nconst jsessionid = flow.get(\"jsessionid\")\n\nlet basic_encrypted = \"Basic \" + new Buffer(msg.user + ':' + msg.password).toString('base64')\n\nif (jsessionid) {\n    msg.headers = {\n        \"Content-Type\": \"application/json\",\n        \"Cookie\": `JSESSIONID=${jsessionid}`\n    }\n} else {\n    msg.headers = {\n        \"Content-Type\": \"application/json\",\n        \"Authorization\": basic_encrypted,\n    };\n}\n\n\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 190,
        "y": 260,
        "wires": [
            [
                "46eda8b884c10357"
            ]
        ]
    },
    {
        "id": "076cd0e938ce4b8e",
        "type": "change",
        "z": "ef7ed1565e0a7ef0",
        "name": "save JSESSION",
        "rules": [
            {
                "t": "set",
                "p": "jsessionid",
                "pt": "flow",
                "to": "responseCookies.JSESSIONID.value",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 920,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "408eb7a7b8d6e3fc",
        "type": "switch",
        "z": "ef7ed1565e0a7ef0",
        "name": "Check if invalid",
        "property": "statusCode",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "401",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 660,
        "y": 260,
        "wires": [
            [
                "f3287043eac46441"
            ],
            [
                "076cd0e938ce4b8e"
            ]
        ]
    },
    {
        "id": "f3287043eac46441",
        "type": "change",
        "z": "ef7ed1565e0a7ef0",
        "name": "Delete JSESSION",
        "rules": [
            {
                "t": "delete",
                "p": "jsessionid",
                "pt": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 930,
        "y": 180,
        "wires": [
            [
                "108202ccd244fdf8"
            ]
        ]
    },
    {
        "id": "c4d4bb86a22d2cde",
        "type": "subflow",
        "name": "construct mqtt payloads",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 80,
                "y": 120,
                "wires": [
                    {
                        "id": "98e4970825473105"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1260,
                "y": 160,
                "wires": [
                    {
                        "id": "b06833068ba12ef6",
                        "port": 0
                    },
                    {
                        "id": "e6605acd490a769a",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "2e4e8fa039385ba6",
        "type": "split",
        "z": "c4d4bb86a22d2cde",
        "name": "split machines",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 440,
        "y": 120,
        "wires": [
            [
                "32c4989b783c5713"
            ]
        ]
    },
    {
        "id": "98e4970825473105",
        "type": "change",
        "z": "c4d4bb86a22d2cde",
        "name": "delete meta data",
        "rules": [
            {
                "t": "delete",
                "p": "payload[0]",
                "pt": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 250,
        "y": 120,
        "wires": [
            [
                "2e4e8fa039385ba6"
            ]
        ]
    },
    {
        "id": "b06833068ba12ef6",
        "type": "function",
        "z": "c4d4bb86a22d2cde",
        "name": "construct mqtt payload for units",
        "func": "let mip_machine_id = msg.payload.data[4]\nmsg.topic = \"oee/sensor/\" + msg.mapping[mip_machine_id].units_sensor + \"/measurement\"\nlet from_iso = new Date(msg.timestamp - 30 * 1000 ).toISOString()\nlet to_iso = new Date(msg.timestamp).toISOString()\nmsg.payload = {\n    \"data\": [\n        {\n            \"attributes\": {\n                \"from\": from_iso, // ISO 8601 timestamp in UTC\n                \"to\": to_iso,   // ISO 8601 timestamp in UTC\n                \"count\": msg.units   // resource.act.yield.primary\n            }\n        }\n    ]\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 970,
        "y": 120,
        "wires": [
            []
        ]
    },
    {
        "id": "e6605acd490a769a",
        "type": "function",
        "z": "c4d4bb86a22d2cde",
        "name": "construct mqtt payload for rejects",
        "func": "let mip_machine_id = msg.payload.data[4]\nmsg.topic = \"oee/sensor/\" + msg.mapping[mip_machine_id].rejects_sensor + \"/measurement\"\nlet from_iso = new Date(msg.timestamp - 30 * 1000).toISOString()\nlet to_iso = new Date(msg.timestamp).toISOString()\nmsg.payload = {\n    \"data\": [\n        {\n            \"attributes\": {\n                \"from\": from_iso, // ISO 8601 timestamp in UTC\n                \"to\": to_iso,   // ISO 8601 timestamp in UTC\n                \"count\": msg.rejects  // resource.act.scrap.primary\n            }\n        }\n    ]\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 980,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "32c4989b783c5713",
        "type": "function",
        "z": "c4d4bb86a22d2cde",
        "name": "calculate units and rejects",
        "func": "let machine_data = global.get(\"machine_data\");\nlet mip_machine_id = msg.payload.data[4];\nlet old_units = 0\nlet old_rejects = 0\n\nif (machine_data) {\n    if (machine_data[mip_machine_id]){\n        old_units = machine_data[mip_machine_id].units\n        old_rejects = machine_data[mip_machine_id].rejects\n\n        machine_data[mip_machine_id].units = msg.payload.data[3]\n        machine_data[mip_machine_id].rejects = msg.payload.data[0]\n    }else{\n        machine_data[mip_machine_id] = {\n            \"units\": msg.payload.data[3],\n            \"rejects\": msg.payload.data[0]\n        }\n    }\n} else {\n    machine_data = {}\n    machine_data[mip_machine_id] = {\n            \"units\": msg.payload.data[3],\n            \"rejects\": msg.payload.data[0]\n        }\n}\n\nglobal.set(\"machine_data\", machine_data)\n\n\nmsg.units = msg.payload.data[3] - old_units\nmsg.rejects = msg.payload.data[0] - old_rejects\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 670,
        "y": 120,
        "wires": [
            [
                "e6605acd490a769a",
                "b06833068ba12ef6"
            ]
        ]
    },
    {
        "id": "2a077db9246dcceb",
        "type": "tab",
        "label": "MIP send measurements",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c990b9ec389ff9b4",
        "type": "inject",
        "z": "2a077db9246dcceb",
        "name": "Every 30 seconds",
        "props": [
            {
                "p": "timestamp",
                "v": "",
                "vt": "date"
            }
        ],
        "repeat": "30",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 190,
        "y": 280,
        "wires": [
            [
                "788e3bf5faff49e9"
            ]
        ]
    },
    {
        "id": "d7014c3e3c66e815",
        "type": "mqtt out",
        "z": "2a077db9246dcceb",
        "name": "send measurements",
        "topic": "",
        "qos": "2",
        "retain": "true",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "",
        "x": 1160,
        "y": 280,
        "wires": []
    },
    {
        "id": "ef3c93c5b0dc33fb",
        "type": "comment",
        "z": "2a077db9246dcceb",
        "name": "^^ configure authentication",
        "info": "",
        "x": 1180,
        "y": 340,
        "wires": []
    },
    {
        "id": "341f1e1c657eeeef",
        "type": "subflow:c4d4bb86a22d2cde",
        "z": "2a077db9246dcceb",
        "name": "",
        "x": 910,
        "y": 280,
        "wires": [
            [
                "d7014c3e3c66e815"
            ]
        ]
    },
    {
        "id": "788e3bf5faff49e9",
        "type": "change",
        "z": "2a077db9246dcceb",
        "name": "set settings",
        "rules": [
            {
                "t": "set",
                "p": "mapping",
                "pt": "msg",
                "to": "{\"$RESSOURCE_ID\":{\"units_sensor\":\"$SENSOR_ID\",\"rejects_sensor\":\"$SENSOR_ID\"}}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "baseUrl",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "xAccessId",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "user",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "password",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "languageKey",
                "pt": "msg",
                "to": "en",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 390,
        "y": 280,
        "wires": [
            [
                "a3271926fd3191e9"
            ]
        ]
    },
    {
        "id": "fedf83a6e2a95cc9",
        "type": "comment",
        "z": "2a077db9246dcceb",
        "name": "^^ configure settings",
        "info": "",
        "x": 420,
        "y": 340,
        "wires": []
    },
    {
        "id": "a3271926fd3191e9",
        "type": "subflow:ef7ed1565e0a7ef0",
        "z": "2a077db9246dcceb",
        "name": "",
        "x": 640,
        "y": 280,
        "wires": [
            [
                "341f1e1c657eeeef"
            ]
        ]
    }
]