Skip to content

Instantly share code, notes, and snippets.

@legotheboss
Last active October 1, 2017 14:50
Show Gist options
  • Save legotheboss/7d29f71273fe63e0f8cc41d6e9889266 to your computer and use it in GitHub Desktop.
Save legotheboss/7d29f71273fe63e0f8cc41d6e9889266 to your computer and use it in GitHub Desktop.
Accessory file for Weaved Smart Outlet
var ipAddr = '10.0.1.240'
var Accessory = require('../').Accessory;
var Service = require('../').Service;
var Characteristic = require('../').Characteristic;
var uuid = require('../').uuid;
var request = require('request');
var fakeStatus = false;
var name = "Staircase Lights"; //accessory name
var weavedUUID = "hap-nodejs:accessories:weavedstand"; //change this to your preferences
var weavedUsername = "1A:2B:3C:4D:5E:FF";
var weavedObject = {
powerOn: false,
setPowerOn: function(on) {
weavedObject.powerOn = on;
if (fakeStatus){
fakeStatus = false;
} else if (on && !fakeStatus) {
request('http://'+ipAddr+'/cgi-bin/devOn.sh', function (error, resp, body) {
if (!error) {
//console.log("On Success");
};
});
} else {
request('http://'+ipAddr+'/cgi-bin/devOff.sh', function (error, resp, body) {
if (!error) {
//console.log("Off Success");
};
});
}
},
getStatus: function(){
request('http://'+ipAddr+'/cgi-bin/devStat.sh', function (error, resp, body) {
if (!error) {
//console.log("Get Status Success!");
};
body=parseInt(body);
//console.log(body);
if (body == 0){
weavedObject.powerOn = false;
}
else{
weavedObject.powerOn = true;
}
});
},
updateStatus: function(){
request('http://'+ipAddr+'/cgi-bin/devStat.sh', function (error, resp, body) {
var tempStatus;
body=parseInt(body);
if (body == 0){
tempStatus = false;
}
else{
tempStatus = true;
}
if (tempStatus != weavedObject.powerOn){
weavedObject.powerOn = tempStatus;
fakeStatus = true;
weaved
.getService(Service.Outlet)
.setCharacteristic(Characteristic.On, weavedObject.powerOn);
}
});
},
identify: function() {
//console.log(name + " Identified!");
}
}
var weaved = exports.accessory = new Accessory(name, uuid.generate(weavedUUID));
weaved.username = weavedUsername;
weaved.pincode = "031-45-154";
// listen for the "identify" event for this Accessory
weaved.on('identify', function(paired, callback) {
weavedObject.identify();
callback();
});
weaved
.addService(Service.Outlet, name)
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
weavedObject.setPowerOn(value);
callback();
});
weaved
.getService(Service.Outlet)
.getCharacteristic(Characteristic.On)
.on('get', function(callback) {
callback(undefined, weavedObject.powerOn);
});
setInterval(function(){weavedObject.updateStatus()}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment