IoT devices, among other purposes, are used to uploading information gathered from different sources to the cloud, where a final user can inspect, manage and keep track of it. This can be done for specific bits of data, like temperature or humidity readings, location, CPU usage, available memory and so on. In Digi Remote Manager, these types of data are stored in data streams, which are collections of data samples called data points. Each data point contains a value for the given stream at a certain point in time. In the case of temperature, it could be the list of temperature values sampled by the device every second during one hour.
Sometimes, simulated devices are only required to upload data periodically to the cloud. Other times, data should only be updated when certain conditions are met.
1. Include custom data points support
Follow next steps to add data points custom samples to a simulation:
-
Add the
user_app
object to thesimulation.json
file. See Simulation configuration. -
Include the
data_streams_id
value in thedata_streams
object of the corresponding profile file inside theprofiles
directory of your simulation. See Device profiles definition. -
Create a
*.json
file named as the value ofdata_streams_id
insidedata_streams
directory of your simulation. -
Configure the list of data points to be generated in code by setting their
data_source
mode ascommand
. See Data streams structure. -
Create the required Python custom code to generate the data point values.
2. Add data point samples custom code
The last step to generate custom samples is the customization of the generate_data_point()
Python method to provide sample values:
-
Create a Python file called
simulation.py
in the root of your simulation directory if it does not exist yet. -
Implement the method
generate_data_point()
insidesimulation.py
.def generate_data_point(device_id, profile_id, stream_name, ptype, last_value, tick)
The implemented generate_data_point()
method:
-
Receives as parameters:
-
device_id
, the identifier of the device to generate the sample for. -
profile_id
, the profile identifier the simulated device is using. -
stream_name
, the name of the data stream to provide a new sample value. -
ptype
, the Python type of the value to generate, it could be an integer, float, string, etc. -
last_value
, last generated value for this stream data and device. -
tick
, number of times this method has been called for this device and stream data.
-
-
Returns a tuple with:
-
A boolean value to batch or immediately upload the generated sample to Remote Manager independently if batch samples support is enabled in the profile. See
data_streams
. -
The new generated value.
-
Returning the tuple
(None, None)
means the generation of a sample is not supported for the provided parameters.
-
For example, find below the code to generate a random integer sample for stream random/integer
that is immediately uploaded every ten samples:
def generate_data_point(device_id, profile_id, stream_name, ptype, last_value, tick):
"""
Generates a data point value.
Args:
device_id (String): Device identifier.
profile_id (String): Identifier of the profile the device is using.
stream_name (String): Name of the stream without the device ID.
ptype (Class): Python type of the value.
last_value (?): Last generated value. Its type must be `ptype`.
tick (Integer): Number of times this method has been called.
Returns:
Tuple: Tuple with data point information:
- upload_now (Boolean): `True` to upload now, `False` otherwise.
- new_value (?): The new generated value.
Returns `(None, None)` if not supported stream.
"""
if stream_name == "random_integer":
return tick % 10 == 0, random.randint(0, 100)
return None, None
You can also review the example at 6. Implement custom code.