Sensor

This module provides base classes to create sensors (gps, gyroscope, audio, video...) and aggregate/push their data towards cryptainers.

Aggregation of records into binary archives

class wacryptolib.sensor.TarfileRecordAggregator(cryptainer_storage, max_duration_s)

Bases: TimeLimitedAggregatorMixin

This class allows sensors to aggregate file-like records of data in memory.

It is in charge of building the filenames of tar records, as well as of completed tarfiles.

Public methods of this class are thread-safe.

add_record(sensor_name, from_datetime, to_datetime, extension, payload)

Add the provided data to the tarfile, using associated metadata.

If, despite included timestamps, several records end up having the exact same name, the last one will have priority when extracting the tarfile, but all of them will be stored in it anyway.

Parameters:
  • sensor_name (str) -- slug label for the sensor

  • from_datetime (datetime) -- start time of the recording

  • to_datetime (datetime) -- end time of the recording

  • extension (str) -- file extension, starting with a dot

  • payload (bytes) -- bytestring of audio/video/other data

finalize_tarfile()

Return the content of current tarfile as a bytestring, possibly empty, and reset the current tarfile.

static read_tarfile_from_bytestring(payload)

Create a readonly TarFile instance from the provided bytestring.

Base classes for poller/pusher sensors

class wacryptolib.sensor.JsonDataAggregator(tarfile_aggregator, sensor_name, max_duration_s)

Bases: TimeLimitedAggregatorMixin

This class allows sensors to aggregate dicts of data, which are periodically pushed as a json bytestring to the underlying TarfileRecordAggregator.

Public methods of this class are thread-safe.

add_data(data_dict)

Flush current data to the tarfile if needed, and append data_dict to the queue.

flush_dataset()

Force the flushing of current data to the tarfile (e.g. when terminating the service).

class wacryptolib.sensor.PeriodicValuePoller(json_aggregator, **kwargs)

Bases: PeriodicValueMixin, PeriodicTaskHandler

This class runs a function at a specified interval, and pushes its result to a json aggregator.

Simultaneous management of multiple sensors

class wacryptolib.sensor.SensorManager(sensors)

Bases: TaskRunnerStateMachineBase

Manage a group of sensors for simultaneous starts/stops.

The underlying aggregators are not supposed to be directly impacted by these operations - they must be flushed separately.

join()

Wait for the periodic system to really finish running. Does nothing if periodic system is already stopped.

start()

Start the periodic system which will poll or push the value.

stop()

Request the periodic system to stop as soon as possible.