s7-sim 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- s7_sim-0.1.1/Dockerfile +33 -0
- s7_sim-0.1.1/LICENSE +21 -0
- s7_sim-0.1.1/MANIFEST.in +9 -0
- s7_sim-0.1.1/PKG-INFO +207 -0
- s7_sim-0.1.1/README.md +181 -0
- s7_sim-0.1.1/compose.yml +54 -0
- s7_sim-0.1.1/examples/gateway.yml +17 -0
- s7_sim-0.1.1/examples/line.yml +63 -0
- s7_sim-0.1.1/grafana/provisioning/dashboards/dashboards.yml +7 -0
- s7_sim-0.1.1/grafana/provisioning/dashboards/filling-line.json +318 -0
- s7_sim-0.1.1/grafana/provisioning/datasources/prometheus.yml +8 -0
- s7_sim-0.1.1/prometheus/prometheus.yml +7 -0
- s7_sim-0.1.1/pyproject.toml +46 -0
- s7_sim-0.1.1/setup.cfg +4 -0
- s7_sim-0.1.1/src/s7_sim.egg-info/SOURCES.txt +24 -0
- s7_sim-0.1.1/src/s7sim/__init__.py +8 -0
- s7_sim-0.1.1/src/s7sim/cli.py +113 -0
- s7_sim-0.1.1/src/s7sim/config.py +194 -0
- s7_sim-0.1.1/src/s7sim/line.py +89 -0
- s7_sim-0.1.1/src/s7sim/server.py +96 -0
- s7_sim-0.1.1/src/s7sim/tags.py +127 -0
- s7_sim-0.1.1/tests/conftest.py +10 -0
- s7_sim-0.1.1/tests/test_cli.py +88 -0
- s7_sim-0.1.1/tests/test_config.py +105 -0
- s7_sim-0.1.1/tests/test_line.py +69 -0
- s7_sim-0.1.1/tests/test_server.py +122 -0
- s7_sim-0.1.1/tests/test_tags.py +94 -0
s7_sim-0.1.1/Dockerfile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
FROM python:3.12-slim AS build
|
|
2
|
+
|
|
3
|
+
WORKDIR /build
|
|
4
|
+
COPY pyproject.toml README.md LICENSE ./
|
|
5
|
+
COPY src ./src
|
|
6
|
+
RUN pip install --no-cache-dir --root-user-action=ignore build \
|
|
7
|
+
&& python -m build --wheel --outdir /dist
|
|
8
|
+
|
|
9
|
+
FROM python:3.12-slim
|
|
10
|
+
|
|
11
|
+
# The S7 port is 102, which is privileged. The container listens on an
|
|
12
|
+
# unprivileged port as a non-root user and lets Docker publish it as 102:
|
|
13
|
+
# docker run -p 102:1102 ...
|
|
14
|
+
ENV S7SIM_PORT=1102 \
|
|
15
|
+
S7SIM_CONFIG=/etc/s7sim/line.yml \
|
|
16
|
+
PYTHONUNBUFFERED=1
|
|
17
|
+
|
|
18
|
+
RUN useradd --system --create-home --uid 10001 s7sim
|
|
19
|
+
|
|
20
|
+
COPY --from=build /dist/*.whl /tmp/
|
|
21
|
+
RUN pip install --no-cache-dir --root-user-action=ignore /tmp/*.whl && rm /tmp/*.whl
|
|
22
|
+
|
|
23
|
+
COPY examples/line.yml /etc/s7sim/line.yml
|
|
24
|
+
|
|
25
|
+
USER s7sim
|
|
26
|
+
EXPOSE 1102
|
|
27
|
+
|
|
28
|
+
# A container that answers the protocol is healthy; one that only has a live
|
|
29
|
+
# process is not, so the check opens a real connection.
|
|
30
|
+
HEALTHCHECK --interval=15s --timeout=10s --start-period=5s --retries=3 \
|
|
31
|
+
CMD s7-sim --config "$S7SIM_CONFIG" --port "$S7SIM_PORT" --health
|
|
32
|
+
|
|
33
|
+
ENTRYPOINT ["sh", "-c", "exec s7-sim --config \"$S7SIM_CONFIG\" --port \"$S7SIM_PORT\" \"$@\"", "--"]
|
s7_sim-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 George Vasiliades
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
s7_sim-0.1.1/MANIFEST.in
ADDED
s7_sim-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: s7-sim
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: A simulated Siemens S7 line you can run in a container
|
|
5
|
+
Author: George Vasiliades
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Poseidonas/s7-sim
|
|
8
|
+
Project-URL: Repository, https://github.com/Poseidonas/s7-sim
|
|
9
|
+
Project-URL: Issues, https://github.com/Poseidonas/s7-sim/issues
|
|
10
|
+
Keywords: siemens,s7,plc,snap7,simulator,industrial,testing
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Manufacturing
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Software Development :: Testing
|
|
16
|
+
Classifier: Topic :: System :: Emulators
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: python-snap7>=3.1
|
|
21
|
+
Requires-Dist: PyYAML>=6.0
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: pytest>=8.0; extra == "test"
|
|
24
|
+
Requires-Dist: ruff>=0.4; extra == "test"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# s7-sim
|
|
28
|
+
|
|
29
|
+
A Siemens S7 line you can run in a container.
|
|
30
|
+
|
|
31
|
+
It answers the S7 protocol like a controller does, and the values it serves
|
|
32
|
+
move the way a line moves: counters advance while it runs and freeze when it
|
|
33
|
+
stops, a temperature climbs and falls back, and the whole thing stops at random
|
|
34
|
+
intervals and starts again.
|
|
35
|
+
|
|
36
|
+
```console
|
|
37
|
+
$ docker run -p 10102:1102 ghcr.io/poseidonas/s7-sim
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Point anything that speaks S7 at it — `python-snap7`, a gateway, your own code —
|
|
41
|
+
and develop against a plant that behaves like one, without hardware, a licence
|
|
42
|
+
or a Siemens account.
|
|
43
|
+
|
|
44
|
+
## The whole stack, in one command
|
|
45
|
+
|
|
46
|
+
```console
|
|
47
|
+
git clone https://github.com/Poseidonas/s7-sim && cd s7-sim
|
|
48
|
+
docker compose up
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
That brings up four things wired together: the simulated line, an
|
|
52
|
+
[s7-gateway](https://github.com/Poseidonas/s7-gateway) reading it over S7,
|
|
53
|
+
Prometheus scraping the gateway, and Grafana with a dashboard already loaded.
|
|
54
|
+
|
|
55
|
+
| | |
|
|
56
|
+
| --- | --- |
|
|
57
|
+
| http://localhost:3000 | Grafana, no login. Line state, filler temperature, bottles per minute, current stop |
|
|
58
|
+
| http://localhost:8088/docs | the gateway's API |
|
|
59
|
+
| http://localhost:9090 | Prometheus |
|
|
60
|
+
| `localhost:10102` | the line itself, over S7 |
|
|
61
|
+
|
|
62
|
+
Everything is provisioned, so the dashboard has data in it the moment it opens.
|
|
63
|
+
|
|
64
|
+
## What it is not
|
|
65
|
+
|
|
66
|
+
**It is not a PLC.** It speaks the protocol and serves plausible values; it runs
|
|
67
|
+
no user program, has no cycle, no I/O, no diagnostics buffer and no safety.
|
|
68
|
+
|
|
69
|
+
Where it is known to differ from a controller, and this matters if you are using
|
|
70
|
+
it to test error handling:
|
|
71
|
+
|
|
72
|
+
- **A read from a data block that does not exist returns fabricated bytes**
|
|
73
|
+
instead of an error. This is `snap7.server` underneath, reported upstream at
|
|
74
|
+
[python-snap7#578](https://github.com/gijzelaerr/python-snap7/discussions/578).
|
|
75
|
+
A real CPU answers with an address error. If you are testing what your code
|
|
76
|
+
does with a wrong address, this is the one thing you cannot test here.
|
|
77
|
+
- **A read past the end of a block is padded with zeros** rather than refused.
|
|
78
|
+
- **A normal client disconnect is logged as an error** by the server.
|
|
79
|
+
- There is no PUT/GET permission and no optimized block access, so neither of
|
|
80
|
+
the two settings that gate a real S7-1200 or S7-1500 applies. Code that works
|
|
81
|
+
here can still be refused by a real CPU for that reason alone.
|
|
82
|
+
|
|
83
|
+
Use it to build and to demonstrate. Confirm against hardware before you trust it.
|
|
84
|
+
|
|
85
|
+
## Describing a plant
|
|
86
|
+
|
|
87
|
+
Everything is one YAML file. Each tag names its own address, exactly as a
|
|
88
|
+
standard-access data block lays them out, and says what it does.
|
|
89
|
+
|
|
90
|
+
```yaml
|
|
91
|
+
line:
|
|
92
|
+
mean_run_minutes: 12 # average time between stops
|
|
93
|
+
mean_stop_minutes: 3 # average length of a stop
|
|
94
|
+
seed: 1 # the same seed gives the same day, every time
|
|
95
|
+
|
|
96
|
+
blocks:
|
|
97
|
+
10:
|
|
98
|
+
tags:
|
|
99
|
+
running:
|
|
100
|
+
offset: 0
|
|
101
|
+
bit: 0
|
|
102
|
+
type: bool
|
|
103
|
+
behaviour: running
|
|
104
|
+
|
|
105
|
+
filler_temp:
|
|
106
|
+
offset: 6
|
|
107
|
+
type: real
|
|
108
|
+
behaviour: temperature
|
|
109
|
+
running: 74.0 # settles here while producing
|
|
110
|
+
idle: 21.5 # and falls back to here when stopped
|
|
111
|
+
tau: 180 # seconds to cover 63% of the gap
|
|
112
|
+
noise: 0.3
|
|
113
|
+
|
|
114
|
+
bottles_filled:
|
|
115
|
+
offset: 10
|
|
116
|
+
type: dint
|
|
117
|
+
behaviour: counter
|
|
118
|
+
per_minute: 420
|
|
119
|
+
|
|
120
|
+
line_speed:
|
|
121
|
+
offset: 18
|
|
122
|
+
type: int
|
|
123
|
+
behaviour: setpoint # written once, then left to the client
|
|
124
|
+
value: 420
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Check it before starting anything. This reads the file only:
|
|
128
|
+
|
|
129
|
+
```console
|
|
130
|
+
$ s7-sim --config examples/line.yml --check
|
|
131
|
+
line: runs 12 min on average, stops for 3 min, seed 1
|
|
132
|
+
DB10 24 bytes
|
|
133
|
+
running bool DB10.DBX0.0 r- running
|
|
134
|
+
stopped_for real DB10.DBX2 r- stoppedfor
|
|
135
|
+
filler_temp real DB10.DBX6 r- temperature
|
|
136
|
+
bottles_filled dint DB10.DBX10 r- counter
|
|
137
|
+
bottles_rejected dint DB10.DBX14 r- counter
|
|
138
|
+
line_speed int DB10.DBX18 rw setpoint
|
|
139
|
+
target_temp real DB10.DBX20 rw setpoint
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Behaviours
|
|
143
|
+
|
|
144
|
+
| | |
|
|
145
|
+
| --- | --- |
|
|
146
|
+
| `running` | true while the line produces |
|
|
147
|
+
| `counter` | advances at `per_minute` **only while running**, so a stop shows as a flat line. `fraction` counts a share of the same production, which is how a reject counter is written |
|
|
148
|
+
| `temperature` | approaches `running` while producing and `idle` while stopped, as a first-order lag with time constant `tau`, plus optional `noise` |
|
|
149
|
+
| `stopped_for` | seconds the current stop has lasted, zero while running |
|
|
150
|
+
| `setpoint` | written once at startup and never again, so a client can write it and have it stay written |
|
|
151
|
+
|
|
152
|
+
Types: `bool` `byte` `sint` `usint` `int` `uint` `word` `dint` `udint` `dword`
|
|
153
|
+
`real` `lreal`.
|
|
154
|
+
|
|
155
|
+
Two tags may share a byte only as different bits. Anything else that overlaps is
|
|
156
|
+
refused when the file is read, because both would write the same bytes every
|
|
157
|
+
tick and the last one would win.
|
|
158
|
+
|
|
159
|
+
## Reproducibility
|
|
160
|
+
|
|
161
|
+
The line is a function of elapsed time rather than a loop that mutates state.
|
|
162
|
+
The same seed produces the same stops, at the same seconds, on every run and on
|
|
163
|
+
every machine. That is what makes it useful for a test rather than only a demo:
|
|
164
|
+
a stop you cannot reproduce cannot be asserted on.
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
from s7sim import Simulator, loads
|
|
168
|
+
|
|
169
|
+
sim = Simulator(loads(open("line.yml").read()))
|
|
170
|
+
sim._register()
|
|
171
|
+
sim.refresh(1800.0) # the plant half an hour in
|
|
172
|
+
print(sim.buffer(10).hex()) # always the same bytes
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Running it without Docker
|
|
176
|
+
|
|
177
|
+
```console
|
|
178
|
+
pip install s7-sim
|
|
179
|
+
s7-sim --config examples/line.yml --port 1102
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Python 3.11+. `python-snap7` 3.1+ speaks S7 in Python over a plain socket, so
|
|
183
|
+
there is nothing to compile.
|
|
184
|
+
|
|
185
|
+
### About port 102
|
|
186
|
+
|
|
187
|
+
S7 clients expect port 102, which is privileged. The container runs as a
|
|
188
|
+
non-root user and listens on 1102, so publish it where you need it:
|
|
189
|
+
|
|
190
|
+
```console
|
|
191
|
+
docker run -p 102:1102 ghcr.io/poseidonas/s7-sim # the real port, needs a privileged bind
|
|
192
|
+
docker run -p 10102:1102 ghcr.io/poseidonas/s7-sim # anywhere else
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Development
|
|
196
|
+
|
|
197
|
+
```console
|
|
198
|
+
pip install -e ".[test]"
|
|
199
|
+
pytest
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
The tests drive a real `snap7` client against the simulator over the actual
|
|
203
|
+
protocol. Nothing about snap7 is mocked.
|
|
204
|
+
|
|
205
|
+
## Licence
|
|
206
|
+
|
|
207
|
+
MIT
|
s7_sim-0.1.1/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# s7-sim
|
|
2
|
+
|
|
3
|
+
A Siemens S7 line you can run in a container.
|
|
4
|
+
|
|
5
|
+
It answers the S7 protocol like a controller does, and the values it serves
|
|
6
|
+
move the way a line moves: counters advance while it runs and freeze when it
|
|
7
|
+
stops, a temperature climbs and falls back, and the whole thing stops at random
|
|
8
|
+
intervals and starts again.
|
|
9
|
+
|
|
10
|
+
```console
|
|
11
|
+
$ docker run -p 10102:1102 ghcr.io/poseidonas/s7-sim
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Point anything that speaks S7 at it — `python-snap7`, a gateway, your own code —
|
|
15
|
+
and develop against a plant that behaves like one, without hardware, a licence
|
|
16
|
+
or a Siemens account.
|
|
17
|
+
|
|
18
|
+
## The whole stack, in one command
|
|
19
|
+
|
|
20
|
+
```console
|
|
21
|
+
git clone https://github.com/Poseidonas/s7-sim && cd s7-sim
|
|
22
|
+
docker compose up
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
That brings up four things wired together: the simulated line, an
|
|
26
|
+
[s7-gateway](https://github.com/Poseidonas/s7-gateway) reading it over S7,
|
|
27
|
+
Prometheus scraping the gateway, and Grafana with a dashboard already loaded.
|
|
28
|
+
|
|
29
|
+
| | |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| http://localhost:3000 | Grafana, no login. Line state, filler temperature, bottles per minute, current stop |
|
|
32
|
+
| http://localhost:8088/docs | the gateway's API |
|
|
33
|
+
| http://localhost:9090 | Prometheus |
|
|
34
|
+
| `localhost:10102` | the line itself, over S7 |
|
|
35
|
+
|
|
36
|
+
Everything is provisioned, so the dashboard has data in it the moment it opens.
|
|
37
|
+
|
|
38
|
+
## What it is not
|
|
39
|
+
|
|
40
|
+
**It is not a PLC.** It speaks the protocol and serves plausible values; it runs
|
|
41
|
+
no user program, has no cycle, no I/O, no diagnostics buffer and no safety.
|
|
42
|
+
|
|
43
|
+
Where it is known to differ from a controller, and this matters if you are using
|
|
44
|
+
it to test error handling:
|
|
45
|
+
|
|
46
|
+
- **A read from a data block that does not exist returns fabricated bytes**
|
|
47
|
+
instead of an error. This is `snap7.server` underneath, reported upstream at
|
|
48
|
+
[python-snap7#578](https://github.com/gijzelaerr/python-snap7/discussions/578).
|
|
49
|
+
A real CPU answers with an address error. If you are testing what your code
|
|
50
|
+
does with a wrong address, this is the one thing you cannot test here.
|
|
51
|
+
- **A read past the end of a block is padded with zeros** rather than refused.
|
|
52
|
+
- **A normal client disconnect is logged as an error** by the server.
|
|
53
|
+
- There is no PUT/GET permission and no optimized block access, so neither of
|
|
54
|
+
the two settings that gate a real S7-1200 or S7-1500 applies. Code that works
|
|
55
|
+
here can still be refused by a real CPU for that reason alone.
|
|
56
|
+
|
|
57
|
+
Use it to build and to demonstrate. Confirm against hardware before you trust it.
|
|
58
|
+
|
|
59
|
+
## Describing a plant
|
|
60
|
+
|
|
61
|
+
Everything is one YAML file. Each tag names its own address, exactly as a
|
|
62
|
+
standard-access data block lays them out, and says what it does.
|
|
63
|
+
|
|
64
|
+
```yaml
|
|
65
|
+
line:
|
|
66
|
+
mean_run_minutes: 12 # average time between stops
|
|
67
|
+
mean_stop_minutes: 3 # average length of a stop
|
|
68
|
+
seed: 1 # the same seed gives the same day, every time
|
|
69
|
+
|
|
70
|
+
blocks:
|
|
71
|
+
10:
|
|
72
|
+
tags:
|
|
73
|
+
running:
|
|
74
|
+
offset: 0
|
|
75
|
+
bit: 0
|
|
76
|
+
type: bool
|
|
77
|
+
behaviour: running
|
|
78
|
+
|
|
79
|
+
filler_temp:
|
|
80
|
+
offset: 6
|
|
81
|
+
type: real
|
|
82
|
+
behaviour: temperature
|
|
83
|
+
running: 74.0 # settles here while producing
|
|
84
|
+
idle: 21.5 # and falls back to here when stopped
|
|
85
|
+
tau: 180 # seconds to cover 63% of the gap
|
|
86
|
+
noise: 0.3
|
|
87
|
+
|
|
88
|
+
bottles_filled:
|
|
89
|
+
offset: 10
|
|
90
|
+
type: dint
|
|
91
|
+
behaviour: counter
|
|
92
|
+
per_minute: 420
|
|
93
|
+
|
|
94
|
+
line_speed:
|
|
95
|
+
offset: 18
|
|
96
|
+
type: int
|
|
97
|
+
behaviour: setpoint # written once, then left to the client
|
|
98
|
+
value: 420
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Check it before starting anything. This reads the file only:
|
|
102
|
+
|
|
103
|
+
```console
|
|
104
|
+
$ s7-sim --config examples/line.yml --check
|
|
105
|
+
line: runs 12 min on average, stops for 3 min, seed 1
|
|
106
|
+
DB10 24 bytes
|
|
107
|
+
running bool DB10.DBX0.0 r- running
|
|
108
|
+
stopped_for real DB10.DBX2 r- stoppedfor
|
|
109
|
+
filler_temp real DB10.DBX6 r- temperature
|
|
110
|
+
bottles_filled dint DB10.DBX10 r- counter
|
|
111
|
+
bottles_rejected dint DB10.DBX14 r- counter
|
|
112
|
+
line_speed int DB10.DBX18 rw setpoint
|
|
113
|
+
target_temp real DB10.DBX20 rw setpoint
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Behaviours
|
|
117
|
+
|
|
118
|
+
| | |
|
|
119
|
+
| --- | --- |
|
|
120
|
+
| `running` | true while the line produces |
|
|
121
|
+
| `counter` | advances at `per_minute` **only while running**, so a stop shows as a flat line. `fraction` counts a share of the same production, which is how a reject counter is written |
|
|
122
|
+
| `temperature` | approaches `running` while producing and `idle` while stopped, as a first-order lag with time constant `tau`, plus optional `noise` |
|
|
123
|
+
| `stopped_for` | seconds the current stop has lasted, zero while running |
|
|
124
|
+
| `setpoint` | written once at startup and never again, so a client can write it and have it stay written |
|
|
125
|
+
|
|
126
|
+
Types: `bool` `byte` `sint` `usint` `int` `uint` `word` `dint` `udint` `dword`
|
|
127
|
+
`real` `lreal`.
|
|
128
|
+
|
|
129
|
+
Two tags may share a byte only as different bits. Anything else that overlaps is
|
|
130
|
+
refused when the file is read, because both would write the same bytes every
|
|
131
|
+
tick and the last one would win.
|
|
132
|
+
|
|
133
|
+
## Reproducibility
|
|
134
|
+
|
|
135
|
+
The line is a function of elapsed time rather than a loop that mutates state.
|
|
136
|
+
The same seed produces the same stops, at the same seconds, on every run and on
|
|
137
|
+
every machine. That is what makes it useful for a test rather than only a demo:
|
|
138
|
+
a stop you cannot reproduce cannot be asserted on.
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from s7sim import Simulator, loads
|
|
142
|
+
|
|
143
|
+
sim = Simulator(loads(open("line.yml").read()))
|
|
144
|
+
sim._register()
|
|
145
|
+
sim.refresh(1800.0) # the plant half an hour in
|
|
146
|
+
print(sim.buffer(10).hex()) # always the same bytes
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Running it without Docker
|
|
150
|
+
|
|
151
|
+
```console
|
|
152
|
+
pip install s7-sim
|
|
153
|
+
s7-sim --config examples/line.yml --port 1102
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Python 3.11+. `python-snap7` 3.1+ speaks S7 in Python over a plain socket, so
|
|
157
|
+
there is nothing to compile.
|
|
158
|
+
|
|
159
|
+
### About port 102
|
|
160
|
+
|
|
161
|
+
S7 clients expect port 102, which is privileged. The container runs as a
|
|
162
|
+
non-root user and listens on 1102, so publish it where you need it:
|
|
163
|
+
|
|
164
|
+
```console
|
|
165
|
+
docker run -p 102:1102 ghcr.io/poseidonas/s7-sim # the real port, needs a privileged bind
|
|
166
|
+
docker run -p 10102:1102 ghcr.io/poseidonas/s7-sim # anywhere else
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Development
|
|
170
|
+
|
|
171
|
+
```console
|
|
172
|
+
pip install -e ".[test]"
|
|
173
|
+
pytest
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The tests drive a real `snap7` client against the simulator over the actual
|
|
177
|
+
protocol. Nothing about snap7 is mocked.
|
|
178
|
+
|
|
179
|
+
## Licence
|
|
180
|
+
|
|
181
|
+
MIT
|
s7_sim-0.1.1/compose.yml
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# A plant, a gateway, a scraper and a dashboard.
|
|
2
|
+
#
|
|
3
|
+
# docker compose up
|
|
4
|
+
# open http://localhost:3000
|
|
5
|
+
#
|
|
6
|
+
# Nothing here needs hardware, a licence or a Siemens account.
|
|
7
|
+
|
|
8
|
+
services:
|
|
9
|
+
plc:
|
|
10
|
+
build: .
|
|
11
|
+
image: ghcr.io/poseidonas/s7-sim:latest
|
|
12
|
+
container_name: s7sim-plc
|
|
13
|
+
ports:
|
|
14
|
+
# Host 10102 rather than 102, so it needs no privileges. Map 102:1102
|
|
15
|
+
# instead if you want to reach it on the port S7 clients expect.
|
|
16
|
+
- "10102:1102"
|
|
17
|
+
|
|
18
|
+
gateway:
|
|
19
|
+
image: python:3.12-slim
|
|
20
|
+
container_name: s7sim-gateway
|
|
21
|
+
depends_on:
|
|
22
|
+
plc:
|
|
23
|
+
condition: service_healthy
|
|
24
|
+
volumes:
|
|
25
|
+
- ./examples/gateway.yml:/etc/s7-gateway/config.yml:ro
|
|
26
|
+
command: >
|
|
27
|
+
sh -c "pip install --quiet --root-user-action=ignore s7-gateway &&
|
|
28
|
+
s7-gateway --config /etc/s7-gateway/config.yml --host 0.0.0.0 --port 8080"
|
|
29
|
+
ports:
|
|
30
|
+
# 8088 on the host, 8080 inside: 8080 is a busy port on a developer
|
|
31
|
+
# machine and a demo that fails to start is worse than an odd number.
|
|
32
|
+
- "8088:8080"
|
|
33
|
+
|
|
34
|
+
prometheus:
|
|
35
|
+
image: prom/prometheus:latest
|
|
36
|
+
container_name: s7sim-prometheus
|
|
37
|
+
depends_on: [gateway]
|
|
38
|
+
volumes:
|
|
39
|
+
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
|
|
40
|
+
ports:
|
|
41
|
+
- "9090:9090"
|
|
42
|
+
|
|
43
|
+
grafana:
|
|
44
|
+
image: grafana/grafana:latest
|
|
45
|
+
container_name: s7sim-grafana
|
|
46
|
+
depends_on: [prometheus]
|
|
47
|
+
environment:
|
|
48
|
+
GF_AUTH_ANONYMOUS_ENABLED: "true"
|
|
49
|
+
GF_AUTH_ANONYMOUS_ORG_ROLE: Admin
|
|
50
|
+
GF_AUTH_DISABLE_LOGIN_FORM: "true"
|
|
51
|
+
volumes:
|
|
52
|
+
- ./grafana/provisioning:/etc/grafana/provisioning:ro
|
|
53
|
+
ports:
|
|
54
|
+
- "3000:3000"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# What s7-gateway reads from the simulator. The addresses are the ones in
|
|
2
|
+
# examples/line.yml, which is the point: the simulator lays out a block the way
|
|
3
|
+
# a plant would, and the gateway is configured against it unchanged.
|
|
4
|
+
plcs:
|
|
5
|
+
filling_line:
|
|
6
|
+
host: plc
|
|
7
|
+
port: 1102
|
|
8
|
+
rack: 0
|
|
9
|
+
slot: 1
|
|
10
|
+
tags:
|
|
11
|
+
running: {db: 10, offset: 0, type: bool, bit: 0}
|
|
12
|
+
stopped_for: {db: 10, offset: 2, type: real, unit: s}
|
|
13
|
+
filler_temp: {db: 10, offset: 6, type: real, unit: "°C", description: Filler jacket temperature}
|
|
14
|
+
bottles_filled: {db: 10, offset: 10, type: dint}
|
|
15
|
+
bottles_rejected: {db: 10, offset: 14, type: dint}
|
|
16
|
+
line_speed: {db: 10, offset: 18, type: int, unit: bottles/min, writable: true}
|
|
17
|
+
target_temp: {db: 10, offset: 20, type: real, unit: "°C", writable: true}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# A filling and packing line, of the shape a food plant runs.
|
|
2
|
+
#
|
|
3
|
+
# Addresses are laid out the way a standard-access data block is: every tag
|
|
4
|
+
# names its own offset, and nothing overlaps.
|
|
5
|
+
|
|
6
|
+
line:
|
|
7
|
+
mean_run_minutes: 12 # average time between stops
|
|
8
|
+
mean_stop_minutes: 3 # average length of a stop
|
|
9
|
+
seed: 1 # the same seed gives the same day, every time
|
|
10
|
+
|
|
11
|
+
plc:
|
|
12
|
+
rack: 0
|
|
13
|
+
slot: 1
|
|
14
|
+
port: 102
|
|
15
|
+
tick_seconds: 0.5 # how often the values are refreshed
|
|
16
|
+
|
|
17
|
+
blocks:
|
|
18
|
+
10:
|
|
19
|
+
tags:
|
|
20
|
+
running:
|
|
21
|
+
offset: 0
|
|
22
|
+
bit: 0
|
|
23
|
+
type: bool
|
|
24
|
+
behaviour: running
|
|
25
|
+
|
|
26
|
+
stopped_for:
|
|
27
|
+
offset: 2
|
|
28
|
+
type: real
|
|
29
|
+
behaviour: stopped_for
|
|
30
|
+
|
|
31
|
+
filler_temp:
|
|
32
|
+
offset: 6
|
|
33
|
+
type: real
|
|
34
|
+
behaviour: temperature
|
|
35
|
+
running: 74.0 # settles here while producing
|
|
36
|
+
idle: 21.5 # and falls back to here when stopped
|
|
37
|
+
tau: 180 # seconds to cover 63% of the gap
|
|
38
|
+
noise: 0.3
|
|
39
|
+
|
|
40
|
+
bottles_filled:
|
|
41
|
+
offset: 10
|
|
42
|
+
type: dint
|
|
43
|
+
behaviour: counter
|
|
44
|
+
per_minute: 420
|
|
45
|
+
|
|
46
|
+
bottles_rejected:
|
|
47
|
+
offset: 14
|
|
48
|
+
type: dint
|
|
49
|
+
behaviour: counter
|
|
50
|
+
per_minute: 420
|
|
51
|
+
fraction: 0.017 # 1.7% of what was filled
|
|
52
|
+
|
|
53
|
+
line_speed:
|
|
54
|
+
offset: 18
|
|
55
|
+
type: int
|
|
56
|
+
behaviour: setpoint # the simulator writes this once, then leaves it
|
|
57
|
+
value: 420
|
|
58
|
+
|
|
59
|
+
target_temp:
|
|
60
|
+
offset: 20
|
|
61
|
+
type: real
|
|
62
|
+
behaviour: setpoint
|
|
63
|
+
value: 74.0
|