Skip to content

Add s7 demo subcommand: live server exposing real host metrics#704

Merged
gijzelaerr merged 2 commits into
masterfrom
demo-server
Apr 22, 2026
Merged

Add s7 demo subcommand: live server exposing real host metrics#704
gijzelaerr merged 2 commits into
masterfrom
demo-server

Conversation

@gijzelaerr
Copy link
Copy Markdown
Owner

Summary

A runnable, demoable S7 PLC backed by your own machine. Real CPU %, memory %, disk/network throughput, temperature and fan RPM write into well-known DB1 offsets every couple of seconds; a writable DB2 block lets clients flip a lamp, set a brightness, push a text message, and the demo prints each write with a timestamp.

pip install "python-snap7[cli,demo]"
s7 demo --port 10102

Optional rich live display shows a full-screen dashboard of current sensor values plus a scrolling write history. Falls back to plain periodic logging when rich isn't installed.

Why

Up to now there was no way to see python-snap7 do anything without wiring up a real PLC or writing test-harness code. This gives:

  • A README GIF path — point a client at localhost:10102, watch CPU % change.
  • A dogfood target for the ha-s7 Home Assistant integration: CPU/temp sensors, lamp switch, brightness slider — all the entity classes on one device.
  • Something non-trivial for the diagnostic-sensor latency/count metrics in ha-s7 to exercise.

DB layout

Documented in the module docstring (and mirrored in constants) so users can copy-paste PLC4X tag addresses straight into a Home Assistant config.

DB1.DBD0:REAL    cpu_percent          0..100
DB1.DBD4:REAL    memory_percent       0..100
DB1.DBD8:REAL    disk_read_mbps       megabytes/second
DB1.DBD12:REAL   disk_write_mbps      megabytes/second
DB1.DBD16:REAL   net_rx_mbps          megabytes/second
DB1.DBD20:REAL   net_tx_mbps          megabytes/second
DB1.DBD24:REAL   cpu_temp_c           0 if sensor not available
DB1.DBD28:REAL   fan_rpm              0 if sensor not available
DB1.DBD32:DINT   uptime_seconds
DB1.DBX36.0:BOOL overheating          cpu_temp > 75
DB1.DBX36.1:BOOL high_load            cpu_percent > 80
DB1.DBX36.2:BOOL disk_busy            disk_read+write > 50 MB/s
---
DB2.DBX0.0:BOOL  lamp_on
DB2.DBX0.1:BOOL  alarm_enable
DB2.DBW2:INT     brightness           0..255
DB2.DBW4:INT     setpoint_c
DB2:10:STRING[32] message

Platform notes

  • CPU temperature and fan speed report 0 where psutil has no sensor backend (macOS, most containers). Intentional, not a bug.
  • psutil lives behind a new demo extra so the core install stays thin.

Test plan

  • Unit tests for MetricCollector, _encode_sensors, threshold BOOL derivation, and ControlWatcher. 5 new tests, all pass.
  • End-to-end smoke: started the demo server, connected a real snap7.Client, read DB1 → got live values (CPU 34.3%, MEM 55.6%). Wrote DB2 → demo logged [WRITE] lamp_on = ON, brightness = 200.
  • Full suite: 1506 passed, 91 skipped (+5 new).
  • mypy + ruff + pre-commit clean.
  • Skips gracefully if psutil isn't installed (via pytest.importorskip).

🤖 Generated with Claude Code

gijzelaerr and others added 2 commits April 21, 2026 16:19
A runnable, demoable S7 PLC backed by your own machine. CPU %, memory %,
disk/network throughput, temperature and fan RPM write into well-known
DB1 offsets every couple of seconds; a writable DB2 block lets clients
(e.g. the ha-s7 Home Assistant integration) flip a lamp, set a
brightness, push a text message, and the demo prints each write with a
timestamp. Optional rich live display shows current sensor values and
scrolling write history in a full-screen dashboard.

Runs as `s7 demo --port 10102` after `pip install "python-snap7[cli,demo]"`.
psutil lives behind a new `demo` extra so the core install stays thin.

DB layout is documented in the module docstring (and mirrored in
constants) so users can copy-paste PLC4X tag addresses straight into a
Home Assistant config.

Platform notes: CPU temperature and fan speed report 0 where psutil has
no sensor backend (macOS, most containers) — intentional, not a bug.

Tests exercise the collector, sensor encoder, threshold BOOL derivation,
and control-write diffing without spinning up the server; end-to-end
server coverage already lives in test_s7_unified and would duplicate
work here.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Move rich and click into the demo extras so `pip install "python-snap7[demo]"`
actually gets you a working demo — the previous split forced users to
install [cli,demo] to unlock the rich live dashboard, which nobody
would have guessed. rich is still listed under cli too; pip dedupes
and it keeps the cli extras independent for anyone who wants the CLI
without the demo.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@gijzelaerr gijzelaerr merged commit abf0d28 into master Apr 22, 2026
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant