@iotize/cli
Version:
IoTize command line interface
170 lines • 8.69 kB
JavaScript
;
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");
const constants_1 = require("../../../constants");
class MyOptions extends device_1.DeviceOptions {
}
__decorate([
clime_1.option({
default: false
}),
__metadata("design:type", String)
], MyOptions.prototype, "mode", void 0);
__decorate([
clime_1.option({
default: false
}),
__metadata("design:type", String)
], MyOptions.prototype, "ssid", void 0);
__decorate([
clime_1.option({
default: false
}),
__metadata("design:type", String)
], MyOptions.prototype, "password", void 0);
__decorate([
clime_1.option({
description: 'TapNLink IP address (if not provided DHCP will be used)',
default: false
}),
__metadata("design:type", String)
], MyOptions.prototype, "ip", 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 wifi';
this.command = 'wifi 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;
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 { ssid, wepKey, networkMode, hostname, ip, gatewayIp, ipMask } = yield ui_1.getBodys({
ssid: tap.service.interface.getWifiSSID(),
wepKey: tap.service.interface.getWepKey(),
networkMode: tap.service.interface.getNetworkMode(),
ip: tap.service.interface.getNetworkInfraIp(),
hostname: tap.service.interface.getWifiHostname(),
ipMask: tap.service.interface.getNetworkInfraIpMask(),
gatewayIp: tap.service.interface.getNetworkGatewayIp()
});
const DHCP_IP = '0.0.0.0';
ui_1.display.table({ SSID: ssid, key: wepKey, mode: device_client_js_1.NetworkMode[networkMode], 'IP/hostname': hostname, DHCP: `${ip !== DHCP_IP ? `NO (${ip})` : 'YES'}`, mask: ipMask, 'Gateway IP': gatewayIp });
if (!((_b = options) === null || _b === void 0 ? void 0 : _b.show)) {
const SSID = ((_c = options) === null || _c === void 0 ? void 0 : _c.ssid) || (yield ui_1.input.string(`Enter the SSID (Wi-Fi network name)`, {
default: userConfig.get(`keystore.${constants_1.CONFIG_KEY_DEFAULT_WIFI_SSID}`, ssid)
}));
const password = ((_d = options) === null || _d === void 0 ? void 0 : _d.password) || (yield ui_1.input.password(`Enter the Wi-Fi key`, {
default: userConfig.get(`keystore.${constants_1.CONFIG_KEY_DEFAULT_WIFI_PASSWORD}`, '')
}));
let newNetworkMode;
if ((_e = options) === null || _e === void 0 ? void 0 : _e.mode) {
switch (options.mode) {
case 'p2p':
newNetworkMode = device_client_js_1.NetworkMode.PEER_TO_PEER;
break;
case 'network':
case 'infra':
case 'infrastructure':
newNetworkMode = device_client_js_1.NetworkMode.INFRASTRUCTURE_ONLY;
break;
}
}
if (!newNetworkMode) {
newNetworkMode = yield ui_1.input.enum(`Enter the network mode`, device_client_js_1.NetworkMode, networkMode);
}
const ipAddress = ((_f = options) === null || _f === void 0 ? void 0 : _f.ip) || DHCP_IP;
(yield tap.service.interface.putWepKey(password)).successful();
(yield tap.service.interface.putWifiSSID(SSID)).successful();
(yield tap.service.interface.putNetworkMode(newNetworkMode)).successful();
(yield tap.service.interface.putNetworkInfraIp(ipAddress)).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 WIFI configuration for tap device'
})
], WifiConnectivityCommand);
exports.default = WifiConnectivityCommand;
//# sourceMappingURL=wifi.js.map