devicestack
Advanced tools
Comparing version 1.1.4 to 1.2.0
@@ -71,3 +71,3 @@ var EventEmitter2 = require('eventemitter2').EventEmitter2, | ||
/** | ||
* [startLookup description] | ||
* Starts to lookup. | ||
* @param {Number} interval The interval milliseconds. [optional] | ||
@@ -95,2 +95,13 @@ * @param {Function} callback The function, that will be called when trigger has started. [optional] | ||
// at the moment the serialport.list function consumes too much memory... | ||
// (function run() { | ||
// if (!self.isRunning) return; | ||
// process.nextTick(function() { | ||
// self.trigger(function() { | ||
// run(); | ||
// }); | ||
// }); | ||
// })(); | ||
var triggering = false; | ||
@@ -120,2 +131,2 @@ self.lookupIntervalId = setInterval(function() { | ||
module.exports = DeviceLoader; | ||
module.exports = DeviceLoader; |
var EventEmitter2 = require('eventemitter2').EventEmitter2, | ||
util = require('util'), | ||
_ = require('lodash'); | ||
_ = require('lodash'), | ||
async = require('async'); | ||
@@ -37,2 +38,3 @@ /** | ||
self.incomming = self.incomming.concat(Array.prototype.slice.call(frame, 0)); | ||
self.trigger(); | ||
} else { | ||
@@ -52,3 +54,2 @@ if (self.unwrapFrame) { | ||
if (self.log) self.log('close'); | ||
self.stop(); | ||
self.emit('close'); | ||
@@ -58,2 +59,3 @@ self.removeAllListeners(); | ||
deviceOrFrameHandler.removeAllListeners('receive'); | ||
self.incomming = []; | ||
}); | ||
@@ -72,4 +74,2 @@ } | ||
}); | ||
this.start(); | ||
} | ||
@@ -80,61 +80,56 @@ | ||
/** | ||
* Only starts if analyzeNextFrame function is defined. | ||
* Creates an interval that calls analyzeNextFrame function. | ||
* @param {Number} interval The interval milliseconds. [optional] | ||
* @param {Function} callback The function, that will be called when framehandler is started. [optional] | ||
* `function(err){}` | ||
* Analyzes the incomming data. | ||
*/ | ||
FrameHandler.prototype.start = function(interval, callback) { | ||
if (!callback && _.isFunction(interval)) { | ||
callback = interval; | ||
interval = null; | ||
FrameHandler.prototype.analyze = function() { | ||
if (this.incomming.length === 0) return; | ||
var nextFrame; | ||
while ((nextFrame = this.analyzeNextFrame(this.incomming))) { | ||
if (this.unwrapFrame) { | ||
var unwrappedFrame = this.unwrapFrame(_.clone(nextFrame)); | ||
if (this.log) this.log('receive unwrapped frame: ' + unwrappedFrame.toHexDebug()); | ||
this.emit('receive', unwrappedFrame); | ||
} else { | ||
if (this.log) this.log('receive frame: ' + nextFrame.toHexDebug()); | ||
this.emit('receive', nextFrame); | ||
} | ||
} | ||
}; | ||
if (!this.analyzeNextFrame) { | ||
if (callback) { callback(null); } | ||
/** | ||
* Triggers for analyzing incomming bytes. | ||
*/ | ||
FrameHandler.prototype.trigger = function() { | ||
if (this.isAnalyzing || this.incomming.length === 0) return; | ||
this.isAnalyzing = true; | ||
this.analyze(); | ||
if (this.incomming.length === 0) { | ||
this.isAnalyzing = false; | ||
return; | ||
} | ||
if (this.lookupIntervalId) { | ||
this.stopLookup(); | ||
} | ||
var self = this; | ||
interval = interval || 50; | ||
var analyzing = false; | ||
this.lookupIntervalId = setInterval(function() { | ||
if (analyzing) { | ||
return; | ||
async.whilst( | ||
function() { | ||
return self.incomming.length > 0; | ||
}, | ||
function(callback) { | ||
setTimeout(function() { | ||
self.analyze(); | ||
callback(); | ||
}, 10); | ||
}, function(err) { | ||
self.isAnalyzing = false; | ||
} | ||
analyzing = true; | ||
var nextFrame; | ||
while ((nextFrame = self.analyzeNextFrame(self.incomming))) { | ||
if (self.unwrapFrame) { | ||
var unwrappedFrame = self.unwrapFrame(_.clone(nextFrame)); | ||
if (self.log) self.log('receive unwrapped frame: ' + unwrappedFrame.toHexDebug()); | ||
self.emit('receive', unwrappedFrame); | ||
} else { | ||
if (self.log) self.log('receive frame: ' + nextFrame.toHexDebug()); | ||
self.emit('receive', nextFrame); | ||
} | ||
} | ||
analyzing = false; | ||
}, interval); | ||
if (callback) { callback(null); } | ||
); | ||
}; | ||
/** | ||
* Stops the interval that calls analyzeNextFrame function. | ||
*/ | ||
FrameHandler.prototype.stop = function() { | ||
if (this.lookupIntervalId) { | ||
clearInterval(this.lookupIntervalId); | ||
this.lookupIntervalId = null; | ||
} | ||
}; | ||
/** | ||
* The send mechanism. | ||
@@ -147,2 +142,2 @@ * @param {Array} data A "byte" array. | ||
module.exports = FrameHandler; | ||
module.exports = FrameHandler; |
{ | ||
"name": "devicestack", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"description": "This module helps you to represent a device and its protocol.", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -156,11 +156,11 @@ <pre> | ||
- reacts on receive of lower layer, calls `unwrapFrame` function if exists and emits `receive` | ||
- automatically calls `start` function | ||
- automatically analyzes incomming data | ||
### start | ||
Only starts if `analyzeNextFrame` function is defined. Creates an interval that calls `analyzeNextFrame` function. | ||
### analyze | ||
Calls `analyzeNextFrame` function in a loop. | ||
- If extending from `require('devicestack').FrameHandler` this mechanism is already defined! | ||
### stop | ||
Stops the interval that calls `analyzeNextFrame` function. | ||
### trigger | ||
Triggers the `analyze` function. | ||
@@ -167,0 +167,0 @@ - If extending from `require('devicestack').FrameHandler` this mechanism is already defined! |
@@ -19,3 +19,3 @@ if (!Buffer.prototype.toHexDebug) { | ||
} | ||
return res; | ||
return res.toUpperCase(); | ||
}; | ||
@@ -22,0 +22,0 @@ } |
1728
82275