In this part your are going to create a list of 2 sensorsensors. Every sensor will have its own organ. Each one of those will have an automate which will changes change its hydrometry and temperature.
create a list of two sensor: - Create a new folder sensor1 and within a file named index.js.
- Require spinal-core.
- Get the connection string from config.js.
- Create a function addItem. This function create, set and add an item to the list given in argument.
- Create a function simulate. This function simulate the value of the sensor.
- Create a function onSuccess. this function will be executed if the loading of ‘List’ is successful. For this part you to add an element to the list if the list is empty and simulate the data of this element this element.
- Create a function onFaill. this function will be executed if the loading of ‘List’ fail. For this part if the loading fail you want to create a new list and store it into spinal-core.
- Load list from spinal-core.
- Copy paste this folder into sensor2 and do the appropriate modification in index.js .
Panel |
---|
| - If you have and API to retrieve the real data of your sensor use it to send real data to the graph.
- You can run every sensor independently by running the corresponding index.js (node index.js)
|
Your file sensor1/index.js should look like this. Code Block |
---|
language | js |
---|
theme | DJango |
---|
title | sensor1/index.js |
---|
linenumbers | true |
---|
| // 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(0);
item.name.set("sensor0");
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 1: data has changed");
simulate(sensor);
}, 1000);
};
//this function will be called if the list is successfully load
onSuccess = (list) => {
if (list.sensors.length < 1)
addItem(list);
simulate(list.sensors[0]);
};
//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 |
---|
language | js |
---|
theme | DJango |
---|
title | Sensor2/index.js |
---|
linenumbers | true |
---|
| // 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);
};
//this function will be called if the list is successfully load
onSuccess = (list) => {
if (list.sensors.length < 2)
addItem(list);
simulate(list.sensors[1]);
};
//this function will be called if the list is failed to load
onFail = () => {
const list = new SensorList();
spinalCore.store(conn, list, "List", () => {
addItem(list);
});
};
// Tries to load 'List'
spinalCore.load(conn, "List", onSuccess, onFail); |
|