cremalink.local_server_app.state module

This module defines the state management for the local server application. It centralizes all runtime data, including device configuration, cryptographic keys, command queues, and the latest received device data.

class cremalink.local_server_app.state.LocalServerState(settings: ServerSettings, logger)[source]

Bases: object

Manages the runtime state of the local server in a thread-safe manner.

This class acts as a state machine, holding all information related to the device connection, including credentials, cryptographic session keys, pending commands, and the most recent data snapshots (monitor and properties). An asyncio.Lock is used to prevent race conditions when accessing state from different asynchronous tasks.

async configure(dsn: str, device_ip: str, lan_key: str, device_scheme: str = 'https', monitor_property_name: str | None = None) None[source]

Configures the state with new device details and resets the session. If the device details are unchanged, this is a no-op.

async get_property_value(property_name: str) Any | None[source]

Retrieves a single property value from the last known snapshot.

async handle_datapoint(decrypted_json: dict) None[source]

Processes a decrypted data payload from the device, updating the appropriate data snapshot (properties or monitor).

async init_crypto(random_1: str, time_1: str | int) None[source]

Derives and initializes all session keys using values from the key exchange.

is_configured() bool[source]

Returns True if the server has been configured with device details.

keys_ready() bool[source]

Returns True if the cryptographic session keys have been derived.

log(event: str, details: dict | None = None) None[source]

Convenience method for logging with redacted details.

async next_command_payload() Dict[str, Any][source]

Retrieves the next command from the queue for sending to the device. If the queue is empty, it returns an empty “heartbeat” payload.

async queue_command(command: str) None[source]

Adds a high-level device command to the outgoing queue.

async queue_monitor() None[source]

Adds a request for the device’s monitoring status to the queue.

async queue_properties() None[source]

Adds a request for all device properties to the queue.

async rekey() None[source]

Resets cryptographic keys and session state to force a new key exchange.

async set_registered(value: bool) None[source]
async snapshot_monitor() Dict[str, Any][source]

Returns the latest monitoring data snapshot.

async snapshot_properties() Dict[str, Any][source]

Returns the latest properties data snapshot.