// 1:Requirements Requirement and connection
varconst spinalCore = require('spinal-core-connectorjs');
const models = require('../spinal- model-buttonmodels/ 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";
varconst conn = spinalCore.connect(`http://${process.env.SPINAL_USER_ID}:${process.env.SPINAL_PASSWORD}@${process.env.SPINALHUB_IP}:${process.env.SPINALHUB_PORT}/`);
// 2: TriesGives random values to load 'List'
spinalCore.load(conn, "List", the hygrometry and temperature of a sensor
function simulate( listsensor) {
// 3:const Ifhydro the list exists but is empty a first equipment is added
if (list.equipments.length < 1)
addItem(list);
// 4: Else the first equipment is pressed
else
press(list.equipments[0]);
}, function () {
// 5: 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, ancreates it
emptyfunction getSensorById(list, isid) created{
and its firstlet equipmentitem;
is
pressed for var(let listi = new equipmentList();
spinalCore.store(conn, list, "List", function () {
addItem(list);
});
});
// 6: Creates a new equipement and adds it to the list
function addItem(list 0; i < list.sensors.length; i++) {
if (list.sensors[i].id.get() === id) {
item = list.sensors[i];
break;
}
}
if (typeof item === "undefined") {
let item = new equipmentmodels.SensorModel();
item.id.set( 0id);
item.name.set(" equipment0sensor" + id);
list. equipmentssensors.push(item);
press(item)}
return item;
}
// 7: Gives random values toThis function will be called if the hydrometry and temperature of an equipment
function press(equipment) {
let hydro = Math.floor(Math.random() * 100);
let degrees = Math.floor(Math.random() * 30);
equipment.hydrometry.set(hydro);
equipment.temperature.set(degrees);
// 8: Repeats every second
setTimeout(function ()list is successfully loaded
function onSuccess(list) {
simulate(getSensorById(list, 0));
};
// This function will 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"simulate(item);
});
};
press(equipment);
}, 1000spinalCore.load(conn, "List", onSuccess, onFailure);
}You can launch your new organ with node. Code Block |
---|
| ~/button-system/equipment1$ node index.js | Go |