diff --git a/unilabos/devices/virtual/virtual_multiway_valve.py b/unilabos/devices/virtual/virtual_multiway_valve.py index e69fd7c..3c288f4 100644 --- a/unilabos/devices/virtual/virtual_multiway_valve.py +++ b/unilabos/devices/virtual/virtual_multiway_valve.py @@ -36,15 +36,20 @@ class VirtualMultiwayValve: """获取当前阀门位置""" return self._current_position - def set_position(self, position: Union[int, str]): + def set_position(self, command: Union[int, str]): """ - 设置阀门位置 + 设置阀门位置 - 兼容 SendCmd 类型 Args: - position: 目标位置 (1-8) + command: 目标位置 (1-8) 或位置字符串 """ try: - pos = int(position) + # 如果是字符串形式的位置,先转换为数字 + if isinstance(command, str): + pos = int(command) + else: + pos = int(command) + if pos < 1 or pos > self.max_positions: raise ValueError(f"Position must be between 1 and {self.max_positions}") diff --git a/unilabos/devices/virtual/virtual_solenoid_valve.py b/unilabos/devices/virtual/virtual_solenoid_valve.py index 92e8baf..fb3b702 100644 --- a/unilabos/devices/virtual/virtual_solenoid_valve.py +++ b/unilabos/devices/virtual/virtual_solenoid_valve.py @@ -80,26 +80,26 @@ class VirtualSolenoidValve: return "Valve closed" - def set_state(self, state: Union[bool, str]): + def set_state(self, command: Union[bool, str]): """ - 设置阀门状态 - + 设置阀门状态 - 兼容 SendCmd 类型 + Args: - state: True/False 或 "open"/"close" + command: True/False 或 "open"/"close" """ - if isinstance(state, bool): - return self.open() if state else self.close() - elif isinstance(state, str): - if state.lower() in ["open", "on", "true", "1"]: + if isinstance(command, bool): + return self.open() if command else self.close() + elif isinstance(command, str): + if command.lower() in ["open", "on", "true", "1"]: return self.open() - elif state.lower() in ["close", "closed", "off", "false", "0"]: + elif command.lower() in ["close", "closed", "off", "false", "0"]: return self.close() else: self._status = "Error" - return "Error: Invalid state" + return "Error: Invalid command" else: self._status = "Error" - return "Error: Invalid state type" + return "Error: Invalid command type" def toggle(self): """切换阀门状态""" diff --git a/unilabos/registry/devices/virtual_device.yaml b/unilabos/registry/devices/virtual_device.yaml index 8122a1f..ffaa05d 100644 --- a/unilabos/registry/devices/virtual_device.yaml +++ b/unilabos/registry/devices/virtual_device.yaml @@ -185,7 +185,7 @@ virtual_multiway_valve: set_position: type: SendCmd goal: - position: position + command: position feedback: {} result: success: success @@ -267,7 +267,7 @@ virtual_solenoid_valve: status_types: status: String valve_state: String # "open" or "closed" - is_open: Boolean + is_open: Bool action_value_mappings: open: type: EmptyIn @@ -282,9 +282,9 @@ virtual_solenoid_valve: result: success: success set_state: - type: BoolSingleInput + type: SendCmd goal: - state: state + command: command feedback: {} result: success: success