-
Notifications
You must be signed in to change notification settings - Fork 31
/
receivedMessages.js
214 lines (188 loc) · 6.6 KB
/
receivedMessages.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
211
212
213
214
//------------------------------------------------------------------------------
// Copyright IBM Corp. 2016
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//------------------------------------------------------------------------------
module.exports = function() {
return {
"handle" : function(data, mqttClient) {
var messageId = data.readUInt8(1);
var date = new Date();
if (messageId == '23') {
// example: <Buffer 01 17>
var desc = 'Ping Response Received';
console.log('Message: ' + messageId, data, desc);
if (mqttClient) {
mqttClient.publish('iot-2/evt/' + messageId + '/fmt/json', JSON.stringify({
"d" : {
"description" : desc,
"date": date
}
}), function () {
});
}
}
else if (messageId == '25') {
// example: <Buffer 05 19 6e 26 00 00>
var desc = 'Version Received';
var version = data.readUInt16LE(2);
console.log('Message: ' + messageId, data, desc + ' - version: ' + version);
if (mqttClient) {
mqttClient.publish('iot-2/evt/' + messageId + '/fmt/json', JSON.stringify({
"d" : {
"description" : desc,
"date": date,
"version": version
}
}), function () {
});
}
}
else if (messageId == '27') {
// example: <Buffer 03 1b 50 0f>
var desc = 'Battery Level Received';
var level = data.readUInt16LE(2);
console.log('Message: ' + messageId, data, desc + ' - level: ' + level);
if (mqttClient) {
mqttClient.publish('iot-2/evt/' + messageId + '/fmt/json', JSON.stringify({
"d" : {
"description" : desc,
"date": date,
"level": level
}
}), function () {
});
}
}
else if (messageId == '39') {
// example: <Buffer 10 27 21 28 48 e1 86 c2 02 01 47 00 00 00 02 fa 00>
var desc = 'Localization Position Update Received';
var pieceLocation = data.readUInt8(2);
var pieceId = data.readUInt8(3); // in my starter kit:
// 1 x straight: 36
// 1 x straight: 39
// 1 x straight: 40
// 1 x curve: 20
// 1 x curve: 23
// 2 x curve: 18
// 2 x curve: 17
// 1 x start/finish: 34 (long) and 33 (short)
var offset = data.readFloatLE(4);
var speed = data.readUInt16LE(8);
console.log('Message: ' + messageId, data, desc + ' - offset: ' + offset + ' speed: ' + speed + ' - pieceId: ' + pieceId + ' pieceLocation: ' + pieceLocation);;
if (mqttClient) {
mqttClient.publish('iot-2/evt/' + messageId + '/fmt/json', JSON.stringify({
"d" : {
"description" : desc,
"date": date,
"offset": offset,
"speed": speed,
"pieceId": pieceId,
"pieceLocation": pieceLocation
}
}), function () {
});
}
}
else if (messageId == '41') {
// example: <Buffer 12 29 00 00 02 2b 55 c2 00 ff 81 46 00 00 00 00 00 25 32>
var desc = 'Localization Transition Update Received';
var offset = data.readFloatLE(4);
console.log('Message: ' + messageId, data, desc + ' - offset: ' + offset);
if (mqttClient) {
mqttClient.publish('iot-2/evt/' + messageId + '/fmt/json', JSON.stringify({
"d" : {
"description" : desc,
"date": date,
"offset": offset
}
}), function () {
});
}
}
else if (messageId == '43') {
// example: <Buffer 01 2b>
var desc = 'Vehicle Delocalized Received';
console.log('Message: ' + messageId, data, desc);
if (mqttClient) {
mqttClient.publish('iot-2/evt/' + messageId + '/fmt/json', JSON.stringify({
"d" : {
"description" : desc,
"date": date
}
}), function () {
});
}
}
else if (messageId == '45') {
// example: <Buffer 06 2d 00 c8 75 3d 03>
var desc = 'Offset from Road Center Update Received';
var offset = data.readFloatLE(2);
console.log('Message: ' + messageId, data, desc + ' - offset: ' + offset);
if (mqttClient) {
mqttClient.publish('iot-2/evt/' + messageId + '/fmt/json', JSON.stringify({
"d" : {
"description" : desc,
"date": date,
"offset": offset
}
}), function () {
});
}
}
else if (messageId == '54') {
// example: <Buffer 03 36 00 00>
//console.log('Message: ' + messageId, data, 'Not documented');
}
else if (messageId == '63') {
// example: <Buffer 05 3f 01 00 00 01>
//console.log('Message: ' + messageId, data, 'Not documented');
}
else if (messageId == '65') {
// example: <Buffer 0e 41 9a 99 7f 42 9a 99 7f 42 00 00 00 02 81>
var desc = 'Changed Offset (not documented)';
var offset = data.readFloatLE(2);
console.log('Message: ' + messageId, data, desc + ' - offset: ' + offset);
if (mqttClient) {
mqttClient.publish('iot-2/evt/' + messageId + '/fmt/json', JSON.stringify({
"d" : {
"description" : desc,
"date": date,
"offset": offset
}
}), function () {
});
}
}
else if (messageId == '67') {
// example: <Buffer 01 43>
//console.log('Message: ' + messageId, data, 'Not documented');
}
else if (messageId == '77') {
// example: <Buffer 03 4d 00 01>
//console.log('Message: ' + messageId, data, 'Not documented');
}
else if (messageId == '134') {
// example: <Buffer 0b 86 8e 00 27 08 00 00 10 10 00 00>
//console.log('Message: ' + messageId, data, 'Not documented');
}
else if (messageId == '201') {
// example: tbd
//console.log('Message: ' + messageId, data, 'Not documented');
}
else {
//console.log('Message: ' + messageId, data, 'Unknown');
}
}
};
};