auto-trainer-api 0.9.22__tar.gz → 0.9.23__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.
- auto_trainer_api-0.9.23/.gitignore +43 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/PKG-INFO +8 -5
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/README.md +7 -4
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/README.md +30 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/pyproject.toml +31 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/__init__.py +0 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/__init__.py +3 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/autotrainer_device.py +212 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/client/__init__.py +2 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/client/__main__.py +3 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/client/cli_app.py +352 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/client/simulator.py +207 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/remote/__init__.py +1 -0
- auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/remote/__main__.py +3 -0
- {auto_trainer_api-0.9.22/scripts → auto_trainer_api-0.9.23/packages/auto-trainer-api-tools/src/autotrainer/tools/api/remote}/remote_console.py +3 -4
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/pyproject.toml +4 -1
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_detector_kind.py +1 -1
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_event_kind.py +39 -12
- auto_trainer_api-0.9.23/uv.lock +820 -0
- auto_trainer_api-0.9.22/.gitignore +0 -355
- auto_trainer_api-0.9.22/scripts/client_application.py +0 -231
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/.gitattributes +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/.github/workflows/publish.yml +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/__init__.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_alarm_kind.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_application_mode.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_event.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_options.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_system_configuration.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_system_status.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/api_training_mode.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/bridge/__init__.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/bridge/__main__.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/bridge/bridge_app.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/bridge/bridge_service.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/command/__init__.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/command/api_command.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/command/api_command_request.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/command/api_command_request_response.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/command/api_command_request_result.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/command/command_request_delegate.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/rpc_service.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/telemetry/__init__.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/telemetry/open_telemetry_service.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/util.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/zeromq/__init__.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/src/autotrainer/api/zeromq/zeromq_api_service.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/tests/test_api_basics.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/tests/test_api_event.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/tests/test_bridge_service.py +0 -0
- {auto_trainer_api-0.9.22 → auto_trainer_api-0.9.23}/tests/test_resolve_enum.py +0 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
.idea
|
|
2
|
+
|
|
3
|
+
# Byte-compiled / optimized / DLL files
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[cod]
|
|
6
|
+
*$py.class
|
|
7
|
+
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
# Usually these files are written by a python script from a template
|
|
33
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
34
|
+
*.manifest
|
|
35
|
+
*.spec
|
|
36
|
+
|
|
37
|
+
site/
|
|
38
|
+
|
|
39
|
+
# Installer logs
|
|
40
|
+
pip-log.txt
|
|
41
|
+
pip-delete-this-directory.txt
|
|
42
|
+
|
|
43
|
+
.DS_Store
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: auto-trainer-api
|
|
3
|
-
Version: 0.9.
|
|
3
|
+
Version: 0.9.23
|
|
4
4
|
Summary: API for interfacing with the core acquisition process via platform and language agnostic message queues.
|
|
5
5
|
License: AGPL-3.0-only
|
|
6
6
|
Classifier: Operating System :: OS Independent
|
|
@@ -120,13 +120,16 @@ into a larger application. Requires the `bridge` optional dependency group (`pi
|
|
|
120
120
|
|
|
121
121
|
## Tools
|
|
122
122
|
|
|
123
|
+
The interactive tools live in the separate `auto-trainer-api-tools` distribution (which requires Python 3.10+);
|
|
124
|
+
the core `auto-trainer-api` library remains Python 3.8 compatible.
|
|
125
|
+
|
|
123
126
|
### Client Application
|
|
124
|
-
`
|
|
127
|
+
`autotrainer.tools.api.client` starts an interactive process that enables the `RpcService` as a "real" autotrainer
|
|
125
128
|
application would. It generates heartbeat events, periodically publishes system status events, and responds to
|
|
126
129
|
commands (`GET_CONFIGURATION`, `GET_STATUS`). Run with:
|
|
127
130
|
|
|
128
131
|
```
|
|
129
|
-
python
|
|
132
|
+
python -m autotrainer.tools.api.client
|
|
130
133
|
```
|
|
131
134
|
|
|
132
135
|
The following interactive commands are available:
|
|
@@ -147,11 +150,11 @@ This is primarily useful for testing remote applications/services without runnin
|
|
|
147
150
|
acquisition application.
|
|
148
151
|
|
|
149
152
|
### Remote Console
|
|
150
|
-
`
|
|
153
|
+
`autotrainer.tools.api.remote` starts an interactive process that connects to an `RpcService` instance in the same manner
|
|
151
154
|
full remote management services would. Run with:
|
|
152
155
|
|
|
153
156
|
```
|
|
154
|
-
python
|
|
157
|
+
python -m autotrainer.tools.api.remote [-H <host>]
|
|
155
158
|
```
|
|
156
159
|
|
|
157
160
|
It supports the `-H`/`--host` argument to specify a remote host address (defaults to `127.0.0.1`).
|
|
@@ -95,13 +95,16 @@ into a larger application. Requires the `bridge` optional dependency group (`pi
|
|
|
95
95
|
|
|
96
96
|
## Tools
|
|
97
97
|
|
|
98
|
+
The interactive tools live in the separate `auto-trainer-api-tools` distribution (which requires Python 3.10+);
|
|
99
|
+
the core `auto-trainer-api` library remains Python 3.8 compatible.
|
|
100
|
+
|
|
98
101
|
### Client Application
|
|
99
|
-
`
|
|
102
|
+
`autotrainer.tools.api.client` starts an interactive process that enables the `RpcService` as a "real" autotrainer
|
|
100
103
|
application would. It generates heartbeat events, periodically publishes system status events, and responds to
|
|
101
104
|
commands (`GET_CONFIGURATION`, `GET_STATUS`). Run with:
|
|
102
105
|
|
|
103
106
|
```
|
|
104
|
-
python
|
|
107
|
+
python -m autotrainer.tools.api.client
|
|
105
108
|
```
|
|
106
109
|
|
|
107
110
|
The following interactive commands are available:
|
|
@@ -122,11 +125,11 @@ This is primarily useful for testing remote applications/services without runnin
|
|
|
122
125
|
acquisition application.
|
|
123
126
|
|
|
124
127
|
### Remote Console
|
|
125
|
-
`
|
|
128
|
+
`autotrainer.tools.api.remote` starts an interactive process that connects to an `RpcService` instance in the same manner
|
|
126
129
|
full remote management services would. Run with:
|
|
127
130
|
|
|
128
131
|
```
|
|
129
|
-
python
|
|
132
|
+
python -m autotrainer.tools.api.remote [-H <host>]
|
|
130
133
|
```
|
|
131
134
|
|
|
132
135
|
It supports the `-H`/`--host` argument to specify a remote host address (defaults to `127.0.0.1`).
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# auto-trainer-api-tools
|
|
2
|
+
|
|
3
|
+
Interactive client and remote console tools for the [auto-trainer-api](../../README.md) library.
|
|
4
|
+
|
|
5
|
+
These tools are split into their own distribution so they can require a newer Python (>=3.10)
|
|
6
|
+
and heavier UI dependencies without constraining the core `auto-trainer-api` library, which
|
|
7
|
+
remains compatible with Python 3.8.
|
|
8
|
+
|
|
9
|
+
## Tools
|
|
10
|
+
|
|
11
|
+
### Client application
|
|
12
|
+
Acts as a proxy/example "real" autotrainer application: enables the `RpcService`, publishes
|
|
13
|
+
periodic system status events, and responds to command requests.
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
python -m autotrainer.tools.api.client
|
|
17
|
+
# or, via the installed console script:
|
|
18
|
+
at-client
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Remote console
|
|
22
|
+
Connects to an `RpcService` instance the way a remote dashboard/management service would.
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
python -m autotrainer.tools.api.remote [-H <host>]
|
|
26
|
+
# or:
|
|
27
|
+
at-remote [-H <host>]
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
See the [main project README](../../README.md) for the full command reference.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[tool.hatch.build.targets.wheel]
|
|
6
|
+
packages = ["src/autotrainer"]
|
|
7
|
+
namespaces = true
|
|
8
|
+
|
|
9
|
+
[project]
|
|
10
|
+
name = "auto-trainer-api-tools"
|
|
11
|
+
version = "0.9.23"
|
|
12
|
+
description = "Interactive client and remote console tools for the auto-trainer API."
|
|
13
|
+
license = { text = "AGPL-3.0-only" }
|
|
14
|
+
readme = "README.md"
|
|
15
|
+
requires-python = ">=3.10"
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Operating System :: OS Independent"
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"auto-trainer-api",
|
|
22
|
+
"pyzmq",
|
|
23
|
+
"textual>=0.60",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.scripts]
|
|
27
|
+
at-client = "autotrainer.tools.api.client.cli_app:main"
|
|
28
|
+
at-remote = "autotrainer.tools.api.remote.remote_console:main"
|
|
29
|
+
|
|
30
|
+
[tool.uv.sources]
|
|
31
|
+
auto-trainer-api = { workspace = true }
|
|
File without changes
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Shared, mutable model of an emulated autotrainer device.
|
|
3
|
+
|
|
4
|
+
:class:`AutotrainerDevice` is the single source of truth for the configuration and live status of a
|
|
5
|
+
simulated device. It mirrors the fields of :class:`ApiSystemConfiguration` and
|
|
6
|
+
:class:`ApiSystemStatus` (and their nested structures) and converts to those frozen API dataclasses
|
|
7
|
+
on demand, so both the client emulator and the remote console can share one representation.
|
|
8
|
+
|
|
9
|
+
The model is a plain tree of values with no parent/child back-references, so it never forms a
|
|
10
|
+
reference cycle (and is therefore safe to ``asdict``/serialize).
|
|
11
|
+
"""
|
|
12
|
+
import json
|
|
13
|
+
import math
|
|
14
|
+
from dataclasses import dataclass, field, replace
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import Any, ClassVar, Dict, List, Mapping, Optional
|
|
17
|
+
|
|
18
|
+
from autotrainer.api import ApiApplicationMode, ApiTrainingMode, ApiSystemConfiguration, ApiSystemStatus, \
|
|
19
|
+
ApiDetectorStatus, ApiDetectorKind, ApiAlarmStatus, ApiAlarmKind, ApiTunnelDeviceStatus, ApiPelletDeviceStatus
|
|
20
|
+
from autotrainer.api.api_system_status import ApiProjectStatus, ApiBehaviorStatus, ApiReachStatus, ApiAnimalStatus
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _default_detectors() -> Dict[ApiDetectorKind, ApiDetectorStatus]:
|
|
24
|
+
"""Every defined detector, enabled but not active."""
|
|
25
|
+
return {
|
|
26
|
+
kind: ApiDetectorStatus(detector_id=kind, is_active=False, is_enabled=True)
|
|
27
|
+
for kind in ApiDetectorKind
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _default_alarms() -> Dict[ApiAlarmKind, ApiAlarmStatus]:
|
|
32
|
+
"""Every defined alarm, enabled but not active, treated as a stop condition."""
|
|
33
|
+
return {
|
|
34
|
+
kind: ApiAlarmStatus(alarm_id=kind, is_active=False, is_enabled=True, is_stop_condition=True)
|
|
35
|
+
for kind in ApiAlarmKind
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _reach_status_from_json(counts: Optional[Mapping[str, Any]]) -> ApiReachStatus:
|
|
40
|
+
"""Build an ApiReachStatus from an animal file's pelletCounts block."""
|
|
41
|
+
counts = counts or {}
|
|
42
|
+
return ApiReachStatus(
|
|
43
|
+
pellets_presented=counts.get("presented", 0),
|
|
44
|
+
pellets_consumed=counts.get("consumed", 0),
|
|
45
|
+
reaches=counts.get("reaches", 0),
|
|
46
|
+
successful_reaches=counts.get("success_reaches", 0),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _animal_status_from_json(data: Mapping[str, Any]) -> ApiAnimalStatus:
|
|
51
|
+
"""Map a parsed ``<name>.json`` animal file onto an ApiAnimalStatus."""
|
|
52
|
+
dcs = (data.get("reach") or {}).get("pelletDcs") or {}
|
|
53
|
+
return ApiAnimalStatus(
|
|
54
|
+
identifier=data["id"],
|
|
55
|
+
name=data["name"],
|
|
56
|
+
dcs_send_x=dcs.get("x", 0.0),
|
|
57
|
+
dcs_send_y=dcs.get("y", 0.0),
|
|
58
|
+
dcs_send_z=dcs.get("z", 0.0),
|
|
59
|
+
target_y_limit=data.get("targetYLimit"),
|
|
60
|
+
reach_status_total=_reach_status_from_json(data.get("pelletCountsTotal")),
|
|
61
|
+
reach_status_day=_reach_status_from_json(data.get("pelletCountsDay")),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class AutotrainerDevice:
|
|
67
|
+
"""Mutable in-memory model of an emulated autotrainer device's configuration and status."""
|
|
68
|
+
|
|
69
|
+
# Valid range for magnet / baseline intensities.
|
|
70
|
+
MIN_INTENSITY: ClassVar[float] = 0.0
|
|
71
|
+
MAX_INTENSITY: ClassVar[float] = 100.0
|
|
72
|
+
|
|
73
|
+
# --- configuration ---
|
|
74
|
+
application_version: str = "2.0.1"
|
|
75
|
+
device_id: str = "agx999"
|
|
76
|
+
root_location: Path = field(default_factory=Path.home)
|
|
77
|
+
|
|
78
|
+
# --- live status ---
|
|
79
|
+
application_mode: ApiApplicationMode = ApiApplicationMode.IDLE
|
|
80
|
+
training_mode: ApiTrainingMode = ApiTrainingMode.MANUAL
|
|
81
|
+
animal: Optional[ApiAnimalStatus] = None
|
|
82
|
+
project: ApiProjectStatus = field(default_factory=lambda: ApiProjectStatus("day_path", 1))
|
|
83
|
+
tunnel_device: ApiTunnelDeviceStatus = field(default_factory=lambda: ApiTunnelDeviceStatus(0.0, True))
|
|
84
|
+
pellet_device: ApiPelletDeviceStatus = field(
|
|
85
|
+
default_factory=lambda: ApiPelletDeviceStatus(math.nan, 1.0, 2.3, 3, 4, 5, 50, 100)
|
|
86
|
+
)
|
|
87
|
+
behavior: ApiBehaviorStatus = field(default_factory=lambda: ApiBehaviorStatus(20.5, ApiReachStatus()))
|
|
88
|
+
detectors: Dict[ApiDetectorKind, ApiDetectorStatus] = field(default_factory=_default_detectors)
|
|
89
|
+
alarms: Dict[ApiAlarmKind, ApiAlarmStatus] = field(default_factory=_default_alarms)
|
|
90
|
+
|
|
91
|
+
_instance: ClassVar[Optional["AutotrainerDevice"]] = None
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def instance(cls) -> "AutotrainerDevice":
|
|
95
|
+
"""Return the process-wide singleton, creating it with defaults on first use."""
|
|
96
|
+
if cls._instance is None:
|
|
97
|
+
cls._instance = cls()
|
|
98
|
+
return cls._instance
|
|
99
|
+
|
|
100
|
+
# --- location helpers (derived from root_location) ---
|
|
101
|
+
@property
|
|
102
|
+
def configuration_location(self) -> Path:
|
|
103
|
+
return self.root_location / "Autotrainer"
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def data_location(self) -> Path:
|
|
107
|
+
return self.root_location / "Documents" / "RawDataLocal"
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def animals_location(self) -> Path:
|
|
111
|
+
return self.configuration_location / "animals"
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def logs_location(self) -> Optional[Path]:
|
|
115
|
+
return None
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def inference_model_location(self) -> Path:
|
|
119
|
+
return self.configuration_location / "inference_model"
|
|
120
|
+
|
|
121
|
+
# --- conversion to API messages ---
|
|
122
|
+
def to_api_system_configuration(self) -> ApiSystemConfiguration:
|
|
123
|
+
"""Convenience snapshot of the device's configuration for sending messages."""
|
|
124
|
+
return ApiSystemConfiguration(
|
|
125
|
+
application_version=self.application_version,
|
|
126
|
+
device_id=self.device_id,
|
|
127
|
+
configuration_location=str(self.configuration_location),
|
|
128
|
+
data_location=str(self.data_location),
|
|
129
|
+
animal_location=str(self.animals_location),
|
|
130
|
+
log_location=str(self.logs_location),
|
|
131
|
+
inference_model=str(self.inference_model_location),
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
def active_alarms(self) -> List[ApiAlarmStatus]:
|
|
135
|
+
"""The alarms currently in the active state."""
|
|
136
|
+
return [alarm for alarm in self.alarms.values() if alarm.is_active]
|
|
137
|
+
|
|
138
|
+
def to_api_system_status(self) -> ApiSystemStatus:
|
|
139
|
+
"""Snapshot of the device's current status for sending messages."""
|
|
140
|
+
return ApiSystemStatus(
|
|
141
|
+
application_mode=self.application_mode,
|
|
142
|
+
training_mode=self.training_mode,
|
|
143
|
+
animal=self.animal,
|
|
144
|
+
project=self.project,
|
|
145
|
+
alarms=list(self.alarms.values()),
|
|
146
|
+
detectors=list(self.detectors.values()),
|
|
147
|
+
pellet_device=self.pellet_device,
|
|
148
|
+
tunnel_device=self.tunnel_device,
|
|
149
|
+
behavior=self.behavior,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# --- mutation (kept in sync so the next status message matches) ---
|
|
153
|
+
def select_animal(self, name: str) -> ApiAnimalStatus:
|
|
154
|
+
"""Load ``<name>.json`` from animals_location and retain it as the current animal.
|
|
155
|
+
|
|
156
|
+
Raises ``ValueError`` if the file is missing, unreadable, or not a valid animal file.
|
|
157
|
+
"""
|
|
158
|
+
path = self.animals_location / f"{name}.json"
|
|
159
|
+
try:
|
|
160
|
+
with path.open() as handle:
|
|
161
|
+
data = json.load(handle)
|
|
162
|
+
except FileNotFoundError as err:
|
|
163
|
+
raise ValueError(f"no animal file: {path}") from err
|
|
164
|
+
except (OSError, json.JSONDecodeError) as err:
|
|
165
|
+
raise ValueError(f"could not read animal file {path}: {err}") from err
|
|
166
|
+
|
|
167
|
+
try:
|
|
168
|
+
animal = _animal_status_from_json(data)
|
|
169
|
+
except (KeyError, TypeError, ValueError) as err:
|
|
170
|
+
raise ValueError(f"invalid animal file {path}: {err}") from err
|
|
171
|
+
|
|
172
|
+
self.animal = animal
|
|
173
|
+
return animal
|
|
174
|
+
|
|
175
|
+
def set_detector(self, detector_id: ApiDetectorKind, is_active: bool,
|
|
176
|
+
is_enabled: Optional[bool] = None) -> ApiDetectorStatus:
|
|
177
|
+
current = self.detectors[detector_id]
|
|
178
|
+
updated = replace(
|
|
179
|
+
current,
|
|
180
|
+
is_active=is_active,
|
|
181
|
+
is_enabled=current.is_enabled if is_enabled is None else is_enabled,
|
|
182
|
+
)
|
|
183
|
+
self.detectors[detector_id] = updated
|
|
184
|
+
return updated
|
|
185
|
+
|
|
186
|
+
def set_alarm(self, alarm_id: ApiAlarmKind, is_active: bool, is_enabled: Optional[bool] = None,
|
|
187
|
+
is_auto_resume_enabled: Optional[bool] = None,
|
|
188
|
+
is_stop_condition: Optional[bool] = None) -> ApiAlarmStatus:
|
|
189
|
+
current = self.alarms[alarm_id]
|
|
190
|
+
updated = replace(
|
|
191
|
+
current,
|
|
192
|
+
is_active=is_active,
|
|
193
|
+
is_enabled=current.is_enabled if is_enabled is None else is_enabled,
|
|
194
|
+
is_auto_resume_enabled=(current.is_auto_resume_enabled if is_auto_resume_enabled is None
|
|
195
|
+
else is_auto_resume_enabled),
|
|
196
|
+
is_stop_condition=current.is_stop_condition if is_stop_condition is None else is_stop_condition,
|
|
197
|
+
)
|
|
198
|
+
self.alarms[alarm_id] = updated
|
|
199
|
+
return updated
|
|
200
|
+
|
|
201
|
+
def _validate_intensity(self, value: float) -> float:
|
|
202
|
+
if not self.MIN_INTENSITY <= value <= self.MAX_INTENSITY:
|
|
203
|
+
raise ValueError(f"intensity must be between {self.MIN_INTENSITY:g} and {self.MAX_INTENSITY:g}")
|
|
204
|
+
return value
|
|
205
|
+
|
|
206
|
+
def set_magnet_intensity(self, value: float) -> ApiTunnelDeviceStatus:
|
|
207
|
+
self.tunnel_device = replace(self.tunnel_device, magnet_intensity=self._validate_intensity(value))
|
|
208
|
+
return self.tunnel_device
|
|
209
|
+
|
|
210
|
+
def set_baseline_intensity(self, value: float) -> ApiBehaviorStatus:
|
|
211
|
+
self.behavior = replace(self.behavior, baseline_magnet_intensity=self._validate_intensity(value))
|
|
212
|
+
return self.behavior
|