Creating your own data types

As FastIoT only provides some very basic data types (s. fastiot.msg) many projects will require their own data models.

As a rule of thumb it is recommended to use the fastiot.msg.thing.Thing for single sensor values. This allows any module to work with the data and common adaptors to provide the data. This should work well when e.g. reading out a PLC or any other sensor device.

If you have more complex data to handle it is usually a good starting point in the project to think about how to structure your data. This is done using data models. FastIoT relies on Pydantic, so you may consult this for any details about data models.

To add the FastIoT handling of subjects it is recommended (though not necessary) to inherit your class from one of the inheritors of fastiot.core.data_models.FastIoTData.

Three basic classes inherit from this class:

A very basic data model for publishing data could thus look like the following:

from typing import Optional, Union

from fastiot.core.data_models import FastIoTPublish


class MyDataModel(FastIoTPublish):
  my_id: str
  """ A required string """

  my_value: Union[float, int]
  """ An required float or integer """

  optional_value: Optional[str] = ""
  """ An optional value defaulting to an empty string """