@hg8496/gridvis-client
Version:
A library to access all GridVis data
491 lines • 25 kB
JavaScript
"use strict";
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
var commander = require("commander");
var Client_1 = require("./Client");
var events_1 = require("./events");
var ISequence_1 = require("./sequences/ISequence");
function setupDefaultArguments(command) {
return command
.arguments("<URL>")
.option("-u, --username <username>", "Specify username", "admin")
.option("-p, --password <password>", "Specify password", "Janitza");
}
function addTimeOptions(command) {
return command
.option("-s, --start <time>", "Specify start time for queries", "NAMED_Today")
.option("-e, --end <time>", "Specify nd time for queries", "NAMED_Today");
}
function setupClient(url, command) {
return new Client_1.GridVisClient({
password: command.password,
url: url,
username: command.username,
});
}
function version(url, command) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = (_a = console).log;
return [4 /*yield*/, setupClient(url, command).fetchGridVisVersion()];
case 1:
_b.apply(_a, [_c.sent()]);
return [2 /*return*/];
}
});
});
}
function projects(url, command) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = (_a = console).log;
return [4 /*yield*/, setupClient(url, command).projects.list()];
case 1:
_b.apply(_a, [_c.sent()]);
return [2 /*return*/];
}
});
});
}
function showProject(url, projectName, command) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = (_a = console).log;
return [4 /*yield*/, setupClient(url, command).projects.get(projectName)];
case 1:
_b.apply(_a, [_c.sent()]);
return [2 /*return*/];
}
});
});
}
function devices(url, projectName, command) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, e_1;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
_b = (_a = console).log;
return [4 /*yield*/, setupClient(url, command).devices.list(projectName)];
case 1:
_b.apply(_a, [_c.sent()]);
return [3 /*break*/, 3];
case 2:
e_1 = _c.sent();
console.log(e_1);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
});
}
function findDevice(client, projectName, deviceIdent) {
return __awaiter(this, void 0, void 0, function () {
var allDevices;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client.devices.list(projectName)];
case 1:
allDevices = _a.sent();
return [2 /*return*/, allDevices.find(function (dev) { return dev.name.indexOf(deviceIdent) >= 0 || dev.serialNr === deviceIdent || dev.id.toString() === deviceIdent; })];
}
});
});
}
function deviceFinder(url, project, deviceIdent, command, caller) {
return __awaiter(this, void 0, void 0, function () {
var client, device, e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 5, , 6]);
client = setupClient(url, command);
return [4 /*yield*/, findDevice(client, project, deviceIdent)];
case 1:
device = _a.sent();
if (!device) return [3 /*break*/, 3];
return [4 /*yield*/, caller(client, device)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
console.log("Device not found");
_a.label = 4;
case 4: return [3 /*break*/, 6];
case 5:
e_2 = _a.sent();
console.log(e_2);
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
});
}
function output(counter) {
counter.forEach(function (value, type) {
console.log(type + ": " + value);
});
}
function recordings(url, projectName, deviceIdent, command) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, deviceFinder(url, projectName, deviceIdent, command, function (client, device) { return __awaiter(_this, void 0, void 0, function () {
var valDesc, valDesc_1, valDesc_1_1, vd;
var e_3, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, client.values.list(projectName, device)];
case 1:
valDesc = _b.sent();
try {
for (valDesc_1 = __values(valDesc), valDesc_1_1 = valDesc_1.next(); !valDesc_1_1.done; valDesc_1_1 = valDesc_1.next()) {
vd = valDesc_1_1.value;
console.log(vd);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (valDesc_1_1 && !valDesc_1_1.done && (_a = valDesc_1.return)) _a.call(valDesc_1);
}
finally { if (e_3) throw e_3.error; }
}
return [2 /*return*/];
}
});
}); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
function values(url, projectName, deviceIdent, value, type, timebase, command) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, deviceFinder(url, projectName, deviceIdent, command, function (client, device) { return __awaiter(_this, void 0, void 0, function () {
var valDesc, valDesc_2, valDesc_2_1, vd, vList, e_4_1;
var e_4, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, client.values.list(projectName, device)];
case 1:
valDesc = _b.sent();
_b.label = 2;
case 2:
_b.trys.push([2, 7, 8, 9]);
valDesc_2 = __values(valDesc), valDesc_2_1 = valDesc_2.next();
_b.label = 3;
case 3:
if (!!valDesc_2_1.done) return [3 /*break*/, 6];
vd = valDesc_2_1.value;
if (!(vd.valueType.value === value && vd.valueType.type === type && "" + vd.timebase === timebase)) return [3 /*break*/, 5];
return [4 /*yield*/, client.values.getValues(projectName, device, vd, command.start, command.end)];
case 4:
vList = _b.sent();
console.log(JSON.stringify(vList));
_b.label = 5;
case 5:
valDesc_2_1 = valDesc_2.next();
return [3 /*break*/, 3];
case 6: return [3 /*break*/, 9];
case 7:
e_4_1 = _b.sent();
e_4 = { error: e_4_1 };
return [3 /*break*/, 9];
case 8:
try {
if (valDesc_2_1 && !valDesc_2_1.done && (_a = valDesc_2.return)) _a.call(valDesc_2);
}
finally { if (e_4) throw e_4.error; }
return [7 /*endfinally*/];
case 9: return [2 /*return*/];
}
});
}); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
function onlinevalues(url, projectName, deviceIdent, valuesComSep, command) {
return __awaiter(this, void 0, void 0, function () {
var vNames;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
vNames = valuesComSep.split(",");
return [4 /*yield*/, deviceFinder(url, projectName, deviceIdent, command, function (client, device) { return __awaiter(_this, void 0, void 0, function () {
var valTypes, useValTypes, valTypes_1, valTypes_1_1, vt, vNames_1, vNames_1_1, vName, result;
var e_5, _a, e_6, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, client.values.listOnline(projectName, device)];
case 1:
valTypes = _c.sent();
useValTypes = [];
try {
for (valTypes_1 = __values(valTypes), valTypes_1_1 = valTypes_1.next(); !valTypes_1_1.done; valTypes_1_1 = valTypes_1.next()) {
vt = valTypes_1_1.value;
try {
for (vNames_1 = (e_6 = void 0, __values(vNames)), vNames_1_1 = vNames_1.next(); !vNames_1_1.done; vNames_1_1 = vNames_1.next()) {
vName = vNames_1_1.value;
if (vt.value === vName) {
useValTypes.push(vt);
}
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (vNames_1_1 && !vNames_1_1.done && (_b = vNames_1.return)) _b.call(vNames_1);
}
finally { if (e_6) throw e_6.error; }
}
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (valTypes_1_1 && !valTypes_1_1.done && (_a = valTypes_1.return)) _a.call(valTypes_1);
}
finally { if (e_5) throw e_5.error; }
}
return [4 /*yield*/, client.values.getOnlineValues(projectName, device, useValTypes)];
case 2:
result = _c.sent();
console.log(result);
return [2 /*return*/];
}
});
}); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
function transients(url, projectName, deviceIdent, command) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, deviceFinder(url, projectName, deviceIdent, command, function (client, device) { return __awaiter(_this, void 0, void 0, function () {
var reducer, typeCount;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
reducer = function (current, trans) {
var counter = current.get(trans.type);
current.set(trans.type, counter ? counter + 1 : 1);
return current;
};
return [4 /*yield*/, client.transients.getTransients(projectName, device, command.start, command.end)];
case 1:
typeCount = (_a.sent()).reduce(reducer, new Map());
output(typeCount);
return [2 /*return*/];
}
});
}); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
function sequences(url, projectName, deviceIdent, command) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, deviceFinder(url, projectName, deviceIdent, command, function (client, device) { return __awaiter(_this, void 0, void 0, function () {
var seq;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client.sequences.getSequences(projectName, device, ISequence_1.SequenceTypes.Waveform, command.start, command.end)];
case 1:
seq = _a.sent();
console.log("Waveforms: " + seq.length);
return [4 /*yield*/, client.sequences.getSequences(projectName, device, ISequence_1.SequenceTypes.EffectiveValues, command.start, command.end)];
case 2:
seq = _a.sent();
console.log("EffectiveValues: " + seq.length);
return [2 /*return*/];
}
});
}); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
function events(url, projectName, deviceIdent, command) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, deviceFinder(url, projectName, deviceIdent, command, function (client, device) { return __awaiter(_this, void 0, void 0, function () {
var reducer, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
reducer = function (current, evt) {
var counter = current.get(evt.eventType);
current.set(evt.eventType, counter ? counter + 1 : 1);
return current;
};
_a = output;
return [4 /*yield*/, client.events.getEvents(projectName, device, [events_1.EventTypes.VoltageOver, events_1.EventTypes.VoltageUnder], command.start, command.end)];
case 1:
_a.apply(void 0, [(_b.sent()).reduce(reducer, new Map())]);
return [2 /*return*/];
}
});
}); })];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
function main() {
return __awaiter(this, void 0, void 0, function () {
var program;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
program = new commander.Command();
program.version("1.0.20");
setupDefaultArguments(program.command("version"))
.description("Returns GridVis version")
.action(version);
setupDefaultArguments(program.command("projects"))
.description("List all projects")
.action(projects);
setupDefaultArguments(program.command("project"))
.description("Fetch a single project")
.arguments("<projectName>")
.action(showProject);
setupDefaultArguments(program.command("devices"))
.description("List all devices of a project")
.arguments("<projectName>")
.action(devices);
addTimeOptions(setupDefaultArguments(program.command("events"))
.description("Counts events of a device in a given time frame")
.arguments("<projectName>")
.arguments("<deviceNameOrSerialOrId>")
.action(events));
addTimeOptions(setupDefaultArguments(program.command("transients"))
.description("Count transients of a device in a given time frame")
.arguments("<projectName>")
.arguments("<deviceNameOrSerialOrId>")
.action(transients));
addTimeOptions(setupDefaultArguments(program.command("sequences"))
.description("Count sequences of a device in a given time frame")
.arguments("<projectName>")
.arguments("<deviceNameOrSerialOrId>")
.action(sequences));
setupDefaultArguments(program.command("recordings"))
.description("List all recorded values of a device")
.arguments("<projectName>")
.arguments("<deviceNameOrSerialOrId>")
.action(recordings);
addTimeOptions(setupDefaultArguments(program.command("values"))
.description("prints values of a device in a given time frame")
.arguments("<projectName>")
.arguments("<deviceNameOrSerialOrId>")
.arguments("<value>")
.arguments("<type>")
.arguments("<timebase>")
.action(values));
setupDefaultArguments(program.command("online"))
.description("Fetch online values of a device")
.arguments("<projectName>")
.arguments("<deviceNameOrSerialOrId>")
.arguments("<valuesCommaSep>")
.action(onlinevalues);
if (process.argv.length <= 2) {
program.outputHelp();
return [2 /*return*/];
}
return [4 /*yield*/, program.parseAsync(process.argv)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
main().catch(function (e) { return console.log(e); });
//# sourceMappingURL=cli.js.map