sendRestart static method

dynamic sendRestart(
  1. ScpDevice device
)

Implementation

static sendRestart(ScpDevice device) async {
  // get NVCN
  print('Fetching NVCN');
  var nvcnResponse = await fetchNVCN(device);
  if (nvcnResponse == null) {
    return ScpStatus.RESULT_ERROR;
  }
  if (nvcnResponse.statusCode != 200 || nvcnResponse.bodyBytes == 0) {
    return ScpStatus.RESULT_ERROR;
  }
  ScpResponseFetchNvcn parsedNvcnResponse =
      await ScpResponseParser.parseNvcnResponse(nvcnResponse);

  String nvcn = parsedNvcnResponse.getNVCN();

  //send new wifi credentials
  // <salt> + ":" + "security-wifi-config" + ":" + <device ID> + ":" + <NVCN>

  String salt = ScpCrypto().generatePassword();
  String payload = "$salt:security-restart:${device.deviceId}:$nvcn";
  ScpJson scpJson =
      await ScpCrypto().encryptThenEncode(device.knownPassword, payload);

  String query = "nonce=${urlEncode(scpJson.encryptedPayload.base64Nonce)}";
  query += "&payload=${urlEncode(scpJson.encryptedPayload.base64Data)}";
  query += "&payloadLength=${scpJson.encryptedPayload.dataLength}";
  query += "&mac=${urlEncode(scpJson.encryptedPayload.base64Mac)}";

  // await response
  print('Restarting device.');
  Uri url = Uri.dataFromString(
      'http://${device.ipAddress}:$PORT/secure-control?$query');
  var restartDeviceResponse = await http
      .get(url)
      .timeout(const Duration(seconds: RESTART_TIMEOUT))
      .catchError((e) {
    print(e);
  });

  if (restartDeviceResponse.bodyBytes != null) {
    if (restartDeviceResponse.statusCode == 200) {
      ScpResponseRestart parsedResponse =
          await ScpResponseParser.parseRestartDeviceResponse(
              restartDeviceResponse, device.knownPassword);
      if (parsedResponse.isValid()) {
        if (parsedResponse.getResult() == ScpStatus.RESULT_SUCCESS) {
          print('Successfully restarted device.');
          return ScpStatus.RESULT_DONE;
        } else if (parsedResponse.getResult() == ScpStatus.RESULT_ERROR) {
          print('failed to restart device');
          return ScpStatus.RESULT_ERROR;
        }
      }
    }
  }
  return ScpStatus.RESULT_ERROR;
}