Understanding Template

A template is a javascript document which describes the behavior of your virtual device during the simulation.

Once we have figured out what our device lifecycle looks like and what kind of device state variables we will have, we can jump to writing templates. A template is essentially a javascript document which describes how your virtual device will behave during simulation. Every aspect of device behavior which needs to be modeled should be described in the template.

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.

Generation of virtual objects from a single 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.

The template document contains information about messages to be sent to the cloud platform and connectivity information, as well as credentials. When you are writing a template, you will need to define javascript functions. At the end, the function must return the payload which should be sent to your IoT cloud platform. Each of the template function is passed an individual javascript object called state which could be used to hold device specific parameters.

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.