Pi

Pi.WmPiUtils module

Utility Module: Raspberry Pi Configuration

This module provides utility functions for reading configuration data for Raspberry Pi from JSON files.

WmPiUtils.read_pi_config_from_json(json_file)[source]

Read and parse the Raspberry Pi configuration from a JSON file.

This function reads a JSON file containing configuration data, parses it, and returns the resulting dictionary.

Parameters:

json_file (str) – The path to the JSON configuration file.

Returns:

A dictionary containing the parsed configuration data.

Return type:

dict

Raises:
  • FileNotFoundError – If the specified JSON file does not exist.

  • json.JSONDecodeError – If the file is not a valid JSON.

Pi.WmSensors module

Sensor Module

This module contains the base class Sensor and the specialized class IFM_O1 for interacting with different sensors, including configuration, calibration, and I2C communication.

class WmSensors.IFM_O1(sensor_dict, cnt_of_vals_per_meas)[source]

Bases: Sensor

Specialized class for the IFM_O1 sensor.

Parameters:
  • sensor_dict (dict) – A dictionary containing the sensor configuration.

  • cnt_of_vals_per_meas (int) – The number of values per measurement.

get_calib_data()[source]

Retrieve calibration data from the calibration file.

Returns:

A dictionary with calibration data (x and y values).

Return type:

dict

get_calib_value()[source]

Get the calibrated value by interpolating raw data.

Returns:

The calibrated value.

Return type:

float

Raises:

Exception – If an error occurs during calibration.

get_full_sensor_config()[source]

Retrieve the full configuration of the IFM_O1 sensor.

Returns:

A formatted string with the sensor configuration.

Return type:

str

get_i2c_address()[source]

Get the I2C address and related information for the sensor.

Returns:

A formatted string with I2C information.

Return type:

str

get_raw_voltage()[source]

Retrieve the raw voltage from the sensor via I2C.

Returns:

The raw voltage value.

Return type:

float

Raises:

Exception – If an error occurs during I2C communication.

perform_measurement()[source]

Perform a series of measurements for the sensor.

Returns:

A list of calibrated values from the measurements.

Return type:

list[float]

class WmSensors.Sensor(sensor_dict, cnt_of_vals_per_meas)[source]

Bases: object

Base class for sensors.

This class serves as a foundation for specialized sensor classes and provides common methods and attributes.

Parameters:
  • sensor_dict (dict) – A dictionary containing the sensor configuration.

  • cnt_of_vals_per_meas (int) – The number of values per measurement.

calibrate_data(raw_data)[source]

Calibrate raw data into meaningful values.

This method must be implemented in the dedicated sensor subclass.

Parameters:

raw_data (any) – The raw data to be calibrated.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

get_full_sensor_config()[source]

Retrieve the full configuration of the sensor.

This method must be implemented in the dedicated sensor subclass.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

perform_measurement()[source]

Perform a measurement for the sensor.

This method must be implemented in the dedicated sensor subclass.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

Pi.generate_key_pair module

Key Management Script

This script provides functionality for managing public and private key pairs. It includes: 1. Reading configuration data from a JSON file. 2. Creating a directory for storing pre-shared keys (PSK) if it does not exist. 3. Generating RSA key pairs and saving them to the specified PSK directory. 4. Converting the public key to SSH format for usage in authorized keys files.

Dependencies: - cryptography.hazmat: Provides cryptographic primitives for generating and handling keys. - json: For reading configuration data from JSON files. - os: For handling file paths and directories.

generate_key_pair.convert_to_ssh_format(public_key_path, name)[source]

Convert the public key to SSH format.

This function reads the public key and converts it to OpenSSH format, appending the provided name.

Parameters:
  • public_key_path (str) – The directory containing the public_key.pem file.

  • name (str) – The name to append to the SSH public key.

generate_key_pair.create_psk_path_if_not_exists(psk_path)[source]

Create the PSK directory if it does not already exist.

Parameters:

psk_path (str) – The path where the PSK directory should be created.

generate_key_pair.generate_key_pair(psk_path, key_size=2048)[source]

Generate an RSA key pair and save them to the PSK directory.

The private key is saved as private_key.pem, and the public key as public_key.pem.

Parameters:
  • psk_path (str) – The directory where the keys will be saved.

  • key_size (int) – The size of the RSA key in bits. Default is 2048.

generate_key_pair.read_config_json()[source]

Read configuration data from the JSON file.

This function reads the PSK path and name from the configuration file.

Returns:

A tuple containing the PSK path and name.

Return type:

tuple[str, str]

Raises:
  • FileNotFoundError – If the configuration file does not exist.

  • json.JSONDecodeError – If the configuration file is not valid JSON.

Pi.datacrawler module

Data Crawler

This script is a data crawler designed to: 1. Create sensor instances from a configuration file. 2. Retrieve measurement values from the sensors. 3. Store the measurement values in temporary storage. 4. Continuously repeat the process at defined intervals.

datacrawler.get_sensors_from_config()[source]

Create sensor instances from the configuration file.

This function reads the sensor configuration and initializes sensor objects.

Returns:

A list of initialized sensor objects.

Return type:

list[Sensor]

datacrawler.measurement()[source]

Perform a measurement for all sensors and save the results.

This function iterates over all sensors, retrieves measurement values, and saves the values to temporary storage with a timestamp.

datacrawler.save_values_to_temp_storage(s, vals, now)[source]

Save measurement values to temporary storage.

This function saves the measurement data for a sensor in a JSON file in the specified temporary storage directory.

Parameters:
  • s (Sensor) – The sensor object.

  • vals (list) – The measurement values.

  • now (datetime.datetime) – The timestamp of the measurement.

Pi.datatransmitter module

This script processes measurement data stored in temporary storage, signs it, and sends it to a specified API.

Steps: 1. Read data from temporary storage. 2. Build a full payload. 3. Sign the data. 4. Send a request to the API.

Configuration: The script reads configuration settings from a config.json file located in predefined paths.

Modules: - psk_sign: Used for signing data with a private RSA key. - WmPiUtils: Utility functions for reading configuration files.

Dependencies: - requests: For sending HTTP POST requests to the API. - json, os, glob, datetime: Standard libraries for file handling and processing.

datatransmitter.build_payload(meas_file)[source]

Build the payload (pl) by reading, augmenting, and signing measurement data.

Parameters:

meas_file (str) – Path to the measurement JSON file.

Returns:

Signed payload containing measurement data.

Return type:

dict

datatransmitter.list_files_from_storage()[source]

List all JSON files in the temporary storage directory.

Returns:

List of file paths to JSON files in the temporary storage.

Return type:

list[str]

datatransmitter.read_json(filename)[source]

Read a JSON file and return its contents as a Python dictionary.

Parameters:

filename (str) – Path to the JSON file.

Returns:

Parsed JSON data.

Return type:

dict

datatransmitter.send_payload(pl)[source]

Send the signed payload to the API.

Parameters:

pl (dict) – The signed measurement payload.

Returns:

Response object from the HTTP POST request.

Return type:

requests.Response

Pi.psk_sign module

Digital Signature Module

This module provides functionality for signing measurement data using a private RSA key. It includes functions to load a private key from a file and to generate a digital signature for a given dataset.

psk_sign.get_priv_key_from_file(file_path)[source]

Load a private key from a PEM file.

This function reads a PEM-encoded private key from a specified file and loads it into memory.

Parameters:

file_path (str) – The path to the PEM file containing the private key.

Returns:

The loaded private key.

Return type:

cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey

Raises:
  • ValueError – If the file is not a valid PEM private key file.

  • TypeError – If the key format is unsupported.

  • Exception – For other errors during key loading.

psk_sign.sign_meas_data(priv_key_file, data)[source]

Sign measurement data using a private RSA key.

This function generates a digital signature for the given data using a private key loaded from the specified file. The data and its corresponding signature are returned as a dictionary.

Parameters:
  • priv_key_file (str) – The path to the PEM file containing the private RSA key.

  • data (dict) – The measurement data to be signed. It must be serializable to JSON.

Returns:

A dictionary containing the original data and the signature in base64 format.

Return type:

dict

Raises:
  • ValueError – If the private key file is invalid or if the data is not serializable to JSON.

  • TypeError – If the key or data format is incorrect.

  • Exception – For other errors during signing.

Module contents