forked from isaac-webb/node-mrcool
-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.js
100 lines (90 loc) · 2.56 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
93
94
95
96
97
98
99
100
/**
* Includes
*/
const commandLineArgs = require('command-line-args');
const {CieloHVAC, CieloAPIConnection} = require('./Cielo.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},
{name: 'macAddress', alias: 'm', type: String},
];
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 CieloAPIConnection(
(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
);
// Uppercase mac address before subscribing
OPTIONS.macAddress = OPTIONS.macAddress.toUpperCase();
await api.subscribeToHVACs([OPTIONS.macAddress]);
console.log("Connected, hvacs: ", api.hvacs.length);
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 68");
await api.hvacs[0].setTemperature("68", 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);
}
})();