Digi IoT Device Simulator allows you to programmatically set the operating mode of your simulated devices: either in service or maintenance mode.
1. Include custom operating mode support
Follow these steps to programmatically generate the maintenance status for simulated devices:
-
Add the
user_app
object to thesimulation.json
file. See Simulation configuration for details. -
Include the
maintenance_window
object in the corresponding profile file inside theprofiles
directory of your simulation. See Device profiles definition. -
Set the
type
property of themaintenance_window
object tocustom
. -
Define the
sampling_rate
property for themaintenance_window
object. -
Implement the custom Python code needed to generate maintenance status values. See the next step Add custom maintenance status code.
2. Add custom maintenance status code
The final step is to customize the generate_maint_status()
Python method to provide maintenance status values:
-
If not already present, create a Python file named
simulation.py
in the root of your simulation directory. -
Implement the
generate_maint_status()
method insidesimulation.py
.def generate_maint_status(device_id, profile_id, last_status, tick)
This method:
-
Accepts the following parameters:
-
device_id
: The identifier of the device to generate the maintenance status for. -
profile_id
: The profile identifier the simulated device is using. -
last_status
: The previous maintenance status (boolean):-
None
: If called for the first time. -
True
: Device is in maintenance. -
False
: Device is in service.
-
-
tick
: The number of times this method has been invoked for this device.
-
-
Returns a boolean value representing the new maintenance status:
-
True
: Device in maintenance. -
False
: Device in service. -
None
: Ignore the value.
-
Below is an example implementation that generates a random maintenance status every five minutes:
MAINTENANCE_CHECK_INTERVAL = 5 * 60
last_reported = {}
# [...]
def generate_maint_status(device_id, profile_id, last_status, tick):
"""
Generates the new maintenance status value: `True` means the device is in
maintenance, `False` the device is in service.
Args:
device_id (String): Device identifier.
profile_id (String): Identifier of the profile the device is using.
last_status (Boolean): Last maintenance status:
- `None`: The first time it is called.
- `True`: Device in maintenance.
- `False`: Device in service.
tick (Integer): Number of times this method has been invoked.
Returns:
Boolean: New maintenance status:
- `True`: Device in maintenance.
- `False`: Device in service.
- `None`: Ignore the value.
"""
last_time = last_reported.get(device_id, None)
if not last_time or time.time() - last_time >= MAINTENANCE_CHECK_INTERVAL:
new_status = random.choice([True, False])
# Report the new value:
# - For the first time
# - Or if it is different from the previous one
if last_status is None or new_status != last_status:
last_reported[device_id] = time.time()
return new_status
return None
# [...]