Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Quick navigation

Child pages (Children Display)
alltrue
pageTutorials


Panel
titleGoal

Anchor
1
1

After the Getting Started tutorial, you should have a Spinal-system called button-system with one model and one organ. We will use it to create an intelligent system with more synchronized information.
System architecture:


In this tutorial you will learn how to create to make lists of items and how to launch your organs with more efficacity.


Panel
titleThe equipment and equipementList models

Anchor
2
2

In this part we will create 2 new models:

  • equipment, a complex model with 5 attributes: id, name, hydrometry, temperature and pressed (boolean)
  • equipmentList, a model that contains an array of equipments

spinal-model-button/model.js

Code Block
languagejs
themeDJango
linenumberstrue
const spinalCore = require("spinal-core-connectorjs");
// 1: Previously used buttonModel
function buttonModel() {
    buttonModel.super(this);
    this.add_attr({
        pressed: false
    });
}
spinalCore.extend(buttonModel, Model);
module.exports = buttonModel;

// 2: Equipment model
function equipment() {
    equipment.super(this);
// 3: Creating attributes
    this.add_attr({
        id: 0,
        name: "",
        hydrometry: 0,
        temperature: 0,
        pressed: false
    });
}
// 4: Registering the equipment model in the Spinal system
spinalCore.extend(equipment, Model);
module.exports = equipment;

// 5: equipmentList model
function equipmentList() {
    equipmentList.super(this);
// 6: Array of equipments
    this.add_attr({
        equipments: []
    });
}
// 4: Registering the equipmentList model in the Spinal system
spinalCore.extend(equipmentList, Model);
module.exports = equipmentList;




How to launch you organs effectively
Panel
titleHow to launch you organs effectively
title


Panel
titleCreate a list of 2 equipments

Anchor
3
3

With our new models we will now create a list of 2 equipments. We will create an organ for each equipment. Each one will have an automate which changes its hydrometry and temperature.


Equipment 1

Make a folder named equipment1 and the file index.js in it.


Code Block
languagebash
themeDJango
~/button-system$ mkdir equipment1
~/button-system$ cd equipment1
~/button-system/equipment1$ touch index.js


​This organ will create the first equipment and will revover data from a simulated sensor.

equipment1/index.js

Code Block
languagejs
themeDJango
linenumberstrue
// 1: Requirement and connection
var spinalCore = require('spinal-core-connectorjs');
require('../spinal-model-button/model.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";

var conn = spinalCore.connect(`http://${process.env.SPINAL_USER_ID}:${process.env.SPINAL_PASSWORD}@${process.env.SPINALHUB_IP}:${process.env.SPINALHUB_PORT}/`);

// 2: Tries to load 'List'
spinalCore.load(conn, "List", function (list) {
// 3: If 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' doesn't exist an empty list is created and its first equipment is pressed
  var list = new equipmentList();
  spinalCore.store(conn, list, "List", function () {
    addItem(list);
  });
});

// 6: Creates a new equipement and adds it to the list
function addItem(list) {
  let item = new equipment();

  item.id.set(0);
  item.name.set("equipment0");
  list.equipments.push(item);
  press(item);
}

// 7: Gives random values to 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 () {
    console.log("equipment 1: has been pressed");
    press(equipment);
  }, 1000);
}


You can launch your new organ with node.

Code Block
languagebash
themeDJango
~/button-system/equipment1$ node index.js

Go to the admin UI and put 'List' into the inspector and you should see the hydrometry and temperature of equipment0 change every second.

Image Modified


Equipment2

Equipment2 will be almost identical to equipment1 so I suggest you copy start by copying equipment1.

Code Block
languagebash
themeDJango
~/button-system/equipement1$ cd ..
~/button-system$ cp -r equipment1 equipment2
~/button-system$ cd equipment2


There are very  few changes to make to index.js.

equipment2/index.js

Code Block
languagejs
themeDJango
linenumberstrue
var spinalCore = require('spinal-core-connectorjs');
require('../spinal-model-button/model.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";

var conn = spinalCore.connect(`http://${process.env.SPINAL_USER_ID}:${process.env.SPINAL_PASSWORD}@${process.env.SPINALHUB_IP}:${process.env.SPINALHUB_PORT}/`);

var list = new equipmentList();
spinalCore.load(conn, "List", function (list) {
// 1: An item is created if there are less than 2 equipments in the list
  if (list.equipments.length < 2)
    addItem(list);
// 2: 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(0);
// 3: The new item is named equipment1 (instead of equipment0)
  item.name.set("equipment1");
  list.equipments.push(item);
  press(item);
}

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);
  setTimeout(function () {
// 4: This message changes
    console.log("equipment 2: has been pressed");
    press(equipment);
  }, 1000);
}


Now launch equipment2 with node.

Code Block
languagebash
themeDJango
~/button-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 Modified

Panel

Anchor
4
4

After the last part you might have noticed that the way we have been launching our organs isn't very effective, you have to launch and stop organs one by one.

This is because this is not the right way to do it. In this part you will learn how to launch  your organs effectively.

To do that we need to look at .apps.json. When you open it for the first time it should look like this:

Code Block
languagejs
themeDJango
linenumberstrue
{
  "apps": [
    {
      "name": "spinal-core-hub",
      "script": "spinalhub.js",
      "cwd": "./nerve-center/"
    }
  ]
}

.apps.json contains the organs that should be launched. They are identified by:

  • name: The name they will be identified by pm2
  • script: The name of the file to be executed to launch the organ
  • cwd: The location of the script (relative to the root of the project)

With all that in mind let's add equipement 1 and 2 to the list.

Code Block
languagejs
themeDJango
linenumberstrue
{
  "apps": [
    {
      "name": "spinal-core-hub",
      "script": "spinalhub.js",
      "cwd": "./nerve-center/"
    },
    {
      "name": "equipment1",
      "script": "index.js",
      "cwd": "./equipment1/"
    },
    {
      "name": "equipment2",
      "script": "index.js",
      "cwd": "./equipment2/"
    }
  ]
}

Now all we have to do is let pm2 do the work.

Code Block
languagebash
themeDJango
~/button-system$ pm2 restart launch.config.json

pm2 will now show you equipment 1 and 2 along with spinal-core-hub.


Conclusion


In this second tutorial you learned:

  • how to create more complicated models
  • how to organize your data coming from multiple organs
  • how to launch you your organs effectively


This tutorial will be continued in Getting Started extended Extended 2.