UNPKG

5.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3// Copyright (C), Siemens AG 2017
4exports.jstemplate = `const { MindConnectAgent, retry } = require ("@mindconnect/mindconnect-nodejs");
5
6(async function () {
7
8 const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
9 const configuration = require("./agentconfig.json");
10 const agent = new MindConnectAgent(configuration);
11 const log = (text) => { console.log(\`[\${new Date().toISOString()}] \${text.toString()}\`); };
12 const RETRYTIMES = 5; // retry the operation before giving up and throwing exception
13
14 for (let index = 0; index < 5; index++) {
15 try {
16
17 log(\`Iteration : \${index}\`);
18 // onboarding the agent
19 if (!agent.IsOnBoarded()) {
20 // wrapping the call in the retry function makes the agent a bit more resillient
21 // if you don't want to retry the operations you can always just call await agent.OnBoard(); instead.
22 await retry(RETRYTIMES, () => agent.OnBoard());
23 log("Agent onboarded");
24 }
25
26 if (!agent.HasDataSourceConfiguration()) {
27 await retry(RETRYTIMES, () => agent.GetDataSourceConfiguration());
28 log("Configuration aquired");
29 }
30
31 const values = [
32 { "dataPointId": "DP-Temperature", "qualityCode": "0", "value": (Math.sin(index) * (20 + index % 2) + 25).toString() },
33 { "dataPointId": "DP-Pressure", "qualityCode": "0", "value": (Math.cos(index) * (20 + index % 25) + 25).toString() },
34 { "dataPointId": "DP-Humidity", "qualityCode": "0", "value": ((index + 30) % 100).toString() },
35 { "dataPointId": "DP-Acceleration", "qualityCode": "0", "value": (1000.0 + index).toString() },
36 { "dataPointId": "DP-Frequency", "qualityCode": "0", "value": (60.0 + (index * 0.1)).toString() },
37 { "dataPointId": "DP-Displacement", "qualityCode": "0", "value": (index % 10).toString() },
38 { "dataPointId": "DP-Velocity", "qualityCode": "0", "value": (50.0 + index).toString() }
39 ];
40
41 // same like above, you can also just call await agent.PostData(values) if you don't want to retry the operation
42 // this is how to send the data with specific timestamp
43 // await agent.PostData(values, new Date(Date.now() - 86400 * 1000));
44
45 await retry(RETRYTIMES, () => agent.PostData(values));
46 log("Data posted");
47 await sleep(1000);
48
49 const event = {
50 "entityId": agent.ClientId(), // use assetid if you want to send event somewhere else :)
51 "sourceType": "Event",
52 "sourceId": "application",
53 "source": "Meowz",
54 "severity": 20, // 0-99 : 20:error, 30:warning, 40: information
55 "timestamp": new Date().toISOString(),
56 "description": "Test"
57 };
58
59 // send event with current timestamp; you can also just call agent.PostEvent(event) if you don't want to retry the operation
60 await retry(RETRYTIMES, () => agent.PostEvent(event));
61 log("event posted");
62 await sleep(1000);
63
64
65 // upload file
66 // the upload-file can be a multipart operation and therefore can be configured to
67 // retry the upload of the chunks instead the upload of the whole file.
68 // if you don't specify the type , the mimetype is automatically determined by the library
69 await agent.UploadFile(agent.ClientId(), "custom/mindsphere/path/package.json", "package.json", {
70 retry: RETRYTIMES,
71 description: "File uploaded with MindConnect-NodeJS Library",
72 chunk: true // the chunk parameter activates multipart upload
73 });
74
75 // upload file; you can also just call await agent.Upload(...) if you don't want to retry the operation
76 await retry(RETRYTIMES, () => agent.Upload("package.json", "application/json", "Demo File"));
77 log("file uploaded");
78 await sleep(1000);
79
80 const yesterday = new Date();
81 yesterday.setDate(yesterday.getDate() - 1);
82 const bulk = [{
83 "timestamp": yesterday.toISOString(),
84 "values":
85 [{ "dataPointId": "DP-Temperature", "qualityCode": "0", "value": "10" },
86 { "dataPointId": "DP-Pressure", "qualityCode": "0", "value": "10" }]
87 },
88 {
89 "timestamp": new Date().toISOString(),
90 "values": [{ "dataPointId": "DP-Temperature", "qualityCode": "0", "value": "10" },
91 { "dataPointId": "DP-Pressure", "qualityCode": "0", "value": "10" }]
92 }];
93
94 await retry(RETRYTIMES, () => agent.BulkPostData(bulk));
95 log("bulk data uploaded");
96 await sleep(1000);
97
98 } catch (err) {
99 // add proper error handling (e.g. store data somewhere, retry later etc. )
100 console.error(err);
101 }
102 }
103})();`;
104//# sourceMappingURL=js-template.js.map
\No newline at end of file