Skip to content

Instantly share code, notes, and snippets.

@zobalogh
Created October 23, 2014 13:21
Show Gist options
  • Save zobalogh/91747f1121c414875ee1 to your computer and use it in GitHub Desktop.
Save zobalogh/91747f1121c414875ee1 to your computer and use it in GitHub Desktop.
Node-RED location storage flow examples
[{"id":"c9b7b9dd.364848","type":"instagram","instagram":"","inputType":"photo","outputType":"link","name":"","x":362,"y":187,"z":"da79d491.258628","wires":[["7cca3aa6.8335c4","aa1d8742.c673c","ec30d402.13cf28"]]},{"id":"78a76c3f.875894","type":"inject","name":"Trigger real life scenario","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":151,"y":187,"z":"da79d491.258628","wires":[["c9b7b9dd.364848"]]},{"id":"7cca3aa6.8335c4","type":"function","name":"Store Location Globally","func":"// only store location if it's new\nif(msg.lat && msg.lon && msg.time) { // do something\n console.log(\"doing something in storer\");\n if(msg.lastGlobalLoc) { // if we're dealing with last known global loc\n console.log(\"dealing with last global loc\");\n \n if(msg.storeInitialValue && msg.storeInitialValue === true) {\n \tcontext.global.lat = context.lat;\n context.global.lon = context.lon;\n context.global.time = context.time;\n msg.storeInitialValue = null;\n } else if(msg.time > context.time) {\n console.log(\"nothing to store\");\n // nothing to store as the global time of location is more recent\n } else {\n console.log(\"updating global location\");\n // need to update global location\n context.global.lat = context.lat;\n context.global.lon = context.lon;\n context.global.time = context.time;\n }\n msg.lastGlobalLoc = null;\n return msg;\n } else { // initial steps\n \n console.log(\"initial send\");\n \n msg.fromStorer = true; // indicate to global location querier that we're the storer\n \n // store the data we received temporarily in case it's new and we need to use it\n context.lat = msg.lat;\n context.lon = msg.lon;\n context.time = msg.time;\n \n\t\treturn msg;\n }\n} else {\n return msg; // just simply retrieve\n}","outputs":"1","x":741,"y":206,"z":"da79d491.258628","wires":[["7394426b.8c6bbc"]]},{"id":"7394426b.8c6bbc","type":"function","name":"Send known global location","func":"if(msg.fromStorer && msg.fromStorer === true) { // need to talk to storer\n\tvar msgToReturn = [];\n\n \tmsg.fromStorer = null;\n\n\tif(context.global.lat && context.global.lon && context.global.time) {\n\t\tconsole.log(\"send global loc talking to storer\");\n\t msg.lat = context.global.lat;\n\t msg.lon = context.global.lon;\n\t msg.time = context.global.time;\n\t msg.lastGlobalLoc = true;\n\t\n\t msgToReturn[0] = msg;\n\t msgToReturn[1] = null;\n\t\n\t console.log(msgToReturn[0]);\n\t} else { // we haven't stored anything yet so we need to store it!\n\t\tmsg.storeInitialValue = true;\n\t\tmsg.lastGlobalLoc = true;\n\t\tmsgToReturn[0] = msg;\n\t msgToReturn[1] = null;\n\t}\n\treturn msgToReturn;\n} else { // simple retrieve\n\tconsole.log(\"the location retriever simply retrieves\");\n\tmsg.lat = context.global.lat;\n\tmsg.lon = context.global.lon;\n\tmsg.time = context.global.time;\n\t\n\t//msg.payload = null;\n\t\n\tvar msgToReturn = [];\n\t\n\tmsgToReturn[0] = null;\n\tmsgToReturn[1] = msg;\n\t\n\treturn msgToReturn;\n}","outputs":"2","x":731,"y":80,"z":"da79d491.258628","wires":[["7cca3aa6.8335c4"],["60242408.9fdbdc"]]},{"id":"60242408.9fdbdc","type":"debug","name":"","active":true,"console":"false","complete":"true","x":1031,"y":200,"z":"da79d491.258628","wires":[]},{"id":"2c5bacbf.d3a454","type":"comment","name":"Only stores location => keeps it modular","info":"","x":613,"y":281,"z":"da79d491.258628","wires":[]},{"id":"a960caf9.569f38","type":"comment","name":"Only retrieves global location => keeps it modular","info":"","x":728,"y":35,"z":"da79d491.258628","wires":[]},{"id":"aa1d8742.c673c","type":"function","name":"Singular Location Handler","func":"if(msg.lat && msg.lon && msg.time) { // USEFUL INFO RECEIVED\n\tif(context.global.lat && context.global.lon && context.global.time) { // we have previous data\n\t\tif(msg.time > context.global.time) { // we have new location, store\n\t\t\tcontext.global.lat = msg.lat;\n\t\t\tcontext.global.lon = msg.lon;\n\t\t\tcontext.global.time = msg.time;\n\t\t} else { //old location is more recent, serve that\n\t\t\tmsg.lat = context.global.lat;\n\t\t\tmsg.lon = context.global.lon;\n\t\t\tmsg.time = context.global.time;\n\t\t}\n\t} else { // we don't have previous data, store\n\t\tcontext.global.lat = msg.lat;\n\t\tcontext.global.lon = msg.lon;\n\t\tcontext.global.time = msg.time;\n\t}\n} else { // NO USEFUL INFO RECEIVED\n // here we have two choices really, one of them is to overwrite the actual location\n msg.lat = context.global.lat;\n msg.lon = context.global.lon;\n msg.time = context.global.time;\n// alternative would be to do nothing\n}\nreturn msg;","outputs":1,"x":587,"y":514,"z":"da79d491.258628","wires":[["50c4ee89.af3b1"]]},{"id":"e34874b6.fa9f28","type":"comment","name":"This location storer deals with both storage and serving of the most recent location and does either store and forward or forward only","info":"","x":649,"y":449,"z":"da79d491.258628","wires":[]},{"id":"50c4ee89.af3b1","type":"debug","name":"","active":true,"console":"false","complete":"true","x":911,"y":516,"z":"da79d491.258628","wires":[]},{"id":"fdf89c20.02076","type":"inject","name":"Demo data storage","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":154,"y":583,"z":"da79d491.258628","wires":[["22c6a64d.dd395a"]]},{"id":"23f52b95.dc0ad4","type":"inject","name":"Demo data retrieval","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":141,"y":62,"z":"da79d491.258628","wires":[["7394426b.8c6bbc"]]},{"id":"2732bffa.d8cd4","type":"inject","name":"Demo data storage","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":131,"y":125,"z":"da79d491.258628","wires":[["265ecb07.d9a134"]]},{"id":"d3d1e26e.2c2e2","type":"inject","name":"Demo data retrieval","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":190,"y":659,"z":"da79d491.258628","wires":[["aa1d8742.c673c"]]},{"id":"265ecb07.d9a134","type":"function","name":"Now!","func":"msg.lat=10;\nmsg.lon=-10;\nmsg.time = new Date();\nreturn msg;","outputs":1,"x":389,"y":112,"z":"da79d491.258628","wires":[["7cca3aa6.8335c4"]]},{"id":"22c6a64d.dd395a","type":"function","name":"Now!","func":"msg.lat=10;\nmsg.lon=-10;\nmsg.time = new Date();\nreturn msg;","outputs":1,"x":322,"y":510,"z":"da79d491.258628","wires":[["aa1d8742.c673c"]]},{"id":"ec30d402.13cf28","type":"debug","name":"See if Instagram returns anything","active":true,"console":"false","complete":"payload","x":276,"y":358,"z":"da79d491.258628","wires":[]},{"id":"e25b0e87.1da4f","type":"comment","name":"I can see why Instagram returning nothing can be useless for some scenarios","info":"","x":732,"y":357,"z":"da79d491.258628","wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment