// 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;
a Sensor in the SensorList, if it doesn't exist, creates it
function getSensorById(list, id) {
let iitem;
for (let i = 0; i < i < list.sensors.length && list.sensors[i].id !== id; i++);
if (i < list.sensors.length) {
if (list.sensors[i].id.get() === returnid) i;{
else item = list.sensors[i];
return -1; }break;
//this function will be}
called if the}
list is successfullyif load(typeof onSuccessitem = (list) =>== "undefined") {
item const= index = findIndexById(1, listnew models.SensorModel();
consoleitem.id.log("success 2 : index:", indexset(id);
item.name.set("sensor" + id);
list.sensors.push(item);
if (index}
!== -1) return item;
}
// This function simulate(list.sensors[index]);
else
addItem(list)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", () => {
addItemsimulate(listitem);
});
};
spinalCore.load(conn, "List", onSuccess, onFailonFailure);
|