Panel | ||||||
---|---|---|---|---|---|---|
| ||||||
In this tutorial you learn how to :
System architecture: |
Panel | ||
---|---|---|
| ||
For this tutorial, you just need the basic requirements |
Panel | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||
In this part you’re going to create two sensor models:
Create your spinal system and digital twin by following these steps:
Your model file should look like this: spinal-model-equipments/model.js
| |||||||||||||||||
Panel | |||||||||||||||||
title | Create a list of 2 Sensor
|
Panel | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||
In this part your are going to create instantiate a list of 2 sensors. Every sensor will have its own organ. Each one of those will have an automate which will change its hydrometry and temperature. create a list of two sensor:sensors digital twin in the hub database and create two "organs" of the system that will simulate the sensor behaviour. Each organ will simulate changes of hygrometry and temperature.
Your file sensor1/index.js should look like this. Code Block | | |||||||||||
|
Panel | ||
---|---|---|
| ||
|
Your file sensor1/index.js should look like this.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// Requirement and connection
const spinalCore = require('spinal-core-connectorjs');
require('../spinal-model/models.js');
const connection_string = require("../config");
const conn = spinalCore.connect(connection_string);
// Gives random values to the hydrometry and temperature of a sensor
simulate = (sensor) => {
const hydro = Math.floor(Math.random() * 100);
const degrees = Math.floor(Math.random() * 30);
sensor.hydrometry.set(hydro);
sensor.temperature.set(degrees);
// Repeats every second
setTimeout(() => {
console.log("sensor 1: data has changed");
simulate(sensor);
}, 1000);
};
findIndexById = (id, list) => {
if (list.sensors.length <= 0)
return -1;
let i;
for (i = 0; i < i < list.sensors.length && list.sensors[i].id !== id; i++);
if (i < list.sensors.length)
return i;
else
return -1;
};
//this function will be called if the list is successfully load
onSuccess = (list) => {
const index = findIndexById(0, list);
console.log("success 2 : index:", index);
if (index !== -1)
simulate(list.sensors[index]);
else
addItem(list);
};
//this function will be called if the list is failed to load
onFail = () => {
const list = new SensorList();
spinalCore.store(conn, list, "List", () => {
addItem(list);
});
};
spinalCore.load(conn, "List", onSuccess, onFail);
|
Run sensor1 and go to the admin UI and put 'List' into the inspector and you should see the hydrometry and temperature of sensor0 change every second.
Your file sensor2/index.js should look like this.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// Requirement and connection const spinalCore = require('spinal-core-connectorjs'); require('../spinal-model/models.js'); const connection_string = require("../config"); const conn = spinalCore.connect(connection_string); // Creates a new sensor and add it to the list addItem = (list) => { const item = new Sensor(); item.id.set(1); item.name.set("sensor1"); list.sensors.push(item); simulate(item); }; // Gives random values to the hydrometry and temperature of a sensor simulate = (sensor) => { const hydro = Math.floor(Math.random() * 100); const degrees = Math.floor(Math.random() * 30); sensor.hydrometry.set(hydro); sensor.temperature.set(degrees); // Repeats every second setTimeout(() => { console.log("sensor 2: data has changed"); simulate(sensor); }, 1000); }; findIndexById = (id, list) => { if (list.sensors.length <= 0) return -1; let i; for (i = 0; i < i < list.sensors.length && list.sensors[i].id !== id; i++); if (i < list.sensors.length) return i; else return -1; }; //this function will be called if the list is successfully load onSuccess = (list) => { const index = findIndexById(1, list); console.log("success 2 : index:", index); if (index !== -1) simulate(list.sensors[index]); else addItem(list); }; //this function will be called if the list is failed to load onFail = () => { const list = new SensorList(); spinalCore.store(conn, list, "List", () => { addItem(list); }); }; spinalCore.load(conn, "List", onSuccess, onFail); |
Panel | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||
You might have noticed that running every organ independently is painful. If you wonder why it’s so painful it’s because you are not supposed to do that :p. In this part you will learn a to run you organs efficiently.
.apps.json contains the organs that should be launched. They are identified by:
Your .app.json should look like this:
pm2 will now show you sensor 1 and 2 along with spinal-core-hub. |
Conclusion
In this tutorial you learned:
- how to create complex models
- how to organize your data coming from multiple organs
- how to launch you your organs effectively
This tutorial will be continued in Basic Automate Tutorial.
...