[
    {
        "id": "d977d82f.15fe3",
        "type": "tab",
        "label": "IOT2000 Communication Example"
    },
    {
        "id": "358c4163.2ab466",
        "type": "inject",
        "z": "d977d82f.15fe3",
        "name": "Select All",
        "topic": "select  * from IOTDatatable",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 1044,
        "y": 404,
        "wires": [
            [
                "66121aad.de8eac"
            ]
        ]
    },
    {
        "id": "f2e37a01.d1ae4",
        "type": "s7 in",
        "z": "d977d82f.15fe3",
        "endpoint": "57958a85.72d06c",
        "mode": "all",
        "variable": "IOT_INT",
        "diff": true,
        "name": "S7Com S7-1200 ",
        "x": 187,
        "y": 544,
        "wires": [
            [
                "314832c9.76e46e"
            ]
        ]
    },
    {
        "id": "ef47d5b2.5e8b28",
        "type": "comment",
        "z": "d977d82f.15fe3",
        "name": "S7Communication to S7-1200 PLC",
        "info": "",
        "x": 247,
        "y": 504,
        "wires": []
    },
    {
        "id": "fb72a25b.aa21f",
        "type": "function",
        "z": "d977d82f.15fe3",
        "name": "FormatS7Time",
        "func": "var time = msg.payload.IOT_TIME;\nvar output = {\n    hours: \"\",\n    minutes:\"\",\n    seconds:\"\",\n    milliseconds:\"\",\n    total:\"\"\n};\nvar timeAlreadyProcessed;\nvar remainingTime;\n\n//Hours\nvar hours = time / 3600000;\nhours =  Math.floor(hours);\nremainingTime = time - (hours * 3600000);\n\n//Minutes\nvar minutes = remainingTime /60000  ;\nminutes =  Math.floor(minutes);\nremainingTime = remainingTime - (minutes * 60000);\n\n//Seconds\nvar seconds = remainingTime /1000 ;\nseconds =  Math.floor(seconds);\nremainingTime = remainingTime - (seconds * 1000);\n\n//Millliseconds\nvar milliseconds = remainingTime;\n\noutput.hours = hours;\noutput.minutes = minutes\noutput.seconds = seconds\noutput.milliseconds =  milliseconds;\n\noutput.total = `${hours}H ${minutes}min ${seconds}s ${milliseconds}ms`;\n\nmsg.payload.time = output;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 958,
        "y": 544,
        "wires": [
            [
                "da93980.b218d68"
            ]
        ]
    },
    {
        "id": "314832c9.76e46e",
        "type": "change",
        "z": "d977d82f.15fe3",
        "name": "Set ID S7Com-1200",
        "rules": [
            {
                "t": "set",
                "p": "payload.sID",
                "pt": "msg",
                "to": "S7Com-1200",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload.ID",
                "pt": "msg",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 387,
        "y": 544,
        "wires": [
            []
        ]
    },
    {
        "id": "87b2f0d.36c111",
        "type": "change",
        "z": "d977d82f.15fe3",
        "name": "Define global variables",
        "rules": [
            {
                "t": "set",
                "p": "averageMaxCount",
                "pt": "global",
                "to": "10",
                "tot": "num"
            },
            {
                "t": "set",
                "p": "bufferMaxCount",
                "pt": "global",
                "to": "3600",
                "tot": "num"
            },
            {
                "t": "set",
                "p": "lastStoredIndex",
                "pt": "global",
                "to": "-1",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 334,
        "y": 84,
        "wires": [
            []
        ]
    },
    {
        "id": "fbb0b360.ebce",
        "type": "inject",
        "z": "d977d82f.15fe3",
        "name": "Set Once",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": true,
        "x": 124,
        "y": 84,
        "wires": [
            [
                "87b2f0d.36c111"
            ]
        ]
    },
    {
        "id": "66121aad.de8eac",
        "type": "mysql",
        "z": "d977d82f.15fe3",
        "mydb": "d47812eb.9b806",
        "name": "INSERT VALUES",
        "x": 1254,
        "y": 464,
        "wires": [
            [
                "bd2f401e.0de4",
                "416f4ca1.a9b5bc"
            ]
        ]
    },
    {
        "id": "cc5950ae.a58e6",
        "type": "OpcUa-Client",
        "z": "d977d82f.15fe3",
        "endpoint": "81aafc6f.ceacd",
        "action": "read",
        "time": 10,
        "timeUnit": "s",
        "name": "",
        "x": 542,
        "y": 204,
        "wires": [
            [
                "3a94b25c.60f996"
            ]
        ]
    },
    {
        "id": "ce5ee714.3141e8",
        "type": "inject",
        "z": "d977d82f.15fe3",
        "name": "Refresh Rate =  1sec",
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": true,
        "x": 162,
        "y": 204,
        "wires": [
            [
                "38cee855.eb0348",
                "88f73759.e4aba8",
                "bff46072.49f2c8",
                "ff69ea9d.1f0508"
            ]
        ]
    },
    {
        "id": "9a4cdb02.ce4938",
        "type": "modbus-getter",
        "z": "d977d82f.15fe3",
        "name": "ModbusTCP S7-1500",
        "showStatusActivities": true,
        "showErrors": true,
        "logIOActivities": false,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "0",
        "quantity": "20",
        "server": "f0685f5e.8dd4d8",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "x": 615.7499389648438,
        "y": 717.25,
        "wires": [
            [],
            [
                "671a29bd.48bc88"
            ]
        ]
    },
    {
        "id": "da857637.56051",
        "type": "inject",
        "z": "d977d82f.15fe3",
        "name": "Refresh Rate= 1sec.",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": true,
        "x": 584,
        "y": 759,
        "wires": [
            [
                "9a4cdb02.ce4938"
            ]
        ]
    },
    {
        "id": "671a29bd.48bc88",
        "type": "function",
        "z": "d977d82f.15fe3",
        "name": "Convert To S7 Values",
        "func": "\nmsg.payload.IOT_BOOL = msg.payload.data[0] >0 ? true : false;\n\nvar data = [];\ndata[0]=msg.payload.buffer[2];\ndata[1]=msg.payload.buffer[1];\n\n\n// Create a buffer\nvar buf = new ArrayBuffer(4);\n// Create a data view of it\nvar view = new DataView(buf);\n/*\ndata.forEach(function (b, i) {\n    view.setUint8(i, b);\n});\n*/\ndata.forEach((element, index) => view.setUint8(index, element));\nmsg.payload.IOT_INT = view.getInt16(0,true);\n\ndata[0]=msg.payload.buffer[5];\ndata[1]=msg.payload.buffer[4];\ndata[2]=msg.payload.buffer[7];\ndata[3]=msg.payload.buffer[6];\ndata.forEach(function (b, i) {\n    view.setUint8(i, b);\n});\n\n// Read the bits as a float; note that by doing this, we're implicitly\n// converting it from a 32-bit float into JavaScript's native 64-bit double\nmsg.payload.IOT_REAL = view.getFloat32(0,true);\n\n\ndata[0]=msg.payload.buffer[9];\ndata[1]=msg.payload.buffer[8];\ndata[2]=msg.payload.buffer[11];\ndata[3]=msg.payload.buffer[10];\n\ndata.forEach(function (b, i) {\n    view.setUint8(i, b);\n});\n\nmsg.payload.IOT_TIME= view.getInt32(0,true);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 584,
        "y": 671,
        "wires": [
            [
                "1e4e0415.d5b20c"
            ]
        ]
    },
    {
        "id": "1e4e0415.d5b20c",
        "type": "change",
        "z": "d977d82f.15fe3",
        "name": "Set ID ModbusTCP-1500",
        "rules": [
            {
                "t": "set",
                "p": "payload.sID",
                "pt": "msg",
                "to": "ModbusTCP-1500",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload.ID",
                "pt": "msg",
                "to": "3",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 705,
        "y": 586,
        "wires": [
            [
                "fb72a25b.aa21f"
            ]
        ]
    },
    {
        "id": "b48ad162.ffaf68",
        "type": "comment",
        "z": "d977d82f.15fe3",
        "name": "Define global variables",
        "info": "",
        "x": 148,
        "y": 40,
        "wires": []
    },
    {
        "id": "5ca0e19e.9950f8",
        "type": "comment",
        "z": "d977d82f.15fe3",
        "name": "Modbus TCP Communication to S7-1500",
        "info": "",
        "x": 624,
        "y": 793,
        "wires": []
    },
    {
        "id": "38cee855.eb0348",
        "type": "OpcUa-Item",
        "z": "d977d82f.15fe3",
        "item": "ns=3;s=\"IOT_Data\".\"IOT_Bool\"",
        "datatype": "Boolean",
        "value": "",
        "name": "IOT_BOOL",
        "x": 372,
        "y": 204,
        "wires": [
            [
                "cc5950ae.a58e6"
            ]
        ]
    },
    {
        "id": "88f73759.e4aba8",
        "type": "OpcUa-Item",
        "z": "d977d82f.15fe3",
        "item": "ns=3;s=\"IOT_Data\".\"IOT_Int\"",
        "datatype": "Int16",
        "value": "",
        "name": "IOT_INT",
        "x": 362,
        "y": 324,
        "wires": [
            [
                "f99cf473.8a37f8"
            ]
        ]
    },
    {
        "id": "f99cf473.8a37f8",
        "type": "OpcUa-Client",
        "z": "d977d82f.15fe3",
        "endpoint": "81aafc6f.ceacd",
        "action": "read",
        "time": 10,
        "timeUnit": "s",
        "name": "",
        "x": 542,
        "y": 324,
        "wires": [
            [
                "3a94b25c.60f996"
            ]
        ]
    },
    {
        "id": "bff46072.49f2c8",
        "type": "OpcUa-Item",
        "z": "d977d82f.15fe3",
        "item": "ns=3;s=\"IOT_Data\".\"IOT_Real\"",
        "datatype": "Float",
        "value": "",
        "name": "IOT_REAL",
        "x": 372,
        "y": 264,
        "wires": [
            [
                "95c051d.0d0f03"
            ]
        ]
    },
    {
        "id": "95c051d.0d0f03",
        "type": "OpcUa-Client",
        "z": "d977d82f.15fe3",
        "endpoint": "81aafc6f.ceacd",
        "action": "read",
        "time": 10,
        "timeUnit": "s",
        "name": "",
        "x": 542,
        "y": 264,
        "wires": [
            [
                "3a94b25c.60f996"
            ]
        ]
    },
    {
        "id": "ff69ea9d.1f0508",
        "type": "OpcUa-Item",
        "z": "d977d82f.15fe3",
        "item": "ns=3;s=\"IOT_Data\".\"IOT_Time\"",
        "datatype": "Int32",
        "value": "",
        "name": "IOT_TIME",
        "x": 372,
        "y": 384,
        "wires": [
            [
                "69dc998d.d86078"
            ]
        ]
    },
    {
        "id": "69dc998d.d86078",
        "type": "OpcUa-Client",
        "z": "d977d82f.15fe3",
        "endpoint": "81aafc6f.ceacd",
        "action": "read",
        "time": 10,
        "timeUnit": "s",
        "name": "",
        "x": 542,
        "y": 384,
        "wires": [
            [
                "3a94b25c.60f996"
            ]
        ]
    },
    {
        "id": "3a94b25c.60f996",
        "type": "join",
        "z": "d977d82f.15fe3",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "browseName",
        "joiner": ",",
        "timeout": "5",
        "count": "4",
        "x": 712,
        "y": 324,
        "wires": [
            [
                "c363d6da.b64568"
            ]
        ]
    },
    {
        "id": "75ea4b6c.e8d054",
        "type": "inject",
        "z": "d977d82f.15fe3",
        "name": "Delete All",
        "topic": "DELETE FROM IOTDatatable",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 1044,
        "y": 444,
        "wires": [
            [
                "66121aad.de8eac"
            ]
        ]
    },
    {
        "id": "da93980.b218d68",
        "type": "function",
        "z": "d977d82f.15fe3",
        "name": "Get average values",
        "func": "var crntCounts = context.get(\"crntCounts\")||[0,0,0,0];\nvar avgIntValues = context.get(\"avgIntValues\")||[0,0,0,0];\nvar avgRealValues = context.get(\"avgRealValues\")||[0,0,0,0];\nvar avgBoolValues = context.get(\"avgBoolValues\")||[0,0,0,0];\n\nvar ID = msg.payload.ID;\n\navgIntValues[ID] += msg.payload.IOT_INT;\navgRealValues[ID] += msg.payload.IOT_REAL;\navgBoolValues[ID] += (msg.payload.IOT_BOOL ? 1: 0);\n\ncrntCounts[ID]++;\nif(crntCounts[ID] == global.get(\"averageMaxCount\"))\n{\n    avgIntValues[ID] /= crntCounts[ID];\n    avgRealValues[ID] /= crntCounts[ID];\n    avgBoolValues[ID]/= crntCounts[ID];\n    \n    crntCounts[ID] = 0;\n    msg.payload.IOT_INT = avgIntValues[ID];\n    msg.payload.IOT_REAL = avgRealValues[ID];\n    msg.payload.IOT_BOOL = avgBoolValues[ID] >= 0.5 ? true : false ;\n    avgIntValues[ID]  = avgRealValues[ID] = avgBoolValues[ID] = 0;\n}\nelse\n{\n    msg =null;\n}\n\ncontext.set(\"avgIntValues\",avgIntValues);\ncontext.set(\"avgRealValues\",avgRealValues);\ncontext.set(\"avgBoolValues\",avgBoolValues);\ncontext.set(\"crntCounts\",crntCounts);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 998,
        "y": 584,
        "wires": [
            [
                "42af6974.5f1aa"
            ]
        ]
    },
    {
        "id": "c363d6da.b64568",
        "type": "change",
        "z": "d977d82f.15fe3",
        "name": "Set ID OPCUA-1500",
        "rules": [
            {
                "t": "set",
                "p": "payload.sID",
                "pt": "msg",
                "to": "OPCUA-1500",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload.ID",
                "pt": "msg",
                "to": "2",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 713,
        "y": 499,
        "wires": [
            [
                "fb72a25b.aa21f"
            ]
        ]
    },
    {
        "id": "73eefc29.1fe9ac",
        "type": "comment",
        "z": "d977d82f.15fe3",
        "name": "OPC-UA Communication to S7-1500",
        "info": "",
        "x": 188,
        "y": 162,
        "wires": []
    },
    {
        "id": "42af6974.5f1aa",
        "type": "function",
        "z": "d977d82f.15fe3",
        "name": "Data handling",
        "func": "var maxValue = global.get(\"bufferMaxCount\")||3600;\nvar plcValues = context.get(\"plcValues\")||[];\nvar actualIndex = context.get(\"actualIndex\")||0;\nvar lastIndex = global.get(\"lastStoredIndex\");\nvar nextColumns = \"\";\n\nvar boolAsTinyInt = msg.payload.IOT_BOOL ? 1 : 0 ;\n\n\nvar newLine = [];\nnewLine[0] = msg.payload.sID;\nnewLine[1] = boolAsTinyInt;\nnewLine[2] = msg.payload.IOT_INT;\nnewLine[3] = msg.payload.IOT_REAL;\nnewLine[4] = msg.payload.IOT_TIME;\n//newLine[5] = msg.payload.time.total;\n\n\nplcValues[actualIndex] = newLine;\n\n\n\nif(actualIndex > lastIndex)\n{\n    for( i= lastIndex+1;i<= actualIndex;i++)\n    {\n        nextColumns += `('${plcValues[i][0]}',${plcValues[i][1]},${plcValues[i][2]},${plcValues[i][3]},${plcValues[i][4]})`;\n        if(i != actualIndex)\n        {\n            nextColumns += \",\";\n        }\n    }\n}\nelse\n{\n    for(i= lastIndex+1;i<maxValue;i++)\n    {\n        nextColumns += `('${plcValues[i][0]}',${plcValues[i][1]},${plcValues[i][2]},${plcValues[i][3]},${plcValues[i][4]})`;\n        if(i != maxValue-1)\n        {\n            nextColumns += \",\";\n        }\n    }\n    \n    for(i= 0;i<= actualIndex;i++)\n    {\n        nextColumns += `('${plcValues[i][0]}',${plcValues[i][1]},${plcValues[i][2]},${plcValues[i][3]},${plcValues[i][4]})`;\n        if(i != actualIndex)\n        {\n            nextColumns += \",\";\n        }\n    }\n}\n    \n\n\n\nmsg.payload = newLine;\nmsg.topic = \"INSERT INTO IOTDatatable VALUES \" + nextColumns +\";\";\nmsg.actualIndex = actualIndex;\nmsg.lastIndex = lastIndex;\n\nactualIndex++;\n\nif(actualIndex == maxValue)\n{\n    actualIndex = 0;\n}\n\ncontext.set(\"plcValues\",plcValues);\ncontext.set (\"actualIndex\",actualIndex);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1042,
        "y": 626,
        "wires": [
            [
                "66121aad.de8eac"
            ]
        ]
    },
    {
        "id": "bd2f401e.0de4",
        "type": "function",
        "z": "d977d82f.15fe3",
        "name": "Data inserted into db",
        "func": "global.set(\"lastStoredIndex\",msg.actualIndex)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1288,
        "y": 405,
        "wires": [
            []
        ]
    },
    {
        "id": "416f4ca1.a9b5bc",
        "type": "debug",
        "z": "d977d82f.15fe3",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "true",
        "x": 1494.5,
        "y": 465,
        "wires": []
    },
    {
        "id": "57958a85.72d06c",
        "type": "s7 endpoint",
        "z": "",
        "address": "192.168.200.253",
        "port": "102",
        "rack": "0",
        "slot": "1",
        "localtsaphi": "01",
        "localtsaplo": "00",
        "remotetsaphi": "01",
        "remotetsaplo": "00",
        "connmode": "rack-slot",
        "cycletime": "1000",
        "timeout": "3000",
        "verbose": "off",
        "name": "S7-1200 ",
        "vartable": [
            {
                "addr": "DB3,INT2",
                "name": "IOT_INT"
            },
            {
                "addr": "DB3,REAL4",
                "name": "IOT_REAL"
            },
            {
                "addr": "DB3,X0.0",
                "name": "IOT_BOOL"
            },
            {
                "addr": "DB3,DINT8",
                "name": "IOT_TIME"
            },
            {
                "addr": "I0.0",
                "name": "E0_0"
            },
            {
                "addr": "IB0",
                "name": "Byte0"
            },
            {
                "addr": "QW0",
                "name": "QWort0"
            },
            {
                "addr": "MB4",
                "name": "Mb4"
            }
        ]
    },
    {
        "id": "d47812eb.9b806",
        "type": "MySQLdatabase",
        "z": "",
        "host": "192.168.200.254",
        "port": "3306",
        "db": "IOTDatabase",
        "tz": ""
    },
    {
        "id": "81aafc6f.ceacd",
        "type": "OpcUa-Endpoint",
        "z": "",
        "endpoint": "opc.tcp://192.168.200.250:4840",
        "secpol": "None",
        "secmode": "NONE",
        "login": false
    },
    {
        "id": "f0685f5e.8dd4d8",
        "type": "modbus-client",
        "z": "",
        "name": "ModbusServerSettings",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "tcpHost": "192.168.200.250",
        "tcpPort": "503",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "unit_id": "2",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectTimeout": "2000"
    }
]