in src/Models/AZ3166Device.ts [614:762]
private flushDeviceConfig(configValue: string, option: ConfigDeviceOptions): Promise<boolean> {
return new Promise(
// eslint-disable-next-line no-async-promise-executor
async (resolve: (value: boolean) => void, reject: (value: Error) => void) => {
let comPort = "";
let command = "";
try {
// Choose COM port that AZ3166 is connected
comPort = await this.chooseCOM();
console.log(`Opening ${comPort}.`);
} catch (error) {
reject(error);
}
if (option === ConfigDeviceOptions.ConnectionString) {
command = "set_az_iothub";
} else if (option === ConfigDeviceOptions.DPS) {
command = "set_az_iotdps";
} else {
command = "set_dps_uds";
}
let configMode = false;
let errorRejected = false;
let commandExecuted = false;
let gotData = false;
let az3166: Board;
try {
az3166 = this.board;
} catch (error) {
return reject(error);
}
const port = new AZ3166Device.serialport(comPort, {
baudRate: az3166.defaultBaudRate,
dataBits: 8,
stopBits: 1,
xon: false,
xoff: false,
parity: "none"
});
const rejectIfError = (err: Error): boolean => {
if (errorRejected) return true;
if (err) {
errorRejected = true;
reject(err);
try {
port.close();
} catch (ignore) {
// Ignore error if fail to close port
}
}
return true;
};
const executeSetAzIoTHub = async (): Promise<void> => {
try {
const data = `${command} "${configValue}"\r\n`;
const maxDataLength = 256;
await this.sendDataViaSerialPort(port, data.slice(0, maxDataLength));
if (data.length > maxDataLength) {
await delay(1000);
await this.sendDataViaSerialPort(port, data.slice(maxDataLength));
}
await delay(1000);
port.close();
} catch (ignore) {
// Ignore error if fail to close port
}
if (errorRejected) {
return;
} else {
resolve(true);
}
};
// Configure serial port callbacks
port.on("open", () => {
port.write(
"\r\nhelp\r\n",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error: any) => {
if (rejectIfError(error)) return;
}
);
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
port.on("data", (data: any) => {
gotData = true;
const output = data.toString().trim();
if (commandExecuted) return;
if (output.includes("set_")) {
commandExecuted = true;
configMode = true;
executeSetAzIoTHub()
.then(() => resolve(true))
.catch(error => reject(error));
} else {
configMode = false;
}
if (configMode) {
forEach(output.split("\n"), line => {
if (line) {
line = trimStart(line.trim(), "#").trim();
if (line && line.length) {
console.log("SerialOutput", line);
}
}
});
}
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
port.on("error", (error: any) => {
if (errorRejected) return;
console.log(error);
rejectIfError(error);
});
setTimeout(() => {
if (errorRejected) return;
// Prompt user to enter configuration mode
if (!gotData || !configMode) {
vscode.window
.showInformationMessage(
"Please hold down button A and then push and release the reset button to enter configuration mode."
)
.then(() => {
port.write(
"\r\nhelp\r\n",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error: any) => {
rejectIfError(error);
}
);
});
}
}, 10000);
}
);
}