@iotize/cli
Version:
IoTize command line interface
175 lines • 7.34 kB
JavaScript
;
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 });
const cli_core_1 = require("@iotize/cli-core");
const files_1 = require("@iotize/common/files");
const device_client_js_1 = require("@iotize/device-client.js");
const impl_1 = require("@iotize/device-client.js/client/impl");
const tap_app_core_node_1 = require("@iotize/tap-app-core.node");
const inquirer_1 = require("inquirer");
const path_1 = require("path");
const operators_1 = require("rxjs/operators");
const config_1 = require("src/config");
const inversify_config_1 = require("src/inversify.config");
const types_1 = require("src/types");
const ui_1 = require("../../ui");
function scanItemToString(item) {
let connectionString;
switch (item.type) {
case 'BLE':
connectionString = `ble://${item.address.replace(/:/g, '.')}`;
break;
}
return `[${item.type}] ${item.name} | ${item.address} ${connectionString}`;
}
exports.scanItemToString = scanItemToString;
class Input extends cli_core_1.Ask {
url(msg, defaultValue) {
return this.string(msg, {
default: defaultValue
// match: /^([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*$/
});
}
androidApplicationRecord(msg, defaultValue) {
return this.string(msg, {
match: /^([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*$/
});
}
ipv4(msg) {
return this.string(msg, {
match: /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
});
}
semanticVersion(msg, options) {
var _a;
return this.string(msg, {
match: /^[0-9]+\.[0-9]+\.[0-9]+$/,
default: (_a = options) === null || _a === void 0 ? void 0 : _a.default
});
}
selectLwm2mCommand() {
return __awaiter(this, void 0, void 0, function* () {
const tap = device_client_js_1.Tap.create();
const services = Object.values(tap.service).reduce((prev, service) => {
for (const name in service.resources) {
const call = service.resources[name];
prev.push({
name: `${service.constructor.name.replace('Service', '')} ${name} - ${call.methodType} ${call.path}`,
value: call
});
}
return prev;
}, []);
const call = yield this.list(`Select command`, services);
const parameters = impl_1.PathParameter.extractParams(call.path);
const pathParameters = {};
if (parameters.length > 0) {
for (const param of parameters) {
pathParameters[param] = yield this.number(param);
}
}
return Object.assign(Object.assign({}, call), { pathParameters });
});
}
tapCredentials(defaultValues = {}) {
return __awaiter(this, void 0, void 0, function* () {
return yield inquirer_1.prompt([
{
message: 'Enter username',
type: 'text',
default: defaultValues.username,
name: 'username'
},
{
message: 'Enter password',
type: 'password',
default: defaultValues.password,
name: 'password'
}
]);
});
}
selectConfigFile(msg = 'Select configuration file') {
return __awaiter(this, void 0, void 0, function* () {
const files = files_1.listFiles(path_1.resolve(config_1.RESOURCE_PATH, 'tap-configurations'));
const { filepath } = yield inquirer_1.prompt({
message: msg,
type: 'list',
choices: files.map(filepath => {
return {
value: filepath,
name: path_1.basename(filepath)
};
}),
name: 'filepath'
});
return filepath;
});
}
/**
* @deprecated
* // TODO use @iotize/tap-app-core.node method
* @param scanTimeout
* @param options
*/
selectTap(scanTimeout = 5000, options = { refreshable: true, cancelable: true }) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const scanner = yield inversify_config_1.container.get(types_1.TYPES.DeviceScannerProvider)();
ui_1.display.infonl(`Starting scan for ${scanTimeout}ms...`);
let value;
do {
const tapList = yield scanner
.list(scanTimeout)
.pipe(operators_1.map(items => items.filter(tap_app_core_node_1.FILTER_IOTIZE_DEVICE)))
.toPromise();
const choices = tapList.map(item => {
return {
value: item,
name: scanItemToString(item)
};
});
if ((_a = options) === null || _a === void 0 ? void 0 : _a.refreshable) {
choices.push({
value: '__REFRESH__',
name: 'Re scan'
});
}
if ((_b = options) === null || _b === void 0 ? void 0 : _b.cancelable) {
choices.push({
value: '__CANCEL__',
name: 'Cancel'
});
}
const answer = yield inquirer_1.prompt({
message: 'Select a Tap',
type: 'list',
choices,
name: 'value'
});
value = answer.value;
} while (value === '__REFRESH__');
if (value === '__CANCEL__') {
throw new Error(`Tap selection cancel by user`);
}
// process.stdin.on('keypress', (ch, key) => {
// console.log('KEY PRESS: ', ...arguments);
// // if (key.ctrl && key.name === 'c') process.exit();
// // console.log(key);
// });
const scanItem = value;
return device_client_js_1.Tap.fromProtocol(scanItem.protocolFactory());
});
}
}
exports.Input = Input;
exports.input = new Input();
//# sourceMappingURL=input.js.map