With our new models we will now create In this part your are going to instantiate a list of 2 equipments. We will create an organ for each equipment. Each one will have an automate which changes its hydrometry virtual sensors in the database and create two "organs" of the system that will simulate the sensor behavior. Each organ will simulate changes of hygrometry and temperature. Equipment 1Make
- Create a
folder named equipment1 and the file - new folder spinal-organ-sensor1 and within it a file named index.js
in it. Code Block |
---|
| ~/equipement-system$ mkdir equipment1
~/equipement-system$ cd equipment1
~/equipement-system/equipment1$ touch index.js |
This organ will create the first equipment and will revover data from a simulated sensor. equipment1/index.js- .
- in the index.js file:
- Require spinal-core.
- Get the connection parameter from the config.js file in the main folder
- 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.
- Create a function onFaill. This function will be executed if the loading of ‘List’ fail.
- Load list from spinal-core.
- Create a new folder spinal-organ-sensor2
- Copy paste the index.js into sensor2 folder and do the appropriate modifications.
- Don't forget to install spinal-core-connectorjs from GitHub in the two folders
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 spinal-organ-sensor1/index.js should look like this. Code Block |
---|
language | js |
---|
theme | DJango |
---|
title | spinal-organ-sensor1/index.js |
---|
linenumbers | true |
---|
| // | RequirementRequirements and connection
const spinalCore = require('spinal-core-connectorjs');
const models = require('../spinal- | modelmodelconsole.log("Configuration Environment notfound, using default config");
process.env.SPINALHUB_PORT = 7777;
process.env.SPINALHUB_IP = "127.0.0.1";
process.env.SPINAL_USER_ID = 168;
process.env.SPINAL_PASSWORD = "JHGgcz45JKilmzknzelf65ddDadggftIO98P";
const conn = spinalCore.connect(`http://${process.env.SPINAL_USER_ID}:${process.env.SPINAL_PASSWORD}@${process.env.SPINALHUB_IP}:${process.env.SPINALHUB_PORT}/`);
// Gives | Tries load 'List'
spinalCore.load(conn, "List", the hygrometry and temperature of a sensor
function simulate( | list//Ifthe list exists but is empty a first equipment is added
if (list.equipments.length < 1)
addItem(list);
// Else the first equipment is pressed
else
press(list.equipments[0]);
}, function () {
// If 'List'= Math.floor(Math.random() * 100);
const degrees = Math.floor(Math.random() * 30);
sensor.hygrometry.set(hydro);
sensor.temperature.set(degrees);
// Repeats every second
setTimeout(() => {
console.log(sensor.name.get() + ": data has changed");
simulate(sensor);
}, 1000);
};
// Finds a Sensor in the SensorList, if it doesn't exist, | anemptyfunction getSensorById(list, | iscreatedanditsfirstequipmentispressedconstlistnew equipmentList();
spinalCore.store(conn, list, "List", function () {
addItem(list);
});
});
// Creates a new equipment and adds it to the list
function addItem(list) {
const0; i < list.sensors.length; i++) {
if (list.sensors[i].id.get() === id) {
item = list.sensors[i];
break;
}
}
if (typeof item === "undefined") {
item = new | equipmentmodels.SensorModel();
item.id.set( | 0equipment0equipmentspress(item)Givesrandomvaluestothehydrometryandtemperatureofanequipmentpressequipmentlist) {
simulate(getSensorById(list, 0));
};
// | consthydro= Math.floor(Math.random() * 100);
const degrees = Math.floor(Math.random() * 30);
equipment.hydrometry.set(hydro);
equipment.temperature.set(degrees);
// Repeats every second
setTimeout(function ()be called if the list cannot be loaded
function onFailure() {
const list = new models.SensorListModel();
const item = getSensorById(list, 0);
spinalCore.store(conn, list, "List", () => {
| console.log("equipment 1: has been pressed" press(equipment);
}, 1000spinalCore.load(conn, "List", onSuccess, onFailure);
| }You can launch your new organ with node. Code Block |
---|
| ~/equipement-system/equipment1$ node index.js | Go
Run spinal-organ-sensor1/index.js and go to the admin UI and put 'List' into the inspector and you should see the hydrometry hygrometry and temperature of equipment0 sensor0 change every second. Image Removed Equipment2Equipment2 will be almost identical to equipment1 so I suggest you start by copying equipment1Image Added
Your file spinal-organ-sensor2/index.js should look like this. Code Block |
---|
Code Block |
---|
language | js |
---|
theme | DJango |
---|
linenumbers | true |
---|
| language | bashjs |
---|
theme | DJango |
---|
| ~/equipement-system/equipement1$ cd ..
~/equipement-system$ cp -r equipment1 equipment2
~/equipement-system$ cd equipment2 |
There are very few changes to make to index.js. equipment2/index.js title | spinal-organ-sensor2/index.js |
---|
linenumbers | true |
---|
| // Requirements and connection
const spinalCore = require('spinal-core-connectorjs');
const models = require('../spinal-modelmodels/modelmodels.js');
console.log("Configuration Environment not found, using default config");
process.env.SPINALHUB_PORT = 7777;
process.env.SPINALHUB_IP = "127.0.0.1";
process.env.SPINAL_USER_ID = 168;
process.env.SPINAL_PASSWORD = "JHGgcz45JKilmzknzelf65ddDadggftIO98P";
const conn = spinalCore.connect(`http://${process.env.SPINAL_USER_ID}:${process.env.SPINAL_PASSWORD}@${process.env.SPINALHUB_IP}:${process.env.SPINALHUB_PORT}/`);
spinalCore.load(conn, "List", function (list)
{
// Gives Anrandom values itemto isthe createdhygrometry ifand theretemperature areof lessa thansensor
2 equipments in the list
if (list.equipments.length < 2)
addItem(list);
// The second item is pressed (instead of the first)
press(list.equipments[1]);
}, function () {
spinalCore.store(conn, list, "List", function () {
addItem(listfunction simulate(sensor) {
const hydro = Math.floor(Math.random() * 100);
const degrees = Math.floor(Math.random() * 30);
sensor.hygrometry.set(hydro);
sensor.temperature.set(degrees);
// Repeats every second
setTimeout(() => {
console.log(sensor.name.get() + ": data has changed");
simulate(sensor);
}, 1000);
});
function addItem(list;
// Finds a Sensor in the SensorList, if it doesn't exist, creates it
function getSensorById(list, id) {
let item;
= newfor equipment()(let i = 0; i < itemlist.id.set(1);
// The new item is named equipment1 (instead of equipment0)
item.name.set("equipment1");
list.equipments.push(item);
press(item);
}
function press(equipment) {
const hydro = Math.floor(Math.random() * 100);
const degrees = Math.floor(Math.random() * 30);
equipment.hydrometry.set(hydro);
equipment.temperature.set(degrees);
setTimeout(function () {
// This message changes
console.log("equipment 2: has been pressed");
press(equipment);
}, 1000);
} |
Now launch equipment2 with node. Code Block |
---|
| ~/equipement-system/equipment2$ node index.js |
If you go back to the inspector you will notice that a new equipment has appeared in the List. Image Removed |