Skip to content

Commit

Permalink
Enable multi-arduinos and filtering
Browse files Browse the repository at this point in the history
following issue #1,adding compatibility with multi-arduinos.
also adding learning mode
  • Loading branch information
isokar committed Mar 21, 2018
1 parent 90d34f5 commit 012627a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 80 deletions.
67 changes: 35 additions & 32 deletions lib/connect.js
Expand Up @@ -5,35 +5,38 @@ var createStateRadio = require('./createStateRadio.js');
var Promise = require('bluebird');

module.exports = function() {

// first we close all opened connection
return shared.reset()
.then(function(){

// we get all devices from this service
return gladys.device.getByService({ service: 'serial' });
})
.then(function(devices) {

// foreach device
return Promise.map(devices, function(device) {

// we connect to the device
var port = new SerialPort(device.identifier, {
baudRate: 57600 , parser: serialport.parsers.readline('\n')
});

port.on('error', function(err) {
sails.log.warn('Gladys serial error : ', err.message);
});

// if we receive data, we parse it
port.on('data', createStateRadio);

// we add the port object to the shared list
shared.addPort(port);

return port;
});
});
};

gladys.param.getValue('RFLink_tty')
.then(function(value){

var port = new SerialPort(value, {
autoOpen: false,
parser: serialport.parsers.readline('\n')
});
// We close connection
port.close(function (err) {
if (err) {
return console.log('Error closing port: ', err.message);
}
});
// Then we connect to the device
port.open(function (err) {
if (err) {
return console.log('Error opening port: ', err.message);
}
});

port.on('error', function(err) {
sails.log.warn('RFLink serial error : ', err.message);
});

// if we receive data, we parse it
port.on('data', createStateRadio);

// we add the port object to the shared list
shared.addPort(port);

return port;

})
};
38 changes: 20 additions & 18 deletions lib/createStateRadio.js
@@ -1,4 +1,5 @@
var Promise = require('bluebird');
var shared = require('./shared.js');

module.exports = function(data){

Expand All @@ -15,7 +16,7 @@ module.exports = function(data){
};

var res = data.split(";");
if(parseInt(res[0])===20 && parseInt(res[1])!==0 && res[2]!=='OK'){
if(parseInt(res[0])===20 && parseInt(res[1])!==0){
param.device.protocol = res[2];
if(param.device.protocol !== 'Debug' && param.device.protocol !== 'Slave'){

Expand Down Expand Up @@ -161,20 +162,21 @@ module.exports = function(data){
}
}
}

return gladys.device.create(param)
.then(function(result) {
return Promise.map(param.types, function(typ){
var state = {
value:1
};
state.value = typ.state;
var key = "state";
delete typ[key];
return gladys.deviceState.createByIdentifier(param.device.identifier, param.device.service, typ.type, state);
});
})
.catch(function(e) {
sails.log.warn('RFLink error during creation or update of device : ' + e);
});
};

if(shared.getConfEn()){
gladys.device.create(param);
}
return Promise.map(param.types, function(typ){
var state = {
value:1
};
state.value = typ.state;
var key = "state";
delete typ[key];
return gladys.deviceState.createByIdentifier(param.device.identifier, param.device.service, typ.type, state);
})
.catch(function(e) {
sails.log.warn('RFLink error during creation or update of device : ' + e + '.');
sails.log.warn('Pass to configuration mode if you want to add new devices.');
});
};
14 changes: 14 additions & 0 deletions lib/install.js
@@ -0,0 +1,14 @@
// Create parameters
module.exports = function(){

var param = {
name: 'RFLink_tty',
value: '/dev/ttyACM0'
};
//then we check if the parameters exists
return gladys.param.getValue(param.name)
.catch(() => {
//is they doesn't, we create them
return gladys.param.setValue(param);
});
};
53 changes: 23 additions & 30 deletions lib/setup.js
@@ -1,53 +1,38 @@
var SerialPort = require('serialport');
var Promise = require('bluebird');
var connect = require('./connect.js');
var shared = require('./shared.js');

module.exports = function() {

// first, we list all connected USB devices
return listUsbDevices()
.then(function(ports) {


sails.log.info(`List of available connected Arduinos(pick the name of the one with RFLink and put it -as it is- in the RFLink_tty variable on Gladys):`);
// we keep only the arduinos
return filterArduino(ports);
})
.then(function(arduinos) {

// foreach arduino, we create a device
return createDevices(arduinos);
})
.then(function() {

// we connect
return connect();
})
.then(function(){
//we enable detection
sails.log.info(`RFLink go to learning mode for 5 minutes`);
startConfig();
//we wait 5 minutes before stopping detection
setTimeout(stopConfig, 300000);
sails.log.info(`RFLink return to standard mode`);
return Promise.resolve();
})
};

function createDevices(arduinos) {

return Promise.map(arduinos, function(arduino) {

var arduinoObj = {
device: {
name: 'Arduino',
protocol: 'usb',
service: 'serial',
identifier: arduino.comName
},
types: []
};

return gladys.device.create(arduinoObj);
});
}

function filterArduino(ports) {
var arduinos = [];

// foreach port we test if it is an arduino
ports.forEach(function(port) {
if (port.manufacturer && port.manufacturer.toLowerCase().search("arduino") != -1) {
arduinos.push(port);
sails.log.info(`-` + port.comName);
arduinos.push(port);
}
});

Expand All @@ -62,4 +47,12 @@ function listUsbDevices() {
return resolve(ports);
});
});
}

function startConfig(){
shared.setConfEn(true);
}

function stopConfig(){
shared.setConfEn(false);
}
8 changes: 8 additions & 0 deletions lib/shared.js
@@ -1,4 +1,5 @@
var ports = [];
var conf_enabled = false;
var connect = require('./connect.js');
var Promise = require('bluebird');

Expand All @@ -11,6 +12,13 @@ module.exports = {
getPorts: function() {
return ports;
},

getConfEn: function() {
return conf_enabled;
},
setConfEn: function(Status) {
conf_enabled = Status;
},

reset: function(){

Expand Down

0 comments on commit 012627a

Please sign in to comment.