Digi IoT Device Simulator allows you to programmatically provide the current location of your simulated devices, whether they are stationary or mobile.
1. Include custom location support
To generate simulated device locations using custom code, you must:
-
Add the
user_app
object to thesimulation.json
file. See Simulation configuration. -
Set
custom
as the value for thetype
property in themobility
objects of your group or groups in thesimulation.json
file. -
Define the
sampling_rate
value in themobility
objects. -
Create the required Python custom code to provide new locations. See the next step Add custom location code.
2. Add custom location code
The final step to generate custom locations is to customize the generate_loc_coordinates()
Python method to provide coordinates:
-
If not already present, create a Python file named
simulation.py
in the root of your simulation directory. -
Implement the
generate_loc_coordinates()
method insidesimulation.py
.def generate_loc_coordinates(device_id, profile_id, last_loc, tick)
This method:
-
Accepts the following parameters:
-
device_id
: The identifier of the device for which the location is generated. -
profile_id
: The profile identifier the simulated device is using. -
last_loc
: The previous location as a tuple(latitude, longitude)
for this device. -
tick
: The number of times this method has been invoked for this device.
-
-
Returns
-
A tuple with the new latitude and longitude values.
-
None
if no new location is provided and the previous one is maintained.
-
For example, below is a sample implementation to:
-
Provide a new location based on a list of coordinates for devices with the
bus
profile. -
Assign a static location for devices with the
streetlight
profile.
bus_route = [
(44.926626946080255, -93.3977120711649),
# [...]
]
lights_coord = [
(44.92640797036998, -93.39859345679719),
(44.926584927233826, -93.39825885081144),
(44.92679644086422, -93.39799578555122),
# [...]
]
# [...]
def generate_loc_coordinates(device_id, profile_id, last_loc, tick):
"""
Generates the new location coordinates.
Args:
device_id (String): Device identifier.
profile_id (String): Identifier of the profile the device is using.
last_loc (Tuple: Float, Float): Tuple with the last location
(latitude, longitude).
`(None, None)` if called for the first time.
tick (Integer): Number of times this method has been invoked.
Returns:
Tuple: Tuple with new location information:
- latitude (Float): Latitude value (-90 to 90).
- longitude (Float): Longitude value (-180 to 180).
`None` to keep the previous location.
"""
if profile_id == "streetlight" and last_loc == (None, None):
index = random.randint(0, len(lights_coord) - 1)
return lights_coord.pop(index)
elif profile_id == "bus":
index = bus_route.index(last_loc) + 1 if last_loc in bus_route else 0
index = 0 if index >= len(bus_route) else index
return bus_route[index]
return None