Skip to content

Commit 6ec72ef

Browse files
Serial 데이터가 일부 잘려오는 경우를 수용하도록 변경
hardwareMonitor 이미지 변경
1 parent 4ff1949 commit 6ec72ef

File tree

3 files changed

+259
-21
lines changed

3 files changed

+259
-21
lines changed

src/playground/blocks/hardwareLite/block_neobot_purple_lite.js

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
(function() {
3+
const HEADER = [0xab, 0xcd];
34
Entry.NeobotPurpleLite = new (class NeobotPurpleLite {
45
constructor() {
56
this.id = '5.5';
@@ -81,7 +82,7 @@
8182

8283
get monitorTemplate() {
8384
return {
84-
imgPath: 'hw_lite/neobot_purple_lite.png',
85+
imgPath: 'hw/neobot_purple.png',
8586
width: 700,
8687
height: 700,
8788
listPorts: {
@@ -161,17 +162,95 @@
161162
}
162163

163164
handleLocalData(data) {
164-
for (let i = 0; i < data.length - 1; i++) {
165-
if (data[i] === 171 && data[i + 1] === 205) {
166-
const dataSet = data.slice(i + 2, i + 7);
167-
dataSet.forEach((value, idx) => {
168-
this.localBuffer[this.LOCAL_MAP[idx]] = value;
169-
});
165+
let validPdu = this.getValidPdu(data);
166+
while (validPdu) {
167+
this.onReceivePdu(validPdu);
168+
if (!this.remainingPdu || this.remainingPdu.length <= 0) {
170169
break;
171170
}
171+
validPdu = this.getValidPdu([]);
172172
}
173173
}
174174

175+
onReceivePdu(pdu) {
176+
if (pdu[0] === HEADER[0] && pdu[1] === HEADER[1]) {
177+
this.localBuffer['IN1'] = pdu[2];
178+
this.localBuffer['IN2'] = pdu[3];
179+
this.localBuffer['IN3'] = pdu[4];
180+
this.localBuffer['IR'] = pdu[5];
181+
this.localBuffer['BAT'] = pdu[6];
182+
}
183+
}
184+
185+
getValidPdu(pdu) {
186+
const mergedPdu = [];
187+
if (this.remainingPdu) {
188+
mergedPdu.push(...this.remainingPdu);
189+
this.remainingPdu = null;
190+
}
191+
mergedPdu.push(...pdu);
192+
if (mergedPdu.length < 2) {
193+
this.remainingPdu = [...mergedPdu];
194+
return null;
195+
}
196+
197+
// 헤더 불일치는 버림
198+
if (!this.checkHeader(mergedPdu)) {
199+
return null;
200+
}
201+
202+
// 유효 데이터 길이는 header 2 + body 5 + checksum 1 = 8
203+
const validDataLength = 8;
204+
/*
205+
전체 길이가 유효 데이터 길이보다 작을 경우
206+
아직 도착하지 않은 부분이 있으므로 병합을 위해 remainingPdu 에 저장
207+
*/
208+
if (mergedPdu.length < validDataLength) {
209+
this.remainingPdu = [...mergedPdu];
210+
return null;
211+
}
212+
213+
/*
214+
전체 길이가 유효 데이터 길이보다 클 경우
215+
유효한 부분만 잘라내고 나머지는 remainingPdu 에 저장
216+
*/
217+
if (mergedPdu.length > validDataLength) {
218+
this.remainingPdu = mergedPdu.slice(validDataLength, mergedPdu.length);
219+
}
220+
221+
const validPdu = mergedPdu.slice(0, validDataLength);
222+
223+
/*
224+
유효 Pdu 의 checksum 확인
225+
*/
226+
const dataLength = 5;
227+
let checkSum = 0;
228+
for (let i = 0; i < dataLength; i++) {
229+
checkSum += validPdu[i + 2];
230+
}
231+
checkSum = checkSum & 255;
232+
const pduCheckSum = validPdu[7];
233+
if (pduCheckSum !== checkSum) {
234+
return null;
235+
}
236+
237+
return validPdu;
238+
}
239+
240+
checkHeader(pdu) {
241+
if (pdu.length < HEADER.length) {
242+
return false;
243+
}
244+
245+
for (let i = 0; i < HEADER.length; i++) {
246+
if (HEADER[i] !== pdu[i]) {
247+
return false;
248+
}
249+
}
250+
251+
return true;
252+
}
253+
175254
requestLocalData() {
176255
const requestData = [];
177256

src/playground/blocks/hardwareLite/block_neobot_soco_lite.js

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
(function() {
3+
const HEADER = [0xab, 0xcd];
34
Entry.NeobotSocoLite = new (class NeobotSocoLite {
45
constructor() {
56
this.id = '5.6';
@@ -81,7 +82,7 @@
8182

8283
get monitorTemplate() {
8384
return {
84-
imgPath: 'hw_lite/neobot_soco_lite.png',
85+
imgPath: 'hw/neobot_soco.png',
8586
width: 700,
8687
height: 700,
8788
listPorts: {
@@ -125,17 +126,95 @@
125126
}
126127

127128
handleLocalData(data) {
128-
for (let i = 0; i < data.length - 1; i++) {
129-
if (data[i] === 171 && data[i + 1] === 205) {
130-
const dataSet = data.slice(i + 2, i + 7);
131-
dataSet.forEach((value, idx) => {
132-
this.localBuffer[this.LOCAL_MAP[idx]] = value;
133-
});
129+
let validPdu = this.getValidPdu(data);
130+
while (validPdu) {
131+
this.onReceivePdu(validPdu);
132+
if (!this.remainingPdu || this.remainingPdu.length <= 0) {
134133
break;
135134
}
135+
validPdu = this.getValidPdu([]);
136136
}
137137
}
138138

139+
onReceivePdu(pdu) {
140+
if (pdu[0] === HEADER[0] && pdu[1] === HEADER[1]) {
141+
this.localBuffer['IN1'] = pdu[2];
142+
this.localBuffer['IN2'] = pdu[3];
143+
this.localBuffer['IN3'] = pdu[4];
144+
this.localBuffer['IR'] = pdu[5];
145+
this.localBuffer['BAT'] = pdu[6];
146+
}
147+
}
148+
149+
getValidPdu(pdu) {
150+
const mergedPdu = [];
151+
if (this.remainingPdu) {
152+
mergedPdu.push(...this.remainingPdu);
153+
this.remainingPdu = null;
154+
}
155+
mergedPdu.push(...pdu);
156+
if (mergedPdu.length < 2) {
157+
this.remainingPdu = [...mergedPdu];
158+
return null;
159+
}
160+
161+
// 헤더 불일치는 버림
162+
if (!this.checkHeader(mergedPdu)) {
163+
return null;
164+
}
165+
166+
// 유효 데이터 길이는 header 2 + body 5 + checksum 1 = 8
167+
const validDataLength = 8;
168+
/*
169+
전체 길이가 유효 데이터 길이보다 작을 경우
170+
아직 도착하지 않은 부분이 있으므로 병합을 위해 remainingPdu 에 저장
171+
*/
172+
if (mergedPdu.length < validDataLength) {
173+
this.remainingPdu = [...mergedPdu];
174+
return null;
175+
}
176+
177+
/*
178+
전체 길이가 유효 데이터 길이보다 클 경우
179+
유효한 부분만 잘라내고 나머지는 remainingPdu 에 저장
180+
*/
181+
if (mergedPdu.length > validDataLength) {
182+
this.remainingPdu = mergedPdu.slice(validDataLength, mergedPdu.length);
183+
}
184+
185+
const validPdu = mergedPdu.slice(0, validDataLength);
186+
187+
/*
188+
유효 Pdu 의 checksum 확인
189+
*/
190+
const dataLength = 5;
191+
let checkSum = 0;
192+
for (let i = 0; i < dataLength; i++) {
193+
checkSum += validPdu[i + 2];
194+
}
195+
checkSum = checkSum & 255;
196+
const pduCheckSum = validPdu[7];
197+
if (pduCheckSum !== checkSum) {
198+
return null;
199+
}
200+
201+
return validPdu;
202+
}
203+
204+
checkHeader(pdu) {
205+
if (pdu.length < HEADER.length) {
206+
return false;
207+
}
208+
209+
for (let i = 0; i < HEADER.length; i++) {
210+
if (HEADER[i] !== pdu[i]) {
211+
return false;
212+
}
213+
}
214+
215+
return true;
216+
}
217+
139218
requestLocalData() {
140219
const requestData = [];
141220

src/playground/blocks/hardwareLite/block_neobot_thinkcar_lite.js

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22
(function() {
3+
const HEADER = [0xab, 0xcd];
4+
35
Entry.NeobotThinkCarLite = new (class NeobotThinkCarLite {
46
constructor() {
57
this.id = '5.7';
@@ -89,7 +91,7 @@
8991

9092
get monitorTemplate() {
9193
return {
92-
imgPath: 'hw_lite/neobot_thinkcar_lite.png',
94+
imgPath: 'hw/neobot_thinkcar.png',
9395
width: 700,
9496
height: 700,
9597
listPorts: {
@@ -133,15 +135,93 @@
133135
}
134136

135137
handleLocalData(data) {
136-
for (let i = 0; i < data.length - 1; i++) {
137-
if (data[i] === 171 && data[i + 1] === 205) {
138-
const dataSet = data.slice(i + 2, i + 7);
139-
dataSet.forEach((value, idx) => {
140-
this.localBuffer[this.LOCAL_MAP[idx]] = value;
141-
});
138+
let validPdu = this.getValidPdu(data);
139+
while (validPdu) {
140+
this.onReceivePdu(validPdu);
141+
if (!this.remainingPdu || this.remainingPdu.length <= 0) {
142142
break;
143143
}
144+
validPdu = this.getValidPdu([]);
145+
}
146+
}
147+
148+
onReceivePdu(pdu) {
149+
if (pdu[0] === HEADER[0] && pdu[1] === HEADER[1]) {
150+
this.localBuffer['IN1'] = pdu[2];
151+
this.localBuffer['IN2'] = pdu[3];
152+
this.localBuffer['IN3'] = pdu[4];
153+
this.localBuffer['IR'] = pdu[5];
154+
this.localBuffer['BAT'] = pdu[6];
155+
}
156+
}
157+
158+
getValidPdu(pdu) {
159+
const mergedPdu = [];
160+
if (this.remainingPdu) {
161+
mergedPdu.push(...this.remainingPdu);
162+
this.remainingPdu = null;
163+
}
164+
mergedPdu.push(...pdu);
165+
if (mergedPdu.length < 2) {
166+
this.remainingPdu = [...mergedPdu];
167+
return null;
168+
}
169+
170+
// 헤더 불일치는 버림
171+
if (!this.checkHeader(mergedPdu)) {
172+
return null;
173+
}
174+
175+
// 유효 데이터 길이는 header 2 + body 5 + checksum 1 = 8
176+
const validDataLength = 8;
177+
/*
178+
전체 길이가 유효 데이터 길이보다 작을 경우
179+
아직 도착하지 않은 부분이 있으므로 병합을 위해 remainingPdu 에 저장
180+
*/
181+
if (mergedPdu.length < validDataLength) {
182+
this.remainingPdu = [...mergedPdu];
183+
return null;
184+
}
185+
186+
/*
187+
전체 길이가 유효 데이터 길이보다 클 경우
188+
유효한 부분만 잘라내고 나머지는 remainingPdu 에 저장
189+
*/
190+
if (mergedPdu.length > validDataLength) {
191+
this.remainingPdu = mergedPdu.slice(validDataLength, mergedPdu.length);
192+
}
193+
194+
const validPdu = mergedPdu.slice(0, validDataLength);
195+
196+
/*
197+
유효 Pdu 의 checksum 확인
198+
*/
199+
const dataLength = 5;
200+
let checkSum = 0;
201+
for (let i = 0; i < dataLength; i++) {
202+
checkSum += validPdu[i + 2];
203+
}
204+
checkSum = checkSum & 255;
205+
const pduCheckSum = validPdu[7];
206+
if (pduCheckSum !== checkSum) {
207+
return null;
208+
}
209+
210+
return validPdu;
211+
}
212+
213+
checkHeader(pdu) {
214+
if (pdu.length < HEADER.length) {
215+
return false;
216+
}
217+
218+
for (let i = 0; i < HEADER.length; i++) {
219+
if (HEADER[i] !== pdu[i]) {
220+
return false;
221+
}
144222
}
223+
224+
return true;
145225
}
146226

147227
requestLocalData() {

0 commit comments

Comments
 (0)