PLC / Generic Device Runtime Entrypoint
How a PLC / Generic Device Node instance runs as a process. engine/plc_generic_device_main.py turns
this Node Type's config file into a running Node the way engine/node_main.py does for MES and
engine/erp_main.py does for ERP.
What a running instance does
python -m engine.plc_generic_device_main \
--node-type-dir node-types/plc-generic-device \
--config /var/lib/kindred-management/staged/config.json \
--node-instance-id device-1
Options:
--node-instance-idis required.--node-type-dirdefaults tonode-types/plc-generic-device.--configdefaults to that directory'sexample_node_config.json.--shared-config-dirnames the directory that a config's"$shared"fragment references resolve against (see Sharing configuration fragments). Without it, a config that uses"$shared"does not load.
Settings:
PENDING_SUMMARY_HEARTBEAT_SECONDSsets the interval of the pending-summary announcement. It takes 1 to 86400 seconds, and an invalid value stops the start.RUNTIME_STATE_PATHnames the file the Node and the management service share to say what is running.
On an appliance this is run by kindred-node.service, which deployment/install_node_service.sh --node-type "PLC / Generic Device" installs. A running instance does five things.
Watches its source. The config carries exactly one of opcua and mqtt.
- With
opcua, the Node subscribes, client-server, to every node the section names:node_id, eachextra_fieldsentry and eachadditional_recordsentry. This Node Type forms no records, so each of those is simply a monitored point.- A target that is still settling after its own restart is retried a bounded number of times. After that the Node gives up and exits non-zero.
- The PubSub fields (
pubsub_*) are accepted by the schema and not subscribed, as with MES. A config that sets them gets a warning at start naming the fields. Without the warning, a PubSub publisher's silence would look like a quiet device, because there is no session to notice it by. The shipped example sets none.
- With
mqtt, the Node subscribes to the configured topics on a clean session. A reading is counted the moment it arrives and nothing is held for later, so the broker does not queue messages for a Node that is down. A config that statespersistent_sessionkeeps its choice.
Classifies and counts. Each delivered reading is mapped through the config's field_map or
state_transition_rules into the Node Type's one event type, ValueChanged, and counted under its
source (client-server, pubsub or mqtt). The mapping may set source itself, as the example's
literals do. When it does not, which is always the case for a bare field_map, the transport the
reading arrived on supplies it. A delivery that does not classify into a valid ValueChanged is
counted as unclassified, never dropped silently.
Reports. Once a minute, when the counts have changed, and once more as the process stops, the Node logs one line:
INFO: PLC readings observed: client-server=1204, unclassified=2
A Node with deviation_rules adds the rule outcomes to the same line (see
Deviation rules), for example client-server=1204, deviation=1, not_evaluable=3.
Keeps its source alive. An OPC UA source is one sidecar process that carries every subscription. Every five seconds the Node checks that the process is still running. If it has exited, the Node logs an error and subscribes every node again on a fresh sidecar. If that fails, it tries again at the next check. The Node itself keeps running, so the unit's start limit is not spent while the target is down.
The MQTT connector reconnects by itself to a broker that goes away, so it is not checked.
It stops cleanly on SIGTERM: the source and its sidecar process close, the runtime-state file is
removed, and the process exits with status 143, which the unit counts as a normal stop.
Deviation rules
A reading is not an event by itself. The Node counts every reading and keeps none, and it enqueues
something only when a rule in the config says a reading is one. The config's deviation_rules is a list
of rules, each over one monitored point:
"deviation_rules": [
{
"node": "ns=1;i=5000",
"op": "gt",
"threshold": 100,
"description": "Reading above 100",
"data_source": "device-1-reading"
}
]
| Key | Meaning |
|---|---|
node |
Required. What the rule watches: a node id the opcua section monitors (its node_id, an extra_fields entry or an additional_records entry), or, for an mqtt source, a topic filter that one of the subscriptions covers. A wildcard filter matches each delivered topic under it, and each topic keeps its own state. A rule on a point the config does not monitor is refused at start |
op |
Required. gt, ge, lt, le, eq or ne. A reading is outside when reading op threshold is true, so gt with 100 is outside above 100 |
threshold |
Required. A number |
description |
Optional. Text carried on the deviation, so whoever reads the case summary can see what the rule is for |
data_source |
Optional; the node when left out. The name the deviation is recorded under. Two rules may not share one, and a config that gives two the same name is refused at start. That includes two rules on the same node (an upper and a lower limit, say) that leave it out: each needs its own data_source |
The rules are the same comparison Deviation Flagging makes. The Node adds what that comparison does not have, which is the edge trigger below.
A rule fires on the crossing. It fires on the reading that takes the value from inside the threshold to
outside it, and not again for the readings that stay outside: a value that is out of range for an hour is one
deviation, not one per change. It fires once more only after a reading has come back inside. Each rule keeps its
own state, so a crossing on one node does not re-arm or suppress another. The state is in memory only, so a
restart starts every rule inside, and a value that is already out of range when the Node starts is one
deviation (counted as deviation_already_queued instead if the same reading is already in the queue, see the
id below). A per-change mode is not built.
Not every reading can be compared. Only a number is: an integer or a float, not a bool and not NaN, and not an integer too large to convert to a float. An integer above 2^53 (9007199254740992) is compared, and carried on the deviation, as the nearest float, so a threshold that close to the edge is not exact.
For a reading on a watched node that is a bool, a string, a null or NaN, the Node counts not_evaluable and does nothing else. Such a reading says nothing about whether the value is inside, so it neither fires a rule nor re-arms it. A reading on a node no rule watches is counted under its source and dropped, as it is without rules.
What the report line adds. These counts sit beside the ones above, and a reading can add to more than one of them, so the counts can add up to more than the readings delivered:
| Count | Meaning |
|---|---|
not_evaluable |
A reading on a watched node that is not a number |
deviation |
A reading crossed its rule's threshold and was put on the Trigger queue |
deviation_already_queued |
It crossed, but a deviation with the same id was already there, so nothing was added. A warning is logged |
deviation_not_handed_on |
It crossed and the queue could not be reached or refused it. The deviation is kept and tried again every 5 seconds, and is counted as deviation once it goes in. Later readings outside the threshold are not a second deviation |
rule_error |
Evaluating the rules raised, which a validated config should not cause |
A deviation's id comes from the reading. The same reading arriving twice is the same deviation, and a later crossing to the same value is a new one.
- OPC UA: the id is built from the rule, the node and the reading's source timestamp, the time the server gave the value. The timestamp reaches the Node from the sidecar's
data_changeevent (see the sidecar's event shapes).- A subscription announces the node's current value when it starts, with the timestamp the server holds for it. After a restart a value that has not changed is therefore the same deviation as before, and is counted as
deviation_already_queuedinstead of queued again. - Whether a server keeps that timestamp, or stamps each read with the time of the read, is the server's own behaviour. A server that stamps each read makes it a new deviation.
- A subscription announces the node's current value when it starts, with the timestamp the server holds for it. After a restart a value that has not changed is therefore the same deviation as before, and is counted as
- MQTT: the id is built from the rule, the topic and the message's own id, which is the value of the
mqttsection'sid_fieldwhen the payload carries one.- A message with no id of its own is identified by the time it arrived, so the same message arriving again after a restart is a second deviation.
- A broker replays a retained message to every new subscription. A topic that carries retained readings and no id therefore gives a new deviation, and a new case summary, each time the Node restarts while the value is out of range. Set
id_fieldfor such a topic.
What a Node with rules needs, and what one without does not
Without deviation_rules a PLC Node needs no database. With deviation_rules it needs the database.
Without deviation_rules | With deviation_rules | |
|---|---|---|
| Database | Not needed. The Node builds no Activity Log or Trigger queue and opens no connection | Needed. It builds both from the same settings as every other Node and creates their tables, so it exits at start while the database is down (see below the table) |
| Settings | None read | Read as the other Nodes read them (environment, then .env): the POSTGRES_* values and OLLAMA_BASE_URL, which the settings require even for a Node that never calls the model. A provisioned appliance already has both. Started where OLLAMA_BASE_URL is unset, the Node exits at start with the settings error and records no state |
| Inference endpoint | Not needed | Needed only for case summaries, which means a config with an inference section |
| Dispatch loop | None | Runs, with the report and the source check beside it |
A database that is down at start. The unit starts after postgresql.service (ordering, not readiness) and restarts a failed Node every 5 seconds, giving up after five failures within 300 seconds. A Node that fails at once, as this one does with the database down, uses the five up in about 25 seconds, not 300. A database that is still not accepting connections about 25 seconds after the unit first starts (a slow boot, or a crash recovery) therefore leaves the Node failed. It stays failed until deployment/install_node_service.sh is run again, or systemctl reset-failed and start are.
What happens to a deviation
It depends on whether the config has an inference section, the same fragment the MES Node Type takes:
- With
inference, the Node drafts a case summary from the deviation and submits it to Sign-off as a Pending record for a person to review. Withinference_enabledfalse it lists the structured facts instead of calling the model, as MES does. See Case Triage. The Node reports the Sign-off store's real Pending count as its pending summary. - Without
inference, the deviation is recorded on the Activity Log as a handled deviation and the Trigger is marked done. No Sign-off record is created and the Node reports none pending. It does not dead-letter.
inference without deviation_rules is refused: nothing would ever be triaged. So is inference_enabled without
inference: with nothing to switch on or off it would be accepted and do nothing. The shipped
node-types/plc-generic-device/example_node_config.json carries one rule and no inference section, so it needs
the database but not an inference endpoint. A config that adds inference takes the same section the MES Node
Type does.
The Sign-off record does not carry the node. It carries the rule's data_source, the value and the rule's
description, and not the node id or the topic the reading was delivered on. An integrator who wants the node in
the record names it in data_source or in description. For a rule on a wildcard topic filter that is not
possible per topic: one rule covers every topic under the filter, so the record names the filter (plant/+/temp,
say) and not the topic that crossed, and a person reading it cannot tell which line it was. Write one rule for
each topic whose deviation must be told apart.
If the inference endpoint is down when the Trigger is dispatched, the Trigger is retried and then dead-lettered with its payload intact, and no Sign-off record is created. Nothing redrives a dead-lettered Trigger, so that deviation is never summarised, and because a rule is edge-triggered it does not fire again until the value has come back inside. This is a current limit, not a recovery path.
What it does not do
- It ingests nothing. A
ValueChangedcarries a value and a source and no identity, so there is no Identity record to resolve. Readings are observed and counted, not stored, and nothing in the Activity Log records them. The only thing that reaches the queue is adeviation, from a rule that fired. - It answers no peer. The Node Type's schema has no
communication_interface, so a config that names one is refused, and there is no listener, no peer TLS and no certificate reload to arrange.
Pitfalls
- Counts are readings delivered, per process. A subscription's own initial value announcement is counted, and so is any value the target announces again after a reconnect, so a count is not a count of changes. The counts restart from zero when the Node restarts.
- A dead sidecar is subscribed again, and every node announces its value again. The OPC UA connector rebuilds a dead sidecar only when a call is made through it, and this entrypoint makes none after the subscriptions are set, so the Node does it itself. The counts carry on across it, and each node's initial value is counted again. A sidecar that is running but no longer receiving, because the connection to the target dropped without the process exiting, is not detected: a count that stops moving on a source that should be busy is the sign there, and restarting the unit clears it.
- Every monitored point maps the same way. The mapping classifies a reading by its raw shape, not by which node it came from, so readings from different nodes are counted together under the same source. The debug log names the node or topic.
- A mapping that never matches shows as
unclassified. A config whose rules do not fit what its target sends starts, runs and counts everything as unclassified. That count in the report is the signal to check the mapping. - A MQTT payload that is not a JSON object or scalar never reaches the count. The connector drops it before this Node sees it, for one of these reasons:
not_utf8,invalid_json,unstorable_text,wrong_shape,oversizeorno_subscription. It counts the reason in its own counters, which the report does not print. Each reason shows as a warning in the log, for the first five and then every hundredth, andunclassifieddoes not move. A topic that is busy but reports nothing is the sign; the log's connector warnings say why. - A deviation that could not be queued is kept and tried again. When the database is unreachable at the
moment a rule fires, the reading is counted as
deviation_not_handed_onand the deviation is tried again every 5 seconds until it goes in, even if the value stays where it is and delivers no further reading. The deviation is held in memory only, so a Node that stops before the database is back loses it; an OPC UA Node then meets the current value again on restart, an MQTT Node does not. - The database is used on the thread that delivers readings. The check for an existing deviation and the enqueue run where the reading arrives, not on a separate thread, so a slow database delays the delivery of the readings behind it.
- An OPC UA reading with no source timestamp is identified by when it arrived. A server that sends none, or an older sidecar, leaves nothing to tell a value announced again from a new one. The Node therefore uses the arrival time, as it does for an MQTT message with no id. A later crossing to the same value is then a new deviation, as it should be. The same out-of-range value announced again after a restart or a resubscribe is a second one, with a second case summary.
Release: v0.11.0