Skip to content

Instantly share code, notes, and snippets.

@zobalogh
Created October 29, 2014 11:29
Show Gist options
  • Save zobalogh/dd5b744de65ea476ba87 to your computer and use it in GitHub Desktop.
Save zobalogh/dd5b744de65ea476ba87 to your computer and use it in GitHub Desktop.
Location based Weather Example
[{"id":"c9b7b9dd.364848","type":"instagram","instagram":"","inputType":"photo","outputType":"link","name":"","x":355,"y":75,"z":"da79d491.258628","wires":[["aa1d8742.c673c","ec30d402.13cf28"]]},{"id":"78a76c3f.875894","type":"inject","name":"Trigger real life scenario","topic":"","payload":"","payloadType":"date","repeat":"300","crontab":"","once":true,"x":144,"y":43,"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":727.5,"y":804,"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":717.5,"y":652,"z":"da79d491.258628","wires":[["7cca3aa6.8335c4"],["60242408.9fdbdc"]]},{"id":"60242408.9fdbdc","type":"debug","name":"","active":true,"console":"false","complete":"true","x":997.5,"y":730,"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 if we have it\n\tif(context.global.lat && context.global.lon && context.global.time) {\n\t msg.lat = context.global.lat;\n \tmsg.lon = context.global.lon;\n \tmsg.time = context.global.time;\n\t} else {\n\t\t// sad but we can't add location info\n\t}\n\t// alternative would be to do nothing\n}\nreturn msg;","outputs":1,"x":572,"y":170,"z":"da79d491.258628","wires":[["50c4ee89.af3b1","9b40dc2f.64bf2"]]},{"id":"50c4ee89.af3b1","type":"debug","name":"","active":true,"console":"false","complete":"true","x":896,"y":172,"z":"da79d491.258628","wires":[]},{"id":"fdf89c20.02076","type":"inject","name":"Demo data storage","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":139,"y":239,"z":"da79d491.258628","wires":[["22c6a64d.dd395a"]]},{"id":"23f52b95.dc0ad4","type":"inject","name":"Demo data retrieval","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":204.5,"y":684,"z":"da79d491.258628","wires":[["7394426b.8c6bbc"]]},{"id":"2732bffa.d8cd4","type":"inject","name":"Demo data storage","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":207.5,"y":743,"z":"da79d491.258628","wires":[["265ecb07.d9a134"]]},{"id":"d3d1e26e.2c2e2","type":"inject","name":"Demo data retrieval","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":175,"y":315,"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":440.5,"y":743,"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":317,"y":169,"z":"da79d491.258628","wires":[["aa1d8742.c673c"]]},{"id":"ec30d402.13cf28","type":"debug","name":"See what Instagram returns","active":true,"console":"false","complete":"payload","x":899,"y":74,"z":"da79d491.258628","wires":[]},{"id":"383256ae.c7cdaa","type":"openweathermap","name":"","lon":"","lat":"","city":"","country":"","x":723,"y":373,"z":"da79d491.258628","wires":[["cca5fefb.335a"]]},{"id":"313b9aa6.cec466","type":"inject","name":"Check weather at location","topic":"","payload":"","payloadType":"none","repeat":"1800","crontab":"","once":false,"x":391,"y":400,"z":"da79d491.258628","wires":[["aa1d8742.c673c"]]},{"id":"cca5fefb.335a","type":"debug","name":"","active":true,"console":"false","complete":"false","x":955,"y":430,"z":"da79d491.258628","wires":[]},{"id":"9b40dc2f.64bf2","type":"function","name":"Temporary Message Converter","func":"msg.payload = {};\nmsg.payload.lat = msg.lat;\nmsg.payload.lon = msg.lon;\n\nconsole.log(\"PAYLOAD TO WEATHER:\\n\" + msg.payload);\n\nreturn msg;","outputs":1,"x":762,"y":258,"z":"da79d491.258628","wires":[["383256ae.c7cdaa"]]},{"id":"bd8fcf1d.42703","type":"comment","name":"Retrieve weather for our known location","info":"","x":972,"y":340,"z":"da79d491.258628","wires":[]},{"id":"86e0d2cb.791f3","type":"comment","name":"Alternative examlpe for storing/retrieving location below => Overcomplicated, might not be useful, left it here for reference only","info":"","x":574,"y":583,"z":"da79d491.258628","wires":[]},{"id":"2b7175fe.d48e8a","type":"comment","name":"Real life example uses Instagram data to extract location","info":"","x":547,"y":32,"z":"da79d491.258628","wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment