[{"id":"97598a17814b14f8","type":"tab","label":"SHARC simulation","disabled":false,"info":"","env":[]},{"id":"d5522d007efa7ff0","type":"tab","label":"Calculate count","disabled":false,"info":"","env":[]},{"id":"76bf22aa55ad43c5","type":"tab","label":"Calculate machine state","disabled":false,"info":"","env":[]},{"id":"619e946ac7946ccf","type":"tab","label":"Add Shift","disabled":false,"info":""},{"id":"a615d1d864eb51d8","type":"mqtt-broker","name":"","broker":"united-manufacturing-hub-mqtt","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"5dff9791.d1b278","type":"mqtt-broker","name":"","broker":"united-manufacturing-hub-vernemq","port":"1883","clientid":"","usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"c8cb7a58b3c6a6df","type":"inject","z":"97598a17814b14f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":180,"wires":[["7a68dbf8f426ea57","03a84e55fdee4c36"]]},{"id":"7a68dbf8f426ea57","type":"function","z":"97598a17814b14f8","name":"TotalCount","func":"// Fetch the current value of \"seq\" from the flow context store\nvar seq = flow.get('seq') || 1;\n\nvar totalCount = flow.get('totalCount') || 0;\n\n// Increase \"seq\" by one\nseq++;\n\n// Increase totalCount by random amount between 1 and 5\ntotalCount += Math.floor(Math.random() * 5) + 1;\n\nmsg.payload = {\n    \"seq\": seq,\n    \"v\": {\n        \"s1\": {\n            \"u\": \"/\",\n            \"v\": totalCount\n        }\n    }\n};\n\nmsg.topic = \"sharc/e831cd9b8e00/evt/io\";\n\n// Store the updated value of \"seq\" in the flow context store for the next message\nflow.set('seq', seq);\n\n// Store the updated value of \"totalCount\" in the flow context store for the next message\nflow.set('totalCount', totalCount);\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":180,"wires":[["deda6a1cf63f60f9"]]},{"id":"deda6a1cf63f60f9","type":"mqtt out","z":"97598a17814b14f8","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"a615d1d864eb51d8","x":650,"y":180,"wires":[]},{"id":"03a84e55fdee4c36","type":"function","z":"97598a17814b14f8","name":"AccumulationMode","func":"// Fetch the current value of \"seq\" from the flow context store\nvar seq = flow.get('seq') || 1;\n\n// Increase \"seq\" by one\nseq++;\n\nmsg.payload = {\n    \"seq\": seq,\n    \"v\": {\n        \"s1\": {\n            \"u\": \"/\",\n            \"v\": 1\n        }\n    }\n};\n\nmsg.topic = \"sharc/e831cd9b8e00/evt/io2\";\n\n// Store the updated value of \"seq\" in the flow context store for the next message\nflow.set('seq', seq);\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":260,"wires":[["deda6a1cf63f60f9"]]},{"id":"6807909e4ea62a27","type":"mqtt in","z":"d5522d007efa7ff0","name":"","topic":"sharc/e831cd9b8e00/evt/io","qos":"2","datatype":"auto-detect","broker":"a615d1d864eb51d8","nl":false,"rap":true,"rh":0,"inputs":0,"x":210,"y":200,"wires":[["fda2aa07fcdb227f"]]},{"id":"fda2aa07fcdb227f","type":"function","z":"d5522d007efa7ff0","name":"function 2","func":"// Retrieve the last total count value from the flow context store\nvar lastCount = flow.get('lastCount') || 0;\n\n// Calculate the difference between the current count and the last count\nvar count = msg.payload.v.s1.v - lastCount;\n\n// Update the 'lastCount' in the flow context store\nflow.set('lastCount', msg.payload.v.s1.v);\n\n// Create the 'count' message\nvar countMsg = {\n    \"timestamp_ms\": Date.now(), // current unix timestamp in milliseconds\n    \"count\": count,\n};\n\n// Set the new payload and topic\nmsg.payload = countMsg;\nmsg.topic = `ia/factoryinsight/jeremytest/v1/count`; // replace placeholders\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":200,"wires":[["537aec714a06e33a"]]},{"id":"537aec714a06e33a","type":"mqtt out","z":"d5522d007efa7ff0","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"a615d1d864eb51d8","x":590,"y":200,"wires":[]},{"id":"6b4e562897c19c71","type":"comment","z":"d5522d007efa7ff0","name":"Option 1 (not recommended)","info":"","x":220,"y":160,"wires":[]},{"id":"2146cb073e39b3f5","type":"comment","z":"d5522d007efa7ff0","name":"Option 2 (recommended, assumes v.s1.v is already the count)","info":"","x":320,"y":320,"wires":[]},{"id":"b8300115c7abfdb1","type":"mqtt in","z":"d5522d007efa7ff0","name":"","topic":"sharc/e831cd9b8e00/evt/io2","qos":"2","datatype":"auto-detect","broker":"a615d1d864eb51d8","nl":false,"rap":true,"rh":0,"inputs":0,"x":220,"y":360,"wires":[["62ea80a43a476683"]]},{"id":"62ea80a43a476683","type":"function","z":"d5522d007efa7ff0","name":"function 3","func":"// Create the 'count' message\nvar countMsg = {\n    \"timestamp_ms\": Date.now(), // current unix timestamp in milliseconds\n    \"count\": msg.payload.v.s1.v,\n};\n\n// Set the new payload and topic\nmsg.payload = countMsg;\nmsg.topic = `ia/factoryinsight/jeremytest/v2/count`; // replace placeholders\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":360,"wires":[["42551d1008e0635c"]]},{"id":"42551d1008e0635c","type":"mqtt out","z":"d5522d007efa7ff0","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"a615d1d864eb51d8","x":590,"y":360,"wires":[]},{"id":"773d59f361536482","type":"mqtt in","z":"76bf22aa55ad43c5","name":"","topic":"ia/factoryinsight/jeremytest/v2/count","qos":"2","datatype":"auto-detect","broker":"a615d1d864eb51d8","nl":false,"rap":true,"rh":0,"inputs":0,"x":200,"y":200,"wires":[["d01cdc0dbdc87cf8"]]},{"id":"36d0a1bede6d3689","type":"inject","z":"76bf22aa55ad43c5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":280,"wires":[["203b0416c27716c3"]]},{"id":"d01cdc0dbdc87cf8","type":"function","z":"76bf22aa55ad43c5","name":"function 5","func":"// Set the the last count timestamp\nflow.set('lastCountTime', Date.now())\n\nmsg.payload = Date.now()\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":200,"wires":[[]]},{"id":"203b0416c27716c3","type":"function","z":"76bf22aa55ad43c5","name":"function 6","func":"// Fetch the last count time from the flow context store\nvar lastCountTime = flow.get('lastCountTime') || Date.now();\n\n// If current time - last count time > 10 seconds (10,000 milliseconds), the machine is not running\nif (Date.now() - lastCountTime > 10000) {\n    // Machine not running\n    msg.payload = {\n        \"timestamp_ms\": Date.now(), // current unix timestamp in milliseconds\n        \"state\": 40000\n    };\n} else {\n    // Machine running\n    msg.payload = {\n        \"timestamp_ms\": Date.now(), // current unix timestamp in milliseconds\n        \"state\": 10000\n    };\n}\nmsg.topic = `ia/factoryinsight/jeremytest/v2/state`; // replace placeholders\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":280,"wires":[["0e3474e48845c050"]]},{"id":"0e3474e48845c050","type":"rbe","z":"76bf22aa55ad43c5","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload.state","topi":"topic","x":550,"y":280,"wires":[["1935b4a101df6fc5"]]},{"id":"1935b4a101df6fc5","type":"mqtt out","z":"76bf22aa55ad43c5","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"a615d1d864eb51d8","x":710,"y":280,"wires":[]},{"id":"195f235c146b5b59","type":"comment","z":"619e946ac7946ccf","name":"Shift starts every weekday at 9am and ends at 5pm","info":"","x":550,"y":140,"wires":[]},{"id":"f66c534fd0177614","type":"function","z":"619e946ac7946ccf","name":"ia/factoryinsight/jeremytest/v2/addShift","func":"msg.payload = {\n  \"timestamp_ms\": Date.now(),\n  \"timestamp_ms_end\": Date.now()+28800000\n}\nmsg.topic = \"ia/factoryinsight/jeremytest/v2/addShift\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":610,"y":260,"wires":[["f86d218bb5a16e14"]]},{"id":"f86d218bb5a16e14","type":"mqtt out","z":"619e946ac7946ccf","name":"","topic":"","qos":"1","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"5dff9791.d1b278","x":870,"y":240,"wires":[]},{"id":"67d71e634b555fc5","type":"cronplus","z":"619e946ac7946ccf","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"schedule2","topic":"topic2","payloadType":"default","payload":"","expressionType":"cron","expression":"0 0 7 ? * MON,TUE,WED,THU,FRI *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":260,"y":280,"wires":[["f66c534fd0177614"]]},{"id":"ec2bba07615bad5a","type":"inject","z":"619e946ac7946ccf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":260,"y":220,"wires":[["f66c534fd0177614"]]}]