scietex.current-amplifier-service 0.1.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_current_amplifier_service-0.1.0/LICENSE +21 -0
  2. scietex_current_amplifier_service-0.1.0/PKG-INFO +257 -0
  3. scietex_current_amplifier_service-0.1.0/README.md +231 -0
  4. scietex_current_amplifier_service-0.1.0/pyproject.toml +46 -0
  5. scietex_current_amplifier_service-0.1.0/setup.cfg +4 -0
  6. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/__init__.py +5 -0
  7. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/amplifier_worker.py +616 -0
  8. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/config.py +192 -0
  9. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/py.typed +0 -0
  10. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/run_worker.py +76 -0
  11. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/task_handlers/__init__.py +61 -0
  12. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/task_handlers/amplifier_control.py +750 -0
  13. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/task_handlers/monitoring.py +110 -0
  14. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/telemetry.py +251 -0
  15. scietex_current_amplifier_service-0.1.0/src/scietex/current_amplifier_service/version.py +3 -0
  16. scietex_current_amplifier_service-0.1.0/src/scietex.current_amplifier_service.egg-info/PKG-INFO +257 -0
  17. scietex_current_amplifier_service-0.1.0/src/scietex.current_amplifier_service.egg-info/SOURCES.txt +26 -0
  18. scietex_current_amplifier_service-0.1.0/src/scietex.current_amplifier_service.egg-info/dependency_links.txt +1 -0
  19. scietex_current_amplifier_service-0.1.0/src/scietex.current_amplifier_service.egg-info/entry_points.txt +2 -0
  20. scietex_current_amplifier_service-0.1.0/src/scietex.current_amplifier_service.egg-info/requires.txt +14 -0
  21. scietex_current_amplifier_service-0.1.0/src/scietex.current_amplifier_service.egg-info/top_level.txt +1 -0
  22. scietex_current_amplifier_service-0.1.0/tests/test_amplifier_control_handler.py +1034 -0
  23. scietex_current_amplifier_service-0.1.0/tests/test_amplifier_worker.py +535 -0
  24. scietex_current_amplifier_service-0.1.0/tests/test_config.py +255 -0
  25. scietex_current_amplifier_service-0.1.0/tests/test_monitoring_handler.py +265 -0
  26. scietex_current_amplifier_service-0.1.0/tests/test_run_worker.py +91 -0
  27. scietex_current_amplifier_service-0.1.0/tests/test_telemetry.py +245 -0
  28. scietex_current_amplifier_service-0.1.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,257 @@
1
+ Metadata-Version: 2.4
2
+ Name: scietex.current_amplifier_service
3
+ Version: 0.1.0
4
+ Summary: Scietex microservice daemon for current 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.current_amplifier_service/issues
8
+ Project-URL: Homepage, https://github.com/bond-anton/scietex.current_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.current_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.current_amplifier_service
28
+
29
+ Scietex microservice daemon for current amplifier monitoring and control.
30
+
31
+ The service drives one or more EBIC v1.0 current-amplifier boards over Modbus
32
+ RTU. Each board is handled by a driver from
33
+ [`scietex.hal.current_amplifier`](https://github.com/bond-anton/scietex.hal.current_amplifier),
34
+ selected by entry point name in the service configuration. The service polls
35
+ each board on its own long-lived task, publishes readings to a per-board Valkey
36
+ stream, and exposes control tasks (`amplifier:read`, `amplifier:status`,
37
+ `amplifier:set-feedback-resistor`, …) on the data lane.
38
+
39
+ ## Supported boards
40
+
41
+ | Board | Vendor | MCU | Bus | Driver entry points |
42
+ | --- | --- | --- | --- | --- |
43
+ | EBIC Preamp v1.0 | SCIETEX.RU | STM32F411 | Modbus RTU over RS485 | `ebic_modbus_v1`, `ebic_modbus_v1_preamp` |
44
+
45
+ `ebic_modbus_v1` is the semantic driver: it maps the relay coils onto feedback
46
+ resistor, boost gain, DC-bias enable and compensation enable, and decodes the
47
+ coil pattern into an `AmplifierState`. `ebic_modbus_v1_preamp` is the raw relay
48
+ driver underneath it: it exposes the coils and the communication settings, but
49
+ its `read_data()` returns an empty snapshot, so a board configured with it
50
+ publishes rows of `None`. Use `ebic_modbus_v1` unless you are driving the
51
+ relays directly.
52
+
53
+ ## Configuration
54
+
55
+ The service reads `current_amplifier/current_amplifier.yml` under the framework
56
+ config directory (`SCIETEX_CONFIG_DIR`, default `/config` in the container). The
57
+ file is created with defaults on first start.
58
+
59
+ ```yaml
60
+ amplifiers:
61
+ - label: photodiode_a
62
+ driver: ebic_modbus_v1
63
+ address: 2
64
+ poll_interval: 1.0
65
+ # relay_settle_s: 0.008
66
+ serial:
67
+ port: /dev/ttyUSB0
68
+ baudrate: 19200
69
+ bytesize: 8
70
+ parity: N
71
+ stopbits: 1
72
+ timeout: 1.0
73
+ framer: RTU
74
+ - label: ion_collector_b
75
+ driver: ebic_modbus_v1
76
+ address: 3
77
+ poll_interval: 2.0
78
+ serial:
79
+ port: /dev/ttyUSB1
80
+ failure_threshold: 3
81
+ stream_maxlen: 10000
82
+ ```
83
+
84
+ | Field | Default | Meaning |
85
+ | --- | --- | --- |
86
+ | `label` | required | Unique label; the registry key and the `{label}` segment of the stream key. |
87
+ | `serial.port` | required | Serial device the board is wired to, e.g. `/dev/ttyUSB0`. Each board entry carries its own connection settings. |
88
+ | `baudrate` | `19200` | RS485 baud rate; must be positive. The EBIC firmware default is 19200. |
89
+ | `bytesize` | `8` | Data bits, one of 5–8. |
90
+ | `parity` | `N` | Parity, one of `N`, `E`, `O`. |
91
+ | `stopbits` | `1` | Stop bits, one of `1`, `1.5`, `2`. |
92
+ | `timeout` | `1.0` | Per-operation serial timeout in seconds; must be non-negative. |
93
+ | `framer` | `RTU` | Modbus framing, `RTU` or `ASCII`. |
94
+ | `driver` | `ebic_modbus_v1` | HAL driver entry point name (see the table above). |
95
+ | `address` | `2` | Modbus slave address, 1–247. Every board on one segment needs a distinct one; the EBIC default is 2. |
96
+ | `poll_interval` | `1.0` | Seconds between reads; must be positive. |
97
+ | `relay_settle_s` | driver default (`0.008`) | Optional. Delay after a make write before the dependent relays move. Forwarded only when set and only if the driver accepts the keyword. |
98
+ | `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. |
99
+ | `stream_maxlen` | `10000` | Entries retained per board's readings stream. |
100
+
101
+ A configuration change at runtime is **restart-required**: the board registry is
102
+ not live-rebuilt.
103
+
104
+ ## Tasks
105
+
106
+ Control lane:
107
+
108
+ | Task | Lane | Required capability | Arguments | Result |
109
+ | --- | --- | --- | --- | --- |
110
+ | `monitoring:start` | control | none | `{"label": "photodiode_a"}` or empty for every board | `MonitoringResponse{action, labels, monitoring}` |
111
+ | `monitoring:stop` | control | none | `{"label": "photodiode_a"}` or empty for every board | `MonitoringResponse{action, labels, monitoring}` |
112
+
113
+ Data lane:
114
+
115
+ | Task | Lane | Required capability | Arguments | Result |
116
+ | --- | --- | --- | --- | --- |
117
+ | `amplifier:list` | data | none | `{}` | `AmplifierListResponse{service, instance_id, amplifiers[]}` with each board's `controls` summary |
118
+ | `amplifier:status` | data | none | `{"label": "photodiode_a"}` or empty for every board | `AmplifierStatusResponse{amplifiers[]}` from cached poll counters; no bus I/O |
119
+ | `amplifier:read` | data | none | `{"label": "photodiode_a"}` | `AmplifierCommandResponse{label, task, state}` from a live `read_data()` |
120
+ | `amplifier:set-feedback-resistor` | data | `can_select_preamp` and `resistance_ohm` in the board's enumerated Rf options | `{"label": ..., "resistance_ohm": 1000000}` | echo `resistance_ohm` plus the state read back after the switch (`readback_error` if the read failed after the write landed) |
121
+ | `amplifier:set-boost-gain` | data | `can_set_boost` and `gain` in the board's accepted gains | `{"label": ..., "gain": 10}` | echo `gain` |
122
+ | `amplifier:set-dc-bias` | data | `capabilities.dc_bias.enabled.write` | `{"label": ..., "enabled": true}` | echo `enabled` |
123
+ | `amplifier:set-dc-bias-level` | data | `capabilities.dc_bias.level.write` | `{"label": ..., "level": 0.5}` | echo `level` |
124
+ | `amplifier:set-bandwidth` | data | `can_set_bandwidth` | `{"label": ..., "bandwidth_hz": 100000}` | echo `bandwidth_hz` |
125
+ | `amplifier:set-filter` | data | `capabilities.filters.enabled.write` and `filter_type` in `capabilities.filters.types`; `capabilities.filters.cutoff.write` when `low_cutoff_hz` or `high_cutoff_hz` is given | `{"label": ..., "filter_type": "low_pass", "enabled": true, "low_cutoff_hz": 10, "high_cutoff_hz": null}` | echo `filter_type`, `enabled`, `low_cutoff_hz`, `high_cutoff_hz` |
126
+ | `amplifier:set-input-compensation` | data | `capabilities.input_compensation.enabled.write` | `{"label": ..., "enabled": true}` | echo `enabled` |
127
+ | `amplifier:set-input-compensation-level` | data | `capabilities.input_compensation.level.write` | `{"label": ..., "level": 1.0}` | echo `level` |
128
+ | `amplifier:set-inverted` | data | `can_set_invert_output` | `{"label": ..., "inverted": true}` | echo `inverted` |
129
+
130
+ Every write task is gated on the board's declared capabilities, so an
131
+ unsupported command is rejected as `UNSUPPORTED` before the driver is called.
132
+ `amplifier:read`, `amplifier:status` and `amplifier:list` are ungated. Error
133
+ codes are `INVALID_PAYLOAD`, `UNKNOWN_AMPLIFIER`, `UNSUPPORTED`,
134
+ `INVALID_PARAMETERS`, `AMPLIFIER_COMMAND_FAILED` (retryable for bus and relay
135
+ failures), and `MONITORING_FAILED`.
136
+
137
+ ## Valkey keys
138
+
139
+ - Readings stream:
140
+ `scietex:{service}:{instance_id}:current_amplifier:{label}:readings` with
141
+ fields `ts` (ISO-8601 UTC), `output_voltage_v`, `output_current_a`,
142
+ `input_current_a`, `boost_gain`, `feedback_resistor_ohm`, `preamp_range`
143
+ (same value as `feedback_resistor_ohm`; both spellings decode), `dc_bias_level`,
144
+ `bandwidth_hz`, `input_compensation_level`, `dc_bias_enabled`,
145
+ `input_compensation_enabled`, `input_overcurrent`, `output_overload`,
146
+ `inverted`, `filters` (JSON array of filter settings), and
147
+ `transconductance_v_per_a` (`Rf × gain` when both are known). A field the
148
+ board does not report is written as an empty value, which is distinguishable
149
+ from a reported `0.0`; a tri-state flag uses `1`/`0`, with empty for unknown.
150
+ - Telemetry: `scietex:{service}:{instance_id}:telemetry`, msgpack, pushed every
151
+ 2 s with a 6 s TTL. Top level: `state` (`running`, `idle`, `unconfigured`),
152
+ `service`, `instance_id`, `configured`, `poll_enabled`, `timestamp`,
153
+ `devices`, `schema_version` (`1`). `devices` is keyed by board label and
154
+ carries `state` (`error`, `monitoring`, `ok`, `idle`), `driver`, `connected`,
155
+ `monitoring`, `ts`, every `AmplifierState` field, `reading_count`,
156
+ `error_count`, `last_error`, `last_ok`, and `transconductance_v_per_a`.
157
+
158
+ ## Domain notes
159
+
160
+ - **Rf must be one of the eight board resistors** — 1 kΩ, 10 kΩ, 100 kΩ, 1 MΩ,
161
+ 10 MΩ, 100 MΩ, 1 GΩ, 10 GΩ. The handler rejects anything else as
162
+ `INVALID_PARAMETERS` from the board's own option list, so a typo never reaches
163
+ the bus; the driver repeats the check before touching hardware.
164
+ - **Rf switching is make-before-break.** The target resistor engages before the
165
+ old one releases and the driver waits `relay_settle_s` (8 ms by default)
166
+ before the channel relays move, so the loop stays closed. It is safe to change
167
+ while measuring; expect a brief parallel-Rf transient.
168
+ - **Boost gains are exactly 1, 3, 10, 30.** A gain change also engages the new
169
+ relay before releasing the old one, so a `×10 ↔ ×30` switch passes through
170
+ about `×39` for the settle window. **Never change gain during a
171
+ measurement.**
172
+ - **Five capability-gated writes are unimplemented on EBIC.** The board exposes
173
+ Rf, boost gain, DC-bias enable and compensation enable; the capability tree
174
+ reports bandwidth, filters, DC-bias level, compensation level and output
175
+ inversion as unsupported, so `amplifier:set-bandwidth`, `amplifier:set-filter`,
176
+ `amplifier:set-dc-bias-level`, `amplifier:set-input-compensation-level` and
177
+ `amplifier:set-inverted` always answer `UNSUPPORTED` on this board.
178
+ - **One serial bus per port.** Boards that share a port share one RS485
179
+ segment, and every poll is a separate Modbus transaction on that bus, so N
180
+ boards on one segment see readings roughly N × `poll_interval` apart. Give a
181
+ board its own port when the timing matters.
182
+ - **Readings are per board, not per channel.** One entry in `amplifiers` is one
183
+ amplifier channel; the stream key and every task address it by `label`.
184
+
185
+ ## Running
186
+
187
+ ```bash
188
+ start-current-amplifier-service --conf-dir /etc/scietex
189
+ ```
190
+
191
+ Flags: `--conf-dir` (auto-resolved when omitted), `--service-name` (default
192
+ `$SCIETEX_SERVICE_NAME` or `CURRENT_AMPLIFIER`), `--logging-level` (default
193
+ `$SCIETEX_LOGGING_LEVEL` or `INFO`). It runs in the foreground until `SIGINT` or
194
+ `SIGTERM`.
195
+
196
+ ## Development
197
+
198
+ ```bash
199
+ python -m venv .venv
200
+ . .venv/bin/activate
201
+ pip install -e ../scietex.hal.current_amplifier -e ../scietex.hal.serial
202
+ pip install -e ".[dev,test,lint]"
203
+ ```
204
+
205
+ The HAL packages are installed from their checkouts, so an editable install is
206
+ the normal case; a plain `pip install -e ".[dev,test,lint]"` pulls the released
207
+ HAL versions from PyPI instead. `pytest.ini` and `pyproject.toml` put the
208
+ checkout on the import path, so the suite does not depend on an installed copy
209
+ of the service package.
210
+
211
+ ```bash
212
+ pytest
213
+ ruff check src tests
214
+ tox -l
215
+ ```
216
+
217
+ ## Container
218
+
219
+ ```bash
220
+ ./build_image.sh [--latest]
221
+ ```
222
+
223
+ Builds and pushes `registry.buro-nts.ru/scietex-current-amplifier-service:<version>`
224
+ for `linux/amd64` and `linux/arm64`. The image installs the released wheel from
225
+ PyPI, so publish the version first. The rootfs is read-only-safe: only the
226
+ mounted config volume is ever written.
227
+
228
+ ### Local build
229
+
230
+ ```bash
231
+ podman build -f Containerfile.local -t scietex-current-amplifier-service:local .
232
+ ```
233
+
234
+ `Containerfile.local` builds the same image from the working tree instead of the
235
+ index, so it works before a version is published — the release build above
236
+ installs `==${VERSION}` and fails until that version exists on PyPI. The release
237
+ path is unchanged.
238
+
239
+ ## Deployment
240
+
241
+ The board is reached through an RS485 USB adapter, so the container needs the
242
+ device and the adapter's group:
243
+
244
+ ```bash
245
+ podman run -d \
246
+ --name current-amplifier-service \
247
+ --device=/dev/ttyUSB0 \
248
+ --group-add dialout \
249
+ -v /etc/scietex:/config:Z \
250
+ --read-only \
251
+ registry.buro-nts.ru/scietex-current-amplifier-service:0.1.0
252
+ ```
253
+
254
+ `--device` exposes the adapter; `--group-add dialout` puts the container's
255
+ non-root user in the group that owns `/dev/ttyUSB*` on the host. Adjust the
256
+ group name when the host uses a different one (`ls -l /dev/ttyUSB0`). The config
257
+ file is created under `/config/current_amplifier/` on first start.
@@ -0,0 +1,231 @@
1
+ # scietex.current_amplifier_service
2
+
3
+ Scietex microservice daemon for current amplifier monitoring and control.
4
+
5
+ The service drives one or more EBIC v1.0 current-amplifier boards over Modbus
6
+ RTU. Each board is handled by a driver from
7
+ [`scietex.hal.current_amplifier`](https://github.com/bond-anton/scietex.hal.current_amplifier),
8
+ selected by entry point name in the service configuration. The service polls
9
+ each board on its own long-lived task, publishes readings to a per-board Valkey
10
+ stream, and exposes control tasks (`amplifier:read`, `amplifier:status`,
11
+ `amplifier:set-feedback-resistor`, …) on the data lane.
12
+
13
+ ## Supported boards
14
+
15
+ | Board | Vendor | MCU | Bus | Driver entry points |
16
+ | --- | --- | --- | --- | --- |
17
+ | EBIC Preamp v1.0 | SCIETEX.RU | STM32F411 | Modbus RTU over RS485 | `ebic_modbus_v1`, `ebic_modbus_v1_preamp` |
18
+
19
+ `ebic_modbus_v1` is the semantic driver: it maps the relay coils onto feedback
20
+ resistor, boost gain, DC-bias enable and compensation enable, and decodes the
21
+ coil pattern into an `AmplifierState`. `ebic_modbus_v1_preamp` is the raw relay
22
+ driver underneath it: it exposes the coils and the communication settings, but
23
+ its `read_data()` returns an empty snapshot, so a board configured with it
24
+ publishes rows of `None`. Use `ebic_modbus_v1` unless you are driving the
25
+ relays directly.
26
+
27
+ ## Configuration
28
+
29
+ The service reads `current_amplifier/current_amplifier.yml` under the framework
30
+ config directory (`SCIETEX_CONFIG_DIR`, default `/config` in the container). The
31
+ file is created with defaults on first start.
32
+
33
+ ```yaml
34
+ amplifiers:
35
+ - label: photodiode_a
36
+ driver: ebic_modbus_v1
37
+ address: 2
38
+ poll_interval: 1.0
39
+ # relay_settle_s: 0.008
40
+ serial:
41
+ port: /dev/ttyUSB0
42
+ baudrate: 19200
43
+ bytesize: 8
44
+ parity: N
45
+ stopbits: 1
46
+ timeout: 1.0
47
+ framer: RTU
48
+ - label: ion_collector_b
49
+ driver: ebic_modbus_v1
50
+ address: 3
51
+ poll_interval: 2.0
52
+ serial:
53
+ port: /dev/ttyUSB1
54
+ failure_threshold: 3
55
+ stream_maxlen: 10000
56
+ ```
57
+
58
+ | Field | Default | Meaning |
59
+ | --- | --- | --- |
60
+ | `label` | required | Unique label; the registry key and the `{label}` segment of the stream key. |
61
+ | `serial.port` | required | Serial device the board is wired to, e.g. `/dev/ttyUSB0`. Each board entry carries its own connection settings. |
62
+ | `baudrate` | `19200` | RS485 baud rate; must be positive. The EBIC firmware default is 19200. |
63
+ | `bytesize` | `8` | Data bits, one of 5–8. |
64
+ | `parity` | `N` | Parity, one of `N`, `E`, `O`. |
65
+ | `stopbits` | `1` | Stop bits, one of `1`, `1.5`, `2`. |
66
+ | `timeout` | `1.0` | Per-operation serial timeout in seconds; must be non-negative. |
67
+ | `framer` | `RTU` | Modbus framing, `RTU` or `ASCII`. |
68
+ | `driver` | `ebic_modbus_v1` | HAL driver entry point name (see the table above). |
69
+ | `address` | `2` | Modbus slave address, 1–247. Every board on one segment needs a distinct one; the EBIC default is 2. |
70
+ | `poll_interval` | `1.0` | Seconds between reads; must be positive. |
71
+ | `relay_settle_s` | driver default (`0.008`) | Optional. Delay after a make write before the dependent relays move. Forwarded only when set and only if the driver accepts the keyword. |
72
+ | `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. |
73
+ | `stream_maxlen` | `10000` | Entries retained per board's readings stream. |
74
+
75
+ A configuration change at runtime is **restart-required**: the board registry is
76
+ not live-rebuilt.
77
+
78
+ ## Tasks
79
+
80
+ Control lane:
81
+
82
+ | Task | Lane | Required capability | Arguments | Result |
83
+ | --- | --- | --- | --- | --- |
84
+ | `monitoring:start` | control | none | `{"label": "photodiode_a"}` or empty for every board | `MonitoringResponse{action, labels, monitoring}` |
85
+ | `monitoring:stop` | control | none | `{"label": "photodiode_a"}` or empty for every board | `MonitoringResponse{action, labels, monitoring}` |
86
+
87
+ Data lane:
88
+
89
+ | Task | Lane | Required capability | Arguments | Result |
90
+ | --- | --- | --- | --- | --- |
91
+ | `amplifier:list` | data | none | `{}` | `AmplifierListResponse{service, instance_id, amplifiers[]}` with each board's `controls` summary |
92
+ | `amplifier:status` | data | none | `{"label": "photodiode_a"}` or empty for every board | `AmplifierStatusResponse{amplifiers[]}` from cached poll counters; no bus I/O |
93
+ | `amplifier:read` | data | none | `{"label": "photodiode_a"}` | `AmplifierCommandResponse{label, task, state}` from a live `read_data()` |
94
+ | `amplifier:set-feedback-resistor` | data | `can_select_preamp` and `resistance_ohm` in the board's enumerated Rf options | `{"label": ..., "resistance_ohm": 1000000}` | echo `resistance_ohm` plus the state read back after the switch (`readback_error` if the read failed after the write landed) |
95
+ | `amplifier:set-boost-gain` | data | `can_set_boost` and `gain` in the board's accepted gains | `{"label": ..., "gain": 10}` | echo `gain` |
96
+ | `amplifier:set-dc-bias` | data | `capabilities.dc_bias.enabled.write` | `{"label": ..., "enabled": true}` | echo `enabled` |
97
+ | `amplifier:set-dc-bias-level` | data | `capabilities.dc_bias.level.write` | `{"label": ..., "level": 0.5}` | echo `level` |
98
+ | `amplifier:set-bandwidth` | data | `can_set_bandwidth` | `{"label": ..., "bandwidth_hz": 100000}` | echo `bandwidth_hz` |
99
+ | `amplifier:set-filter` | data | `capabilities.filters.enabled.write` and `filter_type` in `capabilities.filters.types`; `capabilities.filters.cutoff.write` when `low_cutoff_hz` or `high_cutoff_hz` is given | `{"label": ..., "filter_type": "low_pass", "enabled": true, "low_cutoff_hz": 10, "high_cutoff_hz": null}` | echo `filter_type`, `enabled`, `low_cutoff_hz`, `high_cutoff_hz` |
100
+ | `amplifier:set-input-compensation` | data | `capabilities.input_compensation.enabled.write` | `{"label": ..., "enabled": true}` | echo `enabled` |
101
+ | `amplifier:set-input-compensation-level` | data | `capabilities.input_compensation.level.write` | `{"label": ..., "level": 1.0}` | echo `level` |
102
+ | `amplifier:set-inverted` | data | `can_set_invert_output` | `{"label": ..., "inverted": true}` | echo `inverted` |
103
+
104
+ Every write task is gated on the board's declared capabilities, so an
105
+ unsupported command is rejected as `UNSUPPORTED` before the driver is called.
106
+ `amplifier:read`, `amplifier:status` and `amplifier:list` are ungated. Error
107
+ codes are `INVALID_PAYLOAD`, `UNKNOWN_AMPLIFIER`, `UNSUPPORTED`,
108
+ `INVALID_PARAMETERS`, `AMPLIFIER_COMMAND_FAILED` (retryable for bus and relay
109
+ failures), and `MONITORING_FAILED`.
110
+
111
+ ## Valkey keys
112
+
113
+ - Readings stream:
114
+ `scietex:{service}:{instance_id}:current_amplifier:{label}:readings` with
115
+ fields `ts` (ISO-8601 UTC), `output_voltage_v`, `output_current_a`,
116
+ `input_current_a`, `boost_gain`, `feedback_resistor_ohm`, `preamp_range`
117
+ (same value as `feedback_resistor_ohm`; both spellings decode), `dc_bias_level`,
118
+ `bandwidth_hz`, `input_compensation_level`, `dc_bias_enabled`,
119
+ `input_compensation_enabled`, `input_overcurrent`, `output_overload`,
120
+ `inverted`, `filters` (JSON array of filter settings), and
121
+ `transconductance_v_per_a` (`Rf × gain` when both are known). A field the
122
+ board does not report is written as an empty value, which is distinguishable
123
+ from a reported `0.0`; a tri-state flag uses `1`/`0`, with empty for unknown.
124
+ - Telemetry: `scietex:{service}:{instance_id}:telemetry`, msgpack, pushed every
125
+ 2 s with a 6 s TTL. Top level: `state` (`running`, `idle`, `unconfigured`),
126
+ `service`, `instance_id`, `configured`, `poll_enabled`, `timestamp`,
127
+ `devices`, `schema_version` (`1`). `devices` is keyed by board label and
128
+ carries `state` (`error`, `monitoring`, `ok`, `idle`), `driver`, `connected`,
129
+ `monitoring`, `ts`, every `AmplifierState` field, `reading_count`,
130
+ `error_count`, `last_error`, `last_ok`, and `transconductance_v_per_a`.
131
+
132
+ ## Domain notes
133
+
134
+ - **Rf must be one of the eight board resistors** — 1 kΩ, 10 kΩ, 100 kΩ, 1 MΩ,
135
+ 10 MΩ, 100 MΩ, 1 GΩ, 10 GΩ. The handler rejects anything else as
136
+ `INVALID_PARAMETERS` from the board's own option list, so a typo never reaches
137
+ the bus; the driver repeats the check before touching hardware.
138
+ - **Rf switching is make-before-break.** The target resistor engages before the
139
+ old one releases and the driver waits `relay_settle_s` (8 ms by default)
140
+ before the channel relays move, so the loop stays closed. It is safe to change
141
+ while measuring; expect a brief parallel-Rf transient.
142
+ - **Boost gains are exactly 1, 3, 10, 30.** A gain change also engages the new
143
+ relay before releasing the old one, so a `×10 ↔ ×30` switch passes through
144
+ about `×39` for the settle window. **Never change gain during a
145
+ measurement.**
146
+ - **Five capability-gated writes are unimplemented on EBIC.** The board exposes
147
+ Rf, boost gain, DC-bias enable and compensation enable; the capability tree
148
+ reports bandwidth, filters, DC-bias level, compensation level and output
149
+ inversion as unsupported, so `amplifier:set-bandwidth`, `amplifier:set-filter`,
150
+ `amplifier:set-dc-bias-level`, `amplifier:set-input-compensation-level` and
151
+ `amplifier:set-inverted` always answer `UNSUPPORTED` on this board.
152
+ - **One serial bus per port.** Boards that share a port share one RS485
153
+ segment, and every poll is a separate Modbus transaction on that bus, so N
154
+ boards on one segment see readings roughly N × `poll_interval` apart. Give a
155
+ board its own port when the timing matters.
156
+ - **Readings are per board, not per channel.** One entry in `amplifiers` is one
157
+ amplifier channel; the stream key and every task address it by `label`.
158
+
159
+ ## Running
160
+
161
+ ```bash
162
+ start-current-amplifier-service --conf-dir /etc/scietex
163
+ ```
164
+
165
+ Flags: `--conf-dir` (auto-resolved when omitted), `--service-name` (default
166
+ `$SCIETEX_SERVICE_NAME` or `CURRENT_AMPLIFIER`), `--logging-level` (default
167
+ `$SCIETEX_LOGGING_LEVEL` or `INFO`). It runs in the foreground until `SIGINT` or
168
+ `SIGTERM`.
169
+
170
+ ## Development
171
+
172
+ ```bash
173
+ python -m venv .venv
174
+ . .venv/bin/activate
175
+ pip install -e ../scietex.hal.current_amplifier -e ../scietex.hal.serial
176
+ pip install -e ".[dev,test,lint]"
177
+ ```
178
+
179
+ The HAL packages are installed from their checkouts, so an editable install is
180
+ the normal case; a plain `pip install -e ".[dev,test,lint]"` pulls the released
181
+ HAL versions from PyPI instead. `pytest.ini` and `pyproject.toml` put the
182
+ checkout on the import path, so the suite does not depend on an installed copy
183
+ of the service package.
184
+
185
+ ```bash
186
+ pytest
187
+ ruff check src tests
188
+ tox -l
189
+ ```
190
+
191
+ ## Container
192
+
193
+ ```bash
194
+ ./build_image.sh [--latest]
195
+ ```
196
+
197
+ Builds and pushes `registry.buro-nts.ru/scietex-current-amplifier-service:<version>`
198
+ for `linux/amd64` and `linux/arm64`. The image installs the released wheel from
199
+ PyPI, so publish the version first. The rootfs is read-only-safe: only the
200
+ mounted config volume is ever written.
201
+
202
+ ### Local build
203
+
204
+ ```bash
205
+ podman build -f Containerfile.local -t scietex-current-amplifier-service:local .
206
+ ```
207
+
208
+ `Containerfile.local` builds the same image from the working tree instead of the
209
+ index, so it works before a version is published — the release build above
210
+ installs `==${VERSION}` and fails until that version exists on PyPI. The release
211
+ path is unchanged.
212
+
213
+ ## Deployment
214
+
215
+ The board is reached through an RS485 USB adapter, so the container needs the
216
+ device and the adapter's group:
217
+
218
+ ```bash
219
+ podman run -d \
220
+ --name current-amplifier-service \
221
+ --device=/dev/ttyUSB0 \
222
+ --group-add dialout \
223
+ -v /etc/scietex:/config:Z \
224
+ --read-only \
225
+ registry.buro-nts.ru/scietex-current-amplifier-service:0.1.0
226
+ ```
227
+
228
+ `--device` exposes the adapter; `--group-add dialout` puts the container's
229
+ non-root user in the group that owns `/dev/ttyUSB*` on the host. Adjust the
230
+ group name when the host uses a different one (`ls -l /dev/ttyUSB0`). The config
231
+ file is created under `/config/current_amplifier/` on first start.
@@ -0,0 +1,46 @@
1
+ [project]
2
+ name = "scietex.current_amplifier_service"
3
+ description = "Scietex microservice daemon for current 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.current_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.current_amplifier_service/issues"
22
+ "Homepage" = "https://github.com/bond-anton/scietex.current_amplifier_service"
23
+
24
+ [project.scripts]
25
+ start-current-amplifier-service = "scietex.current_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.current_amplifier_service.version.__version__" }
41
+
42
+ [tool.setuptools.package-data]
43
+ "scietex.current_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
+ """Current amplifier service."""
2
+
3
+ from .version import __version__
4
+
5
+ __all__ = ["__version__"]