-
Notifications
You must be signed in to change notification settings - Fork 59
/
wifi-provision.js
301 lines (280 loc) · 7.77 KB
/
wifi-provision.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2021 Allterco Robotics EOOD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Shelly is a Trademark of Allterco Robotics
// Shelly Script example: Provisioning of new Shelly Plus gen 2 devices
//
// This scripts periodically scans for access points with SSID matching the
// template for Shelly Plus device APs and if found, will connect to that AP
// and provision wifi credentials
let CONFIG = {
scanSSID: [
{
gen: 1,
apn: ["shelly1", "shelly1pm"],
},
{
gen: 2,
apn: ["ShellyPlus", "Shelly"],
},
],
provisionAP: {
ssid: "YourWiFiSSID",
pass: "YourPassword",
enable: false,
},
shellyAPAddress: "192.168.33.1",
//disable the AP once set up
disableShellyAP: false,
runPeriod: 5 * 1000,
};
function shallowCopy(dst, src) {
for (let k in src) {
if (typeof dst[k] === "undefined" && typeof dst[k] !== "object") {
dst[k] = src[k];
}
}
}
let Scanner = {
scanning: false,
provisionining: false,
scanCache: [],
targetCache: [],
eventHandlerId: null,
scanTimer: null,
addToTargetCache: function (ssid) {
this.targetCache.push(ssid);
},
popScanCacheItem: function () {
this.scanCache.splice(0, 1);
},
startScanning: function () {
print("Starting scanning...");
if (this.scanTimer !== null) return;
if (typeof CONFIG.runPeriod !== undefined) {
this.scanTimer = Timer.set(
CONFIG.runPeriod,
true,
function (self) {
self.scan();
},
this
);
}
},
stopScanning: function () {
if (this.scanTimer !== null) {
Timer.clear(this.scanTimer);
this.scanTimer = null;
}
},
initEventListener: function () {
this.eventHandlerId = Shelly.addEventHandler(function (event, self) {
if (typeof event.info.ssid === "undefined") return;
if (typeof event.info.status === "undefined") return;
if (event.info.status.indexOf("got ip") === -1) return;
let ssid = self.scanCache[0];
if (event.info.ssid.indexOf(ssid) !== -1) {
self.provision();
}
}, this);
},
clearEventListener: function () {
Shelly.removeEventHandler(this.eventHandlerId);
},
clearTargetCache: function () {
this.targetCache.splice(0, this.targetCache.length);
},
/**
* Looks for access point name match with prefixes from CONFIG.scanSSID
* @param {string} apName
* @returns {number|false} generation number or false if not found
*/
matchAP: function (ssid) {
for (let gi in CONFIG.scanSSID) {
for (let api in CONFIG.scanSSID[gi].apn) {
if (ssid.indexOf(CONFIG.scanSSID[gi].apn[api]) === -1) continue;
return CONFIG.scanSSID[gi].gen;
}
}
return false;
},
isInCache: function (ssid) {
for (let papk in this.targetCache) {
let tSSID = this.targetCache[papk];
if (tSSID.indexOf(ssid) !== -1) {
return true;
}
}
return false;
},
scan: function () {
//prevent reentry
if (this.scanning || this.provisionining) {
print("Already scanning. Abort.");
return;
}
print("Scanning for wifi networks");
this.scanning = true;
this.stopScanning();
this.scanCache.splice(0, this.scanCache.length);
Shelly.call(
"WiFi.Scan",
{},
function (result, error_code, error_message, self) {
if (error_code !== 0) {
print("WiFi scan failed", error_message);
self.startScanning();
return;
}
let wifiAPs = result.results;
//walk through all the found ssids and look for starting
//with shelly with no auth enabled
for (let apk in wifiAPs) {
let cSSID = wifiAPs[apk].ssid;
if (wifiAPs[apk].auth !== 0) continue;
if (self.matchAP(cSSID) === false) continue;
if (self.isInCache(cSSID) === false) {
print("This AP is a target: ", cSSID);
self.scanCache.push(cSSID);
}
}
self.scanning = false;
if (self.scanCache.length) {
print("Found ", self.scanCache.length, " APs. Start provisioning");
self.startProvision();
} else {
print("APs not found or already provisioned");
self.startScanning();
}
},
this
);
},
startProvision: function () {
this.initEventListener();
Shelly.call(
"wifi.setconfig",
{ config: { sta: { enable: false } } },
function (res, error_code, error_msg) {
print("Disabled station on device");
}
);
this.doNextProvision();
},
doNextProvision: function () {
if (this.scanCache.length === 0) {
this.endProvision();
return;
}
let ssid = this.scanCache[0];
Shelly.call(
"wifi.setconfig",
{ config: { sta1: { ssid: ssid, pass: "", enable: true } } },
function (res, error_code, error_msg, ssid) {
print("Enable sta1 configured to connect to: ", ssid);
},
ssid
);
},
endProvision: function () {
print("End provisioning");
this.clearEventListener();
this.startScanning();
Shelly.call(
"wifi.setconfig",
{ config: { sta1: { enable: false } } },
function (res, error_code, error_msg) {
print("Disable sta1");
}
);
Shelly.call(
"wifi.setconfig",
{ config: { sta: { enable: true } } },
function (res, error_code, error_msg) {
print("Enable sta ");
}
);
},
composeStaEndpoint: function (gen) {
let url = "http://" + CONFIG.shellyAPAddress;
if (gen === 2) {
url += "/rpc/wifi.setconfig";
} else {
url += "/settings/sta?";
}
return url;
},
provision: function () {
print("Will provision ", this.scanCache[0]);
this.provisionining = true;
let gen = this.matchAP(this.scanCache[0]);
let url = this.composeStaEndpoint(gen);
if (gen === 2) {
let wifiData = {};
shallowCopy(wifiData, CONFIG.provisionAP);
let postData = {
url: url,
body: {
config: {
sta1: wifiData,
ap: {
enable: !CONFIG.disableShellyAP,
},
},
},
};
print(JSON.stringify(postData));
Shelly.call(
"HTTP.POST",
postData,
function (response, error_code, error_message, self) {
let rpcCode = response.code;
// if (rpcCode === 200) {
print("Success provisioning: ", self.scanCache[0]);
self.addToTargetCache(self.scanCache[0]);
self.popScanCacheItem();
// };
self.provisionining = false;
self.doNextProvision();
},
this
);
} else if (gen === 1) {
url =
url +
"ssid=" +
CONFIG.provisionAP.ssid +
"&key=" +
CONFIG.provisionAP.pass +
"&enabled=1";
Shelly.call(
"HTTP.GET",
{ url: url },
function (response, error_code, error_message, self) {
let rpcCode = response.code;
// if (rpcCode === 200) {
print("Success provisioning: ", self.scanCache[0]);
self.addToTargetCache(self.scanCache[0]);
self.popScanCacheItem();
// };
self.provisionining = false;
self.doNextProvision();
},
this
);
}
},
};
Scanner.startScanning();