-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
133 lines (119 loc) · 5.37 KB
/
index.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
import {
DataPointValue,
MindConnectAgent,
MindsphereStandardEvent,
retry,
TimeStampedDataPoint
} from "@mindconnect/mindconnect-nodejs";
import * as fs from "fs";
(async function () {
const sleep = (ms: any) => new Promise(resolve => setTimeout(resolve, ms));
const configuration = require("./agentconfig.json");
const agent = new MindConnectAgent(configuration);
agent.SetupAgentCertificate(fs.readFileSync("private.key"));
const log = (text: any) => {
console.log(`[${new Date().toISOString()}] ${text.toString()}`);
};
const RETRYTIMES = 5; // retry the operation before giving up and throwing exception
for (let index = 0; index < 5; index++) {
try {
log(`Iteration : ${index}`);
// onboarding the agent
if (!agent.IsOnBoarded()) {
// wrapping the call in the retry function makes the agent a bit more resilliant
// if you don't want to retry the operations you can always just call await agent.OnBoard(); instaead.
await retry(RETRYTIMES, () => agent.OnBoard());
log("Agent onboarded");
}
if (!agent.HasDataSourceConfiguration()) {
await retry(RETRYTIMES, () => agent.GetDataSourceConfiguration());
log("Configuration aquired");
}
const values: DataPointValue[] = [
// 1570783881302 -> Temperature
// 1570783864134 -> Humidity
{
"dataPointId": "1570783881302",
"qualityCode": "0",
"value": (Math.sin(index) * (20 + index % 2) + 25).toString()
},
{
"dataPointId": "1570783864134",
"qualityCode": "0",
"value": ((index + 30) % 100).toString()
}
];
// same like above, you can also just call await agent.PostData(values) if you don't want to retry the operation
// this is how to send the data with specific timestamp
// await agent.PostData(values, new Date(Date.now() - 86400 * 1000));
await retry(RETRYTIMES, () => agent.PostData(values));
log("Data posted");
await sleep(1000);
const event: MindsphereStandardEvent = {
"entityId": agent.ClientId(), // use assetid if you want to send event somewhere else :)
"sourceType": "Event",
"sourceId": "application",
"source": "Meowz",
"severity": 20, // 0-99 : 20:error, 30:warning, 40: information
"timestamp": new Date().toISOString(),
"description": "Test"
};
// send event with current timestamp; you can also just call agent.PostEvent(event) if you don't want to retry the operation
await retry(RETRYTIMES, () => agent.PostEvent(event));
log("event posted");
await sleep(1000);
// upload file
// the upload-file can be a multipart operation and therefore can be configured to
// retry the upload of the chunks instead the upload of the whole file.
// if you don't specify the type , the mimetype is automatically determined by the library
await agent.UploadFile(agent.ClientId(), "custom/mindsphere/path/package.json", "package.json", {
retry: RETRYTIMES,
description: "File uploaded with MindConnect-NodeJS Library",
chunk: true // the chunk parameter activates multipart upload
});
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
// 1570783881302 -> Temperature
// 1570783864134 -> Humidity
const bulk: TimeStampedDataPoint[] =
[{
"timestamp": yesterday.toISOString(),
"values":
[
{
"dataPointId": "1570783881302",
"qualityCode": "0",
"value": "10"
},
{
"dataPointId": "1570783864134",
"qualityCode": "0",
"value": "30"
}
]
},
{
"timestamp": new Date().toISOString(),
"values": [
{
"dataPointId": "1570783881302",
"qualityCode": "0",
"value": "10"
},
{
"dataPointId": "1570783864134",
"qualityCode": "0",
"value": "30"
}
]
}
];
await retry(RETRYTIMES, () => agent.BulkPostData(bulk));
log("bulk data uploaded");
await sleep(1000);
} catch (err) {
// add proper error handling (e.g. store data somewhere, retry later etc. )
console.error(err);
}
}
})();