forked from wr/homebridge-mpower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·166 lines (137 loc) · 5.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-mpower", "mPower", mPowerPlatform);
}
function mPowerPlatform(log, config){
function stringGen(length) {
var text = "";
var charset = "0123456789";
for( var i=0; i < length; i++ )
text += charset.charAt(Math.floor(Math.random() * charset.length));
return text;
}
this.log = log;
this.airos_sessionid = config["airos_sessionid"] || stringGen(32);
this.outlets = config["outlets"];
}
mPowerPlatform.prototype = {
accessories: function(callback){
var foundAccessories = [];
var count = this.outlets.length;
for(index=0; index< count; ++index){
var accessory = new mPowerAccessory(
this.log,
this.airos_sessionid,
this.outlets[index]);
foundAccessories.push(accessory);
}
callback(foundAccessories);
}
};
function mPowerAccessory(log, airos_sessionid, outlet) {
this.log = log;
this.airos_sessionid = airos_sessionid;
// configuration vars
this.name = outlet["name"];
this.username = outlet["username"];
//this.password = outlet["password"];
this.url = outlet["url"];
this.id = outlet["id"];
// register the service and provide the functions
this.services = [];
this.outletService = new Service.Outlet(this.name);
// the current state (on / off)
this.outletService
.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
/*Power consumption in Watt*/
this.outletService
.getCharacteristic(Characteristic.OutletInUse)
.on('get', this.getOutletInUse.bind(this));
this.services.push(this.outletService);
}
mPowerAccessory.prototype.setState = function(state, callback) {
var exec = require('child_process').exec;
state = (state == true || state == 1) ? 1 : 0;
//use SSH to powerstrip to send on off commands directly to powerstrip
var cmdUpdate = 'echo $(ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 ' + this.username + '@' + this.url + ' "echo ' + state + ' > /proc/power/relay' + this.id + ';cd /proc/power;grep \'\' relay' + this.id + '*;exit;")';
//this.log("state variable = " + state + ".");
var stateName = (state == 1) ? 'on' : 'off';
this.log("Turning " + this.name + " outlet " + stateName + ".");
exec(cmdUpdate, function(error, stdout, stderr) {
if (!error) {
if (stdout != "") {
var response = stdout.trim();
//console.log("set " + this.name + " outlet to " + state + " and checked OnOff state for " + this.name + " outlet is " + response[0] + ".");
if(response == state) {
callback(null);
} else {
callback(error);
}
} else {
console.log("Failed with error: " + error);
}
}
});
}
mPowerAccessory.prototype.getState = function(callback) {
var exec = require('child_process').exec;
var cmdStatus = 'echo $(ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 ' + this.username + '@' + this.url + ' "cd /proc/power;grep \'\' relay' + this.id + '*;exit;")';
exec(cmdStatus, function(error, stdout, stderr) {
if (!error) {
if (stdout != "") {
var state = stdout.trim();
//console.log("OnOff state for " + this.name + " outlet is " + state[0] + ".");
if(state == 1) {
callback(null, true);
} else if(state == 0) {
callback(null, false);
}
else {
callback(error);
}
} else {
console.log("Failed to communicate with mPower accessory");
}
} else {
console.log("Failed with error: " + error);
}
});
}
mPowerAccessory.prototype.getOutletInUse = function(callback) {
var exec = require('child_process').exec;
var cmdOutletInUseStatus = 'echo $(ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 ' + this.username + '@' + this.url + ' "cd /proc/power;grep \'\' active_pwr' + this.id + '*;exit;")';
exec(cmdOutletInUseStatus, function(error, stdout, stderr) {
if (!error) {
if (stdout != "") {
var inUseState = stdout.trim();
//console.log("inUseState value for " + this.name + " outlet is " + inUseState[0] + ".");
if(inUseState != "0.0") {
callback(null, true);
} else if(inUseState == "0.0") {
callback(null, false);
}
else {
callback(error);
}
} else {
console.log("Failed to communicate with mPower accessory");
}
} else {
console.log("Failed with error: " + error);
}
});
}
mPowerAccessory.prototype.getDefaultValue = function(callback) {
callback(null, this.value);
}
mPowerAccessory.prototype.setCurrentValue = function(value, callback) {
this.log("Value: " + value);
callback(null, value);
}
mPowerAccessory.prototype.getServices = function() {
return this.services;
}