-
Notifications
You must be signed in to change notification settings - Fork 7
/
ExtPlaneJs.js
167 lines (129 loc) · 3.43 KB
/
ExtPlaneJs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
/**
*
* @name: ExtPlaneJs
* @author: Wade Wildbore - Bluu Interactive
* @description: ExtPlane TCP Connector for NodeJS
* @see: https://github.com/vranki/ExtPlane - For more information
*/
var EventEmitter = require('events').EventEmitter;
var client = require('./client');
var util = require('util');
var async = require('async');
var base64 = require('base-64');
function ExtPlaneJs(config){
// Ref to &this
var self = this;
// CTOR
EventEmitter.call(this);
// ExtPlaneJs Config
this.config = config;
// ExtPlane TCP Client
this.client = client(this.config);
// Couple the TCP clients on data event to ExtPlaneJs
this.client.on('data', function(data){
self.emit('data', data);
});
/**
*
* Loaded
*
*/
this.on('loaded', function(){
if(this.config.debug) console.log('ExtPlane Ready!');
});
/**
*
* Data
*
* @param {string} data
*/
this.on('data', function(data){
// log incoming TCP stream
//console.log(data.toString());
if(data.toString().includes('EXTPLANE'))
{
// loaded..
return this.emit('loaded');
}
// emit parse
return this.emit('parse', data.toString());
});
/**
*
* Parse
*
* Either emit single data-ref events, or broadcast all data-ref events on one handler
*
* @param {string} data
*/
this.on('parse', function(data){
//
var commands = data.trim().split('\n');
async.each(commands, function(cmd, cb){
self.parseDataRef(cmd, cb);
}, function(err){
if(err && this.config.debug) console.log(err);
});
});
/**
*
* Parse an Individual Data Ref
*
* @param {string} data_ref - The data_ref response
* @param {function} cb - The done callback
*/
this.parseDataRef = function(data_ref, cb){
var params = data_ref.split(' ');
// if not data-ref output, break
if(params[0][0] !== 'u')
return false;
// data-ref
data_ref = params[1];
var type = params[0].substring(1);
var value = this.parseValue(type, params[2]);
// emit - data_ref, value or 'data-ref', data_ref, value
!this.config.broadcast ? this.emit(data_ref, data_ref, value) : this.emit('data-ref', data_ref, value);
// done
return cb(null);
};
/**
*
* Parse ExtPlanes TCP Text Based Response
*
* Override type value conversions from ExtPlanes response
*
* @param {string} - type
* @param {string} - value
*/
this.parseValue = function(type, value){
switch(type)
{
// int
case 'i':
return parseInt(value);
break;
// float
case 'f':
return parseFloat(value);
break;
// int array
case 'ia':
return JSON.parse(value);
break;
// float array
case 'fa':
return JSON.parse(value);
break;
// base64 data
case 'b':
return base64.decode(value);
break;
default:
return value;
break;
}
};
};
util.inherits(ExtPlaneJs, EventEmitter);
module.exports = ExtPlaneJs;