-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest-device-setup-utils.ts
261 lines (250 loc) · 8.75 KB
/
test-device-setup-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { AgentManagementModels, AssetManagementModels, DeviceManagementModels, MindSphereSdk } from "../src";
import { sleep } from "./test-utils";
const timeOffset = new Date().getTime();
export async function setupDeviceTestStructure(sdk: MindSphereSdk) {
const assetMgmt = sdk.GetAssetManagementClient();
const deviceManagementClient = sdk.GetDeviceManagementClient();
const agentMgmt = sdk.GetAgentManagementClient();
const tenant = sdk.GetTenant();
const rootAssetId = `${(await assetMgmt.GetRootAsset()).assetId}`;
const folders = unroll<AssetManagementModels.AssetResource>(
await assetMgmt.GetAssets({
filter: JSON.stringify({
name: {
eq: "UnitTestFolder",
},
}),
})
);
await sleep(2000);
if (folders.length === 0) {
const folder = await assetMgmt.PostAsset({
name: "UnitTestFolder",
typeId: "core.basicarea",
parentId: rootAssetId,
});
await sleep(1000);
folders.push(folder);
}
const folder = folders[0];
const folderid = `${folder.assetId}`;
// Check if we have the asset types setup
const assetTypes = unroll<AssetManagementModels.AssetTypeResource>(
await assetMgmt.GetAssetTypes({
filter: JSON.stringify({
name: {
startsWith: `${tenant}.UnitTestDeviceAT`
}
}),
})
);
await sleep(2000);
if (assetTypes.length === 0) {
const _assetType = await assetMgmt.PutAssetType(
`${tenant}.UnitTestDeviceAT`,
{
name: `${tenant}.UnitTestDeviceAT.${timeOffset}`,
description: `${tenant}.UnitTestDeviceAT.${timeOffset}`,
parentTypeId: "core.basicagent",
instantiable: true,
scope: AssetManagementModels.AssetTypeBase.ScopeEnum.Private,
aspects: [
{
name: "firmwarestatus",
aspectTypeId: "core.firmwarestatus"
},
],
variables: [
{
name: "temperature",
dataType: AssetManagementModels.VariableDefinition.DataTypeEnum.STRING,
unit: "C/F",
searchable: true,
length: 5,
defaultValue: "25/77",
},
],
fileAssignments: [],
},
{ifNoneMatch: "*"}
);
await sleep(1000);
assetTypes.push(_assetType);
}
const deviceAssetType = assetTypes.pop();
// console.log("deviceAssetType", deviceAssetType);
// Check if we have the device types setup
const deviceTypes = unroll<DeviceManagementModels.DeviceType>(
await deviceManagementClient.GetDeviceTypes({
assetTypeId: (deviceAssetType as any).id,
owner: `${tenant}`,
sort: "DESC",
page: 0,
size: 100,
})
);
await sleep(2000);
if (deviceTypes.length === 0) {
const _deviceType = await deviceManagementClient.PostDeviceType(
{
name: `${tenant}.UnitTestDeviceType`,
code: `${tenant}.V001.${timeOffset}`,
assetTypeId: `${(deviceAssetType as any).id}`,
description: " example device type",
properties: {
key1: "value1",
key2: "value2"
}
}
);
await sleep(1000);
deviceTypes.push(_deviceType);
}
const deviceType = deviceTypes.pop();
// console.log("deviceType", deviceType);
// Check if we have the asset setup
const assets = unroll(
await assetMgmt.GetAssets({
filter: JSON.stringify({
and: {
name: {
startsWith: `${tenant}.UnitTestDeviceAsset`,
},
parentId: {
eq: folderid,
},
},
}),
})
);
await sleep(2000);
if (assets.length === 0) {
const _asset = await assetMgmt.PostAsset({
name: `${tenant}.UnitTestDeviceAsset.${timeOffset}`,
typeId: `${(deviceAssetType as any).id}`,
parentId: folderid,
});
await sleep(1000);
assets.push(_asset);
}
const deviceAsset = assets.pop();
// console.log("deviceAsset", deviceAsset);
// Register an agent for this asset
const agents = unroll(
(await agentMgmt.GetAgents({
sort: "DESC",
page: 0,
size: 100
}))
)?.filter((x: any) => (x as any)?.name?.startsWith( `${tenant}.UnitTestDeviceAgent`));
await sleep(2000);
if (agents?.length === 0) {
const _agent = await agentMgmt.PostAgent({
name: `${tenant}.UnitTestDeviceAgent.${timeOffset}`,
securityProfile: AgentManagementModels.AgentUpdate.SecurityProfileEnum.SHAREDSECRET,
entityId: `${deviceAsset.assetId}`,
});
await 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(
await deviceManagementClient.GetDevices({
assetId: `${(deviceAsset as any).assetId}`,
page: 0,
size: 0
})
);
await sleep(2000);
if (devices.length === 0) {
const _device = await deviceManagementClient.PostDevice({
deviceTypeId: `${(deviceType as any).id}`,
assetId: `${(deviceAsset as any).assetId}`,
agents: [`${deviceAgentId}`],
properties: {
name: `${tenant}.UnitTestDevice.${timeOffset}`,
parentId: folderid,
}
});
await sleep(1000);
devices.push(_device);
}
await sleep(2000);
const device = devices.pop();
// console.log("device", device);
return { device: device, deviceAsset: deviceAsset, deviceType: deviceType, deviceAssetType: deviceAssetType, folderid };
}
function unroll<T>(obj: { _embedded?: any, content?: any }) {
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]] as T[];
throw new Error("cant unroll object");
}
export async function tearDownDeviceTestStructure(sdk: MindSphereSdk) {
await sleep(1000);
const assetMgmt = sdk.GetAssetManagementClient();
const deviceManagementClient = sdk.GetDeviceManagementClient();
const agentMgmt = sdk.GetAgentManagementClient();
const tenant = sdk.GetTenant();
// delete devices
const devices: DeviceManagementModels.PaginatedDevice = (await deviceManagementClient.GetDevices({
page: 0,
size: 100
}));
await sleep(2000);
for await (const x of devices.content || []) {
if ((x as any)?.properties?.name?.startsWith( `${tenant}.UnitTestDevice`)) {
await deviceManagementClient.DeleteDevice(x.id!);
}
await sleep(1000);
}
// delete agents
const agents: AgentManagementModels.PagedAgent = (await agentMgmt.GetAgents({
sort: "DESC",
page: 0,
size: 100
}));
await sleep(2000);
for await (const x of agents.content || []) {
if ((x as any)?.name?.startsWith( `${tenant}.UnitTestDeviceAgent`)) {
await agentMgmt.DeleteAgent(x.id!, { ifMatch: `${x.eTag}` });
}
await sleep(1000);
}
// delete assets
const assets: AssetManagementModels.AssetListResource = (await assetMgmt.GetAssets({
filter: JSON.stringify({
name: {
startsWith: `${tenant}.UnitTestDeviceAsset`,
}
}),
sort: "DESC",
size: 100
}));
await sleep(2000);
for await (const x of assets?._embedded?.assets || []) {
await assetMgmt.DeleteAsset(x.assetId!, {ifMatch: `${x.etag}`});
await sleep(1000);
}
// delete asset types
const assetTypes: AssetManagementModels.AssetTypeListResource = (await assetMgmt.GetAssetTypes({
filter: JSON.stringify({
name: {
startsWith: `${tenant}.UnitTestDeviceAT`,
},
}),
sort: "DESC",
size: 100
}));
await sleep(2000);
for await (const x of assetTypes?._embedded?.assetTypes || []) {
await assetMgmt.DeleteAssetType(x.id!, { ifMatch: `${x.etag}` });
await sleep(1000);
}
}