runtime.handleAsync(async function () { // A list of node pairs with the source node and destination node, which should be synchronized. const sourceNodePath = "/System/Devices/S7 TCP-IP Device/Channels/New Channel 1/Variables/"; const destNodePath = "/System/Exchange/SQL Exchange/Channels/DB1/Tables/meinetabelle/Columns/"; const syncNodePaths: [string, string][] = [ [sourceNodePath + "VariableA", destNodePath + "SpalteA"], [sourceNodePath + "VariableB", destNodePath + "SpalteB"], [sourceNodePath + "VariableC", destNodePath + "SpalteC"] ]; // Find the specified nodes. const syncNodes: [codabix.Node, codabix.Node][] = []; for (let syncNodePath of syncNodePaths) { let sourceNode = codabix.findNode(syncNodePath[0], true); let destNode = codabix.findNode(syncNodePath[1], true); syncNodes.push([sourceNode, destNode]); } // Option A: // Run a loop where we synchronize the source nodes into the destination nodes every 2000 ms. while (true) { // Synchronize the nodes await synchronizeNodesAsync(syncNodes); // Wait 2000 ms await timer.delayAsync(2000); } // Option B: // Synchronize the nodes only when the value of the trigger node has changed. // For this, we create a subscription specifying the interval, and then use the value changed event. // let triggerNode = codabix.findNode("path/to/TriggerNode", true); // triggerNode.addValueChangedEventListener(e => { // codabix.scheduleCallback(() => runtime.handleAsync(synchronizeNodesAsync(syncNodes))); // }, true); // // Read the Trigger node value every 500 ms. // codabix.subscribeNodes([triggerNode], 500); async function synchronizeNodesAsync(syncNodes: [codabix.Node, codabix.Node][]) { // Synchronize the source nodes into the destination nodes. let readRequest = syncNodes.map((value => value[0])); logger.log("Reading " + readRequest.length + " node values..."); let values = await codabix.readNodeValuesAsync(readRequest); // Check if every node has a value and a "Good" status, and build the write request. let nodesToWrite: { node: codabix.Node, value: codabix.NodeValue }[] = []; for (let i = 0; i < values.length; i++) { if (values[i] == null) logger.logWarning("Node '" + readRequest[i] + "' doesn't have a value; please check the device!"); else if (values[i]!.status.isBad) logger.logWarning("Node '" + readRequest[i] + "' has status '" + values[i]!.status.statusText + "'; please check the device!"); nodesToWrite.push({ node: syncNodes[i][1], value: values[i] || new codabix.NodeValue(null) }); } // Write the values. logger.log("Writing node values..."); await codabix.writeNodeValuesAsync(nodesToWrite); } } ());