Repository navigation
feat(simulation): add local virtual lab runtime #17
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
3 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
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub mod onvif; | ||
| pub mod simulation; | ||
| pub mod unilabos; | ||
|
|
||
| use crate::protocol::*; | ||
|
|
||
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,63 @@ | ||
| //! Protocol adapter for the built-in local simulation backend. | ||
| //! | ||
| //! Simulation deliberately uses the existing byte-oriented transport path: | ||
| //! the adapter serializes a small JSON action envelope and decodes a JSON | ||
| //! telemetry envelope. This keeps Lab Action Model calls identical for real | ||
| //! and virtual devices while leaving the physics/runtime implementation in | ||
| //! the transport/backend layer. | ||
|
|
||
| use crate::adapter::{DeviceMatch, ProtocolAdapter}; | ||
| use crate::protocol::DeviceCommand; | ||
| use serde_json::Value; | ||
| use std::collections::HashMap; | ||
|
|
||
| pub const PLATFORM: &str = "simulation"; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct SimulationAdapter; | ||
|
|
||
| impl SimulationAdapter { | ||
| pub fn new() -> Self { | ||
| Self | ||
| } | ||
| } | ||
|
|
||
| impl ProtocolAdapter for SimulationAdapter { | ||
| fn platform(&self) -> &str { | ||
| PLATFORM | ||
| } | ||
|
|
||
| fn load_registry(&mut self, _path: &str) -> Result<(), String> { | ||
| // Simulation devices are declared by the simulation world, not by a | ||
| // physical registry directory. | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn match_hardware(&self, _hardware_id: &str) -> Option<DeviceMatch> { | ||
| None | ||
| } | ||
|
|
||
| fn encode_command(&self, _device_type: &str, cmd: &DeviceCommand) -> Result<Vec<u8>, String> { | ||
| serde_json::to_vec(cmd).map_err(|e| format!("simulation: encode command: {e}")) | ||
| } | ||
|
|
||
| fn decode_response(&self, _device_type: &str, bytes: &[u8]) -> Option<HashMap<String, Value>> { | ||
| let envelope: Value = serde_json::from_slice(bytes).ok()?; | ||
| let properties = envelope.get("properties")?.as_object()?; | ||
| let mut decoded = properties | ||
| .iter() | ||
| .map(|(key, value)| (key.clone(), value.clone())) | ||
| .collect::<HashMap<_, _>>(); | ||
| decoded.insert("simulation".into(), Value::Bool(true)); | ||
| if let Some(engine) = envelope.get("engine") { | ||
| decoded.insert("simulation_engine".into(), engine.clone()); | ||
| } | ||
| if let Some(world_id) = envelope.get("world_id") { | ||
| decoded.insert("simulation_world".into(), world_id.clone()); | ||
| } | ||
| if let Some(action) = envelope.get("last_action") { | ||
| decoded.insert("last_action".into(), action.clone()); | ||
| } | ||
| Some(decoded) | ||
| } | ||
| } |
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.
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 (broader_impact): 启用
--simulation只会添加SimulationConfig;它不会禁用已配置的 ESP-NOW 加密狗或由 MQTT 支持的物理设备发现。因此,使用包含硬件传输配置的配置文件运行模拟模式服务器时,仍然会发现并能够控制物理设备,这与文档所描述的隔离本地模式不符。触发条件: 提供的配置包含 ESP-NOW 加密狗或其他物理设备配置时。
建议修复: 启用模拟模式后,应拒绝物理传输,或明确禁用这些传输,除非用户选择混合硬件/模拟运行模式。
Original comment in English
issue (broader_impact): Enabling
--simulationonly addsSimulationConfig; it does not disable configured ESP-NOW dongles or MQTT-backed physical discovery, so a simulation-mode server using a config with hardware transports still discovers and can control physical devices despite the documented isolated local mode.Triggers: When the supplied config contains ESP-NOW dongles or other physical-device configuration.
Suggested fix: When simulation mode is enabled, either reject physical transports or explicitly disable them unless the user opts into mixed hardware/simulation operation.