s7-gateway: an HTTP and Prometheus layer over python-snap7 #843
Poseidonas
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I kept writing the same snap7 script at work — open a client, read a handful of DB offsets, hand the numbers to something else — and eventually wrote it once properly instead. It's an HTTP layer over python-snap7 where the addresses live in a YAML file rather than in a script:
https://github.com/Poseidonas/s7-gateway
Read or write one tag, read them all in one request, write several at once, ask the CPU what it is, and a
/metricsendpoint in the Prometheus text format so a plant can point Grafana at a PLC without anyone writing Python. Nothing is writable unless the config says so, and it binds to loopback by default.Two things it refuses on purpose, both of which snap7 will happily accept and convert:
1.5into aninttag, which would be written as1"off"into abool, which would be written as true, becausebool("off")isTrue— that one would start a motor someone meant to stopOn the testing, since it's your library underneath
All 81 tests run against a real
snap7.serveron localhost over the actual protocol. There's no mocking of snap7 anywhere — one test injects a failure, and only because the server can't be made to fail on demand. That includes 200 concurrent bit writes into the same byte to check the read-modify-write doesn't lose a neighbour, and recovery after the server disappears mid-run.Writing it that way turned up something in the native server that I've written up in #578: a read from an unregistered data block returns fabricated bytes instead of an error, which decodes as a very believable
127.53555297851562. Your own caller, docstring and write path all already expectNonethere, so it looks like a slip rather than a decision — details and measurements are in that thread.What it isn't
First public version, and it has only ever talked to
snap7.serveron localhost — no physical CPU yet. The protocol work is all yours and well travelled; what's new is the thin layer above it, and that's the part that needs someone's actual plant to find the rest of. If anyone here points it at real hardware I'd be glad to hear what breaks.MIT, Python 3.11 to 3.14, and it inherits the no-C-library install from python-snap7 3.1+, which is a nice thing to be able to write in a README.
All reactions