This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wot-node-red.json
1 lines (1 loc) · 14.7 KB
/
wot-node-red.json
1
[{"id":"9000cf6e.f407d","type":"http in","z":"a2bd84f.c4e2378","name":"/things/lamp/v1","url":"/things/lamp/v1/*","method":"all","upload":false,"swaggerDoc":"","x":94,"y":199,"wires":[["6cb0c692.a4fc38","114d47b0.beb9d8"]]},{"id":"6cb0c692.a4fc38","type":"function","z":"a2bd84f.c4e2378","name":"URL routing","func":"msg.req.url = msg.req.url.replace('/things/lamp/v1','')\nif ( msg.req.url.includes(\"properties\"))\n return [ msg, null];\nif ( msg.req.url.includes(\"actions\"))\n return [ null, msg];\nreturn [msg, null];","outputs":2,"noerr":0,"x":271,"y":198,"wires":[["572d78ce.7c3ea8"],["92db8afa.8f8608"]]},{"id":"572d78ce.7c3ea8","type":"switch","z":"a2bd84f.c4e2378","name":"/properties","property":"req.url","propertyType":"msg","rules":[{"t":"eq","v":"/properties/brightness","vt":"str"},{"t":"eq","v":"/properties/on","vt":"str"},{"t":"eq","v":"/properties","vt":"str"},{"t":"eq","v":"/","vt":"str"}],"checkall":"false","repair":false,"outputs":4,"x":406,"y":112,"wires":[["de717730.6e54d8"],["738fbc0c.f29384"],["be3542bb.9c7c7"],["120e787f.6e81d8"]]},{"id":"2c740d63.eca392","type":"http response","z":"a2bd84f.c4e2378","name":"things/lamp/v1","statusCode":"200","headers":{},"x":977,"y":214,"wires":[]},{"id":"738fbc0c.f29384","type":"function","z":"a2bd84f.c4e2378","name":"/properties/on","func":"var property = flow.get(\"properties\") || { on : 0, level: 0 };\n\nswitch ( msg.req.method ) {\ncase \"GET\" : msg.payload = { on : property.on }; \n break;\ncase \"PUT\" : property.on = msg.payload.on;\n flow.set(\"properties\", property); \n msg.payload = { on : property.on };\n break;\n} \nreturn msg;","outputs":1,"noerr":0,"x":591,"y":97,"wires":[["2c740d63.eca392"]]},{"id":"de717730.6e54d8","type":"function","z":"a2bd84f.c4e2378","name":"/properties/brightness","func":"var property = flow.get(\"properties\") || { on : 0, level: 0 };\n\nswitch ( msg.req.method ) {\ncase \"GET\" : msg.payload = { level : property.level }; \n break;\ncase \"PUT\" : property.level = msg.payload.level;\n flow.set(\"properties\", property); \n msg.payload = { level : property.level };\n break;\n} \nreturn msg;","outputs":1,"noerr":0,"x":609.5,"y":62,"wires":[["2c740d63.eca392"]]},{"id":"be3542bb.9c7c7","type":"function","z":"a2bd84f.c4e2378","name":"/properties","func":"// GET method \nmsg.payload = flow.get(\"properties\") || { on : 0, level: 0 };\nreturn msg;","outputs":1,"noerr":0,"x":582.5,"y":132,"wires":[["2c740d63.eca392"]]},{"id":"120e787f.6e81d8","type":"function","z":"a2bd84f.c4e2378","name":"/","func":"\nmsg.payload = JSON.parse(`{\n \"name\": \"WoT Lamp\",\n \"description\": \"A WoT connected Raspberry Lamp\",\n \"bathPath\" : \"/things/lamp/v1\",\n \"attributes\": {\n \"on\": {\n \"title\": \"on/off\",\n \"description\": \"on/off switch\",\n \"propertytype\": \"boolean\",\n \"unit\": \"boolean\",\n \"minimum\": 0,\n \"maximum\": 1,\n \"href\": \"/properties/on\"\n },\n \"level\": {\n \"title\": \"brihtness\",\n \"description\": \"adjust brightness level\",\n \"propertytype\": \"number\",\n \"unit\": \"number\",\n \"minimum\": 0,\n \"maximum\": 100,\n \"href\": \"/properties/brightness\"\n }\n },\n \"actions\": {\n \"fade\": {\n \"level\": 56,\n \"duration\": 30,\n \"href\": \"/actions/fade\"\n },\n \"reboot\": {\n \"delay\": 5,\n \"href\": \"/actions/reboot\"\n }\n },\n \"events\": {\n \"overheated\": {\n \"eventType\": \"overheated\",\n \"data\": \"105\",\n \"timestamp\": \"2017-01-25T15:01:35+00:00\"\n },\n \"reboot\": {\n \"eventType\": \"reboot\",\n \"timestamp\": \"2017-01-24T15:01:35+00:00\"\n }\n }\n}`);\nreturn msg;","outputs":1,"noerr":0,"x":564.5,"y":168,"wires":[["2c740d63.eca392"]]},{"id":"92db8afa.8f8608","type":"switch","z":"a2bd84f.c4e2378","name":"/actions","property":"req.url","propertyType":"msg","rules":[{"t":"regex","v":"\\/actions\\/events\\/overheated|reboot","vt":"str","case":false},{"t":"eq","v":"/actions/events","vt":"str"},{"t":"regex","v":"\\/actions\\/fade\\/\\w+","vt":"str","case":false},{"t":"eq","v":"/actions/fade","vt":"str"},{"t":"eq","v":"/actions","vt":"str"}],"checkall":"false","repair":false,"outputs":5,"x":406.5,"y":291,"wires":[["4f374ce0.8e71b4"],["53a22aa0.2df854"],["7f738b7c.9523d4"],["bb6ce52f.bf3888"],["73894dd1.99db74"]]},{"id":"53a22aa0.2df854","type":"function","z":"a2bd84f.c4e2378","name":"/actions/events","func":"const homeDir = require('os').homedir();\nvar Datastore = require('nedb');\nvar db = new Datastore(homeDir+'/events.db');\ndb.loadDatabase();\n\nfunction sendResponse( err, docs ) {\n var newDocs = JSON.parse(JSON.stringify(docs));\n \n newDocs.sort(function(x, y){\n return y._timestamp - x._timestamp;\n });\n \n newDocs.forEach(function(doc) {\n delete doc._id; \n delete doc._timestamp;\n });\n \n \n msg.payload = newDocs;\n node.send(msg);\n}\n\n\ndb.find({}, sendResponse) ;\n\n","outputs":1,"noerr":0,"x":595.5,"y":255,"wires":[["2c740d63.eca392"]]},{"id":"7f738b7c.9523d4","type":"function","z":"a2bd84f.c4e2378","name":"/actions/fade/{transaction-id}","func":"const homeDir = require('os').homedir();\nvar Datastore = require('nedb');\nvar db = new Datastore(homeDir+'/actions.db');\ndb.loadDatabase();\n\nvar date = new Date();\nvar dateStr = date.toDateString();\nvar timeStr = date.toLocaleTimeString();\n\n\n\nfunction sendResponse( err, docs ) {\n var newDocs = JSON.parse(JSON.stringify(docs));\n \n newDocs.forEach(function(doc) {\n delete doc._id;\n });\n \n msg.payload = newDocs;\n node.send(msg);\n}\n\n\nswitch ( msg.req.method ) {\ncase \"GET\" : \n db.find({ href: msg.req.url }, sendResponse ) ;\n break;\ncase \"DELETE\" :\n\n db.remove({ href: msg.req.url } , function (err, newDoc) { \n // do nothing\n }); \n return msg;\n break; // Never Reach Here\n}\n","outputs":1,"noerr":0,"x":636,"y":290,"wires":[["2c740d63.eca392","d777a7c1.328908"]]},{"id":"bb6ce52f.bf3888","type":"function","z":"a2bd84f.c4e2378","name":"/actions/fade","func":"const homeDir = require('os').homedir();\nvar Datastore = require('nedb');\nvar db = new Datastore(homeDir+'/actions.db');\ndb.loadDatabase();\n\nvar edb = new Datastore(homeDir+'/events.db');\nedb.loadDatabase();\n\nvar date = new Date();\nvar dateStr = date.toDateString();\nvar timeStr = date.toLocaleTimeString();\n\nfunction makeid(length) {\n var text = \"\";\n var possible = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\n for (var i = 0; i < length; i++)\n text += possible.charAt(Math.floor(Math.random() * possible.length));\n\n return text;\n}\n\nfunction sendResponse( err, docs ) {\n var newDocs = JSON.parse(JSON.stringify(docs));\n \n newDocs.sort(function(x, y){\n return y._timestamp - x._timestamp;\n });\n \n newDocs.forEach(function(doc) {\n delete doc._id; \n delete doc._timestamp;\n });\n //node.warn( newDocs);\n msg.payload = newDocs;\n node.send([msg, null]);\n}\n\n\nswitch ( msg.req.method ) {\ncase \"GET\" : \n db.find({}, sendResponse ) ;\n break;\ncase \"POST\" :\n if ( parseInt(msg.payload.fade.duration) < 50) {\n \n var temperature = parseInt(msg.payload.fade.duration) * 2;\n var newEvent = { \n eventType: 'overheated' , \n timestamp: dateStr+' '+timeStr,\n data: temperature.toString(),\n _timestamp: Date.now()\n };\n\n edb.insert( newEvent, function (err, newDoc) { \n // do nothing \n }); \n }\n var transactionId = msg.payload.fade.href + '/'+makeid(5)+'-'+makeid(5)+'-'+makeid(10);\n var newAction = { \n fade: msg.payload.fade ,\n href: transactionId ,\n status: 'pending',\n timeRequested: dateStr+' '+timeStr, \n timeCompleted: 'N/A',\n _timestamp: Date.now()\n };\n\n db.insert(newAction, function (err, newDoc) { \n // do nothing\n }); \n \n var work = {};\n work.payload = transactionId;\n msg.payload = newAction; \n return [ msg, work ];\n break; // Never Reach Here\n}\n","outputs":2,"noerr":0,"x":595,"y":370,"wires":[["2c740d63.eca392","d777a7c1.328908"],["b49c866c.a8df08"]]},{"id":"4f374ce0.8e71b4","type":"function","z":"a2bd84f.c4e2378","name":"/actions/events/{event-type}","func":"const homeDir = require('os').homedir();\nvar Datastore = require('nedb');\nvar db = new Datastore(homeDir+'/events.db');\ndb.loadDatabase();\n\nvar eventReq = msg.req.url.replace(\"/actions/events/\", \"\");\n\nfunction sendResponse( err, docs ) {\n \n var newDocs = JSON.parse(JSON.stringify(docs));\n \n newDocs.sort(function(x, y){\n return y._timestamp - x._timestamp;\n });\n \n newDocs.forEach(function(doc) {\n delete doc._id; \n delete doc._timestamp;\n });\n\n if ( newDocs.length === 0 ) \n msg.payload = [ { eventType: eventReq , timestamps : 'not occurred' }];\n else \n msg.payload = newDocs;\n node.send(msg);\n}\n\n\ndb.find({ eventType : eventReq }, sendResponse) ;","outputs":1,"noerr":0,"x":634.5,"y":220,"wires":[["2c740d63.eca392"]]},{"id":"73894dd1.99db74","type":"function","z":"a2bd84f.c4e2378","name":"/actions","func":"const homeDir = require('os').homedir();\nvar Datastore = require('nedb');\nvar db = new Datastore(homeDir+'/actions.db');\ndb.loadDatabase();\n\nvar edb = new Datastore(homeDir+'/events.db');\nedb.loadDatabase();\n\nvar date = new Date();\nvar dateStr = date.toDateString();\nvar timeStr = date.toLocaleTimeString();\n\nfunction makeid(length) {\n var text = \"\";\n var possible = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\n for (var i = 0; i < length; i++)\n text += possible.charAt(Math.floor(Math.random() * possible.length));\n\n return text;\n}\n\nfunction sendResponse( err, docs ) {\n var newDocs = JSON.parse(JSON.stringify(docs));\n \n newDocs.sort(function(x, y){\n return y._timestamp - x._timestamp;\n });\n \n newDocs.forEach(function(doc) {\n delete doc._id; \n delete doc._timestamp;\n });\n //node.warn( newDocs);\n msg.payload = newDocs;\n node.send([msg, null]);\n}\n\n\nswitch ( msg.req.method ) {\ncase \"GET\" : \n db.find({}, sendResponse ) ;\n break;\ncase \"POST\" :\n var transactionId = msg.payload.fade.href + '/'+makeid(5)+'-'+makeid(5)+'-'+makeid(10);\n \n // if msg.payload.fade.duration < 50\n // trigger oveheated events for testing\n if ( parseInt(msg.payload.fade.duration) < 50) {\n \n var temperature = parseInt(msg.payload.fade.duration) * 2;\n var newEvent = { \n eventType: 'overheated' , \n timestamp: dateStr+' '+timeStr,\n data: temperature.toString(),\n _timestamp: Date.now()\n \n };\n\n edb.insert( newEvent, function (err, newDoc) { \n // do nothing \n }); \n }\n var newAction = { \n fade: msg.payload.fade ,\n href: transactionId,\n status: 'pending',\n timeRequested: dateStr+' '+timeStr, \n timeCompleted: 'N/A',\n _timestamp: Date.now()\n };\n\n db.insert(newAction, function (err, newDoc) { \n // do nothing\n }); \n \n var work = {};\n work.payload = transactionId;\n msg.payload = newAction;\n return [ msg, work ];\n break; // Never Reach Here\n}\n\n","outputs":2,"noerr":0,"x":585.5,"y":408,"wires":[["2c740d63.eca392","d777a7c1.328908"],["b49c866c.a8df08"]]},{"id":"55d769c4.468388","type":"function","z":"a2bd84f.c4e2378","name":"pending to complete","func":"const homeDir = require('os').homedir();\nvar Datastore = require('nedb');\nvar db = new Datastore(homeDir+'/actions.db');\ndb.loadDatabase();\n\nvar date = new Date();\nvar dateStr = date.toDateString();\nvar timeStr = date.toLocaleTimeString();\n\n\ndb.update({ href: msg.payload }, { $set: { status: 'complete',timeCompleted : dateStr+' '+timeStr } }, { }, function (err, numReplaced) {\n \n // Do Nothing. Assume that everything is fine. \n});\nreturn null;","outputs":1,"noerr":0,"x":1005,"y":446,"wires":[[]]},{"id":"843c02b9.376ff","type":"inject","z":"a2bd84f.c4e2378","name":"reboot","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"1","x":89.5,"y":401,"wires":[["9631a762.694b68"]]},{"id":"9631a762.694b68","type":"function","z":"a2bd84f.c4e2378","name":"register \"reboot\" event","func":"const homeDir = require('os').homedir();\nvar Datastore = require('nedb');\nvar db = new Datastore(homeDir+'/events.db');\ndb.loadDatabase();\n\nvar date = new Date();\nvar dateStr = date.toDateString();\nvar timeStr = date.toLocaleTimeString();\n \nvar newEvent = { \n eventType: 'reboot' , \n timestamp: dateStr+' '+timeStr,\n _timestamp: Date.now()\n};\n\ndb.insert(newEvent, function (err, newDoc) { \n // do nothing\n});\n\nreturn null;","outputs":1,"noerr":0,"x":280.5,"y":401,"wires":[[]]},{"id":"d777a7c1.328908","type":"debug","z":"a2bd84f.c4e2378","name":"Debug Actions","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":974,"y":264,"wires":[]},{"id":"114d47b0.beb9d8","type":"debug","z":"a2bd84f.c4e2378","name":"Debug Request","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":234.5,"y":246,"wires":[]},{"id":"15e6074b.08c859","type":"debug","z":"a2bd84f.c4e2378","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":979.5,"y":485,"wires":[]},{"id":"b49c866c.a8df08","type":"function","z":"a2bd84f.c4e2378","name":"2-5 secs delay","func":"var delay = Math.floor(Math.random() * 5) + 2; // 2 -5 secs\nnode.status({fill:\"blue\", shape:\"ring\", text:\"\"});\n\nsetTimeout(function(){\n node.status({fill:\"blue\", shape:\"ring\", text:\"complete\"});\n node.send(msg);\n}, 3000);\nnode.status({fill:\"red\", shape:\"ring\", text:\"pending\"});\n","outputs":1,"noerr":0,"x":797.5,"y":445,"wires":[["55d769c4.468388","15e6074b.08c859"]]},{"id":"aebb3c3f.2c23c","type":"comment","z":"a2bd84f.c4e2378","name":"Whenever a flow is deployed, \"reboot\" event is occurred","info":"","x":223.5,"y":359,"wires":[]},{"id":"572cdd80.dff084","type":"comment","z":"a2bd84f.c4e2378","name":"Base URL Path /things/lamp/v1","info":"","x":143.5,"y":153,"wires":[]},{"id":"2a66c3ea.3b325c","type":"comment","z":"a2bd84f.c4e2378","name":"action event \"pending\" is triggered ","info":"","x":857.5,"y":404,"wires":[]},{"id":"ff76f866.218cc8","type":"comment","z":"a2bd84f.c4e2378","name":"If fade.duration < 50, \"overheated\" event is triggered","info":"","x":710.5,"y":333,"wires":[]}]