UNPKG

@iotize/tap

Version:

IoTize Device client for Javascript

1,175 lines (1,160 loc) 98.6 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@iotize/common/error'), require('@iotize/tap/service/impl/variable'), require('@iotize/common/byte-converter'), require('@iotize/common/byte-stream'), require('@iotize/common/data-accessor'), require('@iotize/tap'), require('@iotize/tap/configurator'), require('@iotize/tap/service/all'), require('@iotize/tap/service/core'), require('fast-xml-parser')) : typeof define === 'function' && define.amd ? define('@iotize/tap/config/iotize-studio', ['exports', '@iotize/common/error', '@iotize/tap/service/impl/variable', '@iotize/common/byte-converter', '@iotize/common/byte-stream', '@iotize/common/data-accessor', '@iotize/tap', '@iotize/tap/configurator', '@iotize/tap/service/all', '@iotize/tap/service/core', 'fast-xml-parser'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.iotize = global.iotize || {}, global.iotize.tap = global.iotize.tap || {}, global.iotize.tap.config = global.iotize.tap.config || {}, global.iotize.tap.config["iotize-studio"] = {}), global.error, global.iotize.tap.service.impl.variable, global.byteConverter, global.byteStream, global.dataAccessor, global.iotize.tap, global.iotize.tap.configurator, global.iotize.tap.service.all, global.iotize.tap.service.core, global.fastXmlParser)); })(this, (function (exports, error, variable, byteConverter, byteStream, dataAccessor, tap, configurator, all, core, fastXmlParser) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function () { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __decorate(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; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); }; } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __awaiter(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()); }); } function __generator(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 __createBinding = Object.create ? (function (o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } }); }) : (function (o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; }); function __exportStar(m, o) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p); } function __values(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."); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } /** @deprecated */ function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } /** @deprecated */ function __spreadArrays() { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; } function __spreadArray(to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); } function __await(v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } function __asyncGenerator(thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } } function __asyncDelegator(o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } } function __asyncValues(o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); } } function __makeTemplateObject(cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; } ; var __setModuleDefault = Object.create ? (function (o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function (o, v) { o["default"] = v; }; function __importStar(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; } function __importDefault(mod) { return (mod && mod.__esModule) ? mod : { default: mod }; } function __classPrivateFieldGet(receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); } function __classPrivateFieldSet(receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; } var InvalidConfigurationError = /** @class */ (function (_super) { __extends(InvalidConfigurationError, _super); function InvalidConfigurationError(msg) { return _super.call(this, msg) || this; } return InvalidConfigurationError; }(error.BaseError)); exports.ByteOrder32Bits = void 0; (function (ByteOrder32Bits) { ByteOrder32Bits[ByteOrder32Bits["B3_B2_B1_B0"] = 0] = "B3_B2_B1_B0"; ByteOrder32Bits[ByteOrder32Bits["B1_B0_B3_B2"] = 1] = "B1_B0_B3_B2"; ByteOrder32Bits[ByteOrder32Bits["B2_B3_B0_B1"] = 2] = "B2_B3_B0_B1"; ByteOrder32Bits[ByteOrder32Bits["B0_B1_B2_B3"] = 3] = "B0_B1_B2_B3"; })(exports.ByteOrder32Bits || (exports.ByteOrder32Bits = {})); exports.ByteOrder16Bits = void 0; (function (ByteOrder16Bits) { ByteOrder16Bits[ByteOrder16Bits["B1_B0"] = 0] = "B1_B0"; ByteOrder16Bits[ByteOrder16Bits["B0_B1"] = 1] = "B0_B1"; })(exports.ByteOrder16Bits || (exports.ByteOrder16Bits = {})); function toEnumFromValue(mapping, value) { var values = Object.keys(mapping).map(function (key) { return mapping[key]; }); if (values.find(function (v) { return v.toString() === value; }) === undefined) { throw new Error("Missing value \"" + value + "\" in enum. Available keys are " + values.join(', ')); } if (value in mapping) { // For string to number convertion return mapping[mapping[value]]; } return value; } function toEnumType(value, mapping) { if (!(value in mapping)) { throw new Error("Missing mapping for " + value + ". Available: " + Object.keys(mapping).join(', ')); } return mapping[value]; } // export function toEnumFromKey<T>(mapping: any, key: string): T { // if (!(key in mapping)) { // throw new Error(`Missing key "${key}" in enum. Available keys are ${Object.keys(mapping).join(', ')}`); // } // return mapping[mapping[key]]; // } var IotizeVariableModel = /** @class */ (function () { function IotizeVariableModel(data, iotizeModule, bundle) { this.data = data; this.iotizeModule = iotizeModule; if (bundle) { this._bundle = bundle; } } IotizeVariableModel.prototype.toJson = function () { return this.data; }; IotizeVariableModel.prototype.module = function () { return this.iotizeModule; }; /** * Size (int bits) of one element */ IotizeVariableModel.prototype.valueSize = function () { return IotizeVariableModel.unitSizeToBitSize(parseInt(this.data.$.Unit)); }; IotizeVariableModel.prototype.valueQuantity = function () { return Number(this.data.$.Quantity); }; IotizeVariableModel.prototype.name = function () { return this.data.$.GUI_Name; }; IotizeVariableModel.prototype.id = function () { return this.data.$.Id; }; IotizeVariableModel.prototype.alias = function () { return this.data.$.GUI_Alias; }; IotizeVariableModel.prototype.description = function () { return this.data.$.GUI_Description; }; IotizeVariableModel.prototype.unit = function () { return this.data.$.GUI_DispUnit; }; IotizeVariableModel.prototype.bundle = function () { return this._bundle; }; // public type(): VariableType.Data { // if (this.data.$.GUI_float && this.data.$.GUI_float == 1) { // return VariableType.Data.FLOAT32; // } else if (this.data.$.GUI_unsigned && this.data.$.GUI_unsigned == 1) { // return VariableType.Data.UINT32; // } else { // return VariableType.Data.INT32; // } // } IotizeVariableModel.prototype.dataType = function () { var unit = parseInt(this.data.$.Unit, 10); if (this.data.$.GUI_float && this.data.$.GUI_float == 1) { return variable.VariableType.Data.FLOAT32; } else if (this.data.$.GUI_unsigned && this.data.$.GUI_unsigned == 1) { switch (unit) { case 0: return variable.VariableType.Data.BOOLEAN; case 1: return variable.VariableType.Data.UINT8; case 2: return variable.VariableType.Data.UINT16; case 3: return variable.VariableType.Data.UINT32; default: throw new Error("Invalid data size \"" + unit + "\" for variable " + this.name()); } } else { switch (unit) { case 0: return variable.VariableType.Data.BOOLEAN; case 1: return variable.VariableType.Data.INT8; case 2: return variable.VariableType.Data.INT16; case 3: return variable.VariableType.Data.INT32; default: throw new Error("Invalid data size \"" + unit + "\" for variable " + this.name()); } } }; /** * Return attribute by name */ IotizeVariableModel.prototype.attribute = function (name) { return this.data.$[name]; }; IotizeVariableModel.prototype.byteOrder = function () { var guiCoding = this.attribute('GUI_coding'); if (guiCoding) { if (this.valueSize() === 16) { return toEnumFromValue(exports.ByteOrder16Bits, guiCoding); } else if (this.valueSize() === 32) { return toEnumFromValue(exports.ByteOrder32Bits, guiCoding); } } return undefined; }; /** * Convert unit size found in configuration file into a number of bits */ IotizeVariableModel.unitSizeToBitSize = function (unitSize) { switch (unitSize) { case 0: return 1; case 1: return 8; case 2: return 16; case 3: return 32; default: throw new Error("Invalid unit size: " + unitSize); } }; return IotizeVariableModel; }()); var IotizeProfileModel = /** @class */ (function () { function IotizeProfileModel(data) { this.data = data; } IotizeProfileModel.prototype.toJson = function () { return this.data; }; IotizeProfileModel.prototype.description = function () { return this.data.$.GUI_Description; }; IotizeProfileModel.prototype.name = function () { return this.data.$.Name; }; IotizeProfileModel.prototype.id = function () { return this.data.$.Id; }; return IotizeProfileModel; }()); var IotizeACL = /** @class */ (function () { function IotizeACL(data, aclGroup) { this.aclGroup = aclGroup; this.data = data; } IotizeACL.create = function (profileName, aclGroup) { return new IotizeACL({ $: { ProfileName: profileName, Rights: '', }, }, aclGroup); }; IotizeACL.prototype.toJson = function () { return this.data; }; IotizeACL.prototype.listRights = function () { var rights = this._getProperty('Rights', ''); if (rights.length === 0) { return []; } return rights.split(',').map(function (entry) { return entry.trim(); }); }; IotizeACL.prototype.username = function () { return this._getProperty('ProfileName'); }; IotizeACL.prototype.hasRight = function (name) { if (this.username() === 'admin') { return true; } var rights = this.listRights(); return rights.indexOf(name) !== -1; }; IotizeACL.prototype._getProperty = function (name, defaultValue) { if (!(name in this.data.$)) { if (defaultValue === undefined) { throw new InvalidConfigurationError('Missing key ' + name + ' in IoTize ACL'); } return defaultValue; } return this.data.$[name]; }; return IotizeACL; }()); var ANONYMOUS_PROFILE = 'anonymous'; /** * */ var IotizeACLGroup = /** @class */ (function () { function IotizeACLGroup(data, bundle) { this.bundle = bundle; this.data = data; } IotizeACLGroup.prototype.toJson = function () { return this.data; }; /** * Get acl for a profile * @param profileName */ IotizeACLGroup.prototype.getByProfileName = function (profileName) { var e_1, _a; var profiles = this.listProfiles(); try { for (var profiles_1 = __values(profiles), profiles_1_1 = profiles_1.next(); !profiles_1_1.done; profiles_1_1 = profiles_1.next()) { var profileAcl = profiles_1_1.value; if (profileAcl.username() === profileName) { return profileAcl; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (profiles_1_1 && !profiles_1_1.done && (_a = profiles_1.return)) _a.call(profiles_1); } finally { if (e_1) throw e_1.error; } } return IotizeACL.create(profileName, this); }; IotizeACLGroup.prototype.hasRight = function (profileName, right) { var anonymousProfile = this.getByProfileName(ANONYMOUS_PROFILE); if (anonymousProfile && anonymousProfile.hasRight(right)) { return true; } if (profileName !== ANONYMOUS_PROFILE) { var profile = this.getByProfileName(profileName); if (profile) { return profile.hasRight(right); } } return false; }; /** * List acl entries for each profile */ IotizeACLGroup.prototype.listProfiles = function () { var e_2, _a; if (!this.data.IOTizeACL) { return []; } var entries = []; try { for (var _b = __values(this.data.IOTizeACL), _c = _b.next(); !_c.done; _c = _b.next()) { var data = _c.value; entries.push(new IotizeACL(data, this)); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } return entries; }; return IotizeACLGroup; }()); var IotizeBundleModel = /** @class */ (function () { function IotizeBundleModel(data, iotizeModule) { this.data = data; this.iotizeModule = iotizeModule; } IotizeBundleModel.prototype.toJson = function () { return this.data; }; IotizeBundleModel.prototype.module = function () { return this.iotizeModule; }; IotizeBundleModel.prototype.id = function () { if (!this.data.$.Id) { throw new InvalidConfigurationError('This bundle does not have an id'); } return this.data.$.Id; }; IotizeBundleModel.prototype.name = function () { return this.data.$.GUI_Name || '#' + this.id(); }; IotizeBundleModel.prototype.dataLogPeriod = function () { if (!this.data.$.DataLogPeriod) { return 0; } return Number(this.data.$.DataLogPeriod); }; IotizeBundleModel.prototype.hasDataLog = function () { return this.dataLogPeriod() > 0; }; IotizeBundleModel.prototype.description = function () { return this.data.$.GUI_Description; }; IotizeBundleModel.prototype.variable = function (variableId) { var e_1, _a; try { for (var _b = __values(this.variables()), _c = _b.next(); !_c.done; _c = _b.next()) { var variable = _c.value; if (variable.id() === variableId) { return variable; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } throw new VariableDoesNotExistError(variableId, this.iotizeModule); }; IotizeBundleModel.prototype.hasVariable = function (variableId) { var e_2, _a; try { for (var _b = __values(this.variables()), _c = _b.next(); !_c.done; _c = _b.next()) { var variable = _c.value; if (variable.id() === variableId) { return variable; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } return false; }; IotizeBundleModel.prototype.profiles = function () { var e_3, _a; if (!this.data.IOTizeAccessControl) { return []; } var profiles = []; try { for (var _b = __values(this.data.IOTizeAccessControl), _c = _b.next(); !_c.done; _c = _b.next()) { var profile = _c.value; profiles.push(new IotizeProfileModel(profile)); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } return profiles; }; IotizeBundleModel.prototype.variables = function () { var e_4, _a; if (!this.data.IOTizeVariable) { return []; } var variables = []; try { for (var _b = __values(this.data.IOTizeVariable), _c = _b.next(); !_c.done; _c = _b.next()) { var variable = _c.value; variables.push(new IotizeVariableModel(variable, this.iotizeModule, this)); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_4) throw e_4.error; } } return variables; }; IotizeBundleModel.prototype.getACLGroup = function () { var data = {}; if (!this.data.IOTizeAccessControl) { this.data.IOTizeAccessControl = []; } if (this.data.IOTizeAccessControl.length > 0) { data = this.data.IOTizeAccessControl[0]; } return new IotizeACLGroup(data, this); }; return IotizeBundleModel; }()); /** * */ var BundleDoesNotExistError = /** @class */ (function (_super) { __extends(BundleDoesNotExistError, _super); // static CODE = 'BundleDoesNotExistError'; function BundleDoesNotExistError(bundleId, config) { var _this = _super.call(this, 'Bundle "' + bundleId + '" does not exist.') || this; _this.bundleId = bundleId; _this.config = config; return _this; } return BundleDoesNotExistError; }(error.BaseError)); /** * */ var VariableDoesNotExistError = /** @class */ (function (_super) { __extends(VariableDoesNotExistError, _super); // static CODE = 'VariableDoesNotExistError'; function VariableDoesNotExistError(variableId, config) { var _this = _super.call(this, "Variable \"" + variableId + "\" does not exist.") || this; _this.variableId = variableId; _this.config = config; return _this; } return VariableDoesNotExistError; }(error.BaseError)); /** * Class representing an iotize studio configuration for a module */ var IotizeConfigModel = /** @class */ (function () { function IotizeConfigModel(data) { if (!data.IOTizeConfiguration) { //console.error('Trying to create an IotizeConfigModel with invalid data: ', data); throw new InvalidConfigurationError('Key IOTizeConfiguration must exists'); } if (!data.IOTizeConfiguration.IOTizeApp) { throw new InvalidConfigurationError('Key IOTizeConfiguration.IOTizeApp must exists'); } if (data.IOTizeConfiguration.IOTizeApp.length === 0 || !data.IOTizeConfiguration.IOTizeApp[0]['$']) { throw new InvalidConfigurationError('No IOTizeConfiguration.IOTizeApp defined in this configuration file'); } this.data = data; } IotizeConfigModel.prototype.toJson = function () { return this.data; }; IotizeConfigModel.prototype.getAppConfig = function () { return this.data.IOTizeConfiguration.IOTizeApp[0]; }; /** * Get application name */ IotizeConfigModel.prototype.appName = function () { if (!this.getAppConfig().$.Name) { throw new InvalidConfigurationError('Application Name is not set'); } return this.getAppConfig().$.Name; }; /** * Get config version */ IotizeConfigModel.prototype.version = function () { if (!this.getAppConfig().$.Config) { throw new InvalidConfigurationError('App config version is not set'); } return this.getAppConfig().$.Config; }; /** * Get module serial number */ IotizeConfigModel.prototype.id = function () { if (!this.getAppConfig().$.DevIoTizeSN) { throw new InvalidConfigurationError('Serial number is not set'); } return this.getAppConfig().$.DevIoTizeSN; }; /** * Set module serial number * @param id */ IotizeConfigModel.prototype.setId = function (id) { this.getAppConfig().$.DevIoTizeSN = id; return this; }; /** * Alias for this.id(); */ IotizeConfigModel.prototype.serialNumber = function () { return this.id(); }; /** * Get an array of all bundles */ IotizeConfigModel.prototype.bundles = function () { var e_1, _a; if (!this.data.IOTizeConfiguration.IOTizeBundle) { return []; } var bundles = []; try { for (var _b = __values(this.data.IOTizeConfiguration.IOTizeBundle), _c = _b.next(); !_c.done; _c = _b.next()) { var bundle = _c.value; // TODO bug if only one bundle bundles.push(new IotizeBundleModel(bundle, this)); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } return bundles; }; /** * Array of bundles available for a specific profile */ IotizeConfigModel.prototype.bundleAvailableForProfileName = function (profileName) { return this.bundles().reduce(function (list, currentValue) { if (currentValue.getACLGroup().hasRight(profileName, 'read')) { list.push(currentValue); } return list; }, []); }; /** * Get an array of all variables */ IotizeConfigModel.prototype.variables = function () { var e_2, _a; if (!this.data.IOTizeConfiguration.IOTizeBundle) { return []; } var res = []; try { for (var _b = __values(this.data.IOTizeConfiguration.IOTizeBundle), _c = _b.next(); !_c.done; _c = _b.next()) { var bundle = _c.value; bundle = new IotizeBundleModel(bundle, this); res.push.apply(res, __spreadArray([], __read(bundle.variables()))); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } return res; }; /** * Get bundle by id, if it does not exist, throw BundleDoesNotExistError * @throws BundleDoesNotExistError * @param id */ IotizeConfigModel.prototype.bundle = function (id) { var e_3, _a; try { for (var _b = __values(this.bundles()), _c = _b.next(); !_c.done; _c = _b.next()) { var bundle = _c.value; if (bundle.id() === id) { return bundle; } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } throw new BundleDoesNotExistError(id, this); }; IotizeConfigModel.prototype.hasBundle = function (id) { var e_4, _a; try { for (var _b = __values(this.bundles()), _c = _b.next(); !_c.done; _c = _b.next()) { var bundle = _c.value; if (bundle.id() === id) { return bundle; } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_4) throw e_4.error; } } return false; }; /** * Get variable by id, if it does not exist, throw VariableDoesNotExistError * @throws VariableDoesNotExistError * @param id */ IotizeConfigModel.prototype.variable = function (variableId) { var e_5, _a; try { for (var _b = __values(this.variables()), _c = _b.next(); !_c.done; _c = _b.next()) { var variable = _c.value; if (variable.id() == variableId) { return variable; } } } catch (e_5_1) { e_5 = { error: e_5_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_5) throw e_5.error; } } throw new VariableDoesNotExistError(variableId, this); }; /** * Get an array of all profiles */ IotizeConfigModel.prototype.profiles = function () { var e_6, _a; if (!this.data.IOTizeConfiguration.IOTizeProfile) { return []; } var profiles = []; try { for (var _b = __values(this.data.IOTizeConfiguration.IOTizeProfile), _c = _b.next(); !_c.done; _c = _b.next()) { var profile = _c.value; // TODO bug if there is only one profile... profiles.push(new IotizeProfileModel(profile)); } } catch (e_6_1) { e_6 = { error: e_6_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_6) throw e_6.error; } } return profiles; }; IotizeConfigModel.prototype.description = function () { return this.getAppConfig().$.GUI_Description; }; IotizeConfigModel.prototype.name = function () { return this.getAppConfig().$.GUI_Name; }; return IotizeConfigModel; }()); /** * Utility class to create iotize config model from XML or JSON file */ var IotizeConfigFactory = /** @class */ (function () { function IotizeConfigFactory() { } /** * Create IotizeConfigModel from a XML string * @param xml */ IotizeConfigFactory.fromXML = function (xml) { throw new Error("Add import to @iotize/tap/config/iotize-studio/parser/xml to create configuration model from XML file"); }; /** * Create IotizeConfigModel from a JSON string * @param json */ IotizeConfigFactory.fromJson = function (json) { return new IotizeConfigModel(JSON.parse(json)); }; /** * Build and IotizeConfigModel * Input can either be * - A json string * - A XML string * - A JSON Object * * TODO create test * @param data */ IotizeConfigFactory.create = function (data) { if (typeof data === 'string') { data.trim(); if (data.length === 0) { throw new Error('Cannot create configuration from empty data'); } if (data[0] === '{') { return IotizeConfigFactory.fromJson(data); } else { return IotizeConfigFactory.fromXML(data); } } else { return new IotizeConfigModel(data); } }; return IotizeConfigFactory; }()); var IoTizeStudioError = /** @class */ (function (_super) { __extends(IoTizeStudioError, _super); function IoTizeStudioError() { return _super !== null && _super.apply(this, arguments) || this; } IoTizeStudioError.invalidKeyValue = function (key, msg) { return new IoTizeStudioError("Invalid IoTize studio value for configuration key \"" + key + "\". " + msg, IoTizeStudioError.Code.InvalidConfigKeyValue); }; return IoTizeStudioError; }(error.CodeError)); (function (IoTizeStudioError) { var Code; (function (Code) { Code["InvalidConfigKeyValue"] = "IoTizeStudioErrorInvalidConfigKeyValue"; })(Code = IoTizeStudioError.Code || (IoTizeStudioError.Code = {})); })(IoTizeStudioError || (IoTizeStudioError = {})); var NetworkModeMapping = { P2P: 'PEER_TO_PEER', NETWORK: 'NETWORK', INFRA: 'NETWORK', INFRA_CLOUD: 'NETWORK', // TODO check }; var LowPowerOptimizationLevelMapping = { 0: 'NO', 1: 'SHUTDOWN', 2: 'STANDBY', // TODO CHECK }; var WIFI_KEY_VISIBILITY_MAPPING = { 0: 'VISIBLE', 1: 'HIDDEN', }; var S3P_CONFIG_MODE_MAPPING = { 0: 'EMULATION', 1: 'INDEXED', }; var WIFI_SSID_VISIBILITY_MAPPING = { 0: 'VISIBLE', 1: 'HIDDEN', 2: 'HIDDEN_FROM_SCAN', }; var WIFI_PROTOCOL_MAPPING = { 1: ['B'], 3: ['B', 'G'], 7: ['B', 'G', 'N'], }; var TargetProtocolMapping = { JTAG: 'JTAG', MODBUS: 'MODBUS', NONE: 'NONE', S3P: 'S3P', SERIAL: 'SERIAL', SERIAL_STANDARD: 'SERIAL', SERIAL_VIA_TAPNPASS: 'SERIAL', CUSTOM: 'CUSTOM', SWD: 'SWD', GPIO: 'DIRECT_IO', DIRECT_IO: 'DIRECT_IO', DIRECTIO: 'DIRECT_IO', CAN: 'CAN', NO_TARGET_PROTOCOL: 'NONE', MODBUSTCP: 'MODBUS_TCP', }; var NFCParingModeMapping = { NO: 'NO', PAIRING: 'MANDATORY', 'PAIRING+LOGIN': 'MANDATORY_FOR_LOGIN', }; var NFC_CONNECTION_PRIORITY_MAPPING = { 0: 'NFC_NON_PRIORITY', 1: 'NFC_PRIORITY', }; // const MAPPING: Record<WriteConfigKey, any> = { // // App // 'application.name': 'IOTizeConfiguration.IOTizeApp.#.Name', // 'application.universalLink': 'IOTizeConfiguration.IOTizeApp.#.WebLink', // 'application.uri': 'IOTizeConfiguration.IOTizeApp.#.GUI_Path', // 'application.data': 'TODO', // // CLOUD // 'cloud.dataLog.uploadPeriod': 'IOTizeConfiguration.IOTizeApp.CloudServices.#.CloudMqttPeriod', // 'cloud.dataLog.mqttPublishTopic': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.Cloud_Publish_Topic', // 'cloud.dataLog.mqttSubscribeTopic': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.Cloud_Subscribe_Topic', // 'cloud.platform': '', // // Target // 'target.protocol.type': 'IOTizeConfiguration.IOTizeApp.#.TargetProtocol', // // Tap // 'tap.certificate.privateKey': 'IOTizeConfiguration.IOTizeApp.GUI_MQTT_Info.GUI_DevMQTT_PrivateKey', // 'tap.certificate.publicKey': 'IOTizeConfiguration.IOTizeApp.GUI_MQTT_Info.GUI_DevMQTT_Certificate', // // JVM // 'jvm.code': 'IOTizeConfiguration.IOTizeApp.#.JVM_Custom_Code', // 'jvm.profileId': 'IOTizeConfiguration.IOTizeApp.#.', // // LORA // 'wireless.protocols.lora.config': 'IOTizeConfiguration.IOTizeApp.LoRa_Info.#.LoRaMode', // 'wireless.protocols.lora.ids': 'IOTizeConfiguration.IOTizeApp.LoRa_Info.#.TODO', // // MQTT // 'wireless.protocols.mqtt.clientId': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.MQTTRelay_Client_ID', // 'wireless.protocols.mqtt.password': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.MQTTRelay_Passwd', // 'wireless.protocols.mqtt.username': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.MQTTRelay_Broker_Login', // 'wireless.protocols.mqtt.brokerPort': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.MQTTRelay_Service', // 'wireless.protocols.mqtt.brokerHostname': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.MQTTRelay_URL', // 'wireless.protocols.mqtt.brokerCertificate': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.Broker_Certificate', // 'wireless.protocols.mqtt.topicPrefix': 'IOTizeConfiguration.IOTizeApp.WIFI_INFO.#.TODO', // // NFC // 'wireless.protocols.nfc.autoLogProfileId': 'IOTizeConfiguration.IOTizeApp.#.NFCLogin', // 'wireless.protocols.nfc.connectionPriority': 'IOTizeConfiguration.IOTizeApp.#.NFCInterruptionLock', // 'wireless.protocols.nfc.pairingMode': 'IOTizeConfiguration.IOTizeApp.#.NFCPairingMandatory', // // WIFI // 'wireless.protocols.wifi.ssid': 'IOTizeConfiguration.IOTizeApp.WIFI_Info.ModuleSSID ||', // 'wireless.protocols.wifi.ssidVisibility': 'IOTizeConfiguration.IOTizeApp.WIFI_Info.Wifi_SSID_Hidden', // 'wireless.protocols.wifi.keyVisibility': 'IOTizeConfiguration.IOTizeApp.WIFI_Info.#.Wifi_WEPKey_Hidden', // 'wireless.protocols.wifi.gatewayIp': 'IOTizeConfiguration.IOTizeApp.WIFI_Info.#.', // 'wireless.protocols.wifi.mask': 'IOTizeConfiguration.IOTi