-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwled_integration.js
More file actions
168 lines (120 loc) · 4.33 KB
/
wled_integration.js
File metadata and controls
168 lines (120 loc) · 4.33 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var XMLHttpRequest = require('xhr2');
async function changeToRed() {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (err) {
console.error(err)
};
var data = '{"on": true, "v":true, "seg": [{"id":0, "on":true, "col":[[255,0,0],[0,0,0],[0,0,0]], "fx":50, "sx":216, "ix": 125, "pal":31}]}';
// Send the request
await xhr.send(data);
}
async function changeToYellow() {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (err) {
console.error(err)
};
var data = '{"on": true, "v":true, "seg": [{"id":0, "on":true, "col":[[255,255,0],[0,0,0],[0,0,0]], "fx":50, "sx":216, "ix": 125, "pal":31}]}';
await xhr.send(data);
}
async function changeToGreen() {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (err) {
console.error(err)
};
var data = '{"on": true, "v":true, "seg": [{"id":0, "on":true, "col":[[0,255,0],[0,0,0],[0,0,0]], "fx":50, "sx":216, "ix": 125, "pal":31}]}';
await xhr.send(data);
}
async function changeToBlue() {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (err) {
console.error(err)
};
var data = '{"on": true, "v":true, "seg": [{"id":0, "on":true, "col":[[0,0,255],[0,0,0],[0,0,0]], "fx":50, "sx":216, "ix": 125, "pal":31}]}';
await xhr.send(data);
}
async function changeToOrange() {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (err) {
console.error(err)
};
var data = '{"on": true, "v":true, "seg": [{"id":0, "on":true, "col":[[255,165,0],[0,0,0],[0,0,0]], "fx":50, "sx":216, "ix": 125, "pal":31}]}';
await xhr.send(data);
}
async function changeToIndigo() {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (err) {
console.error(err)
};
var data = '{"on": true, "v":true, "seg": [{"id":0, "on":true, "col":[[75,0,130],[0,0,0],[0,0,0]], "fx":50, "sx":216, "ix": 125, "pal":31}]}';
await xhr.send(data);
}
async function changeToViolet() {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (err) {
console.error(err)
};
var data = '{"on": true, "v":true, "seg": [{"id":0, "on":true, "col":[[143,0,255],[0,0,0],[0,0,0]], "fx":50, "sx":216, "ix": 125, "pal":31}]}';
await xhr.send(data);
}
async function restoreState(oldState) {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onerror = function (err) {
console.error(err)
};
// Send the request
await xhr.send(oldState);
}
async function get() {
var url = "http://wled-hallway.local/json/state";
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.setRequestHeader("Content-Type", "application/json");
const myPromise = new Promise( (resolutionFunc,rejectionFunc) => {
xhr.onreadystatechange = async function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
resolutionFunc(xhr.responseText);
}
}
});
await xhr.send();
return myPromise;
}
const delay = ms => new Promise(res => setTimeout(res, ms));
module.exports = {get, changeToRed, changeToYellow, changeToGreen, changeToBlue, changeToOrange, changeToIndigo, changeToViolet, restoreState, delay};
(async () => {
try {
/*
var current_state = await get();
await changeToRed();
await delay(10000);
await restoreState(current_state);
*/
} catch (e) {
// Deal with the fact the chain failed
}
// `text` is not available here
})();