scietex.voltage-amplifier-service 0.2.0__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.
Files changed (28) hide show
  1. scietex_voltage_amplifier_service-0.2.0/LICENSE +21 -0
  2. scietex_voltage_amplifier_service-0.2.0/PKG-INFO +310 -0
  3. scietex_voltage_amplifier_service-0.2.0/README.md +284 -0
  4. scietex_voltage_amplifier_service-0.2.0/pyproject.toml +46 -0
  5. scietex_voltage_amplifier_service-0.2.0/setup.cfg +4 -0
  6. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/__init__.py +5 -0
  7. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/amplifier_worker.py +662 -0
  8. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/config.py +328 -0
  9. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/py.typed +0 -0
  10. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/run_worker.py +76 -0
  11. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/task_handlers/__init__.py +37 -0
  12. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/task_handlers/amplifier_control.py +511 -0
  13. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/task_handlers/monitoring.py +110 -0
  14. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/telemetry.py +238 -0
  15. scietex_voltage_amplifier_service-0.2.0/src/scietex/voltage_amplifier_service/version.py +3 -0
  16. scietex_voltage_amplifier_service-0.2.0/src/scietex.voltage_amplifier_service.egg-info/PKG-INFO +310 -0
  17. scietex_voltage_amplifier_service-0.2.0/src/scietex.voltage_amplifier_service.egg-info/SOURCES.txt +26 -0
  18. scietex_voltage_amplifier_service-0.2.0/src/scietex.voltage_amplifier_service.egg-info/dependency_links.txt +1 -0
  19. scietex_voltage_amplifier_service-0.2.0/src/scietex.voltage_amplifier_service.egg-info/entry_points.txt +2 -0
  20. scietex_voltage_amplifier_service-0.2.0/src/scietex.voltage_amplifier_service.egg-info/requires.txt +14 -0
  21. scietex_voltage_amplifier_service-0.2.0/src/scietex.voltage_amplifier_service.egg-info/top_level.txt +1 -0
  22. scietex_voltage_amplifier_service-0.2.0/tests/test_amplifier_control_handler.py +795 -0
  23. scietex_voltage_amplifier_service-0.2.0/tests/test_amplifier_worker.py +1151 -0
  24. scietex_voltage_amplifier_service-0.2.0/tests/test_config.py +527 -0
  25. scietex_voltage_amplifier_service-0.2.0/tests/test_monitoring_handler.py +308 -0
  26. scietex_voltage_amplifier_service-0.2.0/tests/test_run_worker.py +93 -0
  27. scietex_voltage_amplifier_service-0.2.0/tests/test_telemetry.py +359 -0
  28. scietex_voltage_amplifier_service-0.2.0/tests/test_version.py +52 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Anton Bondarenko
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.
@@ -0,0 +1,310 @@
1
+ Metadata-Version: 2.4
2
+ Name: scietex.voltage_amplifier_service
3
+ Version: 0.2.0
4
+ Summary: Scietex microservice daemon for voltage amplifier monitoring and control
5
+ Author-email: Anton Bondarenko <bond.anton@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Bug Tracker, https://github.com/bond-anton/scietex.voltage_amplifier_service/issues
8
+ Project-URL: Homepage, https://github.com/bond-anton/scietex.voltage_amplifier_service
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.10
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: scietex.service[valkey]~=6.2.0
15
+ Requires-Dist: scietex.hal.voltage_amplifier<1.0.0,>=0.3.0
16
+ Requires-Dist: scietex.hal.serial<3.0.0,>=2.5.0
17
+ Provides-Extra: dev
18
+ Requires-Dist: tox>=4.45.0; extra == "dev"
19
+ Provides-Extra: lint
20
+ Requires-Dist: ruff; extra == "lint"
21
+ Requires-Dist: ty; extra == "lint"
22
+ Provides-Extra: test
23
+ Requires-Dist: pytest; extra == "test"
24
+ Requires-Dist: pytest-asyncio; extra == "test"
25
+ Dynamic: license-file
26
+
27
+ # scietex.voltage_amplifier_service
28
+
29
+ Scietex microservice daemon for voltage amplifier monitoring and control.
30
+
31
+ The service drives one or more voltage-amplifier boards over Modbus RTU
32
+ (directly on RS485, or through a Modbus/TCP gateway). Each board is handled by a
33
+ driver from
34
+ [`scietex.hal.voltage_amplifier`](https://github.com/bond-anton/scietex.hal.voltage_amplifier),
35
+ selected by entry point name in the service configuration. The service polls
36
+ each board on its own long-lived task, publishes readings to a per-board Valkey
37
+ stream, and exposes control tasks on the `voltage_amplifier:*` data lane. One
38
+ service instance reaches one segment — one RS485 port or one Modbus/TCP
39
+ gateway.
40
+
41
+ ## Supported devices
42
+
43
+ | Driver family | Device | Bus | Entry points |
44
+ | --- | --- | --- | --- |
45
+ | EBIC boost Modbus v1 | EBIC_ADC boost control board | Modbus RTU over RS485 | `ebic_modbus_v1_boost` |
46
+ | EBIC boost Modbus v1 | EBIC_ADC boost control board | Modbus/TCP through a gateway onto RS485 | `ebic_modbus_tcp_v1_boost` |
47
+ | Generic Modbus | Vendor-neutral register map | Modbus RTU over RS485 | none advertised; not selectable by the service |
48
+
49
+ The accepted driver names are not fixed in this service. `devices.<id>.driver`
50
+ is resolved at runtime against the `scietex.hal.voltage_amplifier.drivers`
51
+ entry-point group of the installed HAL package, so the available names follow
52
+ the installed release rather than the service itself. `ebic_modbus_v1_boost` is
53
+ the advertised direct-serial driver today. It reports output inversion, the state
54
+ snapshot, the statistics window and waveform capture; the EBIC_ADC firmware has
55
+ no gain or filter stage, so the driver declares neither capability and the
56
+ corresponding calls raise `NotImplementedError`. `ebic_modbus_tcp_v1_boost` is
57
+ the same device layer reached through a Modbus/TCP gateway: the register and coil
58
+ surface, the capabilities and the published rows are identical, only the
59
+ transport differs, and the board is selected by its Modbus `address` behind the
60
+ gateway. The generic Modbus driver is a skeleton: its register map is not
61
+ transcribed, so every register access raises `RegisterMapIncomplete` before any
62
+ bus traffic starts, and it is not advertised through the entry-point group, so
63
+ the service cannot select it until it is.
64
+
65
+ ## Configuration
66
+
67
+ The service reads `voltage_amplifier/voltage_amplifier.yml` under the framework
68
+ config directory (`SCIETEX_CONFIG_DIR`, default `/config` in the container). The
69
+ file is created with defaults on first start.
70
+
71
+ ```yaml
72
+ transport:
73
+ port: /dev/ttyUSB0
74
+ baudrate: 19200
75
+ bytesize: 8
76
+ parity: N
77
+ stopbits: 1.0
78
+ timeout: 1.0
79
+ framer: RTU
80
+ devices:
81
+ amp0:
82
+ driver: ebic_modbus_v1_boost
83
+ address: 2
84
+ label: Booster channel 0
85
+ poll: true
86
+ poll_interval: 1.0
87
+ amp1:
88
+ driver: ebic_modbus_v1_boost
89
+ address: 3
90
+ poll_interval: 2.0
91
+ poll_enabled: true
92
+ failure_threshold: 3
93
+ stream_maxlen: 10000
94
+ ```
95
+
96
+ `kind` selects the transport. It defaults to `rs485`, so the serial example above
97
+ is unchanged from earlier releases. A gateway segment uses the same device shape
98
+ with the TCP fields instead of `port`, and no serial settings:
99
+
100
+ ```yaml
101
+ transport:
102
+ kind: tcp
103
+ host: 127.0.0.1
104
+ tcp_port: 502
105
+ tcp_timeout: 30.0
106
+ tcp_retries: 0
107
+ devices:
108
+ amp0:
109
+ driver: ebic_modbus_tcp_v1_boost
110
+ address: 2
111
+ label: Booster channel 0
112
+ poll: true
113
+ poll_interval: 1.0
114
+ poll_enabled: true
115
+ failure_threshold: 3
116
+ stream_maxlen: 10000
117
+ ```
118
+
119
+ The transport is service-wide and the boards are a map keyed by device id: one
120
+ row describes one RS485 segment or one gateway. `rs485` requires `port` and
121
+ forbids `host`; `tcp` requires `host` and forbids `port`, and a TCP row must
122
+ leave the serial fields at their defaults because the gateway owns the serial
123
+ link. The driver family must match the transport too (`ebic_modbus_v1*` is
124
+ RS485, `ebic_modbus_tcp_v1*` is TCP), and a mismatch fails registry construction
125
+ naming the device and both values. Every board on the segment is told apart by
126
+ its Modbus `address`, so two devices may not share one. The struct rejects
127
+ unknown fields, so a stray key fails the config load instead of being ignored.
128
+
129
+ The device id — the `devices` map key — is the board's identity everywhere: it
130
+ is the `{device}` segment of the readings-stream key and the key in the
131
+ telemetry `devices` map, and every task targets it. `label` is a display string
132
+ only and is never an address.
133
+
134
+ | Field | Default | Meaning |
135
+ | --- | --- | --- |
136
+ | `transport.kind` | `rs485` | `rs485` or `tcp`; selects which transport fields apply. |
137
+ | `transport.port` | required for `rs485` | Serial device the segment is wired to, e.g. `/dev/ttyUSB0`. Never a TCP port. |
138
+ | `transport.baudrate` | `9600` | RS485 baud rate; must be positive. EBIC firmware runs 19200, so set it explicitly. |
139
+ | `transport.bytesize` | `8` | Data bits, one of 5–8. |
140
+ | `transport.parity` | `N` | Parity, one of `N`, `E`, `O`. |
141
+ | `transport.stopbits` | `1.0` | Stop bits, one of `1`, `1.5`, `2`. |
142
+ | `transport.timeout` | `1.0` | Per-response serial wait in seconds; must be non-negative. Not the gateway client timeout. |
143
+ | `transport.write_timeout` | `None` | Per-write serial timeout; must be non-negative when set. The Modbus serial config of the installed HAL carries no such field, so setting it fails startup rather than configuring a timeout that never takes effect. |
144
+ | `transport.inter_byte_timeout` | `None` | Inter-byte serial gap; same rule as `write_timeout`. |
145
+ | `transport.framer` | `RTU` | Modbus framing, `RTU` or `ASCII`. |
146
+ | `transport.host` | required for `tcp` | Gateway host name or address; forbidden for `rs485`. |
147
+ | `transport.tcp_port` | `502` | Gateway TCP port, 1–65535. |
148
+ | `transport.tcp_timeout` | `30.0` | Gateway client timeout, positive. It must *exceed* the gateway's published `client_timeout_floor`, not merely the device response time, because the gateway holds the RS485 bus through reconnect and retry work before it answers or gives up. 30.0 is sized for `serial.timeout: 1.0`, `bus_retries: 0`, `bus_queue_depth: 4` (floor 26.2 s); a smaller value reports normal bus contention as an unreachable board. |
149
+ | `transport.tcp_retries` | `0` | pymodbus retries per request; non-negative. Zero keeps an ambiguous write from being sent twice. |
150
+ | `devices` | `{}` | Map of device id to board settings; the key is the identity. |
151
+ | `devices.<id>.driver` | required | HAL driver entry point name (see the table above). |
152
+ | `devices.<id>.address` | `1` | Modbus slave address, 1–247; unique per segment. |
153
+ | `devices.<id>.label` | `None` | Display string only; not an address and not a key. |
154
+ | `devices.<id>.poll` | `true` | `false` keeps the board out of the poll plan; it stays readable on demand. |
155
+ | `devices.<id>.poll_interval` | `1.0` | Seconds between reads; must be positive. |
156
+ | `poll_enabled` | `true` | Gates the poll plan as a whole; `false` starts nothing. |
157
+ | `failure_threshold` | `3` | Consecutive failed reads before a board is reported as disconnected. One dropped frame on an RS485 segment must not flip the status. |
158
+ | `stream_maxlen` | `10000` | Entries retained per board's readings stream. |
159
+
160
+ One transport per instance: boards on two serial ports, or behind two gateways,
161
+ need two service instances — the same way `vfd_service` treats its gateway.
162
+
163
+ A configuration change at runtime is **restart-required**: the board registry is
164
+ not live-rebuilt.
165
+
166
+ ## Tasks
167
+
168
+ The API plugin enqueues these tasks on two lanes: the `monitoring:*` verbs on the
169
+ control lane, the `voltage_amplifier:*` verbs on the data lane. Payloads are
170
+ `{"device": "<id>", ...}`.
171
+
172
+ | Task | Payload | Effect |
173
+ | --- | --- | --- |
174
+ | `voltage_amplifier:read` | `device` | One on-demand read; refreshes telemetry and appends a reading. |
175
+ | `voltage_amplifier:set-gain` | `device`, `gain` | Sets the dimensionless gain in V/V. |
176
+ | `voltage_amplifier:set-filter` | `device`, `filter_type`, optional `filter_enabled`, `filter_low_cutoff_hz`, `filter_high_cutoff_hz` | Read-modify-write: an omitted field keeps the device's current value. |
177
+ | `voltage_amplifier:set-inverted` | `device`, `inverted` | Sets the output polarity. |
178
+ | `monitoring:start` | none or `device` | Starts the poll loop; an empty payload selects every device. |
179
+ | `monitoring:stop` | none or `device` | Stops the poll loop. |
180
+
181
+ A write is gated on the driver's declared capability and answered `UNSUPPORTED`
182
+ when the board cannot honour it, so the EBIC_ADC board rejects `set-gain` and
183
+ `set-filter` instead of reaching a `NotImplementedError` stub. A write whose
184
+ acknowledgement timed out is answered `AMPLIFIER_WRITE_OUTCOME_UNKNOWN`: the
185
+ command may have been applied, so it is not reported as failed.
186
+
187
+ ## Running
188
+
189
+ ```bash
190
+ start-voltage-amplifier-service --conf-dir ~/.config/scietex
191
+ ```
192
+
193
+ Flags: `--conf-dir` (auto-resolved when omitted), `--service-name` (default
194
+ `$SCIETEX_SERVICE_NAME` or `VoltageAmplifier`), `--logging-level` (default
195
+ `$SCIETEX_LOGGING_LEVEL` or `INFO`). It runs in the foreground until `SIGINT` or
196
+ `SIGTERM`.
197
+
198
+ ## Container
199
+
200
+ ```bash
201
+ ./build_image.sh [--latest]
202
+ ```
203
+
204
+ Builds and pushes `registry.buro-nts.ru/scietex-voltage-amplifier-service:<version>`
205
+ for `linux/amd64` and `linux/arm64`. The image installs the released wheel from
206
+ PyPI, so publish the version first. The rootfs is read-only-safe: only the
207
+ mounted config volume is ever written.
208
+
209
+ ### Local build
210
+
211
+ ```bash
212
+ podman build -f Containerfile.local -t scietex-voltage-amplifier-service:local .
213
+ ```
214
+
215
+ `Containerfile.local` builds the same image from the working tree instead of the
216
+ index, so it works before a version is published — the release build above
217
+ installs `==${VERSION}` and fails until that version exists on PyPI. The release
218
+ path is unchanged.
219
+
220
+ ## Deployment
221
+
222
+ An RS485 board is reached through a USB adapter, so the adapter node must be
223
+ passed into the container:
224
+
225
+ ```bash
226
+ podman run --rm \
227
+ --device /dev/ttyUSB0 \
228
+ -v ~/.config/scietex:/config:z \
229
+ registry.buro-nts.ru/scietex-voltage-amplifier-service:latest
230
+ ```
231
+
232
+ | Option | Purpose |
233
+ | --- | --- |
234
+ | `--device /dev/ttyUSB0` | Passes the adapter into the container |
235
+ | `-v ~/.config/scietex:/config:z` | Host-owned config tree, SELinux-relabelled as shared (`:z`) |
236
+
237
+ The config file is created under `~/.config/scietex/voltage_amplifier/` on first
238
+ start. `:z` applies the shared SELinux relabel (`container_file_t`, no private
239
+ MCS category), which is what a tree mounted by more than one container needs.
240
+ Rootless, a non-root container user reaches the node only with the same three
241
+ settings the Quadlet drop-in carries (`--userns=keep-id`,
242
+ `--group-add keep-groups`, `--security-opt label=disable`).
243
+
244
+ A TCP instance needs no device node: it requires network access to
245
+ `host:tcp_port` instead, and its unit drops `AddDevice=/dev/ttyUSB0` along with
246
+ the serial drop-in. The gateway, not the container, owns the RS485 segment and
247
+ the board addresses behind it.
248
+
249
+ ### Quadlet (systemd user unit)
250
+
251
+ Install `deploy/voltage-amplifier-service.container` as
252
+ `~/.config/containers/systemd/scietex-voltage-amplifier-service.container`
253
+ and the serial drop-in as
254
+ `~/.config/containers/systemd/scietex-voltage-amplifier-service.container.d/50-serial-devices.conf`,
255
+ then reload and enable the generated unit:
256
+
257
+ ```bash
258
+ systemctl --user daemon-reload
259
+ systemctl --user enable --now scietex-voltage-amplifier-service
260
+ ```
261
+
262
+ The `Volume=` line bind-mounts the host config tree instead of using a named
263
+ volume, so `~/.config/scietex/voltage_amplifier/voltage_amplifier.yml` is
264
+ editable on the host and survives container replacement; the service creates
265
+ `voltage_amplifier/` and the YAML inside it on first start.
266
+ `~/.config/scietex/voltage-amplifier.env` takes optional `KEY=VALUE` environment
267
+ overrides. Create it before starting the unit — it may stay empty:
268
+ `install -m 600 /dev/null ~/.config/scietex/voltage-amplifier.env`.
269
+
270
+ The unit runs rootless, so the adapter is reachable only through the drop-in:
271
+ `UserNS=keep-id` maps the container uid to your own user and
272
+ `GroupAdd=keep-groups` passes your supplementary groups through. Your host user
273
+ must therefore be in the group that owns `/dev/ttyUSB*`, normally `dialout`:
274
+
275
+ ```bash
276
+ sudo usermod -aG dialout "$USER"
277
+ ```
278
+
279
+ Log out and back in so the new group membership reaches the systemd user
280
+ manager. Confirm the owning group with `ls -l /dev/ttyUSB0` and substitute a
281
+ different name if your host uses one.
282
+
283
+ Every `/dev/ttyUSB*` node named by `transport.port` must be passed into the
284
+ container, and at most one node per instance: a second adapter means a second
285
+ service instance with its own unit and `AddDevice=`. The board's `address` must
286
+ match its Modbus slave address and stay unique on the segment. The `ttyUSB`
287
+ number is assigned in enumeration order and can change across reboots or
288
+ replugs, so pin a udev symlink when the assignment matters. The segment's line
289
+ settings must also match the `transport` block (19200 8-N-1 for EBIC boards).
290
+
291
+ ## Development
292
+
293
+ ```bash
294
+ python -m venv .venv
295
+ . .venv/bin/activate
296
+ pip install -e ../scietex.hal.voltage_amplifier -e ../scietex.hal.serial
297
+ pip install -e ".[dev,test,lint]"
298
+ ```
299
+
300
+ The HAL packages are installed from their checkouts, so an editable install is
301
+ the normal case; a plain `pip install -e ".[dev,test,lint]"` pulls the released
302
+ HAL versions from PyPI instead. `pytest.ini` and `pyproject.toml` put the
303
+ checkout on the import path, so the suite does not depend on an installed copy
304
+ of the service package.
305
+
306
+ ```bash
307
+ pytest
308
+ ruff check src tests
309
+ tox -l
310
+ ```
@@ -0,0 +1,284 @@
1
+ # scietex.voltage_amplifier_service
2
+
3
+ Scietex microservice daemon for voltage amplifier monitoring and control.
4
+
5
+ The service drives one or more voltage-amplifier boards over Modbus RTU
6
+ (directly on RS485, or through a Modbus/TCP gateway). Each board is handled by a
7
+ driver from
8
+ [`scietex.hal.voltage_amplifier`](https://github.com/bond-anton/scietex.hal.voltage_amplifier),
9
+ selected by entry point name in the service configuration. The service polls
10
+ each board on its own long-lived task, publishes readings to a per-board Valkey
11
+ stream, and exposes control tasks on the `voltage_amplifier:*` data lane. One
12
+ service instance reaches one segment — one RS485 port or one Modbus/TCP
13
+ gateway.
14
+
15
+ ## Supported devices
16
+
17
+ | Driver family | Device | Bus | Entry points |
18
+ | --- | --- | --- | --- |
19
+ | EBIC boost Modbus v1 | EBIC_ADC boost control board | Modbus RTU over RS485 | `ebic_modbus_v1_boost` |
20
+ | EBIC boost Modbus v1 | EBIC_ADC boost control board | Modbus/TCP through a gateway onto RS485 | `ebic_modbus_tcp_v1_boost` |
21
+ | Generic Modbus | Vendor-neutral register map | Modbus RTU over RS485 | none advertised; not selectable by the service |
22
+
23
+ The accepted driver names are not fixed in this service. `devices.<id>.driver`
24
+ is resolved at runtime against the `scietex.hal.voltage_amplifier.drivers`
25
+ entry-point group of the installed HAL package, so the available names follow
26
+ the installed release rather than the service itself. `ebic_modbus_v1_boost` is
27
+ the advertised direct-serial driver today. It reports output inversion, the state
28
+ snapshot, the statistics window and waveform capture; the EBIC_ADC firmware has
29
+ no gain or filter stage, so the driver declares neither capability and the
30
+ corresponding calls raise `NotImplementedError`. `ebic_modbus_tcp_v1_boost` is
31
+ the same device layer reached through a Modbus/TCP gateway: the register and coil
32
+ surface, the capabilities and the published rows are identical, only the
33
+ transport differs, and the board is selected by its Modbus `address` behind the
34
+ gateway. The generic Modbus driver is a skeleton: its register map is not
35
+ transcribed, so every register access raises `RegisterMapIncomplete` before any
36
+ bus traffic starts, and it is not advertised through the entry-point group, so
37
+ the service cannot select it until it is.
38
+
39
+ ## Configuration
40
+
41
+ The service reads `voltage_amplifier/voltage_amplifier.yml` under the framework
42
+ config directory (`SCIETEX_CONFIG_DIR`, default `/config` in the container). The
43
+ file is created with defaults on first start.
44
+
45
+ ```yaml
46
+ transport:
47
+ port: /dev/ttyUSB0
48
+ baudrate: 19200
49
+ bytesize: 8
50
+ parity: N
51
+ stopbits: 1.0
52
+ timeout: 1.0
53
+ framer: RTU
54
+ devices:
55
+ amp0:
56
+ driver: ebic_modbus_v1_boost
57
+ address: 2
58
+ label: Booster channel 0
59
+ poll: true
60
+ poll_interval: 1.0
61
+ amp1:
62
+ driver: ebic_modbus_v1_boost
63
+ address: 3
64
+ poll_interval: 2.0
65
+ poll_enabled: true
66
+ failure_threshold: 3
67
+ stream_maxlen: 10000
68
+ ```
69
+
70
+ `kind` selects the transport. It defaults to `rs485`, so the serial example above
71
+ is unchanged from earlier releases. A gateway segment uses the same device shape
72
+ with the TCP fields instead of `port`, and no serial settings:
73
+
74
+ ```yaml
75
+ transport:
76
+ kind: tcp
77
+ host: 127.0.0.1
78
+ tcp_port: 502
79
+ tcp_timeout: 30.0
80
+ tcp_retries: 0
81
+ devices:
82
+ amp0:
83
+ driver: ebic_modbus_tcp_v1_boost
84
+ address: 2
85
+ label: Booster channel 0
86
+ poll: true
87
+ poll_interval: 1.0
88
+ poll_enabled: true
89
+ failure_threshold: 3
90
+ stream_maxlen: 10000
91
+ ```
92
+
93
+ The transport is service-wide and the boards are a map keyed by device id: one
94
+ row describes one RS485 segment or one gateway. `rs485` requires `port` and
95
+ forbids `host`; `tcp` requires `host` and forbids `port`, and a TCP row must
96
+ leave the serial fields at their defaults because the gateway owns the serial
97
+ link. The driver family must match the transport too (`ebic_modbus_v1*` is
98
+ RS485, `ebic_modbus_tcp_v1*` is TCP), and a mismatch fails registry construction
99
+ naming the device and both values. Every board on the segment is told apart by
100
+ its Modbus `address`, so two devices may not share one. The struct rejects
101
+ unknown fields, so a stray key fails the config load instead of being ignored.
102
+
103
+ The device id — the `devices` map key — is the board's identity everywhere: it
104
+ is the `{device}` segment of the readings-stream key and the key in the
105
+ telemetry `devices` map, and every task targets it. `label` is a display string
106
+ only and is never an address.
107
+
108
+ | Field | Default | Meaning |
109
+ | --- | --- | --- |
110
+ | `transport.kind` | `rs485` | `rs485` or `tcp`; selects which transport fields apply. |
111
+ | `transport.port` | required for `rs485` | Serial device the segment is wired to, e.g. `/dev/ttyUSB0`. Never a TCP port. |
112
+ | `transport.baudrate` | `9600` | RS485 baud rate; must be positive. EBIC firmware runs 19200, so set it explicitly. |
113
+ | `transport.bytesize` | `8` | Data bits, one of 5–8. |
114
+ | `transport.parity` | `N` | Parity, one of `N`, `E`, `O`. |
115
+ | `transport.stopbits` | `1.0` | Stop bits, one of `1`, `1.5`, `2`. |
116
+ | `transport.timeout` | `1.0` | Per-response serial wait in seconds; must be non-negative. Not the gateway client timeout. |
117
+ | `transport.write_timeout` | `None` | Per-write serial timeout; must be non-negative when set. The Modbus serial config of the installed HAL carries no such field, so setting it fails startup rather than configuring a timeout that never takes effect. |
118
+ | `transport.inter_byte_timeout` | `None` | Inter-byte serial gap; same rule as `write_timeout`. |
119
+ | `transport.framer` | `RTU` | Modbus framing, `RTU` or `ASCII`. |
120
+ | `transport.host` | required for `tcp` | Gateway host name or address; forbidden for `rs485`. |
121
+ | `transport.tcp_port` | `502` | Gateway TCP port, 1–65535. |
122
+ | `transport.tcp_timeout` | `30.0` | Gateway client timeout, positive. It must *exceed* the gateway's published `client_timeout_floor`, not merely the device response time, because the gateway holds the RS485 bus through reconnect and retry work before it answers or gives up. 30.0 is sized for `serial.timeout: 1.0`, `bus_retries: 0`, `bus_queue_depth: 4` (floor 26.2 s); a smaller value reports normal bus contention as an unreachable board. |
123
+ | `transport.tcp_retries` | `0` | pymodbus retries per request; non-negative. Zero keeps an ambiguous write from being sent twice. |
124
+ | `devices` | `{}` | Map of device id to board settings; the key is the identity. |
125
+ | `devices.<id>.driver` | required | HAL driver entry point name (see the table above). |
126
+ | `devices.<id>.address` | `1` | Modbus slave address, 1–247; unique per segment. |
127
+ | `devices.<id>.label` | `None` | Display string only; not an address and not a key. |
128
+ | `devices.<id>.poll` | `true` | `false` keeps the board out of the poll plan; it stays readable on demand. |
129
+ | `devices.<id>.poll_interval` | `1.0` | Seconds between reads; must be positive. |
130
+ | `poll_enabled` | `true` | Gates the poll plan as a whole; `false` starts nothing. |
131
+ | `failure_threshold` | `3` | Consecutive failed reads before a board is reported as disconnected. One dropped frame on an RS485 segment must not flip the status. |
132
+ | `stream_maxlen` | `10000` | Entries retained per board's readings stream. |
133
+
134
+ One transport per instance: boards on two serial ports, or behind two gateways,
135
+ need two service instances — the same way `vfd_service` treats its gateway.
136
+
137
+ A configuration change at runtime is **restart-required**: the board registry is
138
+ not live-rebuilt.
139
+
140
+ ## Tasks
141
+
142
+ The API plugin enqueues these tasks on two lanes: the `monitoring:*` verbs on the
143
+ control lane, the `voltage_amplifier:*` verbs on the data lane. Payloads are
144
+ `{"device": "<id>", ...}`.
145
+
146
+ | Task | Payload | Effect |
147
+ | --- | --- | --- |
148
+ | `voltage_amplifier:read` | `device` | One on-demand read; refreshes telemetry and appends a reading. |
149
+ | `voltage_amplifier:set-gain` | `device`, `gain` | Sets the dimensionless gain in V/V. |
150
+ | `voltage_amplifier:set-filter` | `device`, `filter_type`, optional `filter_enabled`, `filter_low_cutoff_hz`, `filter_high_cutoff_hz` | Read-modify-write: an omitted field keeps the device's current value. |
151
+ | `voltage_amplifier:set-inverted` | `device`, `inverted` | Sets the output polarity. |
152
+ | `monitoring:start` | none or `device` | Starts the poll loop; an empty payload selects every device. |
153
+ | `monitoring:stop` | none or `device` | Stops the poll loop. |
154
+
155
+ A write is gated on the driver's declared capability and answered `UNSUPPORTED`
156
+ when the board cannot honour it, so the EBIC_ADC board rejects `set-gain` and
157
+ `set-filter` instead of reaching a `NotImplementedError` stub. A write whose
158
+ acknowledgement timed out is answered `AMPLIFIER_WRITE_OUTCOME_UNKNOWN`: the
159
+ command may have been applied, so it is not reported as failed.
160
+
161
+ ## Running
162
+
163
+ ```bash
164
+ start-voltage-amplifier-service --conf-dir ~/.config/scietex
165
+ ```
166
+
167
+ Flags: `--conf-dir` (auto-resolved when omitted), `--service-name` (default
168
+ `$SCIETEX_SERVICE_NAME` or `VoltageAmplifier`), `--logging-level` (default
169
+ `$SCIETEX_LOGGING_LEVEL` or `INFO`). It runs in the foreground until `SIGINT` or
170
+ `SIGTERM`.
171
+
172
+ ## Container
173
+
174
+ ```bash
175
+ ./build_image.sh [--latest]
176
+ ```
177
+
178
+ Builds and pushes `registry.buro-nts.ru/scietex-voltage-amplifier-service:<version>`
179
+ for `linux/amd64` and `linux/arm64`. The image installs the released wheel from
180
+ PyPI, so publish the version first. The rootfs is read-only-safe: only the
181
+ mounted config volume is ever written.
182
+
183
+ ### Local build
184
+
185
+ ```bash
186
+ podman build -f Containerfile.local -t scietex-voltage-amplifier-service:local .
187
+ ```
188
+
189
+ `Containerfile.local` builds the same image from the working tree instead of the
190
+ index, so it works before a version is published — the release build above
191
+ installs `==${VERSION}` and fails until that version exists on PyPI. The release
192
+ path is unchanged.
193
+
194
+ ## Deployment
195
+
196
+ An RS485 board is reached through a USB adapter, so the adapter node must be
197
+ passed into the container:
198
+
199
+ ```bash
200
+ podman run --rm \
201
+ --device /dev/ttyUSB0 \
202
+ -v ~/.config/scietex:/config:z \
203
+ registry.buro-nts.ru/scietex-voltage-amplifier-service:latest
204
+ ```
205
+
206
+ | Option | Purpose |
207
+ | --- | --- |
208
+ | `--device /dev/ttyUSB0` | Passes the adapter into the container |
209
+ | `-v ~/.config/scietex:/config:z` | Host-owned config tree, SELinux-relabelled as shared (`:z`) |
210
+
211
+ The config file is created under `~/.config/scietex/voltage_amplifier/` on first
212
+ start. `:z` applies the shared SELinux relabel (`container_file_t`, no private
213
+ MCS category), which is what a tree mounted by more than one container needs.
214
+ Rootless, a non-root container user reaches the node only with the same three
215
+ settings the Quadlet drop-in carries (`--userns=keep-id`,
216
+ `--group-add keep-groups`, `--security-opt label=disable`).
217
+
218
+ A TCP instance needs no device node: it requires network access to
219
+ `host:tcp_port` instead, and its unit drops `AddDevice=/dev/ttyUSB0` along with
220
+ the serial drop-in. The gateway, not the container, owns the RS485 segment and
221
+ the board addresses behind it.
222
+
223
+ ### Quadlet (systemd user unit)
224
+
225
+ Install `deploy/voltage-amplifier-service.container` as
226
+ `~/.config/containers/systemd/scietex-voltage-amplifier-service.container`
227
+ and the serial drop-in as
228
+ `~/.config/containers/systemd/scietex-voltage-amplifier-service.container.d/50-serial-devices.conf`,
229
+ then reload and enable the generated unit:
230
+
231
+ ```bash
232
+ systemctl --user daemon-reload
233
+ systemctl --user enable --now scietex-voltage-amplifier-service
234
+ ```
235
+
236
+ The `Volume=` line bind-mounts the host config tree instead of using a named
237
+ volume, so `~/.config/scietex/voltage_amplifier/voltage_amplifier.yml` is
238
+ editable on the host and survives container replacement; the service creates
239
+ `voltage_amplifier/` and the YAML inside it on first start.
240
+ `~/.config/scietex/voltage-amplifier.env` takes optional `KEY=VALUE` environment
241
+ overrides. Create it before starting the unit — it may stay empty:
242
+ `install -m 600 /dev/null ~/.config/scietex/voltage-amplifier.env`.
243
+
244
+ The unit runs rootless, so the adapter is reachable only through the drop-in:
245
+ `UserNS=keep-id` maps the container uid to your own user and
246
+ `GroupAdd=keep-groups` passes your supplementary groups through. Your host user
247
+ must therefore be in the group that owns `/dev/ttyUSB*`, normally `dialout`:
248
+
249
+ ```bash
250
+ sudo usermod -aG dialout "$USER"
251
+ ```
252
+
253
+ Log out and back in so the new group membership reaches the systemd user
254
+ manager. Confirm the owning group with `ls -l /dev/ttyUSB0` and substitute a
255
+ different name if your host uses one.
256
+
257
+ Every `/dev/ttyUSB*` node named by `transport.port` must be passed into the
258
+ container, and at most one node per instance: a second adapter means a second
259
+ service instance with its own unit and `AddDevice=`. The board's `address` must
260
+ match its Modbus slave address and stay unique on the segment. The `ttyUSB`
261
+ number is assigned in enumeration order and can change across reboots or
262
+ replugs, so pin a udev symlink when the assignment matters. The segment's line
263
+ settings must also match the `transport` block (19200 8-N-1 for EBIC boards).
264
+
265
+ ## Development
266
+
267
+ ```bash
268
+ python -m venv .venv
269
+ . .venv/bin/activate
270
+ pip install -e ../scietex.hal.voltage_amplifier -e ../scietex.hal.serial
271
+ pip install -e ".[dev,test,lint]"
272
+ ```
273
+
274
+ The HAL packages are installed from their checkouts, so an editable install is
275
+ the normal case; a plain `pip install -e ".[dev,test,lint]"` pulls the released
276
+ HAL versions from PyPI instead. `pytest.ini` and `pyproject.toml` put the
277
+ checkout on the import path, so the suite does not depend on an installed copy
278
+ of the service package.
279
+
280
+ ```bash
281
+ pytest
282
+ ruff check src tests
283
+ tox -l
284
+ ```
@@ -0,0 +1,46 @@
1
+ [project]
2
+ name = "scietex.voltage_amplifier_service"
3
+ description = "Scietex microservice daemon for voltage amplifier monitoring and control"
4
+ readme = "README.md"
5
+ requires-python = ">=3.10"
6
+ license = "MIT"
7
+ license-files = ["LICEN[CS]E*"]
8
+ authors = [{ name = "Anton Bondarenko", email = "bond.anton@gmail.com" }]
9
+ classifiers = [
10
+ "Operating System :: OS Independent",
11
+ "Programming Language :: Python :: 3",
12
+ ]
13
+ dependencies = [
14
+ "scietex.service[valkey]~=6.2.0",
15
+ "scietex.hal.voltage_amplifier>=0.3.0,<1.0.0",
16
+ "scietex.hal.serial>=2.5.0,<3.0.0",
17
+ ]
18
+ dynamic = ["version"]
19
+
20
+ [project.urls]
21
+ "Bug Tracker" = "https://github.com/bond-anton/scietex.voltage_amplifier_service/issues"
22
+ "Homepage" = "https://github.com/bond-anton/scietex.voltage_amplifier_service"
23
+
24
+ [project.scripts]
25
+ start-voltage-amplifier-service = "scietex.voltage_amplifier_service.run_worker:main"
26
+
27
+ [project.optional-dependencies]
28
+ dev = ["tox>=4.45.0"]
29
+ lint = ["ruff", "ty"]
30
+ test = ["pytest", "pytest-asyncio"]
31
+
32
+ [build-system]
33
+ requires = ["setuptools>=61.0"]
34
+ build-backend = "setuptools.build_meta"
35
+
36
+ [tool.pytest.ini_options]
37
+ pythonpath = ["src"]
38
+
39
+ [tool.setuptools.dynamic]
40
+ version = { attr = "scietex.voltage_amplifier_service.version.__version__" }
41
+
42
+ [tool.setuptools.package-data]
43
+ "scietex.voltage_amplifier_service" = ["py.typed"]
44
+
45
+ [tool.setuptools.packages.find]
46
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,5 @@
1
+ """Voltage amplifier service."""
2
+
3
+ from .version import __version__
4
+
5
+ __all__ = ["__version__"]