Add direct E1 material name signals

This commit is contained in:
admin committed 2026-10-07 19:08:39 +02:00
1 parent 824b1caca7
commit 1b060377aa
6 files changed
+341 -30

No files matched your search

+41 -10
View File
@@ -4,6 +4,7 @@ from dataclasses import dataclass
from datetime import UTC, datetime
from math import isfinite
from typing import TYPE_CHECKING
from uuid import UUID
from production_analytics.calculations.channel_material import DosingChannelSample
from production_analytics.calculations.material_consumption import (
@@ -308,8 +309,8 @@ class EnlyzeApiGateway:
) -> dict[str, list[DosingChannelSample]]:
"""Read configured dosing channels through the gateway only.
Configurations name stable PLC origins; ENLYZE UUIDs are resolved at poll
time and must be unique, preventing a silently stale variable mapping.
Process and material-number refs name stable PLC origins resolved uniquely
at poll time. Direct material-name refs explicitly name machine variable UUIDs.
"""
if (
start.tzinfo is None
@@ -355,9 +356,17 @@ class EnlyzeApiGateway:
if not isinstance(row, list) or len(row) != len(columns):
raise ValueError("record must match columns")
timestamp = _parse_timestamp(row[columns.index("time")])
for key, (total, status, percentage, screw, material) in resolved.items():
for key, (total, status, percentage, screw, material, name) in resolved.items():
sample = self._dosing_sample(
row, columns, timestamp, total, status, percentage, screw, material
row,
columns,
timestamp,
total,
status,
percentage,
screw,
material,
name,
)
if sample is not None:
result[key].append(sample)
@@ -376,10 +385,11 @@ class EnlyzeApiGateway:
*,
machine_id: str,
channels: tuple["ChannelDefinition", ...],
) -> dict[str, tuple[str, str, str, str, str | None]]:
"""Resolve configured PLC origins without reading samples or writing state."""
origins = self._variables_by_origin(machine_id)
resolved: dict[str, tuple[str, str, str, str, str | None]] = {}
) -> dict[str, tuple[str, str, str, str, str | None, str | None]]:
"""Resolve unique PLC origins and explicit machine variable UUID references."""
variable_ids: set[str] = set()
origins = self._variables_by_origin(machine_id, variable_ids=variable_ids)
resolved: dict[str, tuple[str, str, str, str, str | None, str | None]] = {}
for channel in channels:
refs = (
channel.total_rate_signal_ref,
@@ -394,7 +404,17 @@ class EnlyzeApiGateway:
else None
)
key = channel.channel + "\0" + channel.extruder
resolved[key] = (*ids, material)
name = None
if channel.material_name_signal_ref is not None:
if material is not None:
raise ValueError("Material number and name signals are mutually exclusive")
ref = channel.material_name_signal_ref
if not ref.startswith("uuid:"):
raise ValueError("material_name_signal_ref must be uuid:<UUID>")
name = str(UUID(ref[5:]))
if name not in variable_ids:
raise ValueError(f"Configured material-name UUID {name!r} is unavailable")
resolved[key] = (*ids, material, name)
return resolved
@staticmethod
@@ -407,6 +427,7 @@ class EnlyzeApiGateway:
percentage: str,
screw: str,
material: str | None,
name: str | None = None,
) -> DosingChannelSample | None:
def numeric(variable: str) -> float:
value = row[columns.index(variable)]
@@ -438,6 +459,9 @@ class EnlyzeApiGateway:
material_value = None if material is None else row[columns.index(material)]
if material_value is not None and not isinstance(material_value, (str, int, float)):
raise ValueError("material assignment must be scalar or null")
name_value = None if name is None else row[columns.index(name)]
if name_value is not None and not isinstance(name_value, str):
raise ValueError("material name must be a string or null")
return DosingChannelSample(
timestamp,
numeric(total),
@@ -445,9 +469,12 @@ class EnlyzeApiGateway:
numeric(percentage),
numeric(screw),
None if material_value is None else str(material_value),
name_value if name_value is not None and name_value.strip() else None,
)
def _variables_by_origin(self, machine_id: str) -> dict[str, list[str]]:
def _variables_by_origin(
self, machine_id: str, *, variable_ids: set[str] | None = None
) -> dict[str, list[str]]:
params = {"machine": machine_id}
result: dict[str, list[str]] = {}
cursors: set[str] = set()
@@ -461,6 +488,10 @@ class EnlyzeApiGateway:
uuid = item["uuid"]
if not isinstance(uuid, str):
raise ValueError("variable uuid must be a string")
if variable_ids is not None:
if uuid in variable_ids:
raise ValueError("variable UUID must be unique")
variable_ids.add(uuid)
details = item.get("details")
if not isinstance(details, dict):
raise ValueError("variable details must be an object")