Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
});
app.use(express.static(__dirname + '/'));
const io =socketIO.listen(app.listen(port));
io.sockets.on('connection', function (socket) {
});
console.log("Listening on port " + port);
console.log("visit http://localhost:" + port);
// --------------------------------------------------------
const itemToMonitor = {
nodeId: nodeIdToMonitor,
attributeId: AttributeIds.Value
};
const parameters = {
samplingInterval: 100,
discardOldest: true,
queueSize: 100
};
const monitoredItem = await subscription.monitor(itemToMonitor, parameters, TimestampsToReturn.Both);
monitoredItem.on("changed", (dataValue) => {
console.log(dataValue.value.toString());
io.sockets.emit('message', {
value: dataValue.value.value,
timestamp: dataValue.serverTimestamp,
nodeId: nodeIdToMonitor,
browseName: "Temperature"
});
const subscription = await session.createSubscription2({
maxNotificationsPerPublish: 9000,
publishingEnabled: true,
requestedLifetimeCount: 10,
requestedMaxKeepAliveCount: 10,
requestedPublishingInterval: 1000
});
console.log(subscription.toString());
subscription.on("raw_notification", (n) => {
//console.log(n.toString());
});
const itemToMonitor = {
attributeId: AttributeIds.Value,
nodeId: resolveNodeId("ns=0;i=2258")
};
const parameters = {
discardOldest: true,
queueSize: 10,
samplingInterval: 2000
};
const monitoredItem = ClientMonitoredItem.create(subscription, itemToMonitor, parameters, TimestampsToReturn.Both);
// subscription.on("item_added",function(monitoredItem){
monitoredItem.on("initialized", () => {
console.log(" Initialized !");
});
console.log("references of RootFolder :");
for (const reference of browseResult.references) {
console.log(" -> ", reference.browseName.toString());
}
// step 4 : read a variable with readVariableValue
const dataValue2 = await session.readVariableValue(
"ns=3;s=Scalar_Simulation_Double"
);
console.log(" value = ", dataValue2.toString());
// step 4' : read a variable with read
const maxAge = 0;
const nodeToRead = {
nodeId: "ns=3;s=Scalar_Simulation_String",
attributeId: AttributeIds.Value
};
const dataValue = await session.read(nodeToRead, maxAge);
console.log(" value ", dataValue.toString());
// step 5: install a subscription and install a monitored item for 10 seconds
const subscription = ClientSubscription.create(session, {
requestedPublishingInterval: 1000,
requestedLifetimeCount: 100,
requestedMaxKeepAliveCount: 10,
maxNotificationsPerPublish: 100,
publishingEnabled: true,
priority: 10
});
subscription
.on("started", function() {
queueSize: 10,
samplingInterval: 2000
};
const monitoredItem = ClientMonitoredItem.create(subscription, itemToMonitor, parameters, TimestampsToReturn.Both);
// subscription.on("item_added",function(monitoredItem){
monitoredItem.on("initialized", () => {
console.log(" Initialized !");
});
monitoredItem.on("changed", (dataValue) => {
console.log("Changed on ", dataValue.value.toString());
});
const nodeToRead = { nodeId: "i=2258", attributeId: AttributeIds.Value };
const timer = setInterval(async () => {
try {
const dv = await session.read(nodeToRead);
console.log("dv =", session.sessionId.toString(), session.timeout, dv.statusCode.toString());
} catch (err) {
console.log(chalk.red(" session.read error !!! "), err.message, "reconnection =", session.isReconnecting);
console.log(session.toString());
}
}, 10000);
process.on("SIGINT", async () => {
console.log(chalk.bgWhite(
"--------------------------------------------------- NOW STOPING CLIENT"));
} catch (err) {
console.log("Server doesn't seems to implement getMonitoredItems method ", err.message);
}
// get_monitored_item
// monitor_a_variable_node_value
console.log("Monitoring monitor_a_variable_node_value");
// ---------------------------------------------------------------
// monitor a variable node value
// ---------------------------------------------------------------
console.log(" Monitoring node ", monitored_node.toString());
const monitoredItem = ClientMonitoredItem.create(
the_subscription,
{
attributeId: AttributeIds.Value,
nodeId: monitored_node
},
{
discardOldest: true,
queueSize: 10000,
samplingInterval: 1000
// xx filter: { parameterTypeId: "ns=0;i=0", encodingMask: 0 },
}
);
monitoredItem.on("initialized", () => {
console.log("monitoredItem initialized");
});
monitoredItem.on("changed", (dataValue1: DataValue) => {
console.log(monitoredItem.itemToMonitor.nodeId.toString(), " value has changed to " + dataValue1.value.toString());
});
monitoredItem.on("err", (err_message: string) => {
client.on("backoff", (retry: number, delay: number) => {
console.log(" cannot connect to endpoint retry = ", retry,
" next attempt in ", delay / 1000, "seconds");
});
// put the endpoint to your OPCUA Server here
const endpointUrl = "opc.tcp://localhost:48020";
try {
await client.connect(endpointUrl);
const session = await client.createSession();
const dataValue = await session.read({
attributeId: AttributeIds.Value,
nodeId: "i=2258" // Server CurrentTime
});
console.log("Server time is ", dataValue.value.toString());
await session.close();
await client.disconnect();
} catch (err) {
console.log(" err ", err.message);
}
}