forked from node-opcua/node-opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypescript_test.ts
More file actions
111 lines (80 loc) · 2.98 KB
/
typescript_test.ts
File metadata and controls
111 lines (80 loc) · 2.98 KB
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
import {
AttributeIds,
ClientSession,
coerceNodeId,
OPCUAClient
} from "../packages/node-opcua";
const endpointUrl = "opc.tcp://opcuademo.sterfive.com:26543";
async function main0() {
const client = OPCUAClient.create({applicationName: "MyClientApp"});
// async version
await client.withSession(endpointUrl, async (session) => {
const dataValue = await session.read({
attributeId: AttributeIds.BrowseName,
nodeId: "i=84",
});
console.log(dataValue.toString());
});
}
// main0();
// note compile with tsc typescript_test.ts --lib ES2015
//
// http://thecodebarbarian.com/80-20-guide-to-async-await-in-node.js.html
//
async function main1() {
try {
const client = OPCUAClient.create({clientName: "DemoClient"});
await client.withSessionAsync(endpointUrl, async (session) => {
const dataValue = await session.read(
{nodeId: "ns=1;s=Temperature", attributeId: AttributeIds.BrowseName});
console.log("Temperature = ", dataValue.toString());
});
} catch (err) {
console.log("Error = ", err);
}
}
async function main3() {
try {
const client = OPCUAClient.create({clientName: "DemoClient"});
await client.withSessionAsync(endpointUrl, async (session: ClientSession) => {
console.log("sessionId = ", session.sessionId.toString());
const dataValue = await session.read({nodeId: session.sessionId, attributeId: AttributeIds.Value});
console.log("value = ", dataValue.toString());
});
} catch (err) {
console.log("Error = ", err);
}
}
main();
async function main2() {
try {
const client = OPCUAClient.create({clientName: "DemoClient"});
const subscriptionParameters = {
maxNotificationsPerPublish: 10,
priority: 10,
publishingEnabled: true,
requestedLifetimeCount: 1000,
requestedMaxKeepAliveCount: 12,
requestedPublishingInterval: 100,
};
await client.withSubscriptionAsync(
endpointUrl,
subscriptionParameters,
async (session, subscription) => {
const nodeId = coerceNodeId("ns=1;s=Temperature");
const itemToMonitor = {nodeId, attributeId: AttributeIds.Value, indexRange: null};
const requestedParameters = {
samplingInterval: 1000,
queueSize: 10000,
discardOldest: true
};
const monitoredItem = await subscription.monitor(itemToMonitor, requestedParameters);
monitoredItem.on("changed", (dataValue) => console.log("Temperature ", dataValue.value.value));
monitoredItem.on("err", (err) => console.log(err));
await new Promise((resolve) => setTimeout(resolve, 5000));
});
} catch (err) {
console.log("Main 2", err);
}
}
main2();