Templates are at the heart of IoTIFY's Network & Database simulator. Let's have a closer look at them.
When a network simulation is started, the simulator will process template and create virtual devices. Each virtual device will then independently behave and connect to the IoT cloud platform, until your simulation is running.
If you are familiar with C++ or any other object oriented design, think of templates as Classes and virtual devices as Objects.
Every IoT device has several important parameters to be reported to the cloud platform. An example could be a temperature value read through a sensor or the current CPU load. The device has also some internal parameters such as avaiable SSD and memory usage, which are required to be tracked. While modelling the device, you will need a local storage to save these values and any other intermediate variable which needs to be preserved across the simulation. state is a JSON object which can be used for this purpose. E.g. consider following code snippet of a simple template:
here, in the very first iteration, the state object will be empty. So we will initialize the temperature value with 50. Since state is preserved across simulation, upon each subsequent call of the template, the temperature variable will increment by 1.
It is important that at the end of processing, function body must return a value to be sent to the cloud platform. The return value could either be a string or a binary Buffer, depending upon what values are accepted by your cloud platform provider. If you would like to skip sending anything for that particular iteration, simply return ‘undefined’ as a string.
In the above example, every alternate invocation will be skipped and every other iteration will return ‘hello world’ to the cloud platform.
Chancejs is a random content generation library which is quite useful to generate data. E.g. to generate a random street addresss
And voila, you have a perfectly looking fake street address.
Momentjs is another super useful library, especially to manipulate time. In below example, we will calculate the elapsed time since the simulation started (iteration 0)
There are several built in module available in the function body. Math is one of them which is quite useful for arithmatic.
We have written several helper libraries which can be used in the template. Check them out in the next section.