st-ten-1/src/components/pipe_cutter_component.py

60 lines
2.4 KiB
Python
Raw Normal View History

2025-01-17 14:14:28 +00:00
from .modbus_component import ModbusComponent
2025-01-21 09:51:06 +00:00
import logging
2025-01-17 14:14:28 +00:00
class PipeCutterComponent(ModbusComponent):
2025-01-21 09:51:06 +00:00
def __init__(self, config=None, name=None, period=1, lazy=True, paused=False, threaded=True):
2025-01-17 14:14:28 +00:00
"""Initialize the Pipe Cutter Component."""
super().__init__(config=config, name=name, period=period, lazy=lazy, paused=paused, threaded=threaded)
self.log = logging.getLogger(self.name)
self.current_total_length = None
self.current_od_of_pipe = None
self.machine_status = None
2025-01-21 09:51:06 +00:00
def config_changed(self):
super().config_changed()
2025-01-17 14:14:28 +00:00
def read_total_length(self):
2025-01-21 09:51:06 +00:00
"""Read the total length from Register 4X30."""
2025-01-17 14:14:28 +00:00
try:
2025-01-21 09:51:06 +00:00
self.current_total_length = self._read(register=30)
self.log.info(f"Total Length Read (Register 4X30): {self.current_total_length}")
2025-01-17 14:14:28 +00:00
return self.current_total_length
except Exception as e:
2025-01-21 09:51:06 +00:00
self.log.error(f"Error reading total length: {e}")
2025-01-17 14:14:28 +00:00
raise
def read_od_of_pipe(self):
2025-01-21 09:51:06 +00:00
"""Read the OD of the pipe from Register 4X60."""
2025-01-17 14:14:28 +00:00
try:
2025-01-21 09:51:06 +00:00
self.current_od_of_pipe = self._read(register=60)
self.log.info(f"OD of Pipe Read (Register 4X60): {self.current_od_of_pipe}")
2025-01-17 14:14:28 +00:00
return self.current_od_of_pipe
except Exception as e:
2025-01-21 09:51:06 +00:00
self.log.error(f"Error reading OD of pipe: {e}")
2025-01-17 14:14:28 +00:00
raise
def read_machine_status(self):
2025-01-21 09:51:06 +00:00
"""Read the machine status from Register 4X766."""
2025-01-17 14:14:28 +00:00
try:
2025-01-21 09:51:06 +00:00
if not hasattr(self, 'client') or self.client is None:
self.log.error(
"Attempting to read machine status but 'client' is not initialized.")
raise AttributeError(
"'PipeCutterComponent' object has no attribute 'client'.")
self.machine_status = self._read(register=766)
self.log.info(f"Machine Status Read (Register 4X766): {self.machine_status}")
2025-01-17 14:14:28 +00:00
return self.machine_status
2025-01-21 09:51:06 +00:00
except AttributeError as ae:
# Log more detailed instance state for debugging
self.log.error(
f"AttributeError while reading machine status: {ae}. Component State: "
f"name={self.name}, config={self.config}, client={getattr(self, 'client', None)}"
)
2025-01-17 14:14:28 +00:00
raise
except Exception as e:
2025-01-21 09:51:06 +00:00
self.log.error(f"Error reading machine status: {e}")
2025-01-17 14:14:28 +00:00
raise