Created
December 16, 2018 15:26
-
-
Save erossignon/6688775cbd3efd9d7f690bf6d239422e to your computer and use it in GitHub Desktop.
experimenting async/await in es6 with node-opcua client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const opcua = require("node-opcua"); | |
| (async () => { | |
| const client = opcua.OPCUAClient.create(); | |
| client.on("backoff", () => console.log("reconnection in progress", client.endpointUrl)); | |
| const endpointUrl = "opc.tcp://opcuademo.sterfive.com:26543"; | |
| await client.withSessionAsync(endpointUrl, async (session) => { | |
| const dataValue1 = await session.read({nodeId: "i=2285", attributeId: opcua.AttributeIds.Value}); | |
| console.log(dataValue1.toString()); | |
| const dataValue2 = await session.read({ | |
| nodeId: "ns=1;s=Temperature", | |
| attributeId: opcua.AttributeIds.Value | |
| }); | |
| console.log(dataValue2.toString()); | |
| }); | |
| const result = await client.withSessionAsync(endpointUrl, async (session) => { | |
| return [ | |
| await session.read({ | |
| nodeId: "i=2285", | |
| attributeId: opcua.AttributeIds.Value | |
| }), | |
| await session.read({ | |
| nodeId: "ns=1;s=Temperature", | |
| attributeId: opcua.AttributeIds.Value | |
| }) | |
| ]; | |
| }); | |
| console.log(result); | |
| const result2 = await client.withSessionAsync(endpointUrl, async (session) => { | |
| return await session.read( | |
| [ | |
| { | |
| nodeId: "i=2285", | |
| attributeId: opcua.AttributeIds.Value | |
| }, | |
| { | |
| nodeId: "ns=1;s=Temperature", | |
| attributeId: opcua.AttributeIds.Value | |
| } | |
| ] | |
| ); | |
| }); | |
| console.log(result2[0].toString()); | |
| console.log(result2[1].toString()); | |
| })(); | |
| (async () => { | |
| const result3 = await client.withSessionAsync(endpointUrl, async (session) => { | |
| const result = []; | |
| await session.createSubscription(async (subscription) => { | |
| const requestedParameters= { | |
| requestedPublishingInterval: 100, | |
| requestedLifetimeCount: 60, | |
| requestedMaxKeepAliveCount: 10, | |
| maxNotificationsPerPublish: 10, | |
| publishingEnabled: true, | |
| priority: 6 | |
| }; | |
| const item ={nodeId:"ns=1;s=Temperature", attributeId: opcua.AttributeIds.Value}; | |
| const monitoredItem = opcua.ClientMonitoredItem.create(subscription, item,requestedParameters); | |
| monitoredItem.on("changed",(dataValue)=> result.push(result.length)); | |
| await { then: (resolve)=>setTimeout(resolve,1000) }; | |
| //xx await subscription.terminate(); | |
| }); | |
| return result; | |
| }); | |
| console.log(result3); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment