-
Notifications
You must be signed in to change notification settings - Fork 0
/
apc100.js
210 lines (156 loc) · 4.68 KB
/
apc100.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'use strict';
var util = require('util');
var SerialPort = require('serialport');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var logger = require('./index').Sensor.getLogger('Sensor');
var serialOpts = {
baudRate: 9600,
parity: 'none',
parser: SerialPort.parsers.byteDelimiter(']'.charCodeAt(0))
};
//var SERIAL_PORT_FILE = '/dev/ttyUSB0';
var SERIAL_PORT_FILE = '/dev/ttyS1';
var POLLING_INTERVAL = 1000; // 1 sec
var DEVICE_ID = '0000'; // Only one device can be connected because polling
// interval should not be shorter than 1 sec.
var POLLING_MSG = '[' + DEVICE_ID + 'BTR]';
var RETRY_OPEN_INTERVAL = 3000; // 3sec
function isInvalid() {
return false;
}
function parseMessage(data) {
var result = {};
var dataArray = new Buffer(data).toString().split(',');
result.totalIn = parseInt(dataArray[1]);
result.totalOut = parseInt(dataArray[2]);
result.current = parseInt(dataArray[3]);
return result;
}
function openSerialPort(apc100, cb) {
var self;
if (_.isFunction(apc100)) {
self = module.exports;
cb = apc100;
} else {
self = apc100;
}
self.port = new SerialPort(SERIAL_PORT_FILE, serialOpts, function onOpen(err) {
logger.info('[APC100] Connected');
if (err) {
logger.error('Serial port error during opening:', err);
return cb;
} else {
logger.info('[APC100] No err, Connected');
}
self.port.on('error', function onError(err) {
logger.error('Serial port error:', err);
return;
});
self.port.on('close', function onError(err) {
if (err) {
logger.error('Serial port error during closing:', err);
// TODO: if error, isn't this closed?
} else {
logger.info('Serial port is closed');
}
return;
});
self.port.on('disconnect', function onError(err) {
logger.error('Serial port is disconnected:', err);
return;
});
self.isOpen = true;
self.port.on('data', function onData(data) {
var values;
logger.trace('[APC100] onData():', new Buffer(data).toString());
if (isInvalid(data)) {
logger.error('Invalid message:', new Buffer(data).toString());
return;
}
values = parseMessage(data);
self.emit('data', values);
});
});
}
function openSerialCallback(err) {
setTimeout(function () {
openSerialPort(openSerialCallback);
}, RETRY_OPEN_INTERVAL);
}
// TODO: Find serial port file. ttyUSB0 or ttyS1(E220)
// TODO: If opening port takes long time, async function cannot be finished.
function APC100 () {
var self = this;
EventEmitter.call(self);
self.timer = null;
self.deviceId = DEVICE_ID;
self.registeredSensors = [];
openSerialPort(self, openSerialCallback);
/*
self.port = new SerialPort(SERIAL_PORT_FILE, serialOpts, function onOpen(err) {
if (err) {
logger.error('Serial port error during opening:', err);
return;
} else {
logger.info('[APC100] Connected');
}
self.port.on('error', function onError(err) {
logger.error('Serial port error:', err);
return;
});
self.port.on('close', function onError(err) {
if (err) {
logger.error('Serial port error during closing:', err);
// TODO: if error, isn't this closed?
} else {
logger.info('Serial port is closed');
}
return;
});
self.port.on('disconnect', function onError(err) {
logger.error('Serial port is disconnected:', err);
return;
});
self.isOpen = true;
self.port.on('data', function onData(data) {
var values;
logger.trace('[APC100] onData():', new Buffer(data).toString());
if (isInvalid(data)) {
logger.error('Invalid message:', new Buffer(data).toString());
return;
}
values = parseMessage(data);
self.emit('data', values);
});
});
*/
}
util.inherits(APC100, EventEmitter);
APC100.prototype.startPolling = function() {
var self = this;
if (!self.timer) {
self.timer = setInterval(function () {
logger.trace(POLLING_MSG, self.port.isOpen());
self.port.write(POLLING_MSG);
}, POLLING_INTERVAL);
}
};
APC100.prototype.stopPolling = function() {
if (this.registeredSensors.length && this.timer) {
clearInterval(this.timer);
this.timer = null;
}
};
APC100.prototype.registerSensor = function(id) {
this.registeredSensors.push(id);
this.registeredSensors = _.uniq(this.registeredSensors);
};
APC100.prototype.deregisterSensor = function(id) {
_.pull(this.registeredSensors, id);
};
APC100.prototype.close = function() {
logger.info('Closing serial port');
this.port.close();
};
module.exports = new APC100();