Services¶
Python API reference for the tolomeo.services package.
A service is the top-level orchestrator: it owns the messaging strategy and hosts the plugins. Subclass the service that
matches your use case, or NATSService to start from the messaging layer alone. For how services, plugins, and messaging fit together, see
Architecture.
NATSService¶
tolomeo.services.NATSService
¶
Bases: ServiceBase
Service implementation using NATS as the messaging backend.
Subscribes to commands.{name}.req for inbound commands, publishes
responses to events.params, sensor data to events.data, and
OTA/status updates to events.infos. A heartbeat is published to
heartbeat.{name}.service at the configured cadence, which defaults to
every ten seconds.
Incoming messages are typically SenML-formatted JSON lists. Each record
must have an n field (command name) and a vs field (a
JSON-encoded string containing the command payload).
Example
Subclass NATSService to build a complete microservice, naming the
plugin it drives on the inner Meta::
class TemperatureService(NATSService):
class Meta:
plugin_class = TemperaturePlugin
Source code in tolomeo/services/nats_service.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
params_sync
property
¶
The sync context guarding the params-publication loop.
Meta
¶
ServiceSubject
¶
Bases: str, Enum
NATS subject constants for outbound publications.
Attributes:
| Name | Type | Description |
|---|---|---|
MESSAGES |
Subject for raw sensor data (SenML records). |
|
PARAMS |
Subject for command responses and parameter updates. |
|
INFO |
Subject for OTA status events and service info. |
Source code in tolomeo/services/nats_service.py
connect()
async
¶
Connect to NATS and subscribe to this service's command subject.
heartbeat(interval_ms=10000)
async
¶
Publish a heartbeat message at the configured interval.
Publishes an empty message to heartbeat.{name}.service every
interval_ms milliseconds. Intended to run as a managed
background task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval_ms
|
int
|
Heartbeat interval in milliseconds. Defaults to 10 000.
|
10000
|
Source code in tolomeo/services/nats_service.py
notify_params_changed()
async
¶
Wake params_sync_loop so the plugin topology and params get published.
Called by on_plugins_changed after every attach and detach, and
safe to call directly. Registration must already be complete, because
the loop reads self._plugins as soon as it wakes. Wake-ups that
arrive before the loop runs are merged into one publication.
Source code in tolomeo/services/nats_service.py
on_plugins_changed()
async
¶
params_sync_loop()
async
¶
Publish the plugin topology and params whenever the plugin registry changes.
Runs until cancelled, woken by notify_params_changed. Each wake-up
calls publish_plugins, then sync_params.
Source code in tolomeo/services/nats_service.py
publish_plugins()
async
¶
Publish the plugin topology as a plugins record.
Published also when no plugin is attached, so a consumer learns the set emptied. A failed publish is logged rather than raised, so it cannot take the loop down.
Source code in tolomeo/services/nats_service.py
remote_handler(msg)
async
¶
Handle an incoming NATS message from commands.{name}.req.
Decodes the SenML envelope, extracts the command name and payload,
dispatches via command_handler, and publishes the response to
events.params. When msg.reply is set it is passed through as the
NATS reply-to (request/reply correlation), not as an alternative subject.
A malformed envelope (an empty list, a non-object record, or a missing
or non-string n or vs) gets the "Invalid message format" reply.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Msg
|
The raw NATS message. |
required |
Source code in tolomeo/services/nats_service.py
setup()
async
¶
Set the service up, then announce its command catalog once.
The commands record goes out after the plugins are acquired, so
it reflects every command the service and its plugins expose. A
failure to build or publish it is logged rather than raised, so it
cannot abort start-up.
Source code in tolomeo/services/nats_service.py
start()
async
¶
Announce the plugins, schedule the params loop and heartbeat, and start the service.
Source code in tolomeo/services/nats_service.py
sync_params()
async
¶
Publish service-specific params after the plugin topology.
A hook for subclasses, called by params_sync_loop after
publish_plugins. Does nothing by default.
SensorService¶
tolomeo.services.SensorService
¶
Bases: NATSService
Sensor service publishing plugin readings and params over NATS.
Declares no plugin_class: SensorPlugin is abstract, so defaulting to it
would only defer the failure from construction to the first new_plugin
call. A deployment names its own concrete plugin.
Source code in tolomeo/services/sensor.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
data_sync
property
¶
The sync context guarding the reading-publication loop.
Meta
¶
async_data_handler(data)
async
¶
Queue a plugin's readings and wake data_sync_loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List
|
The datapoints pushed by the plugin. |
required |
Source code in tolomeo/services/sensor.py
async_info_handler(info)
async
¶
Publish a device-info dict to the INFO subject.
Unlike sensor data (SenML-wrapped in sync_data), info is
published as-is to events.infos.
Source code in tolomeo/services/sensor.py
data_sync_loop()
async
¶
Publish queued readings whenever a plugin signals new data.
Runs until cancelled. Waking is edge-triggered by async_data_handler
rather than polled, so an idle sensor costs nothing.
Source code in tolomeo/services/sensor.py
start()
async
¶
Start the reading loop, then the base loops, and block until the tasks finish.
stop()
async
¶
Disconnect every attached sensor, then stop the service.
Source code in tolomeo/services/sensor.py
sync_data()
async
¶
Drain the data queue, publishing each batch as SenML records.
A batch that fails to publish is logged and dropped rather than retried, so one bad batch cannot stall the queue behind it.
Source code in tolomeo/services/sensor.py
sync_params()
async
¶
Publish the output params of every plugin.
Skipped when no plugin is attached. A failed publish is logged rather than raised, so it cannot take the loop down.
Source code in tolomeo/services/sensor.py
wire_plugin(plugin)
¶
Route the plugin's data and info callbacks to this service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin
|
PluginBase
|
The plugin about to be attached. |
required |
Source code in tolomeo/services/sensor.py
SingleSensorService¶
tolomeo.services.SingleSensorService
¶
Bases: SensorService
Sensor service driving exactly one plugin, attached at setup.
Source code in tolomeo/services/sensor.py
OTAService¶
tolomeo.services.OTAService
¶
Bases: NATSService
Service driving a single OTA plugin and publishing its status.
The plugin pushes status as it changes rather than being polled, so an update's progress reaches subscribers without a sync loop.
Source code in tolomeo/services/ota.py
acquire_plugins()
async
¶
notify_ota_status(data)
async
¶
Publish an OTA status update on the service's info subject.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List
|
The status records pushed by the plugin. |
required |
Source code in tolomeo/services/ota.py
wire_plugin(plugin)
¶
Route the plugin's data callback to notify_ota_status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin
|
PluginBase
|
The plugin about to be attached. |
required |
BLESensorService¶
tolomeo.services.BLESensorService
¶
Bases: SensorService
Sensor service that discovers and registers BLE devices by address.
Everything here is device-agnostic: addresses come from the configuration
file, BLEConfigManager persists them, and parsing is delegated to the
plugin. Declaring no plugin_class is therefore deliberate — every BLE
sensor needs its own BLESensorPlugin subclass::
class MyBLEService(BLESensorService):
class Meta:
plugin_class = MyBLESensorPlugin
All registered devices share that one plugin class, so a deployment mixing two sensor models is not expressible yet.
Source code in tolomeo/services/bluetooth.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
config_manager
property
¶
The manager owning this service's device configuration.
new_config_manager(logger=None)
¶
Build the manager holding which devices to discover, and persisting them.
Reads and writes .devices.yml by default. Override to keep the device
list somewhere else.
Source code in tolomeo/services/bluetooth.py
register_device(address)
async
¶
Discover, attach and persist a BLE device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str
|
The BLE address to connect to. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the device was discovered, attached and written to the |
bool
|
configuration; False if discovery or attach failed. |
Source code in tolomeo/services/bluetooth.py
start()
async
¶
Register every configured device, then start the service.
Registration happens here rather than in setup because the device
addresses come from a file loaded here, and BLE discovery is slow
enough that doing it during setup would delay startup. A device
that fails to register is skipped, so one unreachable sensor does not
prevent the others from coming up.
Source code in tolomeo/services/bluetooth.py
unregister_device(address)
async
¶
Disconnect a BLE device and drop it from the configuration.
A device that fails to disconnect is put back in the registry, so a failed removal leaves the service's view of it intact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str
|
The BLE address to remove. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the device was disconnected and removed; False when it |
bool
|
was not registered or could not be disconnected. |