IoTGateway/Device Tracking

From ESS-WIKI
Jump to: navigation, search

Open Node-RED page on Gateway

  • Use the url (http://<gateway's ip>:1880/) to connect to the homepage of node-red

Import code into Node-RED

  • open the import text window
Device tracking Import.png Device tracking Paste.png
  • paste the follow code
[{"id":"e4eae45f.44fe18","type":"mqtt-broker","z":"3d8b5061.ac304","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"willTopic":"","willQos":"0","willRetain":null,"willPayload":"","birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":""},{"id":"efffc0ff.efc02","type":"mqtt in","z":"3d8b5061.ac304","name":"","topic":"/cagent/admin/+/deviceinfo","broker":"e4eae45f.44fe18","x":188,"y":128,"wires":[["bcb067b8.af2618"]]},{"id":"87f6a6c7.f3fdf8","type":"debug","z":"3d8b5061.ac304","name":"","active":true,"console":"false","complete":"payload","x":599,"y":41,"wires":[]},{"id":"bcb067b8.af2618","type":"function","z":"3d8b5061.ac304","name":"Avilable list","func":"var mac = msg.topic.substring(18,30);\nvar size = global.get('size')||0;\nvar tag = \"list\";\nvar alive = \"alive\";\n\nif(size == 0) {\n var itag = tag + 0;\n var iAlive = alive + 0;\n size++;\n var device = global.get(itag);\n device = mac;\n global.set(itag,device);\n global.set(iAlive,Date.now());\n global.set('size',size);\n} else {\n for(i = 0 ; i < size ; i++) {\n var itag = tag + i;\n var device = global.get(itag);\n if(device === mac) {\n var iAlive = alive + i;\n global.set(iAlive,Date.now());\n break;\n }\n }\n msg.topic;\n if(i == size) {\n var itag = tag + size;\n var iAlive = alive + size;\n size++;\n var device = global.get(itag);\n device = mac;\n global.set(itag,device);\n global.set(iAlive,Date.now());\n global.set('size',size);\n }\n}\n\nreturn msg;","outputs":"1","noerr":0,"x":413,"y":128,"wires":[[]]},{"id":"b6817c24.6717a","type":"inject","z":"3d8b5061.ac304","name":"Timer","topic":"","payload":"","payloadType":"date","repeat":"10","crontab":"","once":true,"x":153,"y":93,"wires":[["836f735b.94d9c"]]},{"id":"836f735b.94d9c","type":"function","z":"3d8b5061.ac304","name":"Check and print","func":"var last = global.get('last')||0;\nvar now = msg.payload||0;\nvar interval = (now-last);\nvar size = global.get('size')||0;\nvar tag = \"list\";\nvar alive = \"alive\";\n\nvar countin = 0;\nvar countout = 0;\nvar ininterval = \"\";\nvar outinterval = \"\";\nfor(i = 0 ; i < size ; i++) {\n var itag = tag + i;\n var iAlive = alive + i;\n var device = global.get(itag);\n var a = global.get(iAlive)||0;\n if(now - a < interval) {\n if(countin == 0) {\n ininterval += device + \"(\" + new Date(a).toString() + \")\";\n } else {\n ininterval += \", \" + device + \"(\" + new Date(a).toString() + \")\";\n }\n countin++;\n } else {\n if(countout == 0) {\n outinterval += device + \"(\" + new Date(a).toString() + \")\";\n } else {\n outinterval += \", \" + device + \"(\" + new Date(a).toString() + \")\";\n }\n countout++;\n \n }\n}\nlast = now;\nglobal.set('last', last);\nvar result1 = \"Available: [ \" + ininterval + \" ]\";\nvar result2 = \"OverTime: [ \" + outinterval + \" ]\";\nvar msg1 = {payload:result1};\nvar msg2 = {payload:result2};\n\nreturn [msg1, msg2];","outputs":"2","noerr":0,"x":423,"y":93,"wires":[["87f6a6c7.f3fdf8"],["fb905b9f.84e1c8"]]},{"id":"fb905b9f.84e1c8","type":"debug","z":"3d8b5061.ac304","name":"","active":true,"console":"false","complete":"payload","x":599,"y":149,"wires":[]},{"id":"87e46619.54fc78","type":"inject","z":"3d8b5061.ac304","name":"Reset","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":155,"y":270,"wires":[["41293137.f3da"]]},{"id":"41293137.f3da","type":"function","z":"3d8b5061.ac304","name":"Reset All","func":"var size = global.get('size')||0;\nvar tag = \"list\";\nvar alive = \"alive\";\n\nfor(i = 0 ; i < size ; i++) {\n var itag = tag + i;\n var iAlive = alive + i;\n var a = global.get(iAlive)||0;\n global.set(iAlive, 0);\n global.set(itag, \"\");\n}\nglobal.set('size', 0);\nglobal.set('last', 0);\n\nreturn msg;","outputs":1,"noerr":0,"x":314,"y":270,"wires":[[]]},{"id":"a9e2e60d.c26178","type":"http in","z":"3d8b5061.ac304","name":"DeviceTracking","url":"/devicetracking","method":"get","swaggerDoc":"","x":157,"y":356,"wires":[["b9e40669.f3b5c8"]]},{"id":"8f5a71e7.feae4","type":"debug","z":"3d8b5061.ac304","name":"","active":true,"console":"false","complete":"false","x":589,"y":352,"wires":[]},{"id":"80cef633.eb5378","type":"http response","z":"3d8b5061.ac304","name":"DeviceTracking","x":601,"y":445,"wires":[]},{"id":"b9e40669.f3b5c8","type":"function","z":"3d8b5061.ac304","name":"HTML","func":"var last = global.get('weblast')||0;\nvar now = Date.now()||0;\nvar interval = (now-last);\nvar size = global.get('size')||0;\nvar tag = \"list\";\nvar alive = \"alive\";\n\nvar countin = 0;\nvar countout = 0;\nvar ininterval = \"\";\nvar outinterval = \"\";\nfor(i = 0 ; i < size ; i++) {\n var itag = tag + i;\n var iAlive = alive + i;\n var device = global.get(itag);\n var a = global.get(iAlive)||0;\n if(now - a < interval) {\n if(countin == 0) {\n ininterval += device + \"<br>*(\" + new Date(a).toString() + \")\";\n } else {\n ininterval += \"<br>\" + device + \"<br>*(\" + new Date(a).toString() + \")\";\n }\n countin++;\n } else {\n if(countout == 0) {\n outinterval += device + \"<br>*(\" + new Date(a).toString() + \")\";\n } else {\n outinterval += \"<br>\" + device + \"<br>*(\" + new Date(a).toString() + \")\";\n }\n countout++;\n \n }\n}\nlast = now;\nglobal.set('weblast', last);\n\n\nmsg.payload = {\"result\":ininterval, \"overtime\":outinterval};\nreturn msg;","outputs":1,"noerr":0,"x":311,"y":447,"wires":[["80cef633.eb5378"]]}]
  • By pasting, you can see the layout in Node-RED like this.
Device tracking Layout.png

Deploy and Tracking the result

  • Push the Deploy button and choose the debug tag
Device tracking Deploy.png
  • Tracking the results
Device tracking Result.png

The format of the result

The results include two parts.
  • The available table shows the device that is still alive.
  • The overtime table shows the device that hangs over the trigger time.
  • The elements include mac and time.
MAC(TIME)
000E40000000(Wed Jun 22 2016 09:53:05 GMT+0800 (CST)), 000E40000001(Wed Jun 22 2016 09:53:05 GMT+0800 (CST))

Advance Operating

  • Reset all table
You can push the Reset button to reset the cache in the Node-RED.
Device Tracking Reset.png
  • Change the trigger time
You can change the period by modifying the parameter, Repeat, of the timer component.
Device Tracking Timer.png