-
Notifications
You must be signed in to change notification settings - Fork 0
promote: hardware preflight to main #11
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
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
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
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,124 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import configparser | ||
| import re | ||
| import socket | ||
| import sys | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| PLACEHOLDER_VALUES = {"your_wifi_ssid", "your_wifi_password", "your_mqtt_server"} | ||
|
|
||
|
|
||
| @dataclass | ||
| class CheckResult: | ||
| name: str | ||
| ok: bool | ||
| detail: str | ||
|
|
||
|
|
||
| def _load_platformio_env(repo_root: Path) -> tuple[str, dict[str, str]]: | ||
| config = configparser.ConfigParser(inline_comment_prefixes=(";", "#")) | ||
| config.read(repo_root / "platformio.ini", encoding="utf-8") | ||
| env_sections = [section for section in config.sections() if section.startswith("env:")] | ||
| if not env_sections: | ||
| raise ValueError("platformio.ini does not define an [env:*] section") | ||
| section = env_sections[0] | ||
| return section, {key: value.strip() for key, value in config[section].items()} | ||
|
|
||
|
|
||
| def _read_config_values(config_path: Path) -> dict[str, str]: | ||
| content = config_path.read_text(encoding="utf-8") | ||
| values: dict[str, str] = {} | ||
| for key in ("ssid", "password", "mqtt_server"): | ||
| match = re.search(rf'{key}\s*=\s*"([^"]+)"', content) | ||
| if match: | ||
| values[key] = match.group(1).strip() | ||
| return values | ||
|
|
||
|
|
||
| def _is_serial_target(value: str) -> bool: | ||
| upper = value.upper() | ||
| return upper.startswith("COM") or value.startswith("/dev/") | ||
|
|
||
|
|
||
| def _check_host(host: str, port: int | None = None) -> CheckResult: | ||
| try: | ||
| resolved = socket.gethostbyname(host) | ||
| except OSError as exc: | ||
| return CheckResult(f"resolve:{host}", False, f"name resolution failed: {exc}") | ||
|
|
||
| if port is None: | ||
| return CheckResult(f"resolve:{host}", True, f"resolved to {resolved}") | ||
|
|
||
| try: | ||
| with socket.create_connection((host, port), timeout=2): | ||
| return CheckResult(f"connect:{host}:{port}", True, f"reachable at {resolved}:{port}") | ||
| except OSError as exc: | ||
| return CheckResult(f"connect:{host}:{port}", False, f"connection failed: {exc}") | ||
|
|
||
|
|
||
| def main() -> int: | ||
| repo_root = Path(__file__).resolve().parent.parent | ||
| config_path = repo_root / "src" / "config.h" | ||
|
|
||
| results: list[CheckResult] = [] | ||
|
|
||
| try: | ||
| env_name, env_config = _load_platformio_env(repo_root) | ||
| except ValueError as exc: | ||
| print(f"preflight error: {exc}") | ||
| return 1 | ||
|
|
||
| upload_target = env_config.get("upload_port", "") | ||
| upload_protocol = env_config.get("upload_protocol", "") | ||
|
|
||
| results.append(CheckResult("platformio-env", True, f"using {env_name}")) | ||
| results.append(CheckResult("upload-protocol", bool(upload_protocol), f"value={upload_protocol or 'missing'}")) | ||
|
|
||
| if not config_path.exists(): | ||
| results.append(CheckResult("config.h", False, f"missing local config: {config_path}")) | ||
| else: | ||
| values = _read_config_values(config_path) | ||
| missing_keys = [key for key in ("ssid", "password", "mqtt_server") if key not in values] | ||
| if missing_keys: | ||
| results.append(CheckResult("config.h", False, f"missing keys: {', '.join(missing_keys)}")) | ||
| else: | ||
| placeholders = [key for key, value in values.items() if value in PLACEHOLDER_VALUES] | ||
| if placeholders: | ||
| results.append(CheckResult("config.h", False, f"placeholder values still set: {', '.join(placeholders)}")) | ||
| else: | ||
| results.append(CheckResult("config.h", True, "real local credentials/config detected")) | ||
|
|
||
| mqtt_host = values.get("mqtt_server", "") | ||
| if mqtt_host: | ||
| results.append(_check_host(mqtt_host, 1883)) | ||
|
|
||
| if not upload_target: | ||
| results.append(CheckResult("upload-target", False, "upload_port missing")) | ||
| elif _is_serial_target(upload_target): | ||
| results.append(CheckResult("upload-target", True, f"serial target configured: {upload_target}")) | ||
| else: | ||
| results.append(_check_host(upload_target, 3232)) | ||
|
|
||
| failures = 0 | ||
| print("HiveTech hardware preflight") | ||
| print("") | ||
| for result in results: | ||
| status = "PASS" if result.ok else "FAIL" | ||
| print(f"[{status}] {result.name}: {result.detail}") | ||
| if not result.ok: | ||
| failures += 1 | ||
|
|
||
| print("") | ||
| if failures: | ||
| print(f"preflight result: FAIL ({failures} failed checks)") | ||
| return 1 | ||
|
|
||
| print("preflight result: PASS") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
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.
The logic for checking
config.hand the MQTT host can be improved. The current nested structure has a few issues:config.h(e.g.,ssid), the check for MQTT host connectivity is skipped, even ifmqtt_serveris correctly defined. This can lead to incomplete diagnostics.mqtt_serveris set to a placeholder value, the script reports it as a placeholder but then still attempts a connection to that placeholder value, resulting in a redundant failure message.I suggest refactoring this block to flatten the logic. This will ensure that all relevant checks are performed independently and that error messages are not duplicated.