WIP More Wifi setup
This commit is contained in:
@@ -28,12 +28,13 @@ const OpCodes = {
|
||||
|
||||
|
||||
export default class SwimTrackerWebsocketConnection {
|
||||
constructor(swimTrackerHost, onData, onStarted, onStopped, onConnect, onDisconnect) {
|
||||
constructor(swimTrackerHost, onData, onStarted, onStopped, onWifiStateInfo, onConnect, onDisconnect) {
|
||||
this.swimTrackerHost = swimTrackerHost;
|
||||
|
||||
this.onData = onData;
|
||||
this.onStarted = onStarted;
|
||||
this.onStopped = onStopped;
|
||||
this.onWifiStateInfo = onWifiStateInfo;
|
||||
this.onConnect = onConnect;
|
||||
this.onDisconnect = onDisconnect;
|
||||
|
||||
@@ -54,48 +55,68 @@ export default class SwimTrackerWebsocketConnection {
|
||||
return result;
|
||||
});
|
||||
|
||||
this._wifiScanPromise = null;
|
||||
this._wifiScanPromises = [];
|
||||
}
|
||||
|
||||
sendStartCommand() {
|
||||
const data = new Uint8Array(1);
|
||||
data[0] = OpCodes.START_SESSION;
|
||||
this.ws.send(data);
|
||||
this._sendMsg(OpCodes.START_SESSION);
|
||||
}
|
||||
|
||||
sendStopCommand() {
|
||||
const data = new Uint8Array(1);
|
||||
data[0] = OpCodes.STOP_SESSION;
|
||||
this.ws.send(data);
|
||||
this._sendMsg(OpCodes.STOP_SESSION);
|
||||
}
|
||||
|
||||
sendTareCommand() {
|
||||
const data = new Uint8Array(1);
|
||||
data[0] = OpCodes.TARE;
|
||||
this.ws.send(data);
|
||||
this._sendMsg(OpCodes.TARE);
|
||||
}
|
||||
|
||||
scanWifiNetworks() {
|
||||
// trigger scan
|
||||
const data = new Uint8Array(1);
|
||||
data[0] = OpCodes.WIFI_TRIGGER_SCAN;
|
||||
this.ws.send(data);
|
||||
console.log("Trigger wifi scan");
|
||||
this._sendMsg(OpCodes.WIFI_TRIGGER_SCAN);
|
||||
|
||||
if (this._wifiScanPromise !== null) {
|
||||
return Promise.reject("Scan in progress");
|
||||
}
|
||||
else {
|
||||
let conn = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
conn._wifiScanPromises.push({ resolve: resolve, reject: reject });
|
||||
});
|
||||
|
||||
let conn = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
conn._wifiScanPromise = { resolve: resolve, reject: reject };
|
||||
});
|
||||
}
|
||||
|
||||
wifiResetToProvisioning() {
|
||||
this._sendMsg(OpCodes.WIFI_STATE_SET, {
|
||||
"reset_to_provisioning": true,
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
wifiSetModeAP(password) {
|
||||
this._sendMsg(OpCodes.WIFI_STATE_SET, {
|
||||
"ap_password": password,
|
||||
});
|
||||
}
|
||||
|
||||
wifiSetModeSTA(ssid, password) {
|
||||
this._sendMsg(OpCodes.WIFI_STATE_SET, {
|
||||
"sta_ssid": ssid,
|
||||
"sta_password": password,
|
||||
});
|
||||
}
|
||||
|
||||
_sendMsg(code, data) {
|
||||
let msg = undefined;
|
||||
if (data) {
|
||||
const serializedData = msgpack.encode(data);
|
||||
msg = new Uint8Array([code, ...serializedData]);
|
||||
} else {
|
||||
msg = new Uint8Array(1);
|
||||
msg[0] = OpCodes.WIFI_TRIGGER_SCAN;
|
||||
}
|
||||
this.ws.send(msg);
|
||||
}
|
||||
|
||||
_onMessage = (e) => {
|
||||
const dv = new DataView(e.data);
|
||||
const opCode = dv.getInt8(0);
|
||||
const payload = new Uint8Array(e.data).slice(1);
|
||||
|
||||
if (opCode === OpCodes.INITIAL_INFO) {
|
||||
const headerSize = 6;
|
||||
@@ -116,14 +137,15 @@ export default class SwimTrackerWebsocketConnection {
|
||||
const data = new Uint16Array(e.data.slice(1));
|
||||
this.onData(data);
|
||||
} else if (opCode === OpCodes.WIFI_SCAN_RESPONSE) {
|
||||
console.log("got data", e.data);
|
||||
const scanResult = msgpack.decode(new Uint8Array(e.data).slice(1), { codec: this.msgpackCodec });
|
||||
if (this._wifiScanPromise !== null) {
|
||||
this._wifiScanPromise.resolve(scanResult);
|
||||
this._wifiScanPromise = null;
|
||||
} else {
|
||||
console.log("Got unexpected WiFi scan result", scanResult);
|
||||
const scanResult = msgpack.decode(payload, { codec: this.msgpackCodec });
|
||||
|
||||
for (let i = 0; i < this._wifiScanPromises.length; ++i) {
|
||||
this._wifiScanPromises[i].resolve(scanResult);
|
||||
}
|
||||
this._wifiScanPromises.length = 0;
|
||||
} else if (opCode === OpCodes.WIFI_STATE_RESPONSE) {
|
||||
const wifiInfo = msgpack.decode(payload, { codec: this.msgpackCodec });
|
||||
this.onWifiStateInfo(wifiInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user