Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'node-opcua-service-read' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
if (!self.subscription) {
// some server do not provide subscription support, do not treat this as an error.
return callback(null); // new Error("No subscription"));
}
const itemToMonitor = { // ReadValueId
nodeId: proxyObject.nodeId,
attributeId: AttributeIds.Value
};
const monitoringParameters = { // MonitoringParameters
samplingInterval: 0, /* event-based */
discardOldest: true,
queueSize: 10
};
const requestedParameters = read_service.TimestampsToReturn.Both;
const monitoredItem = self.subscription.monitor(itemToMonitor, monitoringParameters, requestedParameters, callback);
// console.log("xxxxxx installing monitored Item",monitoredItem.itemToMonitor.nodeId.toString(),monitoredItem.itemToMonitor.attributeId);
//xx proxyObject.monitoredItem = monitoredItem;
Object.defineProperty(proxyObject, "__monitoredItem", {value: monitoredItem, enumerable: false});
proxyObject.__monitoredItem.on("changed", function (dataValue) {
proxyObject.dataValue = dataValue;
proxyObject.emit("value_changed", dataValue);
//xx console.log("xxx Value Changed ".red,proxyObject.nodeId.toString() , proxyObject.browseName,proxyObject.dataValue.toString());
});
};
if (!self.subscription) {
// some server do not provide subscription support, do not treat this as an error.
return callback(null); // new Error("No subscription"));
}
const itemToMonitor = { // ReadValueId
nodeId: proxyObject.nodeId,
attributeId: AttributeIds.Executable
};
const monitoringParameters = { // MonitoringParameters
samplingInterval: 0, /* event-based */
discardOldest: true,
queueSize: 10
};
const requestedParameters = read_service.TimestampsToReturn.None;
const monitoredItem = self.subscription.monitor(itemToMonitor, monitoringParameters, requestedParameters, callback);
Object.defineProperty(proxyObject, "__monitoredItem_execution_flag", {value: monitoredItem, enumerable: false});
proxyObject.__monitoredItem_execution_flag.on("changed", function (dataValue) {
proxyObject.executableFlag = dataValue.value.value;
//xx console.log(" execution flag = ", proxyObject.executableFlag , proxyObject.browseName , proxyObject.nodeId.toString());
//xx proxyObject.emit("execution_flag_changed",proxyObject.executableFlag);
});
};
// note : OutputArguments property is optional thus may be missing
outputArgumentRef = (outputArgumentRef.length === 1) ? outputArgumentRef[0] : null;
//xx console.log("xxxx argument", util.inspect(argument, {colors: true, depth: 10}));
//xx console.log("xxxx argument nodeId", argument.nodeId.toString());
let inputArguments = [], outputArguments = [];
const nodesToRead = [];
const actions = [];
if (inputArgumentRef) {
nodesToRead.push({
nodeId: inputArgumentRef.nodeId,
attributeId: read_service.AttributeIds.Value
});
actions.push(function (result) {
inputArguments = result.value.value;
});
}
if (outputArgumentRef) {
nodesToRead.push({
nodeId: outputArgumentRef.nodeId,
attributeId: read_service.AttributeIds.Value
});
actions.push(function (result) {
outputArguments = result.value.value;
});
}
if (nodesToRead.length === 0) {
console.log(" session.read(nodesToRead,function(err,nodesToRead,results) {}".cyan);
console.log(" with .... :".red);
console.log(" session.read(nodesToRead,function(err,dataValues) {}".cyan);
console.log("");
console.log("please make sure to refactor your code and check that he second argument of your callback function is named".yellow,("dataValue" + (isArray?"s":"")).cyan);
console.log("to make this exception disappear".yellow);
throw new Error("ERROR ClientSession#read API has changed !!, please fix the client code");
}
}
// coerce nodeIds
for (const node of nodesToRead) {
node.nodeId = resolveNodeId(node.nodeId);
}
const request = new read_service.ReadRequest({
nodesToRead: nodesToRead,
maxAge: maxAge,
timestampsToReturn: read_service.TimestampsToReturn.Both
});
self.performMessageTransaction(request, function (err, response) {
/* istanbul ignore next */
if (err) {
return callback(err, response);
}
assert(response instanceof read_service.ReadResponse);
const result =isArray? response.results : response.results[0];
return callback(null,result);
attributeId: read_service.AttributeIds.Value,
indexRange: null,
dataEncoding: {namespaceIndex: 0, name: null}
});
} else {
assert(node instanceof Object);
return new read_service.ReadValueId(node);
}
}
const nodesToRead = nodes.map(coerceReadValueId);
const request = new read_service.ReadRequest({
nodesToRead: nodesToRead,
timestampsToReturn: read_service.TimestampsToReturn.Neither
});
assert(nodes.length === request.nodesToRead.length);
self.performMessageTransaction(request, function (err, response) {
/* istanbul ignore next */
if (err) {
return callback(err, response);
}
if (response.responseHeader.serviceResult.isNot(StatusCodes.Good)) {
return callback(new Error(response.responseHeader.serviceResult.toString()));
}
assert(response instanceof read_service.ReadResponse);
assert(nodes.length === response.results.length);
value: new DataValue({value: value})
};
this.write(nodeToWrite, function (err, statusCode) {
/* istanbul ignore next */
if (err) {
return callback(err);
}
assert(statusCode);
callback(null, statusCode);
});
};
const keys = Object.keys(read_service.AttributeIds).filter(function (k) {
return k !== "INVALID";
});
function composeResult(nodes, nodesToRead, dataValues) {
assert(nodesToRead.length === dataValues.length);
let i = 0, c = 0;
const results = [];
let dataValue, k, nodeToRead;
for (let n = 0; n < nodes.length; n++) {
const node = nodes[n];
const data = {};
nodeId: resolveNodeId("Server"), // i=2253
};
const fields = await extractConditionFields(session, "AcknowledgeableConditionType");
const eventFilter = constructEventFilter(fields, [resolveNodeId("AcknowledgeableConditionType")]);
const monitoringParameters: MonitoringParametersOptions = {
discardOldest: false,
filter: eventFilter,
queueSize: 100,
samplingInterval: 0,
};
const event_monitoringItem =
await subscription.monitor(itemToMonitor, monitoringParameters, TimestampsToReturn.Both);
const acknowledgeableConditions: EventStuff[] = [];
let refreshStartEventHasBeenReceived = false;
let RefreshEndEventHasBeenReceived = false;
const RefreshStartEventType = resolveNodeId("RefreshStartEventType").toString();
const RefreshEndEventType = resolveNodeId("RefreshEndEventType").toString();
const promise = new Promise((resolve, reject) => {
// now create a event monitored Item
event_monitoringItem.on("changed", (_eventFields: any) => {
const eventFields = _eventFields as Variant[];
try {
// re-create monitored items
const itemsToCreate = [];
_.forEach(monitoredItems_old, function (monitoredItem /*, clientHandle*/) {
assert(monitoredItem.monitoringParameters.clientHandle > 0);
itemsToCreate.push({
itemToMonitor: monitoredItem.itemToMonitor,
monitoringMode: monitoredItem.monitoringMode,
requestedParameters: monitoredItem.monitoringParameters
});
});
const createMonitorItemsRequest = new subscription_service.CreateMonitoredItemsRequest({
subscriptionId: subscription.subscriptionId,
timestampsToReturn: TimestampsToReturn.Both, // self.timestampsToReturn,
itemsToCreate: itemsToCreate
});
subscription.session.createMonitoredItems(createMonitorItemsRequest, function (err, response) {
if (!err) {
assert(response instanceof subscription_service.CreateMonitoredItemsResponse);
const monitoredItemResults = response.results;
monitoredItemResults.forEach(function (monitoredItemResult, index) {
const clientHandle = itemsToCreate[index].requestedParameters.clientHandle;
const monitoredItem = subscription.monitoredItems[clientHandle];
if (monitoredItemResult.statusCode === StatusCodes.Good) {
nodeId: resolveNodeId("Server"), // i=2253
};
const fields = await extractConditionFields(session, "AlarmConditionType");
const eventFilter = constructEventFilter(fields, [resolveNodeId("AcknowledgeableConditionType")]);
const monitoringParameters: MonitoringParametersOptions = {
discardOldest: false,
filter: eventFilter,
queueSize: 10000,
samplingInterval: 0,
};
// now create a event monitored Item
const event_monitoringItem = ClientMonitoredItem.create(subscription, itemToMonitor, monitoringParameters, TimestampsToReturn.Both);
const RefreshStartEventType = resolveNodeId("RefreshStartEventType").toString();
const RefreshEndEventType = resolveNodeId("RefreshEndEventType").toString();
event_monitoringItem.on("changed", (eventFields: Variant[]) => {
const pojo = fieldsToJson(fields, eventFields) as EventStuff;
try {
if (pojo.eventType.value.toString() === RefreshStartEventType) {
return;
}
if (pojo.eventType.value.toString() === RefreshEndEventType) {
return;
}
if (!pojo.conditionId || !pojo.conditionId.value || pojo.conditionId.dataType === 0) {
// not a acknowledgeable condition
return;
return new read_service.ReadValueId({
nodeId: resolveNodeId(node),
attributeId: read_service.AttributeIds.Value,
indexRange: null,
dataEncoding: {namespaceIndex: 0, name: null}
});
} else {
assert(node instanceof Object);
return new read_service.ReadValueId(node);
}
}
const nodesToRead = nodes.map(coerceReadValueId);
const request = new read_service.ReadRequest({
nodesToRead: nodesToRead,
timestampsToReturn: read_service.TimestampsToReturn.Neither
});
assert(nodes.length === request.nodesToRead.length);
self.performMessageTransaction(request, function (err, response) {
/* istanbul ignore next */
if (err) {
return callback(err, response);
}
if (response.responseHeader.serviceResult.isNot(StatusCodes.Good)) {
return callback(new Error(response.responseHeader.serviceResult.toString()));
}
assert(response instanceof read_service.ReadResponse);