Last active
December 25, 2015 14:08
-
-
Save carlossg/6988526 to your computer and use it in GitHub Desktop.
on{X} rule for POSTing to a url while connected to a specific wifi access point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initializing variables | |
// url to post to. If using ninjablocks this is a webhook you created in the dashboard | |
var url = "https://api.ninja.is/rest/v0/device/WEBHOOK_0_0_108/subdevice/fIOAY/tickle/xxxxxxxxxxx"; | |
// the wifi SSID | |
var ssid = "myhomewifi"; | |
// how often to trigger the POST | |
var seconds = 5*60; | |
var method = "POST"; | |
// End of variables initializing | |
var msg = "Welcome home!"; | |
var timerName = "timer_ws"; | |
function webServiceCall() { | |
device.ajax( | |
{ | |
url: url, | |
type: method, | |
data: "" | |
}, | |
function onSuccess(body, textStatus, response) { | |
console.log("Sending " + method + " to " + url + ': DONE'); | |
}, | |
function onError(textStatus, response) { | |
statusCode = ((typeof response != 'undefined') && (response !== null)) ? response.status : 0; | |
device.notifications.createNotification('Error calling url : ', textStatus + ' [' + statusCode + ']').show(); | |
console.error('Error calling url : ', textStatus, error.statusCode); | |
}); | |
} | |
device.network.on("wifiOn", function (ns) { | |
var now = new Date(); | |
var hour = now.getHours(); | |
var lastStarted = device.localStorage.getItem('lastStarted') || 0; | |
var lastStartedDate = new Date(0); | |
lastStartedDate.setTime(lastStarted) | |
var lastStartedSince = (now.valueOf() - lastStarted) / 1000; | |
console.log("now", now.toUTCString()); | |
console.log("lastStartedDate", lastStartedDate.toUTCString()); | |
console.log("lastStartedSince", lastStartedSince); | |
// ns.ssid is quoted | |
if (ns.ssid.replace(/"/g,"") != ssid) { | |
console.log("won't try to start since not on home network", ns.ssid); | |
return; | |
} | |
device.notifications.createNotification(msg).show(); | |
console.log('invoking wifiOn: ' + msg); | |
device.localStorage.setItem('lastStarted', now.valueOf()); | |
scheduleCalls(); | |
}); | |
function scheduleCalls() { | |
device.scheduler.setTimer({ | |
name: timerName, | |
time: 0, | |
interval: seconds*1000, | |
exact: false }, | |
function () { webServiceCall(); }); | |
} | |
device.network.on("wifiOff", function (ns) { | |
if (ns.ssid === null) { | |
console.log("out-of-wifi event, null network "); | |
device.scheduler.removeTimer(timerName); | |
return; | |
} | |
if (ns.ssid.replace(/"/g,"") != ssid) { | |
console.log("out-of-wifi event, not in the home network " + ns.ssid, ns.ssid); | |
device.scheduler.removeTimer(timerName); | |
return; | |
} | |
device.notifications.createNotification('triggering out-of-wifi event, leaving the home network').show(); | |
console.log("leaving the home network " + ns.ssid, ns.ssid); | |
device.scheduler.removeTimer(timerName); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment