@twilio/plugin-microvisor
Version:
Interact with your Twilio Microvisor devices
113 lines (112 loc) • 4.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCommand = void 0;
exports.BaseCommand = require('@twilio/cli-core').baseCommands.BaseCommand;
const { DelimiterParser } = require('@serialport/parser-delimiter');
const serialport_1 = require("serialport");
const common_1 = require("../../../lib/provision/common");
const proto_1 = require("../../../../proto_gen/proto");
const MAIN_ERROR = "Could not set WiFi credentials";
const REQUEST_TYPE = proto_1.provision.ProvisioningCommands_MessageTypes_F2D.TYPE_F2D_SET_WIFI_CONFIGURATION_REQUEST;
const EOL = (process.platform == "linux" || process.platform == "darwin") ? "\n" : "\r\n";
class MicrovisorWiFiConfigSet extends exports.BaseCommand {
async run() {
await super.run();
const passThis = this;
passThis.requestType = REQUEST_TYPE;
passThis.mainError = MAIN_ERROR;
setDeviceData(passThis);
}
async runCommand() {
return run();
}
}
exports.default = MicrovisorWiFiConfigSet;
MicrovisorWiFiConfigSet.description = "Set a Microvisor device's WiFi credentials";
MicrovisorWiFiConfigSet.args = [
{
name: 'wifissid',
required: true,
description: 'The WiFi network name'
},
{
name: 'wifipwd',
required: true,
description: 'The WiFI password'
},
{
name: 'devicePath',
required: true,
description: 'The path to the device file or COM port'
}
];
function setDeviceData(passThis) {
// Check user perms on Linux
if (!(0, common_1.checkUserPermissions)()) {
passThis.logger.error(`${passThis.mainError}. Please re-run as root or as a member of dialout.${EOL}`);
process.exit(1);
}
const port = new serialport_1.SerialPort({ path: passThis.args.devicePath, baudRate: 115200 });
port.on("open", function () {
// Clear the pipes
port.flush();
// Establish the payload data
const utf8Encoder = new TextEncoder();
const wc = {
configuration: {
ssid: utf8Encoder.encode(passThis.args.wifissid),
password: utf8Encoder.encode(passThis.args.wifipwd)
}
};
const errMsg = proto_1.provision.SetWifiConfigurationCommand.verify(wc);
if (errMsg)
throw Error(errMsg);
const protoMsg = proto_1.provision.SetWifiConfigurationCommand.create(wc);
const protoMsgBuffer = proto_1.provision.SetWifiConfigurationCommand.encode(protoMsg).finish();
const fullMsg = new Uint8Array(3 + protoMsgBuffer.length);
fullMsg[0] = REQUEST_TYPE;
fullMsg[1] = (protoMsgBuffer.length >> 8) & 0xFF;
fullMsg[2] = protoMsgBuffer.length & 0xFF;
fullMsg.set(protoMsgBuffer, 3);
// Base64 the message
const b64Data = Buffer.from(fullMsg).toString('base64');
const payload = utf8Encoder.encode("CMD:" + b64Data);
// Compose the packet
const head = new Uint8Array([0x0D, 0x0A]);
const tail = new Uint8Array([0x3A, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x0A]);
// Insert CRC16
const crc = (0, common_1.crc16)(payload);
const crcs = crc.toString(16).padStart(4, '0');
const crc_data = utf8Encoder.encode(crcs);
tail.set(crc_data, 1);
const packet = new Uint8Array(head.length + payload.length + tail.length);
packet.set(head);
packet.set(payload, head.length);
packet.set(tail, head.length + payload.length);
// Prepare a listener for lines ending in \n
const parser = port.pipe(new DelimiterParser({ delimiter: '\r\n' }));
parser.on('data', function (rxData) {
if ((0, common_1.processResponse)(passThis, rxData)) {
clearTimeout(timer);
port.close();
}
});
// Set a timeout value
const timer = setTimeout(common_1.didTimeout, 5000, passThis, port);
// Issue the command to write the credentials
port.write(packet, "utf8", function (err) {
if (err instanceof Error) {
clearTimeout(timer);
port.close();
passThis.logger.error(`${passThis.mainError} (${err.message}).${EOL}`);
}
});
});
port.on("error", function (err) {
passThis.logger.error(`${passThis.mainError} (${err.message}).${EOL}`);
});
}
// Remove some unnecessary options
delete MicrovisorWiFiConfigSet.flags['cli-output-format'];
delete MicrovisorWiFiConfigSet.flags.silent;
delete MicrovisorWiFiConfigSet.flags.profile;