UNPKG

@iotize/cli

Version:
142 lines 6.23 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 }); const clime_1 = require("clime"); const monitoring_sreen_1 = require("../../../lib/ui/monitoring-sreen"); const operators_1 = require("rxjs/operators"); const rxjs_1 = require("rxjs"); const device_1 = require("../../options/device"); const ui_1 = require("src/ui"); class MyOptions extends device_1.DeviceOptions { } __decorate([ clime_1.option({ description: 'Duration (in millisecond). Script will stop after this delay. By default duration is infinite', required: false, default: undefined }), __metadata("design:type", Number) ], MyOptions.prototype, "duration", void 0); __decorate([ clime_1.option({ description: 'List of variables to monitor (comma separated ie: v1,v2,...)', required: false, default: undefined // validator: (value: string, context: ValidationContext) => { // } }), __metadata("design:type", String) ], MyOptions.prototype, "variables", void 0); __decorate([ clime_1.option({ description: 'Pretty display (will not work properly if console logs are enabled)', required: false, default: false, toggle: true }), __metadata("design:type", Boolean) ], MyOptions.prototype, "pretty", void 0); __decorate([ clime_1.option({ description: 'Refresh period (in milliseconds)', required: false, default: 1000 }), __metadata("design:type", Number) ], MyOptions.prototype, "refreshPeriod", void 0); exports.MyOptions = MyOptions; let default_1 = class default_1 extends clime_1.Command { execute(options) { return __awaiter(this, void 0, void 0, function* () { // if (!options.config) { // throw new ExpectedError(`--config option is required`); // } // display.info(`Read configuration file from ${options.config}`); let device = yield options.getDevice(true, true); let variableManager = device.variables; let values = {}; let variableNames = []; if (options.variables) { variableNames = options.variables .split(',') .map(entry => entry.trim()); } else { for (let variableIdString in variableManager.variables) { let variableId = parseInt(variableIdString); let variable = variableManager.variables[variableId]; variableNames.push(variableIdString); } } if (variableNames.length === 0) { yield device.disconnect(); throw new clime_1.ExpectedError(`No variable to monitor`); } let observables = []; let screen; if (options.pretty) { screen = new monitoring_sreen_1.MonitoringScreen(); } variableNames.forEach(function (variableName, index) { let variable = variableManager.variable(variableName); let monitor = variable.monitor(); monitor .start({ period: options.refreshPeriod }) .values() .subscribe((value) => { values[variableName] = value.toString(); if (options.pretty) { screen.updateScreen(values); } else { ui_1.display.success(`---------------------------`); ui_1.display.pretty(values); } }, err => { ui_1.display.error(`Error variable: ${variableName}: ${err}`); }); console.info(`\t- Adding target variable "${variableName}" for monitoring`); }); if (options.duration) { return rxjs_1.of(null) .pipe(operators_1.delay(options.duration)) .toPromise(); } else { return new Promise((resolve, reject) => { }); } }); } }; __decorate([ clime_1.metadata, __metadata("design:type", Function), __metadata("design:paramtypes", [MyOptions]), __metadata("design:returntype", Promise) ], default_1.prototype, "execute", null); default_1 = __decorate([ clime_1.command({ description: 'Monitor Tap device variables' }) ], default_1); exports.default = default_1; //# sourceMappingURL=monitor.js.map