-
Notifications
You must be signed in to change notification settings - Fork 1
/
shelly.js
140 lines (119 loc) · 3.85 KB
/
shelly.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
var logging = require('./logging.js');
var moment = require('moment');
const request = require('request');
var events = require('./events.js');
let status = {
busy: false,
time: null,
relay: {
ison: null,
},
intervalSec: null,
source: null
}
var config = {
url: null,
}
const configure = (url, intervalSec) => {
logging.add(`Shelly configure ${url}`);
config.url = url;
status.intervalSec = intervalSec;
}
config.url = 'http://192.168.31.77';
getShellyStatus = (noRepeat = false) => {
if (!status.busy && config.url !== null) {
logging.add("Shelly getShellyStatus() getting relay data");
status.busy = true;
request(config.url+'/status', {json: true}, (error, res, body) => {
status.busy = false;
if (!error && res.statusCode == 200) {
setShellyRelayStatus(body.relays[0].ison,'getShellyStatus()');
};
if(status.intervalSec && !noRepeat) {
logging.add("Shelly next value in "+status.intervalSec,'debug');
setTimeout(function erneutLesen() {
getShellyStatus();
}, status.intervalSec * 1000);
}
if (error) {
logging.add(error,'warn');
return false;
};
});
}
else {
logging.add("Shelly getShellyStatus() - busy (skip)");
}
}
const shellyRequestOptions = {
json: true,
maxAttempts: 5, // (default) try 5 times
retryDelay: 5000, // (default) wait for 5s before trying again
}
const turnShellyRelay = (onOff,retryCount = null) => {
if(retryCount > 900) {
// Try max 30mins
logging.add("Shelly turnShellyRelay() - retried too often, not trying anymore");
return false;
}
else if (config.url !== null && (onOff == 'on' || onOff == 'off')) {
logging.add(`Shelly turnShellyRelay(${onOff})`);
request(config.url+'/relay/0?turn='+onOff, shellyRequestOptions, (error, res, body) => {
if (!error && res.statusCode == 200) {
setShellyRelayStatus(onOffToBool(onOff),'turnShellyRelay()');
};
if (error) {
logging.add(error,'warn');
// Try again if failed
if(retryCount === null) {
retryCount = 0;
}
logging.add("Shelly turnShellyRelay() - failed - try again in 2s");
setTimeout(() => {
turnShellyRelay(onOff, (retryCount + 1));
},2000);
return false;
};
});
}
else {
logging.add("Shelly turnShellyRelay() - invalid config or command (skip)");
}
}
// Shelly IO URL Actions will push the Relay State (on/off) to the coop, no need to poll regularly
setShellyRelayStatusOnOff = (onOff) => {
if(onOff == 'on' || onOff == 'off') {
logging.add(`Shelly receiving setShellyRelayStatusOnOff(${onOff})`);
setShellyRelayStatus(onOffToBool(onOff),'setShellyRelayStatusOnOff');
}
else {
logging.add(`Shelly receiving invalid setShellyRelayStatusOnOff(on,off)`);
}
}
const onOffToBool = (onOff) => {
if(onOff === 'on') {
return true;
}
else if(onOff === 'off') {
return false;
}
}
const boolToOnOff = (bool) => {
if(bool === true) {
return 'on';
}
else if(bool === false) {
return 'off';
}
}
const setShellyRelayStatus = (onOffBool,source) => {
status.time = moment();
status.relay.ison = onOffBool;
status.source = source;
events.send('shellyRelayIsOn',onOffBool);
}
exports.configure = configure;
exports.getShellyStatus = getShellyStatus;
exports.turnShellyRelay = turnShellyRelay;
exports.setShellyRelayStatusOnOff = setShellyRelayStatusOnOff;
exports.status = status;