Last active
May 14, 2016 10:09
-
-
Save keigezellig/f61f6175f4a02e9fe01ee3639c4c1fb1 to your computer and use it in GitHub Desktop.
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
var fs = require('fs'); | |
var http = require('http'); | |
var config = get_config(__dirname + "/config.json"); | |
var context = new require('rabbit.js').createContext(config.mq.connection_string); | |
var Blynklib = require('blynk-library'); | |
var blynk = new Blynklib.Blynk(config.blynk.auth_key); | |
var v9 = new blynk.VirtualPin(9); | |
var v1 = new blynk.VirtualPin(1); | |
var v2 = new blynk.VirtualPin(2); | |
var sub = context.socket('SUB', {routing: 'topic', persistent: true}); | |
var sub2 = context.socket('SUB', {routing: 'topic', persistent: true}); | |
sub.connect(config.mq.exchange_name, config.mq.routing_key); | |
sub.setEncoding('utf8'); | |
blynk.syncAll(); | |
sub.on('data', function (alert) { | |
var alertObject = JSON.parse(alert); | |
console.log("Received sensor update!: " + alertObject); | |
var value = alertObject.Data.Content.Value.Value; | |
blynk.notify("helloo!!"); | |
v9.write(value); | |
}); | |
v1.on('write', function (param) { | |
console.log('V1:', param); | |
if (param == 0) { | |
switchLight("192.168.1.67", 8888, "Staande%20lamp", false) | |
} | |
else { | |
switchLight("192.168.1.67", 8888, "Staande%20lamp", true) | |
} | |
}); | |
v2.on('write', function (param) { | |
console.log('V2:', param); | |
if (param == 0) { | |
switchLight("192.168.1.67", 8888, "TV", false) | |
} | |
else { | |
switchLight("192.168.1.67", 8888, "TV", true) | |
} | |
}); | |
//http://sarajevo:8888/api/sensor/Staande%20lamp/TurnOn | |
blynk.on('connect', function () { | |
console.log("Blynk ready."); | |
}); | |
blynk.on('disconnect', function () { | |
console.log("DISCONNECT"); | |
}); | |
function get_config(filename) { | |
var nconf = require('nconf'); | |
nconf.use('file', {file: filename}); | |
nconf.load(); | |
console.log("Loaded configuration: " + nconf.get()) | |
return nconf.get(); | |
} | |
function switchLight(host, port, lightname, isOn) { | |
var operation = "TurnOff"; | |
if (isOn) { | |
operation = "TurnOn"; | |
} | |
var options = { | |
host: host, | |
port: port, | |
path: '/api/sensor/' + lightname + '/'+operation | |
}; | |
//http://192.168.1.67:8888/api/sensor/TV/TurnOn | |
http.get(options, function (res) { | |
console.log("Got response: " + res.statusCode); | |
}).on('error', function (e) { | |
console.log("Got error: " + e.message); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment