Skip to content

Commit 5cb6e42

Browse files
Merge pull request #196 from seokhee1106/master
elio block created
2 parents 498774e + 9d2f8cd commit 5cb6e42

File tree

3 files changed

+416
-0
lines changed

3 files changed

+416
-0
lines changed

app/modules/elio.js

Lines changed: 387 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,387 @@
1+
function Module() {
2+
this.sp = null;
3+
4+
this.MAX_PACKET_BUFFER = 1024;
5+
this.STX = 0x02;
6+
this.ETX = 0x03;
7+
this.ESC = 0x23;
8+
this.DLE = 0x40;
9+
this.DEFAULT_CRC = 0xff;
10+
11+
this.CMD_DEVICE_OK = 0x00;
12+
this.CMD_EXECUTE = 0x01;
13+
this.CMD_STATUS = 0x02;
14+
this.CMD_ERROR = 0x03;
15+
this.CMD_SENSOR_CFG = 0xf0;
16+
17+
18+
this.DAT= 0x10;
19+
this.CMD= 0x20;
20+
this.UDP= 0x30;
21+
22+
this.sensor_sonic = 0x01;
23+
this.sensor_line1 = 0x02;
24+
this.sensor_line2 = 0x04;
25+
26+
this.configSend = false;
27+
28+
29+
this.ELIO = {
30+
DC1: 0,
31+
DC2: 1,
32+
SV1: 2,
33+
SV2: 3,
34+
35+
V3: 4,
36+
V5: 5,
37+
38+
IO1: 6,
39+
IO2: 7,
40+
IO3: 8,
41+
IO4: 9,
42+
},
43+
44+
45+
this.StatusData = {
46+
V3: 0,
47+
V5: 1,
48+
IO1: 2,
49+
IO2: 3,
50+
IO3: 4,
51+
IO4: 5,
52+
DC1: 6,
53+
DC2: 7,
54+
SV1: 8,
55+
SV2: 9,
56+
57+
58+
SONIC: 'SONIC',
59+
LINE1: 'LINE1',
60+
LINE2: 'LINE2',
61+
},
62+
63+
64+
65+
66+
this.localBuffer = new Array(14);
67+
68+
this.PacketState =
69+
{
70+
PS_STX : 0,
71+
PS_DATA : 1,
72+
PS_ESC : 2,
73+
};
74+
75+
this.buffer = new Array(this.MAX_PACKET_BUFFER);
76+
this.pos = 0;
77+
this.packetState = this.PacketState.PS_STX;
78+
79+
80+
this.sendBuffers = [];
81+
82+
this.lastTime = 0;
83+
this.lastSendTime = 0;
84+
this.isDraing = false;
85+
}
86+
87+
Module.prototype.initPacket = function()
88+
{
89+
this.packetState = this.PacketState.PS_STX;
90+
this.pos = 0;
91+
}
92+
93+
94+
Module.prototype.addData = function(data, len)
95+
{
96+
97+
//var nonPacketStream = [];
98+
99+
for (var i = 0; i < len; i++)
100+
{
101+
var ch = data[i];
102+
103+
if (ch == this.STX)
104+
{
105+
this.initPacket();
106+
this.packetState = this.PacketState.PS_DATA;
107+
continue;
108+
}
109+
110+
switch (this.packetState)
111+
{
112+
case this.PacketState.PS_STX:
113+
//nonPacketStream.push(ch);
114+
115+
//if (this.nonPacketStream.Length == this.nonPacketStream.Capacity)
116+
// this.fireNonPacketStream();
117+
//break;
118+
119+
case this.PacketState.PS_DATA:
120+
if (ch == this.ETX)
121+
{
122+
if (this.verifyCRC())
123+
{
124+
console.log("CRC ok : " +this.buffer + "len : " + this.pos);
125+
126+
this.buffer[0] ; //48 (udp)
127+
this.buffer[1]; //2 (status)
128+
129+
this.StatusData.DC1 = this.buffer[2];
130+
this.StatusData.DC2 = this.buffer[3];
131+
132+
this.StatusData.SV1 = this.buffer[4];
133+
this.StatusData.SV2 = this.buffer[5];
134+
135+
this.StatusData.V3 = this.buffer[6];
136+
this.StatusData.V5 = this.buffer[7];
137+
138+
this.StatusData.IO1 = this.buffer[8];
139+
this.StatusData.IO2 = this.buffer[9];
140+
this.StatusData.IO3 = this.buffer[10];
141+
this.StatusData.IO4 = this.buffer[11];
142+
143+
144+
this.StatusData.SONIC = (this.buffer[12] | this.buffer[13] << 8);
145+
this.StatusData.LINE1 = (this.buffer[14] | this.buffer[15] << 8) == 0 ? 1 : 0 ;
146+
this.StatusData.LINE2 = (this.buffer[16] | this.buffer[17] << 8) == 0 ? 1 : 0 ;
147+
148+
149+
}
150+
else
151+
{
152+
console.log("CRC error");
153+
}
154+
155+
this.initPacket();
156+
}
157+
else if (ch == this.ESC)
158+
{
159+
this.packetState = this.PacketState.PS_ESC;
160+
}
161+
else
162+
{
163+
if (ch < this.ETX)
164+
this.initPacket();
165+
else
166+
this.appendChar(ch);
167+
}
168+
break;
169+
case this.PacketState.PS_ESC:
170+
if (ch ==this.ESC)
171+
{
172+
this.packetState = this.PacketState.PS_DATA;
173+
this.appendChar(ch);
174+
}
175+
else
176+
{
177+
this.packetState = this.PacketState.PS_DATA;
178+
this.appendChar(ch ^ this.DLE);
179+
}
180+
break;
181+
default:
182+
break;
183+
}
184+
}
185+
186+
//this.fireNonPacketStream();
187+
}
188+
189+
Module.prototype.appendChar = function(ch)
190+
{
191+
if (this.pos >= this.MAX_PACKET_BUFFER)
192+
{
193+
this.initPacket();
194+
return;
195+
}
196+
197+
this.buffer[this.pos++] = ch;
198+
}
199+
200+
201+
Module.prototype.verifyCRC=function()
202+
{
203+
var crc = this.DEFAULT_CRC;
204+
for (var i = 0; i < this.pos; i++)
205+
{
206+
crc ^= this.buffer[i];
207+
}
208+
209+
return crc == 0;
210+
}
211+
212+
213+
214+
Module.prototype.encode=function(ch,stream)
215+
{
216+
if (ch <= this.ETX)
217+
{
218+
stream.push(this.ESC);
219+
stream.push(ch ^ this.DLE);
220+
}
221+
else if (ch == this.ESC)
222+
{
223+
stream.push(ch);
224+
stream.push(ch);
225+
}
226+
else
227+
{
228+
stream.push(ch);
229+
}
230+
};
231+
232+
Module.prototype.encodePacket=function(arr)
233+
{
234+
var data = [];
235+
data.push(this.STX);
236+
237+
var ch = 0xFF;
238+
var crc = this.DEFAULT_CRC;
239+
240+
for (var i = 0; i < arr.length; i++) {
241+
ch = arr[i];
242+
this.encode(ch, data);
243+
crc ^= ch;
244+
}
245+
246+
this.encode(crc, data);
247+
data.push(this.ETX);
248+
249+
return data;
250+
};
251+
252+
253+
Module.prototype.init = function(handler, config) {
254+
};
255+
256+
Module.prototype.setSerialPort = function (sp) {
257+
var self = this;
258+
this.sp = sp;
259+
};
260+
261+
Module.prototype.requestInitialData = function() {
262+
263+
configSend = false;
264+
var initialBuffer = new Array(5);
265+
266+
initialBuffer[0] = 0x20; // header1
267+
initialBuffer[1] = 0x50; // header2
268+
initialBuffer[2] = 0x00; // command
269+
initialBuffer[3] = 0x00; // length
270+
initialBuffer[4] = 0x00; // tail1
271+
272+
return initialBuffer;
273+
};
274+
275+
Module.prototype.checkInitialData = function(data, config) {
276+
return true;
277+
};
278+
279+
Module.prototype.afterConnect = function(that, cb) {
280+
that.connected = true;
281+
this.configSend = false;
282+
if(cb) {
283+
cb('connected');
284+
}
285+
};
286+
287+
Module.prototype.validateLocalData = function(data) {
288+
return true;
289+
};
290+
291+
292+
293+
Module.prototype.handleRemoteData = function(handler) {
294+
var self = this;
295+
296+
this.localBuffer = new Array(15);
297+
298+
this.localBuffer[0] = this.UDP;
299+
this.localBuffer[1] = this.CMD_EXECUTE;
300+
301+
for (var port in this.ELIO) {
302+
var idx = this.ELIO[port];
303+
var value = handler.read(port);
304+
305+
if (value === undefined)continue;
306+
this.localBuffer[idx + 2] = value;
307+
}
308+
309+
this.localBuffer[12] = handler.read("SONIC") == undefined ? 0 : handler.read("SONIC");
310+
this.localBuffer[13] = handler.read("LINE1") == undefined ? 0 : handler.read("LINE1");
311+
this.localBuffer[14] = handler.read("LINE2") == undefined ? 0 : handler.read("LINE2");
312+
313+
this.localBuffer = this.encodePacket(this.localBuffer);
314+
315+
};
316+
317+
Module.prototype.requestLocalData = function() {
318+
return this.localBuffer;
319+
};
320+
321+
322+
Module.prototype.isRecentData = function(port, type, data) {
323+
var isRecent = false;
324+
325+
if(port in this.recentCheckData) {
326+
if(type != this.sensorTypes.TONE && this.recentCheckData[port].type === type && this.recentCheckData[port].data === data) {
327+
isRecent = true;
328+
}
329+
}
330+
331+
return isRecent;
332+
}
333+
334+
/*
335+
ff 55 idx size data a
336+
*/
337+
Module.prototype.handleLocalData = function(data) {
338+
339+
console.log(data);
340+
this.addData(data, data.length);
341+
};
342+
343+
Module.prototype.requestRemoteData = function(handler) {
344+
for (var key in this.StatusData) {
345+
handler.write(key, this.StatusData[key]);
346+
}
347+
};
348+
349+
Module.prototype.disconnect = function(connect) {
350+
var self = this;
351+
connect.close();
352+
if(self.sp) {
353+
delete self.sp;
354+
}
355+
};
356+
357+
Module.prototype.reset = function() {
358+
this.initialize();
359+
configSend = false;
360+
};
361+
362+
363+
364+
Module.prototype.initialize = function() {
365+
this.localBuffer = new Array(15);
366+
367+
this.localBuffer[0] = this.UDP;
368+
this.localBuffer[1] = this.CMD_EXECUTE;
369+
370+
for (var port in this.ELIO) {
371+
var idx = this.ELIO[port];
372+
var value = handler.read(port);
373+
374+
if (value === undefined)continue;
375+
this.localBuffer[idx + 2] = 0;
376+
}
377+
378+
this.localBuffer[12] =0;
379+
this.localBuffer[13] = 0;
380+
this.localBuffer[14] =0;
381+
382+
this.localBuffer = this.encodePacket(this.localBuffer);
383+
};
384+
385+
386+
387+
module.exports = new Module();

0 commit comments

Comments
 (0)