Search and contents

PLC / Generic Device Node Type: the semantic-mapping profile

The simplest Node Type in this repo: a bare device with no application layer above it, watched over OPC UA or MQTT.

It reuses every mechanism the MES Node Type already built (the open62541 sidecar, OpcuaConnector and the Data Transformation Layer) and adds nothing new to the engine. For the sidecar, wire protocol and connector, see docs/operations/mes-node-type-opc-ua.md, which this document doesn't repeat.

This Node Type's own work is just two things:

  • the Node Type Agreement declaration (node-types/plc-generic-device/);
  • the minimal semantic-mapping profile below.

1. The Node Type Agreement

node-types/plc-generic-device/node_type.json holds three things:

  • identity, with relevant_peer_node_types: []. A bare device reading needs no correlation against another Node Type's data, unlike MES's Costing Milestone and ERP dependency.
  • config_schema, which is the same opcua fragment MES uses (OPCUA_CONFIG_SCHEMA in engine/opcua_config.py). It is kept in sync by hand, and tests/test_plc_generic_device.py::test_plc_node_type_fragment_matches_python_constant guards against drift.
  • one event_types entry:
{
  "name": "ValueChanged",
  "description": "A monitored value on the device changed, classified from raw OPC-UA or MQTT telemetry.",
  "fields": {
    "type": "object",
    "required": ["value", "source"],
    "properties": {
      "value": {},
      "source": { "type": "string", "enum": ["client-server", "pubsub", "mqtt"] }
    }
  }
}
  • value is deliberately left schema-less ({} matches any JSON type). A generic device's monitored value can be numeric, boolean, or anything else site-specific, and constraining its type here would make this Node Type less generic than its name promises.
  • source records which transport the raw event arrived over: client-server and pubsub for OPC UA, mqtt for a plant broker's message. It is the one thing every raw shape genuinely differs on (see section 2), so it is worth keeping on the canonical event rather than discarding.

2. The semantic-mapping profile: raw telemetry → ValueChanged

The sidecar emits two distinct raw wire shapes, documented in docs/operations/mes-node-type-opc-ua.md §3:

{"event": "data_change", "subscription_id": 1, "monitored_item_id": 1, "value": 42, "source_timestamp": 134380448721849500}
{"event": "pubsub_data", "value": 42}

source_timestamp is the OPC UA server's time for the value (100 ns ticks since 1601-01-01 UTC) and is absent when the server sent none; the mapping below does not use it.

node-types/plc-generic-device/example_config.json is a sample Node instance config that validates against node_type.json's own config_schema. Its state_transition_rules are the minimal semantic-mapping profile this Node Type needs: they route both raw shapes to the same canonical ValueChanged shape, tagged by transport.

"state_transition_rules": [
  {
    "when": { "event": "data_change" },
    "then": { "value": "value" },
    "literals": { "source": "client-server" }
  },
  {
    "when": { "event": "pubsub_data" },
    "then": { "value": "value" },
    "literals": { "source": "pubsub" }
  }
]

The rules are built and used the same way MES's Inferred-mapping path is:

  1. build_state_transition_rules (engine/opcua_config.py) turns the schema-validated opcua object into MappingRules.
  2. build_rule_based_transform (engine/transformation.py) turns those into a TransformFunc.
  3. The TransformFunc is registered against a TransformationRegistry under whatever source_type a Node instance's own config wiring picks.
import json
from engine.opcua_config import build_state_transition_rules
from engine.transformation import TransformationRegistry, build_rule_based_transform

config = json.loads(open("node-types/plc-generic-device/example_config.json").read())
rules = build_state_transition_rules(config["opcua"])

registry = TransformationRegistry()
registry.register("plc-generic-device-opcua", build_rule_based_transform(rules))

canonical = registry.transform("plc-generic-device-opcua", raw_event)
# {"value": 42, "source": "client-server"} or {"value": 42, "source": "pubsub"}

A raw record matching neither rule (an event value other than data_change or pubsub_data) passes through unchanged. That is build_rule_based_transform's own safe default (engine/transformation.py), not the outer registry's GENERIC_TRANSFORM fallback, since source_type here is registered.

What happens to the result

This profile doesn't wire the result any further: there is no MesIngestion equivalent and no Identity Resolution.

  • The process that runs this Node Type (engine/plc_generic_device_main.py, see plc-generic-device-runtime-entrypoint.md) applies the same mapping to each delivered reading and counts it by source. It stores nothing.
  • ValueChanged carries no identity-bearing field (no order_id or equivalent), and this Node Type has no ERP or MES peer to correlate against.
  • The one thing that can leave a reading is a deviation Trigger, when the config's deviation_rules says the reading crossed a threshold. The runtime document describes the rules, what a Node with rules needs that one without does not, and what becomes of a deviation.

A future Node Type that actually consumes ValueChanged events is free to add other wiring on top without changing anything here.

3. The live target

tests/live_targets/opcua_test_server/ is the same minimal open62541 server that MES's own live tests use: one Int32 variable, ns=1;i=5000, incremented every 200 ms and served over both client-server and PubSub. It is this Node Type's own declared live target, not just a stand-in for it, and it is also the live target for the MES Node Type's protocol test.

test_live_telemetry_classifies_through_the_semantic_mapping_profile in tests/test_plc_generic_device.py subscribes to it through the real sidecar and runs a genuine live event through the semantic-mapping profile above.

To provision the sidecar and build the test server locally, see section 1 of docs/operations/mes-node-type-opc-ua.md. The build runs these in order:

  1. deployment/install_open62541_build_tools.sh
  2. install_open62541.sh
  3. build_opcua_sidecar.sh
  4. tests/live_targets/opcua_test_server/build.sh

Pitfalls

  • The live test is skip-not-fail, the same as MES's own live tests. It only runs when kindred-opcua-sidecar is on PATH and tests/live_targets/opcua_test_server/build/opcua-test-server exists. Both are true in CI, which builds them before pytest, but not necessarily on a bare local checkout. A green local run with this test showing SKIPPED is not itself proof that the live path works. Check CI, or build both locally first, before trusting that result.
  • relevant_peer_node_types: [] is a deliberate scope boundary, not an oversight. This Node Type's own event (ValueChanged) carries no identity-bearing field and correlates against nothing, so adding a peer here would document a dependency that doesn't exist. Add a peer only if ValueChanged gains a downstream consumer that needs one.

Release: v0.11.0