Demo Server
Implementation of the optional live host-metrics demo used by s7 demo.
Install python-snap7[demo] before running it. This module is intended for
examples and integration testing, not production monitoring.
Live demo server that exposes real host metrics as S7 PLC tags.
Starts an emulated S7 server and continuously writes real CPU / memory /
disk / network readings into well-known DB1 offsets, plus a writable
DB2 block that clients (e.g. the ha-s7 Home Assistant integration) can
write to. An optional rich live display shows the current values
and logs any writes with a timestamp.
This is a demo, not a production tool. Install with:
pip install "python-snap7[demo]"
s7 demo --port 10102
The demo extras pull in everything the demo needs: psutil for
metrics, rich for the live dashboard, and click for the CLI
entry point.
DB layout
DB1 (read-only — updated by the host):
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 (writable — observe writes from a client):
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
- class snap7.demo.ControlWatcher(buffer: bytearray, on_change: Callable[[str, str], None])[source]
Diffs the controls DB buffer on each tick and reports human-readable changes.
The server emulator doesn’t expose a write callback, so we just snapshot the buffer and compare — simpler than monkey-patching internals and it’s good enough for a demo (sub-second resolution).
- class snap7.demo.MetricCollector[source]
Polls psutil for metrics, computing rate deltas against the previous reading.
- class snap7.demo.Metrics(cpu_percent: float = 0.0, memory_percent: float = 0.0, disk_read_mbps: float = 0.0, disk_write_mbps: float = 0.0, net_rx_mbps: float = 0.0, net_tx_mbps: float = 0.0, cpu_temp_c: float = 0.0, fan_rpm: float = 0.0, uptime_seconds: int = 0)[source]
Snapshot of host metrics.
- snap7.demo.run_demo(port: int = 10102, refresh_seconds: float = 2.0, live: bool = True) None[source]
Run the demo server until interrupted.
- Parameters:
port – TCP port the server listens on.
refresh_seconds – Interval between metric samples and DB2 diffs.
live – Use the rich live display. Falls back to plain logging if
richis not installed.