Add direct E1 material name signals
This commit is contained in:
1 parent
824b1caca7
commit
1b060377aa
6 files changed
+341
-30
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from uuid import UUID
|
||||
|
||||
import yaml
|
||||
|
||||
@@ -78,6 +79,7 @@ def load_channel_material_calculation(path: str | Path) -> ChannelMaterialCalcul
|
||||
"percentage_signal_ref",
|
||||
"screw_speed_signal_ref",
|
||||
"material_number_signal_ref",
|
||||
"material_name_signal_ref",
|
||||
}
|
||||
if (
|
||||
not isinstance(row, dict)
|
||||
@@ -99,6 +101,20 @@ def load_channel_material_calculation(path: str | Path) -> ChannelMaterialCalcul
|
||||
material = row.get("material_number_signal_ref")
|
||||
if material is not None and (not isinstance(material, str) or not material.strip()):
|
||||
raise CalculationConfigError("material signal must be a string or null")
|
||||
material_name = row.get("material_name_signal_ref")
|
||||
if material_name is not None:
|
||||
try:
|
||||
if not isinstance(material_name, str) or not material_name.startswith("uuid:"):
|
||||
raise ValueError("expected uuid:<UUID>")
|
||||
UUID(material_name[5:])
|
||||
except ValueError as exc:
|
||||
raise CalculationConfigError(
|
||||
"material_name_signal_ref must be uuid:<UUID> or null"
|
||||
) from exc
|
||||
if material is not None and material_name is not None:
|
||||
raise CalculationConfigError(
|
||||
"Material number and name signals are mutually exclusive"
|
||||
)
|
||||
channels.append(
|
||||
ChannelDefinition(
|
||||
extruder["id"],
|
||||
@@ -108,6 +124,7 @@ def load_channel_material_calculation(path: str | Path) -> ChannelMaterialCalcul
|
||||
row["percentage_signal_ref"],
|
||||
row["screw_speed_signal_ref"],
|
||||
material,
|
||||
material_name,
|
||||
)
|
||||
)
|
||||
if len({(c.extruder, c.channel) for c in channels}) != len(channels):
|
||||
|
||||
@@ -21,6 +21,7 @@ class DosingChannelSample:
|
||||
dosing_percentage: float
|
||||
screw_speed: float
|
||||
material_number: str | None = None
|
||||
material_name: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
@@ -55,9 +56,10 @@ def channel_rates(
|
||||
) -> tuple[ChannelRate, ...]:
|
||||
"""Derive rates and conservative material identity for one aligned timestamp.
|
||||
|
||||
A material value which occurs on more than one channel is deliberately not
|
||||
A material number which occurs on more than one channel is deliberately not
|
||||
attributed to either channel: PLC assignments are not reliable enough to
|
||||
resolve that ambiguity from a previous sample.
|
||||
resolve that ambiguity from a previous sample. Direct per-channel names are
|
||||
already assigned and may be shared by multiple channels.
|
||||
"""
|
||||
values = tuple(samples)
|
||||
validate_active_percentage_sum(values, tolerance=percentage_tolerance)
|
||||
@@ -71,7 +73,9 @@ def channel_rates(
|
||||
for value in values:
|
||||
if not isfinite(value.total_rate_kg_per_hour) or not isfinite(value.screw_speed):
|
||||
raise ValueError("total rate and screw speed must be finite")
|
||||
if value.material_number is None:
|
||||
if value.material_name is not None and value.material_name.strip():
|
||||
number, name, status = None, value.material_name, MATERIAL_ASSIGNED
|
||||
elif value.material_number is None:
|
||||
number, name, status = None, None, MATERIAL_UNAVAILABLE
|
||||
elif counts[value.material_number] > 1:
|
||||
number, name, status = None, None, MATERIAL_AMBIGUOUS
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -28,6 +28,7 @@ class ChannelDefinition:
|
||||
percentage_signal_ref: str
|
||||
screw_speed_signal_ref: str
|
||||
material_number_signal_ref: str | None = None
|
||||
material_name_signal_ref: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
|
||||
Reference in new issue
Block a user