doProvisioning method
Implementation
Future<ScpStatus> doProvisioning(ScpDevice device, String ssid,
String wifiPassword, String jsonPath) async {
if (ssid == "" || wifiPassword == "") {
log("provisioning without ssid or wifiPassword not possible.");
return ScpStatus(status: ScpStatus.RESULT_ERROR);
}
// for each new device
log('Provisioning device: ${device.deviceId}');
// send security-pw-change
await ScpMessageSender.sendNewPassword(device);
// send security-wifi-config
final ScpResponseSetWifiConfig wifiConfigResponse =
await ScpMessageSender.sendWifiConfig(device, ssid, wifiPassword);
// send security-restart
if (wifiConfigResponse.isValid()) {
log('wifiConfig response is not valid, shutting down.');
return ScpStatus(status: ScpStatus.RESULT_ERROR);
} else if (wifiConfigResponse == ScpStatus.RESULT_ERROR) {
log('failed to set wifi config.');
return ScpStatus(status: ScpStatus.RESULT_ERROR);
}
final ScpResponseRestart restartResponse =
await ScpMessageSender.sendRestart(device);
// move device from new devices to known devices
if (restartResponse.isValid()) {
log('Restarting device successfull, removing from new devices and adding to known devices.');
this.knownDevices.add(device);
this
.newDevices
.removeWhere((element) => element.deviceId == device.deviceId);
//print all device info
log(device.toString());
JsonStorage.storeDevice(device, jsonPath);
return ScpStatus(status: ScpStatus.RESULT_SUCCESS);
}
return ScpStatus(status: ScpStatus.RESULT_ERROR);
}