UNPKG

@mindconnect/mindconnect-nodejs

Version:

NodeJS Library for MindSphere Connectivity - TypeScript SDK for MindSphere - MindSphere Command Line Interface - MindSphere Development Proxy

305 lines 13.3 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) { var _a; return __awaiter(this, void 0, void 0, function* () { 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, _b; return (_b = (_a = x) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.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) { var e_1, _a, e_2, _b, e_3, _c, e_4, _d; var _e, _f, _g, _h, _j, _k, _l; return __awaiter(this, void 0, void 0, function* () { 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 _m = __asyncValues(devices.content || []), _o; _o = yield _m.next(), !_o.done;) { const x = _o.value; if ((_g = (_f = (_e = x) === null || _e === void 0 ? void 0 : _e.properties) === null || _f === void 0 ? void 0 : _f.name) === null || _g === void 0 ? void 0 : _g.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 (_o && !_o.done && (_a = _m.return)) yield _a.call(_m); } 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 _p = __asyncValues(agents.content || []), _q; _q = yield _p.next(), !_q.done;) { const x = _q.value; if ((_j = (_h = x) === null || _h === void 0 ? void 0 : _h.name) === null || _j === void 0 ? void 0 : _j.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 (_q && !_q.done && (_b = _p.return)) yield _b.call(_p); } 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 _r = __asyncValues(((_k = assets === null || assets === void 0 ? void 0 : assets._embedded) === null || _k === void 0 ? void 0 : _k.assets) || []), _s; _s = yield _r.next(), !_s.done;) { const x = _s.value; 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 (_s && !_s.done && (_c = _r.return)) yield _c.call(_r); } 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 _t = __asyncValues(((_l = assetTypes === null || assetTypes === void 0 ? void 0 : assetTypes._embedded) === null || _l === void 0 ? void 0 : _l.assetTypes) || []), _u; _u = yield _t.next(), !_u.done;) { const x = _u.value; 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 (_u && !_u.done && (_d = _t.return)) yield _d.call(_t); } finally { if (e_4) throw e_4.error; } } }); } exports.tearDownDeviceTestStructure = tearDownDeviceTestStructure; //# sourceMappingURL=test-device-setup-utils.js.map