UNPKG

@iotize/cli

Version:
175 lines 8.81 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); require("@iotize/device-client.js/ext/configurator"); const device_client_js_1 = require("@iotize/device-client.js"); const clime_1 = require("clime"); const ui_1 = require("../../../../ui"); const device_1 = require("../../../options/device"); class MyOptions extends device_1.DeviceOptions { } __decorate([ clime_1.option({ default: false }), __metadata("design:type", String) ], MyOptions.prototype, "clientId", void 0); __decorate([ clime_1.option({ default: false }), __metadata("design:type", String) ], MyOptions.prototype, "url", void 0); __decorate([ clime_1.option({ default: false }), __metadata("design:type", String) ], MyOptions.prototype, "port", void 0); __decorate([ clime_1.option({ default: false }), __metadata("design:type", String) ], MyOptions.prototype, "password", void 0); __decorate([ clime_1.option({}), __metadata("design:type", String) ], MyOptions.prototype, "netKey", void 0); __decorate([ clime_1.option({ default: false, description: 'Only display wifi configuration.', toggle: true }), __metadata("design:type", Boolean) ], MyOptions.prototype, "show", void 0); exports.MyOptions = MyOptions; let WifiConnectivityCommand = class WifiConnectivityCommand extends clime_1.Command { constructor() { super(...arguments); this.help = 'Setup mqtt connectivity'; this.command = 'mqtt setup'; } execute(options) { return __awaiter(this, void 0, void 0, function* () { const tap = yield options.getDevice(true, false, true); yield this.run({ tap }, { options }); yield tap.disconnect().catch(err => { }); }); } run(context, parms) { var _a, _b, _c, _d, _e, _f, _g; return __awaiter(this, void 0, void 0, function* () { const { tap } = context; const { options } = parms; const userConfig = global.configProvider.config(); const isConfigured = yield tap.configurator.isConfigured(); if (!((_a = options) === null || _a === void 0 ? void 0 : _a.show)) { if (isConfigured) { const configVersion = (yield tap.service.interface.getConfigVersion()).body(); ui_1.display.success(`✅ Tap is configured. Version: ${configVersion.major}.${configVersion.minor}.${configVersion.patch}`); } else { ui_1.display.warn(`❌ Tap is not configured.`); } yield tap.configurator.setupConfigState(); } const hostProtocols = (yield tap.service.interface.getAvailableHostProtocols()).body(); if (hostProtocols.indexOf(device_client_js_1.HostProtocol.WIFI) < 0) { throw new Error(`This Tap does not support WiFi`); } const infos = yield ui_1.getBodys({ clientId: tap.service.interface.getMqttRelayClientId(), username: tap.service.interface.getMqttRelayLogin(), url: tap.service.interface.getMqttRelayUrl(), port: tap.service.interface.getMqttRelayPort(), password: tap.service.interface.getMqttRelayPassword(), netKey: tap.service.interface.getMqttRelayNetKey() }); ui_1.display.table(infos); // const infoCloud = await getBodys({ // clientId: tap.service.interface.getCloudClientId(), // login: tap.service.interface.getCloudLogin(), // connectionMode: tap.service.interface.getCloudConnectionMode(), // password: tap.service.interface.getCloudPassword(), // broker: tap.service.interface.getCloudGatewayUrl(), // port: tap.service.interface.getCloudServiceName(), // }); // display.table(infoCloud); if (!((_b = options) === null || _b === void 0 ? void 0 : _b.show)) { const url = ((_c = options) === null || _c === void 0 ? void 0 : _c.url) || (yield ui_1.input.string(`Enter MQTT broker url`, { default: infos.url || 'user.cloud.iotize.com' })); const port = ((_d = options) === null || _d === void 0 ? void 0 : _d.port) || (yield ui_1.input.string(`Enter MQTT broker port`, { default: infos.port })); const clientId = ((_e = options) === null || _e === void 0 ? void 0 : _e.clientId) || (yield ui_1.input.string(`Enter MQTT broker client id`, { // default: })); // const username = options?.username || await input.string(`Enter MQTT broker username`, { // // default: // }); const password = ((_f = options) === null || _f === void 0 ? void 0 : _f.password) || (yield ui_1.input.password(`Enter MQTT broker password`, { default: infos.password })); const netKey = ((_g = options) === null || _g === void 0 ? void 0 : _g.netKey) || (yield ui_1.input.password(`Enter net key (used to hash topic name)`, { default: infos.netKey || 'testnetkey' })); (yield tap.service.interface.putMqttRelayUrl(url)).successful(); (yield tap.service.interface.putMqttRelayClientId(clientId)).successful(); (yield tap.service.interface.putMqttRelayPassword(password)).successful(); (yield tap.service.interface.putMqttRelayPort(port)).successful(); (yield tap.service.interface.putMqttRelayNetKey(netKey)).successful(); if (!isConfigured) { const newVersion = yield ui_1.input.semanticVersion(`New config version`, { default: '1.0.0' }); yield tap.configurator.applyConfig(newVersion); } ui_1.display.success(`Successfully configured!`); ui_1.display.infonl(`Rebooting...`); yield tap.service.device.reboot().catch(err => { if (process.env.DEBUG) { console.error(`Error code: ${err.code}, message: ${err.message}`, err); } }); if (context.continue) { ui_1.display.infonl(`Reconnecting...`); yield tap.disconnect(); yield tap.connect(); } } }); } }; __decorate([ clime_1.metadata, __metadata("design:type", Function), __metadata("design:paramtypes", [MyOptions]), __metadata("design:returntype", Promise) ], WifiConnectivityCommand.prototype, "execute", null); WifiConnectivityCommand = __decorate([ clime_1.command({ description: 'Setup MQTT configuration for tap device' }) ], WifiConnectivityCommand); exports.default = WifiConnectivityCommand; //# sourceMappingURL=mqtt.js.map