Read SunSprite values
Reads sun light exposure as stored in the SunSprite data portal and does a bit of silliness with it. Mostly an example how to deal with the Kinvey API. See blog post here: http://logic.sysbiol.cam.ac.uk/?p=1573
[{"id":"7096baea.8f6944","type":"debug","name":"","active":true,"console":"true","complete":"payload","x":846,"y":74,"z":"aa100ec1.55eff","wires":[]},{"id":"8d563c5a.72a9c","type":"http request","name":"post login","method":"POST","url":"","x":457,"y":74,"z":"aa100ec1.55eff","wires":[["b492d620.4b6d28"]]},{"id":"9c1c9574.63e368","type":"function","name":"login details","func":"// the app credentials as supplied by SunSprite\nvar appname = \"kid_Ve5xRCs7Mm\";\nvar appkey = \"a7f19fa982f4448e84b9ab29bd105a8b\";\n\nvar applogin = new Buffer(appname+\":\"+appkey).toString('base64'); // b64 formatted login string\n\nmsg.url = \"http://baas.kinvey.com/user/\"+appname+\"/login\"; // REST endpoint for login\n\n// define header with app-specific login and specify user name and password of SunSprite user\nmsg.headers = { 'Content-Type' : 'application/json', 'Authorization' : 'Basic '+applogin }\nmsg.payload = JSON.stringify({\"username\": \"xxxxx\", \"password\": \"xxxxx\"});\n\nmsg.method = \"POST\"; // POST this request\n\nreturn msg;","outputs":1,"x":292,"y":74,"z":"aa100ec1.55eff","wires":[["8d563c5a.72a9c"]]},{"id":"915339ae.6eacc8","type":"http request","name":"get data","method":"GET","url":"","x":423,"y":136,"z":"aa100ec1.55eff","wires":[["2b50611e.d4af9e"]]},{"id":"c6e06893.391f98","type":"inject","name":"login","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":137,"y":74,"z":"aa100ec1.55eff","wires":[["9c1c9574.63e368"]]},{"id":"bca606b3.4359f8","type":"function","name":"get day","func":"var appname = \"kid_Ve5xRCs7Mm\";\n\n// get today and tomorrow in format yyyy-mm-dd (the 86400000 are 24hrs in unixtime, relative to payload from get day)\nvar today = new Date( msg.payload-(0*86400000) ).toISOString().substring(0,10);\nvar tomorrow = new Date( msg.payload+(1*86400000) ).toISOString().substring(0,10);\n\n// a query for just today where isgoodLux=true, and we want goodLux and date\nmsg.url = \"http://baas.kinvey.com/appdata/\"+appname+\n'/luxRecords/?query={\"date\":{\"$lt\":\"'+tomorrow+'\",\"$gte\":\"'+today+'\"},\"isGoodLux\":true}&fields=goodLux,date';\n\nmsg.method = \"GET\";\n\n// use the AUTHTOKEN obtained in the login flow\nmsg.headers = { 'Authorization' : 'Kinvey '+context.global.AUTHTOKEN };\n\nreturn msg;","outputs":1,"x":280,"y":136,"z":"aa100ec1.55eff","wires":[["915339ae.6eacc8"]]},{"id":"176c032c.e893fd","type":"inject","name":"get day","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":129,"y":136,"z":"aa100ec1.55eff","wires":[["bca606b3.4359f8"]]},{"id":"2b50611e.d4af9e","type":"function","name":"accumulateGoodLux","func":"var qualityRegex = /goodLux\\\"\\:(\\d+),\\\"date\\\"\\:\\\".{11}(.{8})\\./g,\n matches, goodLux, goodLuxTime = \"\";\n\nmsg.topic = \"too dark\";\n\n// parse out goodLux and date values coming as msg.payload,\n// sum up all goodLux values and if totalLux > LUXGOAL, get the first time\n// that was the case and set the topic of the outgoing message \n// to \"enough light\" \nvar totalgoodLux = 0;\n while (matches = qualityRegex.exec(msg.payload)) {\n goodLux = parseInt(matches[1]);\n totalgoodLux += goodLux;\n if (totalgoodLux > context.global.LUXGOAL && goodLuxTime == \"\") {\n msg.topic = \"enough light\";\n goodLuxTime = matches[2];\n }\n }\ntotalgoodLux = Math.round( totalgoodLux/context.global.LUXGOAL *100);\n\nmsg.payload = totalgoodLux+\"% of minimal, 100% at: \"+goodLuxTime;\n\nreturn msg;","outputs":1,"x":610,"y":135,"z":"aa100ec1.55eff","wires":[["1bb2949a.e44d6b"]]},{"id":"b492d620.4b6d28","type":"function","name":"extract on success","func":"var parameters = JSON.parse(msg.payload);\n\ncontext.global.AUTHTOKEN = parameters[\"_kmd\"][\"authtoken\"];\ncontext.global.SUNSPRITE = parameters[\"sunSpriteUUID\"];\ncontext.global.BATTERY = parameters[\"lastBatteryPercent\"];\ncontext.global.LUXGOAL = parameters[\"luxGoal\"];\ncontext.global.LASTSYNC = parameters[\"syncRefDate\"];\ncontext.global.TIMEZONE = parameters[\"timezoneOffsetSec\"];\n\nmsg.payload = \"Authorised with: \"+context.global.AUTHTOKEN;\n\nreturn msg;","outputs":1,"x":650,"y":74,"z":"aa100ec1.55eff","wires":[["7096baea.8f6944"]]},{"id":"de283582.21d7c8","type":"twitter out","twitter":"","name":"Tweet","x":931,"y":135,"z":"aa100ec1.55eff","wires":[]},{"id":"1bb2949a.e44d6b","type":"switch","name":"","property":"topic","rules":[{"t":"cont","v":"light"}],"checkall":"true","outputs":1,"x":792,"y":135,"z":"aa100ec1.55eff","wires":[["de283582.21d7c8"]]}]