-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
195 lines (147 loc) · 5.63 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var util = require('util');
var stream = require('stream');
var nest = require('unofficial-nest-api');
util.inherits(Driver,stream);
function Driver(opts, app) {
var self = this;
this.devices = {};
this.log = app.log;
this.opts = opts;
opts.lastSeen = opts.lastSeen || {};
opts.pollInterval = opts.pollInterval || 120000; // 2min default poll
app.once('client::up',function(){
self.save();
if (opts.username && opts.password) {
self.login(opts.username, opts.password);
}
});
}
Driver.prototype.config = function(rpc,cb) {
var self = this;
if (!rpc) {
return cb(null, {
"contents":[
{ "type": "input_field_text", "field_name": "username", "value": self.opts.username || '', "label": "Nest Username", "placeholder": "", "required": true},
{ "type": "input_field_password", "field_name": "password", "value": "", "label": "Nest Password", "placeholder": "", "required": true},
{ "type": "submit", "name": "Add", "rpc_method": "setCredentials" }
]
});
}
switch (rpc.method) {
case 'setCredentials':
self.login(rpc.params.username, rpc.params.password, function(err, data) {
if (err) {
cb(null, {
"contents": [
{ "type":"paragraph", "text":"There was an error logging into your nest account : " + err},
{ "type":"close", "text":"Close"}
]
});
return;
}
self.opts.username = rpc.params.username;
self.opts.password = rpc.params.password;
self.save();
cb(null, {
"contents": [
{ "type":"paragraph", "text":"Successfully logged in. (User id: " + data.userid + ')'},
{ "type":"close", "text":"Close"}
]
});
});
break;
default:
log('Unknown rpc method', rpc.method, rpc);
}
};
Driver.prototype.login = function(username, password, cb) {
nest.login(username, password, function (err, data) {
this.log.info('Nest - Logged in ' + JSON.stringify(data));
if (cb) {
cb(err, data);
}
if (err) {
this.log.info('Nest - Failed to log in - ', err.message);
return;
}
// Start continuous polling..
setInterval(this.fetchStatus.bind(this), this.opts.pollInterval);
// and do one now too.
this.fetchStatus();
}.bind(this));
};
Driver.prototype.fetchStatus = function() {
nest.fetchStatus(function (data) {
for (var deviceId in data.shared) {
var deviceData = data.shared[deviceId];
if (!this.opts.lastSeen[deviceId] || this.opts.lastSeen[deviceId] < deviceData['$timestamp']) {
var topic = 'data.' + deviceId;
if (!this.listeners(topic).length) {
this.log.info('Nest - Creating Ninja devices for device: ' + deviceId);
this.createDevices(deviceId, deviceData, topic);
}
this.opts.lastSeen[deviceId] = deviceData['$timestamp'];
this.save();
this.emit(topic, deviceData);
}
}
}.bind(this));
};
Driver.prototype.createDevices = function(id, deviceData, topic) {
var self = this;
function CurrentTemp() {
this.writable = false;
this.readable = true;
this.V = 0;
this.D = 9;
this.G = 'nestcurrent' + id;
this.name = 'Nest - ' + (deviceData.name||id) + ' Current Temperature';
self.on(topic, function(deviceData) {
self.log.debug('Nest - Device ' + id + ' - Current temperature:' + deviceData.current_temperature);
if (typeof deviceData.current_temperature == 'undefined') {
self.log.error('Nest - Device ' + id + '- ERROR: No Current Temperature!');
} else {
this.emit('data', deviceData.current_temperature);
}
}.bind(this));
}
util.inherits(CurrentTemp,stream);
var current = new CurrentTemp();
this.emit('register', current);
function TargetTemp() {
this.writable = true;
this.readable = true;
this.V = 0;
this.D = 9;
this.G = 'nesttarget' + id;
this.name = 'Nest - ' + (deviceData.name||id) + ' Target Temperature';
self.on(topic, function(deviceData) {
self.log.debug('Nest - Device ' + id + ' - Target temperature:' + deviceData.target_temperature);
if (typeof deviceData.target_temperature == 'undefined') {
self.log.error('Nest - Device ' + id + '- ERROR: No Target Temperature!');
} else {
this.emit('data', deviceData.target_temperature);
}
}.bind(this));
this.write = function(data) {
if (typeof data == 'string') {
try {
data = parseFloat(data);
} catch(e) {}
}
if (typeof data != 'number' || isNaN(data) ) {
self.log.error('Nest - Device ' + id + ' - Tried to set target temperature with a non-number : ' + data);
return;
}
self.log.info('Nest - Device ' + id + ' - Setting target temperature to :' + data);
nest.setTemperature(id, data, function(response) {
console.log('response', response);
self.fetchStatus();
});
};
}
util.inherits(TargetTemp,stream);
var target = new TargetTemp();
this.emit('register', target);
};
module.exports = Driver;