Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamlining of sunrise operation #8

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Version 0.0.6:

First release to support the new protocol with the gateway.

[![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=NKVWX2AJRLDT2)

Version 0.0.6:

First release to support the new protocol with the gateway.

This contains an example which will turn off and on the lights in a room based on the name of the room.

For more information see:
Expand Down
60 changes: 30 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TCPConnected.prototype.Init = function(cb){
this.LoadToken(cb);
}
TCPConnected.prototype.GWEnd = function(){
tcpSocket.end();
tcpSocket.end();
}
TCPConnected.prototype.GWRequest = function(payload,cb){
if(!this._hasToken){
Expand All @@ -51,23 +51,23 @@ TCPConnected.prototype.GWRequest = function(payload,cb){
rejectUnauthorized: false,
agent: false
};

tcpSocket = https.request(options, function(res) {
res.on('data', function(data){
cb(data);
});
});

tcpSocket.write(payload);
}
}
TCPConnected.prototype.SyncGateway = function(cb){
var myuuid = uuid.v4();
var username = myuuid;
var password = myuuid;

var gLogInCommand = util.format(LogInCommand,username,password);

var payload = util.format(RequestString,'GWRLogin',encodeURIComponent(gLogInCommand));
var options = {
hostname: this._host,
Expand All @@ -81,15 +81,15 @@ TCPConnected.prototype.SyncGateway = function(cb){
rejectUnauthorized: false,
agent: false
};

tcpSocket = https.request(options, function(res) {
res.on('data', function(data){
process.stdout.write(data);
if(data == "<gip><version>1</version><rc>404</rc></gip>"){
console.log("Permission Denied: Gateway Not In Sync Mode. Press Button on Gateway to Sync.");
cb(1);
}else{
xml(data,function(error,result){
xml(data,function(error,result){
if(result['token'] != undefined){
this._token = result['token'];
nconf.use('file', { file: './config.json' });
Expand All @@ -107,7 +107,7 @@ TCPConnected.prototype.SyncGateway = function(cb){
}
});
});

tcpSocket.write(payload);
}
TCPConnected.prototype.LoadToken = function(cb){
Expand All @@ -126,7 +126,7 @@ TCPConnected.prototype.LoadToken = function(cb){
TCPConnected.prototype.GetState = function (cb){
var StateString = util.format(GetStateString,this._token);
var payload = util.format(RequestString,'GWRBatch',encodeURIComponent(StateString));

this.GWRequest(payload,function(data){
//process.stdout.write(data);
if(data == "<gip><version>1</version><rc>401</rc></gip>"){
Expand All @@ -153,7 +153,7 @@ TCPConnected.prototype.GetState = function (cb){
});
}
TCPConnected.prototype.TurnOnDevice = function (did, cb){

var DeviceCommand = util.format(DeviceSendCommand,this._token,did,1);
var payload = util.format(RequestString,'DeviceSendCommand',encodeURIComponent(DeviceCommand));

Expand All @@ -162,41 +162,41 @@ TCPConnected.prototype.TurnOnDevice = function (did, cb){
});
}
TCPConnected.prototype.TurnOffDevice = function (did, cb){

var DeviceCommand = util.format(DeviceSendCommand,this._token,did,0);
var payload = util.format(RequestString,'DeviceSendCommand',encodeURIComponent(DeviceCommand));

this.GWRequest(payload,function(data){
cb(0);
});
}
TCPConnected.prototype.SetDeviceLevel = function (did, level, cb){
TCPConnected.prototype.SetDeviceLevel = function (did, level, cb){
var DeviceLevelCommand = util.format(DeviceSendLevelCommand,this._token,did,level);
var payload = util.format(RequestString,'DeviceSendCommand',encodeURIComponent(DeviceLevelCommand));

this.GWRequest(payload,function(data){
cb(0);
});
}
TCPConnected.prototype.GetRoomHueByName = function (name, cb){
Rooms.forEach(function(room) {
Rooms.forEach(function(room) {
if(room["name"] == name){
var color = room["color"];

var r = parseInt(color.substr(0,2), 16); // Grab the hex representation of red (chars 1-2) and convert to decimal (base 10).
var g = parseInt(color.substr(2,2), 16);
var b = parseInt(color.substr(4,2), 16);

console.log(r + "." + g + "." + b);

var hue = parseInt(rgb2hsv(r, g, b)["h"] * 182);

cb(null,hue);
}
});
}
TCPConnected.prototype.GetRoomStateByName = function (name, cb){
Rooms.forEach(function(room) {
Rooms.forEach(function(room) {
if(room["name"] == name){
state = 0;
var i = 0;
Expand All @@ -209,14 +209,14 @@ TCPConnected.prototype.GetRoomStateByName = function (name, cb){
sum = sum + parseInt(devices["level"]);
}
}else{
devices.forEach(function(device) {
devices.forEach(function(device) {
i = i+1;
if(device["state"] != "0"){
state = 1;
sum = sum + parseInt(device["level"]);
}
});

}
if(i == 0){
sum = 0;
Expand All @@ -238,7 +238,7 @@ TCPConnected.prototype.GetRIDByName = function (name){
return rid;
}
TCPConnected.prototype.TurnOnRoom = function (rid, cb){

var RoomCommand = util.format(RoomSendCommand,this._token,rid,1);
var payload = util.format(RequestString,'RoomSendCommand',encodeURIComponent(RoomCommand));

Expand All @@ -248,7 +248,7 @@ TCPConnected.prototype.TurnOnRoom = function (rid, cb){
}
TCPConnected.prototype.TurnOnRoomByName = function (name, cb){
rid = this.GetRIDByName(name);

this.TurnOnRoom(rid,cb);
}

Expand All @@ -267,25 +267,25 @@ TCPConnected.prototype.TurnOffRoom = function (rid, cb){
this.GWRequest(payload,function(data){
cb(0);
});

}
TCPConnected.prototype.TurnOffRoomByName = function (name, cb){
rid = this.GetRIDByName(name);

this.TurnOffRoom(rid,cb);
}
TCPConnected.prototype.SetRoomLevel = function (rid, level, cb){
TCPConnected.prototype.SetRoomLevel = function (rid, level, cb){
var RoomLevelCommand = util.format(RoomSendLevelCommand,this._token,rid,level);
var payload = util.format(RequestString,'RoomSendCommand',encodeURIComponent(RoomLevelCommand));

this.GWRequest(payload,function(data){
cb(0);
});

}
TCPConnected.prototype.SetRoomLevelByName = function (name, level, cb){
rid = this.GetRIDByName(name);

this.SetRoomLevel(rid,level,cb);
}

Expand Down Expand Up @@ -327,4 +327,4 @@ function rgb2hsv () {
s: Math.round(s * 100),
v: Math.round(v * 100)
};
}
}
36 changes: 18 additions & 18 deletions sunrise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ var sunrise;
// script
var updateFadeLevel = function() {
Sunrise.GetState(function(error,system){
Sunrise.GetRoomStateByName(room, function(error,state,level){
if(state == 0){
Sunrise.TurnOnRoomWithLevelByName(room, 1, function(){
sunrise = setTimeout(updateFadeLevel, (fadeDuration * 60));
});
}else{
if (level >= 100) {
console.log('Lights are all the way on.');
clearInterval(sunrise);
Sunrise.GWEnd();
} else {
Sunrise.SetRoomLevelByName(room, Math.min(100,level+step), function(){
sunrise = setTimeout(updateFadeLevel, (fadeDuration * 60));
} );
}
}
});
Sunrise.GetRoomStateByName(room, function(error,state,level){
if(state == 0){
Sunrise.TurnOnRoomWithLevelByName(room, 1, function(){
sunrise = setTimeout(updateFadeLevel, (fadeDuration * 60));
});
}else{
if (level >= 100) {
console.log('Lights are all the way on.');
clearInterval(sunrise);
Sunrise.GWEnd();
} else {
Sunrise.SetRoomLevelByName(room, Math.min(100,level+step), function(){
sunrise = setTimeout(updateFadeLevel, (fadeDuration * 60));
} );
}
}
});
});
}

Expand All @@ -51,4 +51,4 @@ Sunrise.Init(function(error){
}else{
console.log("There was an issue initializing the token");
}
});
});
2 changes: 1 addition & 1 deletion toggle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var TCPConnected = require('./index.js');

Test = new TCPConnected("192.168.1.137");
Test = new TCPConnected("10.0.1.3");

Test.Init(function(error){
if(!error){
Expand Down