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: Create a new folder sensor1 and within 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.
- Create a new folder spinal-organ-sensor1 and within it a file named index.js.
- in the index.js file:
- Require spinal-core.
- Get the connection
string - 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 - 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 - 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 - Load list from spinal-core.
Load list from spinal-core. - Create a new folder spinal-organ-sensor2
- Copy paste this folder the index.js into sensor2 folder and do the appropriate modification in index.js .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-modelmodels/models.js');
const connection_string
process.env.SPINALHUB_PORT = 7777;
process.env.SPINALHUB_IP = require("127.0./config0.1");
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);
};
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 random values to the hydrometryhygrometry and temperature of a sensor
function simulate = (sensor) => {
const hydro = Math.floor(Math.random() * 100);
const degrees = Math.floor(Math.random() * 30);
sensor.hydrometryhygrometry.set(hydro);
sensor.temperature.set(degrees);
// Repeats every second
setTimeout(() => {
console.log("sensor 1sensor.name.get() + ": data has changed");
simulate(sensor);
}, 1000);
};
findIndexById// =Finds (id, list) => {
if (list.sensors.length <= 0)
return -1;
let ia Sensor in the SensorList, if it doesn't exist, creates it
function getSensorById(list, id) {
let item;
for (let i = 0; i < i < list.sensors.length && list.sensors[i].id !== id; i++); {
if (i < list.sensors.length[i].id.get() === id) {
item return= list.sensors[i];
else break;
}
return -1;
};
//this functionif will(typeof beitem called if the list is successfully load
onSuccess = (list) => {
const index = findIndexById(0, list=== "undefined") {
item = new models.SensorModel();
item.id.set(id);
consoleitem.name.logset("sensor"success 2 : index:", index+ id);
if (index !== -1)list.sensors.push(item);
}
return item;
}
// simulate(list.sensors[index]);
else
addItem(list)This function will be called if the list is successfully loaded
function onSuccess(list) {
simulate(getSensorById(list, 0));
};
//this This function will be called if the list iscannot failedbe toloaded
load
onFail = function onFailure() => {
const list = new SensorListmodels.SensorListModel();
const item = getSensorById(list, 0);
spinalCore.store(conn, list, "List", () => {
addItem(listsimulate(item);
});
};
spinalCore.load(conn, "List", onSuccess, onFailonFailure);
|
Run sensor1 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 sensor0 change every second. Image RemovedImage Added
Your file spinal-organ-sensor2/index.js should look like this. Code Block |
---|
language | js |
---|
theme | DJango |
---|
title | Sensor2spinal-organ-sensor2/index.js |
---|
linenumbers | true |
---|
| // RequirementRequirements and connection
const spinalCore = require('spinal-core-connectorjs');
const models = require('../spinal-modelmodels/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);
};
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}/`);
// Gives random values to the hydrometryhygrometry and temperature of a sensor
function simulate = (sensor) => {
const hydro = Math.floor(Math.random() * 100);
const degrees = Math.floor(Math.random() * 30);
sensor.hydrometryhygrometry.set(hydro);
sensor.temperature.set(degrees);
// Repeats every second
setTimeout(() => {
console.log("sensor 2.name.get() + ": data has changed");
simulate(sensor);
}, 1000);
};
findIndexById// =Finds (id, list) => {
if (list.sensors.length <= 0)
return -1;
let i;
a Sensor in the SensorList, if it doesn't exist, creates it
function getSensorById(list, id) {
let item;
for (let i = 0; i < i < list.sensors.length && list.sensors[i].id !== id; i++); {
if (i < list.sensors.length[i].id.get() === id) {
item return= list.sensors[i];
else break;
}
return -1; };
//this functionif will(typeof beitem called if the list is successfully load
onSuccess = (list) => {
const index = findIndexById(1, list=== "undefined") {
item = new models.SensorModel();
item.id.set(id);
consoleitem.name.logset("successsensor" 2 : index:", index+ id);
if (index !== -1)
list.sensors.push(item);
}
return item;
}
simulate(list.sensors[index]);
else
addItem(list// This function will be called if the list is successfully loaded
function onSuccess(list) {
simulate(getSensorById(list, 1));
};
//this This function will be called if the list iscannot failedbe toloaded
load
onFail = function onFailure() => {
const list = new SensorListmodels.SensorListModel();
const item = getSensorById(list, 1);
spinalCore.store(conn, list, "List", () => {
addItem(listsimulate(item);
});
};
spinalCore.load(conn, "List", onSuccess, onFailonFailure);
|
Image RemovedImage Added |