forked from deepmodeling/Uni-Lab-OS
-
Notifications
You must be signed in to change notification settings - Fork 0
Virtual printer #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "nodes": [ | ||
| { | ||
| "id": "xyz_and_pipette", | ||
| "name": "laiyu_liquid xyz and pipette", | ||
| "children": [], | ||
| "parent": "", | ||
| "type": "device", | ||
| "class": "xyz_pipette_device", | ||
| "position": { | ||
| "x": 450, | ||
| "y": 450, | ||
| "z": 0 | ||
| }, | ||
| "config": { | ||
| "port": "/dev/ttyUSB0" | ||
| } | ||
| } | ||
| ], | ||
| "links": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "nodes": [ | ||
| { | ||
| "id": "virtual_printer_device", | ||
| "name": "虚拟打印设备", | ||
| "children": [], | ||
| "parent": "", | ||
| "type": "device", | ||
| "class": "virtual_printer", | ||
| "position": { | ||
| "x": 600, | ||
| "y": 450, | ||
| "z": 0 | ||
| }, | ||
| "config": { | ||
| "host_id": "demo-host", | ||
| "port": "VIRTUAL", | ||
| "prefix": "[VIRTUAL-PRINTER]", | ||
| "pretty": true | ||
| } | ||
| } | ||
| ], | ||
| "links": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import json | ||
| import logging | ||
| from datetime import datetime | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from unilabos.ros.nodes.base_device_node import BaseROS2DeviceNode | ||
|
|
||
|
|
||
| class VirtualPrinter: | ||
| _ros_node: BaseROS2DeviceNode | ||
|
|
||
| def __init__(self, device_id: Optional[str] = None, config: Optional[Dict[str, Any]] = None, **kwargs): | ||
| if device_id is None and "id" in kwargs: | ||
| device_id = kwargs.pop("id") | ||
| if config is None and "config" in kwargs: | ||
| config = kwargs.pop("config") | ||
|
|
||
| self.device_id = device_id or "virtual_printer" | ||
| self.config = config or {} | ||
|
|
||
| self.logger = logging.getLogger(f"VirtualPrinter.{self.device_id}") | ||
| self.data: Dict[str, Any] = {} | ||
|
|
||
| self.port = self.config.get("port") or kwargs.get("port", "VIRTUAL") | ||
| self.prefix = self.config.get("prefix") or kwargs.get("prefix", "[VIRTUAL-PRINTER]") | ||
| self.pretty = bool(self.config.get("pretty", True)) | ||
|
|
||
| print(f"{self.prefix} created: id={self.device_id}, port={self.port}") | ||
|
|
||
| def post_init(self, ros_node: BaseROS2DeviceNode): | ||
| self._ros_node = ros_node | ||
|
|
||
| async def initialize(self) -> bool: | ||
| self.data.update( | ||
| { | ||
| "status": "Idle", | ||
| "message": "Ready", | ||
| "last_received": None, | ||
| "received_count": 0, | ||
| } | ||
| ) | ||
| self.logger.info("Initialized") | ||
| return True | ||
|
|
||
| async def cleanup(self) -> bool: | ||
| self.data.update({"status": "Offline", "message": "System offline"}) | ||
| self.logger.info("Cleaned up") | ||
| return False | ||
|
|
||
| async def print_message(self, content: Any = None, **kwargs) -> Dict[str, Any]: | ||
| """打印虚拟设备接收到的内容(推荐 action)""" | ||
| await self._record_and_print(action="print_message", content=content, kwargs=kwargs) | ||
| return {"success": True, "message": "printed", "return_info": "printed"} | ||
|
|
||
| async def receive(self, *args, **kwargs) -> Dict[str, Any]: | ||
| payload = {"args": list(args), "kwargs": kwargs} | ||
| await self._record_and_print(action="receive", content=payload, kwargs={}) | ||
| return {"success": True, "message": "received", "return_info": "received"} | ||
|
|
||
| async def _record_and_print(self, action: str, content: Any, kwargs: Dict[str, Any]) -> None: | ||
| ts = datetime.now().isoformat(timespec="seconds") | ||
| record = { | ||
| "timestamp": ts, | ||
| "device_id": self.device_id, | ||
| "action": action, | ||
| "content": content, | ||
| "kwargs": kwargs, | ||
| } | ||
|
|
||
| self.data["last_received"] = record | ||
| self.data["received_count"] = int(self.data.get("received_count", 0)) + 1 | ||
| self.data["status"] = "Idle" | ||
| self.data["message"] = f"Last action: {action} @ {ts}" | ||
|
|
||
| if self.pretty: | ||
| try: | ||
| txt = json.dumps(record, ensure_ascii=False, indent=2, default=str) | ||
| except Exception: | ||
| txt = str(record) | ||
| else: | ||
| txt = str(record) | ||
|
|
||
| print(f"{self.prefix} received:\n{txt}") | ||
| self.logger.info("Received: %s", record) | ||
|
|
||
| @property | ||
| def status(self) -> str: | ||
| return self.data.get("status", "Unknown") | ||
|
|
||
| @property | ||
| def message(self) -> str: | ||
| return self.data.get("message", "") | ||
|
|
||
| @property | ||
| def last_received(self) -> Any: | ||
| return self.data.get("last_received") | ||
|
|
||
| @property | ||
| def received_count(self) -> int: | ||
| return int(self.data.get("received_count", 0)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): 将
mix_times从整数改为整型数组,可能会破坏那些期望标量值的现有使用方。由于 schema/默认值现在变成了整型数组(
[0]),请确认所有调用点、序列化逻辑以及任何外部集成(工作流、UI、设备驱动)都能正确处理数组形式以及任何遗留的标量值,或者为使用方文档化一个清晰的迁移路径。Original comment in English
issue (bug_risk): Changing
mix_timesfrom an integer to an integer array may break existing consumers expecting a scalar.Since the schema/default is now an integer array (
[0]), please verify that all call sites, serializers, and any external integrations (workflows, UIs, device drivers) correctly handle the array form and any legacy scalar values, or document a clear migration path for consumers.