With our new models we will now In this part your are going to create a list of 2 equipmentssensor. We will create an organ for each equipment. Each one Every sensor will have its own organ. Each one of those will have an automate which will changes its hydrometry and temperature. Equipment 1Make a folder named equipment1 and the file
create a list of two sensor: - Create a new folder sensor1 within a file named index.js
in itThis organ will create the first equipment and will revover data from a simulated sensor. equipment1/index.js- .
Code Block |
---|
| ~/equipement-system$ mkdir equipment1
~/equipement-system$ cd equipment1
~/equipement-system/equipment1$ touch 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/ | model
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 connection_string = require("../config");
const conn = spinalCore.connect( | `http://${process.env.SPINAL_USER_ID}:${process.env.SPINAL_PASSWORD}@${process.env.SPINALHUB_IP}:${process.env.SPINALHUB_PORT}/`connection_string);
// Creates a new sensor | Triesload'List'
spinalCore.load(conn, "List", function list
addItem = (list) => {
| //Ifthelistexistsbut is empty a first equipment is addednew Sensor();
item.id.set(0);
| if item.name.set("sensor0");
list. | equipments.length < 1)addItemlistElse the first equipment is pressed
else
press(list.equipments[0]);
}, function () {
// If 'List' doesn't exist an empty list is created and its first equipment is pressed
const list = new equipmentList();
spinalCore.store(conn, list, "List", function () {
addItem(list);
});
});
// Creates a new equipment and adds it to the list
function addItem(list) {
const item = new equipment();
item.id.set(0);
item.name.set("equipment0");
list.equipments.push(item);
press(item);
}
// Gives random values to the hydrometry and temperature of an equipment
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);
// Repeats every second
setTimeout(function () {
console.log("equipment 1: has been pressed");
press(equipment);
}, 1000);
}You can launch your new organ with node. Code Block |
---|
| ~/equipement-system/equipment1$ node index.js | Go 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 equipment0 sensor0 change every second.
Equipment2Equipment2 will be almost identical to equipment1 so I suggest you start by copying equipment1
Your file sensor2/index.js should look like this. equipment2 Code Block |
---|
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. | Code Block |
---|
language | js |
---|
theme | DJango |
---|
linenumbers | true |
---|
| // Requirement and connection
const spinalCore = require('spinal-core-connectorjs');
require('../spinal-model/modelmodels.js');
console.log("Configuration Environment not found, using default const connection_string = require("../config");
process.env.SPINALHUB_PORT
const conn = 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) {
// An item is created if there are less than 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(list);
});
});
function addItem(list) {
let item = new equipment();
item.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 |