runtime.handleAsync(async function () { const rfc1006ChannelReceiveBufferPath = "/System/Devices/RFC-1006 Device/Channels/Channel1/Nodes/ReceiveBuffer"; const rfc1006ChannelReceiveBufferNode = codabix.findNode(rfc1006ChannelReceiveBufferPath); if (!rfc1006ChannelReceiveBufferNode) throw new Error(`Could not find node '${rfc1006ChannelReceiveBufferPath}'.`); const tagBaseNodePath = "/Nodes/XML"; const triggerNodePath = tagBaseNodePath + "/TriggerNode"; const triggerNode = codabix.findNode(triggerNodePath); if (!triggerNode) throw new Error(`Could not find node '${triggerNodePath}'.`); const tagNames = ["Steuerung", "Ort", "tagid", "Inhalte", "Info"]; // Find the nodes for each tag name. const tagNodes = new Map(); for (let tagName of tagNames) { const nodePath = tagBaseNodePath + "/" + tagName; const node = codabix.findNode(nodePath); if (!node) throw new Error(`Could not find node '${nodePath}'.`); tagNodes.set(tagName, node); } // Reset the trigger node. void codabix.writeNodeValueAsync(triggerNode, 0); // Add a value changed event listener to the Receive Buffer. rfc1006ChannelReceiveBufferNode.addValueChangedEventListener(e => { if (typeof e.newValue.value != "string") { logger.logError(`Value Type of node '${rfc1006ChannelReceiveBufferPath}' is not 'String'!`); } else { let stringValue = e.newValue.value; // Log the received XML. logger.log("Received: " + stringValue); // Find the tags. let tags = extractTagsFromXmlString(stringValue); // Now write the tags into their corresponding nodes. codabix.scheduleCallback(() => { let valuesToWrite: { node: codabix.Node, value: string | number }[] = []; tags.forEach((value, key) => valuesToWrite.push({ node: tagNodes.get(key)!, value })); // Also, write 1 to the Trigger Node. valuesToWrite.push({ node: triggerNode, value: 1 }); // Write the values. codabix.writeNodeValuesAsync(valuesToWrite); // After writing the values, switch the trigger node back to 0. codabix.writeNodeValueAsync(triggerNode, 0); }); } }); function extractTagsFromXmlString(xmlString: string): Map { let map = new Map(); for (let tagName of tagNames) { // Search for the start tag. let startTag = `<${tagName}>`; let endTag = ``; let startTagIndex = xmlString.indexOf(startTag); if (startTagIndex >= 0) { // OK, we found the start tag. Now search for the end tag. let endTagIndex = xmlString.indexOf(endTag, startTagIndex + startTag.length); if (endTagIndex >= 0) { // OK, we now have the value. // TODO: To get the correct string value, we would need to XML-decode the string. let content = xmlString.substring(startTagIndex + startTag.length, endTagIndex); logger.log(`Found Tag '${tagName}' with value '${content}'.`); map.set(tagName, content); } else { logger.logWarning(`Could not find start tag '${startTag}'.`); } } else { logger.logWarning(`Could not find start tag '${startTag}'.`); } } return map; } }());