UNPKG

@mindconnect/mindconnect-nodejs

Version:

NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)

313 lines 13.6 kB
"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 __asyncValues = (this && this.__asyncValues) || function (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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tearDownDeviceTestStructure = exports.setupDeviceTestStructure = void 0; const src_1 = require("../src"); const test_utils_1 = require("./test-utils"); const timeOffset = new Date().getTime(); function setupDeviceTestStructure(sdk) { return __awaiter(this, void 0, void 0, function* () { var _a; const assetMgmt = sdk.GetAssetManagementClient(); const deviceManagementClient = sdk.GetDeviceManagementClient(); const agentMgmt = sdk.GetAgentManagementClient(); const tenant = sdk.GetTenant(); const rootAssetId = `${(yield assetMgmt.GetRootAsset()).assetId}`; const folders = unroll(yield assetMgmt.GetAssets({ filter: JSON.stringify({ name: { eq: "UnitTestFolder", }, }), })); yield (0, test_utils_1.sleep)(2000); if (folders.length === 0) { const folder = yield assetMgmt.PostAsset({ name: "UnitTestFolder", typeId: "core.basicarea", parentId: rootAssetId, }); yield (0, test_utils_1.sleep)(1000); folders.push(folder); } const folder = folders[0]; const folderid = `${folder.assetId}`; // Check if we have the asset types setup const assetTypes = unroll(yield assetMgmt.GetAssetTypes({ filter: JSON.stringify({ name: { startsWith: `${tenant}.UnitTestDeviceAT` } }), })); yield (0, test_utils_1.sleep)(2000); if (assetTypes.length === 0) { const _assetType = yield assetMgmt.PutAssetType(`${tenant}.UnitTestDeviceAT`, { name: `${tenant}.UnitTestDeviceAT.${timeOffset}`, description: `${tenant}.UnitTestDeviceAT.${timeOffset}`, parentTypeId: "core.basicagent", instantiable: true, scope: src_1.AssetManagementModels.AssetTypeBase.ScopeEnum.Private, aspects: [ { name: "firmwarestatus", aspectTypeId: "core.firmwarestatus" }, ], variables: [ { name: "temperature", dataType: src_1.AssetManagementModels.VariableDefinition.DataTypeEnum.STRING, unit: "C/F", searchable: true, length: 5, defaultValue: "25/77", }, ], fileAssignments: [], }, { ifNoneMatch: "*" }); yield (0, test_utils_1.sleep)(1000); assetTypes.push(_assetType); } const deviceAssetType = assetTypes.pop(); // console.log("deviceAssetType", deviceAssetType); // Check if we have the device types setup const deviceTypes = unroll(yield deviceManagementClient.GetDeviceTypes({ assetTypeId: deviceAssetType.id, owner: `${tenant}`, sort: "DESC", page: 0, size: 100, })); yield (0, test_utils_1.sleep)(2000); if (deviceTypes.length === 0) { const _deviceType = yield deviceManagementClient.PostDeviceType({ name: `${tenant}.UnitTestDeviceType`, code: `${tenant}.V001.${timeOffset}`, assetTypeId: `${deviceAssetType.id}`, description: " example device type", properties: { key1: "value1", key2: "value2" } }); yield (0, test_utils_1.sleep)(1000); deviceTypes.push(_deviceType); } const deviceType = deviceTypes.pop(); // console.log("deviceType", deviceType); // Check if we have the asset setup const assets = unroll(yield assetMgmt.GetAssets({ filter: JSON.stringify({ and: { name: { startsWith: `${tenant}.UnitTestDeviceAsset`, }, parentId: { eq: folderid, }, }, }), })); yield (0, test_utils_1.sleep)(2000); if (assets.length === 0) { const _asset = yield assetMgmt.PostAsset({ name: `${tenant}.UnitTestDeviceAsset.${timeOffset}`, typeId: `${deviceAssetType.id}`, parentId: folderid, }); yield (0, test_utils_1.sleep)(1000); assets.push(_asset); } const deviceAsset = assets.pop(); // console.log("deviceAsset", deviceAsset); // Register an agent for this asset const agents = (_a = unroll((yield agentMgmt.GetAgents({ sort: "DESC", page: 0, size: 100 })))) === null || _a === void 0 ? void 0 : _a.filter((x) => { var _a; return (_a = x === null || x === void 0 ? void 0 : x.name) === null || _a === void 0 ? void 0 : _a.startsWith(`${tenant}.UnitTestDeviceAgent`); }); yield (0, test_utils_1.sleep)(2000); if ((agents === null || agents === void 0 ? void 0 : agents.length) === 0) { const _agent = yield agentMgmt.PostAgent({ name: `${tenant}.UnitTestDeviceAgent.${timeOffset}`, securityProfile: src_1.AgentManagementModels.AgentUpdate.SecurityProfileEnum.SHAREDSECRET, entityId: `${deviceAsset.assetId}`, }); yield (0, test_utils_1.sleep)(1000); agents.push(_agent); } const agent = agents.pop(); const deviceAgentId = `${agent.id}`; // console.log("agent", agent); // Check if we have the asset setup const devices = unroll(yield deviceManagementClient.GetDevices({ assetId: `${deviceAsset.assetId}`, page: 0, size: 0 })); yield (0, test_utils_1.sleep)(2000); if (devices.length === 0) { const _device = yield deviceManagementClient.PostDevice({ deviceTypeId: `${deviceType.id}`, assetId: `${deviceAsset.assetId}`, agents: [`${deviceAgentId}`], properties: { name: `${tenant}.UnitTestDevice.${timeOffset}`, parentId: folderid, } }); yield (0, test_utils_1.sleep)(1000); devices.push(_device); } yield (0, test_utils_1.sleep)(2000); const device = devices.pop(); // console.log("device", device); return { device: device, deviceAsset: deviceAsset, deviceType: deviceType, deviceAssetType: deviceAssetType, folderid }; }); } exports.setupDeviceTestStructure = setupDeviceTestStructure; function unroll(obj) { if (obj.content) return obj.content; const embedded = obj._embedded; if (!embedded) return []; const keys = Object.keys(embedded); if (keys.length === 0) return []; if (keys.length === 1) return embedded[keys[0]]; throw new Error("cant unroll object"); } function tearDownDeviceTestStructure(sdk) { return __awaiter(this, void 0, void 0, function* () { var _a, e_1, _b, _c, _d, e_2, _e, _f, _g, e_3, _h, _j, _k, e_4, _l, _m; var _o, _p, _q, _r, _s; yield (0, test_utils_1.sleep)(1000); const assetMgmt = sdk.GetAssetManagementClient(); const deviceManagementClient = sdk.GetDeviceManagementClient(); const agentMgmt = sdk.GetAgentManagementClient(); const tenant = sdk.GetTenant(); // delete devices const devices = (yield deviceManagementClient.GetDevices({ page: 0, size: 100 })); yield (0, test_utils_1.sleep)(2000); try { for (var _t = true, _u = __asyncValues(devices.content || []), _v; _v = yield _u.next(), _a = _v.done, !_a; _t = true) { _c = _v.value; _t = false; const x = _c; if ((_p = (_o = x === null || x === void 0 ? void 0 : x.properties) === null || _o === void 0 ? void 0 : _o.name) === null || _p === void 0 ? void 0 : _p.startsWith(`${tenant}.UnitTestDevice`)) { yield deviceManagementClient.DeleteDevice(x.id); } yield (0, test_utils_1.sleep)(1000); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_t && !_a && (_b = _u.return)) yield _b.call(_u); } finally { if (e_1) throw e_1.error; } } // delete agents const agents = (yield agentMgmt.GetAgents({ sort: "DESC", page: 0, size: 100 })); yield (0, test_utils_1.sleep)(2000); try { for (var _w = true, _x = __asyncValues(agents.content || []), _y; _y = yield _x.next(), _d = _y.done, !_d; _w = true) { _f = _y.value; _w = false; const x = _f; if ((_q = x === null || x === void 0 ? void 0 : x.name) === null || _q === void 0 ? void 0 : _q.startsWith(`${tenant}.UnitTestDeviceAgent`)) { yield agentMgmt.DeleteAgent(x.id, { ifMatch: `${x.eTag}` }); } yield (0, test_utils_1.sleep)(1000); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_w && !_d && (_e = _x.return)) yield _e.call(_x); } finally { if (e_2) throw e_2.error; } } // delete assets const assets = (yield assetMgmt.GetAssets({ filter: JSON.stringify({ name: { startsWith: `${tenant}.UnitTestDeviceAsset`, } }), sort: "DESC", size: 100 })); yield (0, test_utils_1.sleep)(2000); try { for (var _z = true, _0 = __asyncValues(((_r = assets === null || assets === void 0 ? void 0 : assets._embedded) === null || _r === void 0 ? void 0 : _r.assets) || []), _1; _1 = yield _0.next(), _g = _1.done, !_g; _z = true) { _j = _1.value; _z = false; const x = _j; yield assetMgmt.DeleteAsset(x.assetId, { ifMatch: `${x.etag}` }); yield (0, test_utils_1.sleep)(1000); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_z && !_g && (_h = _0.return)) yield _h.call(_0); } finally { if (e_3) throw e_3.error; } } // delete asset types const assetTypes = (yield assetMgmt.GetAssetTypes({ filter: JSON.stringify({ name: { startsWith: `${tenant}.UnitTestDeviceAT`, }, }), sort: "DESC", size: 100 })); yield (0, test_utils_1.sleep)(2000); try { for (var _2 = true, _3 = __asyncValues(((_s = assetTypes === null || assetTypes === void 0 ? void 0 : assetTypes._embedded) === null || _s === void 0 ? void 0 : _s.assetTypes) || []), _4; _4 = yield _3.next(), _k = _4.done, !_k; _2 = true) { _m = _4.value; _2 = false; const x = _m; yield assetMgmt.DeleteAssetType(x.id, { ifMatch: `${x.etag}` }); yield (0, test_utils_1.sleep)(1000); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (!_2 && !_k && (_l = _3.return)) yield _l.call(_3); } finally { if (e_4) throw e_4.error; } } }); } exports.tearDownDeviceTestStructure = tearDownDeviceTestStructure; //# sourceMappingURL=test-device-setup-utils.js.map