-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·128 lines (108 loc) · 3.36 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
process.env.DEBUG = "ThermostatHost,HostBase";
process.title = process.env.TITLE || "nest-microservice";
const debug = require("debug")("ThermostatHost"),
request = require("superagent"),
EventSource = require("eventsource"),
HostBase = require("microservice-core/HostBase");
const auth = process.env.NEST_AUTH,
topicRoot = process.env.TOPIC_ROOT || "nest",
mqttHost = process.env.MQTT_HOST || "mqtt://mqtt";
/**
* Thermostat
*/
class ThermostatHost extends HostBase {
constructor(structure, thermostat) {
super(
mqttHost,
topicRoot + "/" + structure.name + "/" + thermostat.name_long
);
try {
console.log("new ThermostatHost", structure.name, thermostat.name);
this.weather = structure.postal_code;
this.auth = auth;
} catch (e) {
console.log(e);
}
}
async command(setting, value) {
const id = this.state.device_id,
uri = `https://developer-api.nest.com/devices/thermostats/${id}?auth=${
this.auth
}`;
debug(this.device, setting, value);
const o = {};
// Nest API is picky about numbers being JSON encoded as Numbers.
o[setting] = isNaN(value) ? value : Number(value);
return new Promise((resolve, reject) => {
request
.put(uri)
.send(o)
.end((error, result) => {
if (error) {
return reject(error.response.body);
} else {
return resolve(JSON.parse(result.text));
}
});
});
}
}
class ProtectHost extends HostBase {
constructor(structure, protect) {
super(mqttHost, topicRoot + "/" + structure.name + "/" + protect.name_long);
}
async command(setting, value) {
console.log("protect", setting, value);
}
}
const hosts = {};
async function connect() {
const eventSource = new EventSource(
"https://developer-api.nest.com?auth=" + auth
);
eventSource.addEventListener("put", async e => {
const state = (this.raw = JSON.parse(e.data).data),
devices = state.devices;
Object.keys(state.structures).forEach(id => {
const s = state.structures[id];
s.thermostats.forEach(async id => {
const t = devices.thermostats[id],
ndx = s.name + "/" + t.name_long;
hosts[ndx] = hosts[ndx] || new ThermostatHost(s, t);
hosts[ndx].state = Object.assign({}, t, {
structure_name: s.name,
country_code: s.country_code,
postal_code: s.postal_code,
time_zone: s.time_zone,
away: s.away,
rhr_enrollment: s.rhr_enrollment,
eta_begin: s.eta_begin
});
});
s.smoke_co_alarms.forEach(async id => {
const t = devices.smoke_co_alarms[id],
ndx = s.name + "/" + t.name_long;
hosts[ndx] = hosts[ndx] || new ProtectHost(s, t);
hosts[ndx].state = Object.assign({}, t, {
structure_name: s.name,
country_code: s.country_code,
postal_code: s.postal_code,
time_zone: s.time_zone,
away: s.away,
rhr_enrollment: s.rhr_enrollment,
eta_begin: s.eta_begin
});
});
});
});
eventSource.addEventListener("auth_revoked", e => {
debug("auth_revoked", e);
});
eventSource.addEventListener("open", e => {
debug("eventsource opened", e);
});
eventSource.addEventListener("error", e => {
debug("eventsource error", e);
});
}
connect();