forked from hau21um/homebridge-smartplug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·127 lines (105 loc) · 3.73 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
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-smartplug", "smartPlug", smartPlugPlatform);
}
function smartPlugPlatform(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"];
}
smartPlugPlatform.prototype = {
accessories: function(callback){
var foundAccessories = [];
var count = this.outlets.length;
for(index=0; index< count; ++index){
var accessory = new smartPlugAccessory(
this.log,
this.airos_sessionid,
this.outlets[index]);
foundAccessories.push(accessory);
}
callback(foundAccessories);
}
};
function smartPlugAccessory(log, airos_sessionid, outlet) {
this.log = log;
this.airos_sessionid = airos_sessionid;
this.name = outlet["name"];
this.username = outlet["username"];
this.password = outlet["password"];
this.url = outlet["url"];
this.id = outlet["id"];
this.services = [];
this.outletService = new Service.Outlet(this.name);
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);
}
smartPlugAccessory.prototype.setState = function(state, callback) {
var exec = require('child_process').exec;
state = (state == true || state == 1) ? 1 : 0;
var cmdUpdate = 'sshpass -p' + this.password + ' ssh -o StrictHostKeyChecking=no -o KexAlgorithms=+diffie-hellman-group1-sha1 ' + this.username + '@' + this.url + ' "echo ' + state + ' > /sys/class/leds/tp-link\:blue\:relay/brightness"';
var stateName = (state == 1) ? 'on' : 'off';
this.log("Turning " + this.name + " outlet " + stateName + ".");
exec(cmdUpdate, function(error, stdout, stderr) {
if (!error) {
var response = stdout;
if("success" == "success") {
callback(null);
} else {
callback(error);
}
}
});
}
smartPlugAccessory.prototype.getState = function(callback) {
var exec = require('child_process').exec;
var cmdStatus = 'sshpass -p' + this.password + ' ssh -o StrictHostKeyChecking=no -o KexAlgorithms=+diffie-hellman-group1-sha1 ' + this.username + '@' + this.url + ' "cat /sys/class/leds/tp-link\:blue\:relay/brightness"';
exec(cmdStatus, function(error, stdout, stderr) {
if (!error) {
if (stdout != "") {
var state = stdout;
if(state == 1) {
callback(null, true);
} else if(state == 0) {
callback(null, false);
}
else {
callback(error);
}
} else {
console.log("Failed to communicate with smartPlug accessory");
}
} else {
console.log("Failed with error: " + error);
}
});
}
smartPlugAccessory.prototype.getOutletInUse = function(callback) {
return callback(null, true);
}
smartPlugAccessory.prototype.getDefaultValue = function(callback) {
callback(null, this.value);
}
smartPlugAccessory.prototype.setCurrentValue = function(value, callback) {
this.log("Value: " + value);
callback(null, value);
}
smartPlugAccessory.prototype.getServices = function() {
return this.services;
}