-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathemulate_shutter_cycleswitch.js
77 lines (70 loc) · 2.33 KB
/
emulate_shutter_cycleswitch.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
/**
* @copyright shelly-tools contributors
* @license GNU Affero General Public License (https://www.gnu.org/licenses/agpl-3.0.de.html)
* @authors https://github.com/shelly-tools/shelly-script-examples/graphs/contributors
*
* This script can emulate a cycle switch for a remote Shelly 2.5 in roller shutter mode
* with a Shelly Plus device.
* Once the button is pushed it checks the former direction and sends a open, stop or close
* command to the remote Shelly 2.5.
*/
// CONFIG START
// this is the remote shelly which we want to control.
// Input is the number of the switch
let CONFIG = {
ip: '192.168.178.209',
input: 0,
btnevent: 'single_push'
};
// CONFIG END
// no need to change anything below this line..
// add an evenHandler for button type input and single push events
Shelly.addEventHandler(
function (event, user_data) {
//print(JSON.stringify(event));
if (typeof event.info.event !== 'undefined') {
if (event.info.id === CONFIG.input && event.info.event === CONFIG.btnevent) {
getCurrentState(CONFIG.ip);
} else {
return true;
}
} else {
return true;
}
},
);
// query a remote shelly and determine if next step is open, stop or close
function getCurrentState(ip) {
Shelly.call(
"http.get", {
url: 'http://' + ip + '/roller/0'
},
function (res, error_code, error_message, ud) {
if (res.code === 200) {
let st = JSON.parse(res.body);
if (st.state === 'stop' && st.last_direction === 'close') {
controlShutter(CONFIG.ip, 'open');
print("open shutter");
} else if (st.state === 'stop' && st.last_direction === 'open') {
controlShutter(CONFIG.ip, 'close');
print("close shutter");
} else {
controlShutter(CONFIG.ip, 'stop');
print("stop shutter");
}
}
},
null
);
};
// send shutter command
function controlShutter(ip, command) {
Shelly.call(
"http.get", {
url: 'http://' + ip + '/roller/0?go=' + command
},
function (response, error_code, error_message, ud) {
},
null
);
};