feat(mes): Week 2 — Modbus machine state reader service#140
Merged
Conversation
Background asyncio poller reads plc-modbus HTTP API every 5s per line,
detects state transitions (RUNNING/DOWN/IDLE/OFFLINE), and writes them
to machine_states table. REST endpoints expose live state.
New:
- backend/services/plc_client.py — async httpx wrapper for /api/plc/io
- backend/services/state_machine.py — pure detect_state() from IO snapshot
- backend/services/state_poller.py — asyncio poll loop with in-memory cache
- backend/routes/lines.py — GET /api/mes/lines, GET /lines/{id}/state
- tests/test_machine_states.py — 21 unit tests, all mocked
State detection:
VFDStatus=1 → RUNNING
VFDStatus=2 / ErrorCode>0 → DOWN + reason_code (OVERLOAD/JAM/E_STOP/...)
VFDStatus=0 → IDLE
HTTP failure → OFFLINE
Design: MES never touches Modbus TCP directly — all reads go through
the existing plc-modbus HTTP service (http://plc-modbus:8001).
Tests: 32/32 passing (21 Week 2 + 11 Week 1, no regressions)
Closes: Mikecranesync/MIRA#320
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
cffaabe to
6b4689c
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
plc-modbusHTTP API every 5s per configured linedetect_state()function mapsVFDStatus+ErrorCoderegisters → machine statemachine_statestable (open/close rows)Architecture
MES never touches Modbus TCP directly — fully decoupled from hardware.
New endpoints
ErrorCode → reason_code map
Test plan
pytest tests/test_machine_states.py -v— 21/21 passing (all mocked, no docker)pytest tests/— 32/32 passing (Week 1 + Week 2, no regressions)detect_statecovers RUNNING, IDLE, DOWN (VFDStatus=2), DOWN (ErrorCode priority), empty IO, all 5 error codes + unmappedis_transitioncovers None→any, same→same, different→differentfetch_ioraises PLCOfflineError on ConnectError and HTTP>=400Related
🤖 Generated with Claude Code
via Happy