How are the Templates used in the simulation? When a simulation is started, the simulation engine will parse the template and create virtual devices out of it. Each virtual device will have its own memory, state, and connect to the IoT cloud platform, until your simulation is running. Following diagram describes the instantiation of the template.
If you are familiar with C++ or any other object oriented design, think of templates as Classes and virtual devices as Objects. Once a template is defined, you could create as many instances of it as you want.
Here is a diagram which explains how the functions within a template are invoked during the simulation run.
The setup function in the template is triggered as the very first function before even the connection is established from the backend. Use this function to retrieve values from persistent storage, initialize one time variables and assign some random names. The setup function doesn't need to return any value.
Once the setup is done, the message function will be invoked with the same state object which was passed earlier. The message function is the core function of the simulation and will define most of the simulation logic. The function is invoked for the specified number of iterations in the template run. The duration between each iteration is also configurable and needs to be specified in the template run parameters.
Should we describe the entire behavior of a device into the single template? Not necessarily. A template could correspond to a single lifecycle event such as software download, user interaction etc. E.g. in the light bulb scenarion we saw earlier, we could create several template for each lifecycle stage or combine all into a single one. So what should be the standard practice? In our recommendation, each template should describe at least one lifecycle stage of the simulated device which can be independently run. E.g. Software update could be simulated using a single template. However, user interaction should be prohibited during the software update. In order to implement this, software update template will create a glob storage variable in order to notify any other templates that the software is currently updating.