refactor code

The communication with the arduino is now handled by a designated module (called ar-com).
This commit is contained in:
Shannon Sterz 2016-01-14 21:19:41 +01:00
parent 5f26eb8c47
commit 54850cde26
2 changed files with 59 additions and 27 deletions

55
nodejs/ar-com/arduino.js Normal file
View file

@ -0,0 +1,55 @@
var events = require('events'),
child = require('child_process'),
util = require('util'),
serial = require("serialport").SerialPort;
var Arduino = function (baudrate) {
this.baudrate = baudrate && baudrate || 9600;
this.writeBuffer = [];
this.success;
}
Arduino.prototype.setup = function () {
child.exec('cd ../arduino && ino build && ino upload', function(err, stdout, stderr){
if(err){console.log('Please install the ino utility and connect arduino!\nIf there are other open programs that communicate with the arduino, close them!'); return this.success = false;}
var buff = stdout.slice(stdout.indexOf('Guessing serial port ... ')+25);
buff = buff.slice(0, buff.indexOf('\n'));
console.log('Guessing serial port ... ' + buff);
this.serial = new serial(buff, {
baudrate: this.baudrate,
parser: serial.parsers.readline('\n')
}, false);
this.serial.open(function (error) {
if ( error ) {
console.log('Please check arduino!')
return this.success = false;
}
serialPort.write(new Buffer('00000000'));
});
this.success = true;
this.processWriteBuffer();
});
}
Arduino.prototype.write = function (m) {
if (this.success) {
this.serial.write(m);
} else {
this.writeBuffer.push(m);
console.log('Board isn\'t available (yet?).')
}
}
Arduino.prototype.processWriteBuffer = function () {
while (this.writeBuffer.length > 0) {
this.write(this.writeBuffer.shift());
}
}
module.exports = Arduino;

View file

@ -1,32 +1,9 @@
var express = require('express');
var router = express.Router();
var child = require("child_process");
var Arduino = new require('../ar-com/arduino');
var arduino = new Arduino();
var SerialPort = require("serialport").SerialPort
var success;
child.exec('cd ../arduino && ino build && ino upload', function(err, stdout, stderr){
if(err){console.log('Please install the ino utility and connect arduino!\nIf there are other open programs that communicate with the arduino, close them!'); return success = false;}
var buff = stdout.slice(stdout.indexOf('Guessing serial port ... ')+25);
buff = buff.slice(0, buff.indexOf('\n'));
console.log('Guessing serial port ... ' + buff);
serialPort = new SerialPort(buff, {
baudrate: 9600
}, false);
serialPort.open(function (error) {
if ( error ) {
console.log('Please check arduino!')
return successfalse;
}
});
return success = true;
});
//console.log(success);
arduino.setup()
/* GET home page. */
router.get('/', function(req, res, next) {
@ -34,7 +11,7 @@ router.get('/', function(req, res, next) {
});
router.get('/toggleLight', function (req, res) {
if(success) { serialPort.write(new Buffer('~','ascii')); } else {console.log("arduino not connected")}
arduino.write(new Buffer('~','ascii'));
res.send('switch light');
});