forked from nicholasrobinson/node-smartcielo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.js
92 lines (83 loc) · 2.6 KB
/
demo.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
/**
* Includes
*/
const commandLineArgs = require('command-line-args');
const {MrCoolHVAC, MrCoolAPIConnection} = require('./MrCool.js');
const HttpsProxyAgent = require('https-proxy-agent');
const url = require('url');
/**
* Constants
*/
const OPTION_DEFINITIONS = [
{ name: 'username', alias: 'u', type: String },
{ name: 'password', alias: 'p', type: String },
{ name: 'ip', alias: 'i', type: String },
{ name: 'verbose', alias: 'v', type: Boolean }
];
const OPTIONS = commandLineArgs(OPTION_DEFINITIONS);
/**
* Debug Proxy Settings
*/
const PROXY = 'http://127.0.0.1:8888';
const agentOptions = url.parse(PROXY);
const agent = OPTIONS.verbose ? new HttpsProxyAgent(agentOptions) : undefined;
if (agent) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
/**
* Example Usage.
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
(async () => {
const api = new MrCoolAPIConnection(
commandedState => {
console.log('Commanded State Change:', JSON.stringify(commandedState));
},
roomTemperature => {
console.log('Updated Room Temperature:', roomTemperature);
},
err => {
console.error('Communication Error:', err);
}
);
console.log('Connecting...');
try {
await api.establishConnection(OPTIONS.username, OPTIONS.password,
OPTIONS.ip, agent);
await api.subscribeToHVACs(['000000000000'])
console.log('Connected.');
api.hvacs.forEach((hvac) => {
console.log(hvac.toString());
});
const temp = api.hvacs[0].getTemperature();
console.log('Sending power off');
await api.hvacs[0].powerOff(api);
await sleep(10000);
api.hvacs.forEach((hvac) => {
console.log(hvac.toString());
});
console.log('Sending power on');
await api.hvacs[0].powerOn(api);
await sleep(10000);
api.hvacs.forEach((hvac) => {
console.log(hvac.toString());
});
console.log('Sending temperature 75');
await api.hvacs[0].setTemperature('75', api);
await sleep(10000);
api.hvacs.forEach((hvac) => {
console.log(hvac.toString());
});
console.log('Sending temperature ' + temp);
await api.hvacs[0].setTemperature(temp, api);
await sleep(10000);
api.hvacs.forEach((hvac) => {
console.log(hvac.toString());
});
} catch (error) {
console.error("Caught an error...");
console.error(error);
}
})();