-
Notifications
You must be signed in to change notification settings - Fork 0
/
oada_parse.js
105 lines (93 loc) · 3.14 KB
/
oada_parse.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
/* Copyright 2014 Open Ag Data Alliance
*
* 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.
*/
'use strict';
var ISOBUS_PARSE = (function() {
var pgnref;
var pgnarray;
var config;
//----- Exported functions ------
var _configure = function(configuration){
pgnref = {};
pgnarray = [];
config = configuration;
for (var i in config) {
pgnarray.push(config[i].pgn);
if(typeof pgnref[config[i].pgn] == 'undefined'){
pgnref[config[i].pgn] = [];
}
pgnref[config[i].pgn].push(i);
}
};
var parseArray = function(input) {
var obj = {};
var message_list = [];
for (var i = 0; i < input.length; i++) {
message_list = [];
if (pgnarray.indexOf(input[i].pgn) > -1) {
message_list = pgnref[input[i].pgn];
for (var j = 0; j < message_list.length; j++) {
if(typeof obj[message_list[j]] == 'undefined'){
obj[message_list[j]] = [];
}
obj[message_list[j]].push({timestamp: input[i].timestamp, measurement: parseData(input[i].data, config[message_list[j]].low, config[message_list[j]].high, config[message_list[j]].mul, config[message_list[j]].add)});
}
}
}
return obj;
};
var _getBytes = function(payload) {
// function returns an 8-byte array from a hex string payload (padded with 0 bytes if necessary)
var data = [];
var str = payload;
while (str.length > 0) {
if (str.length == 1) {
data.push(h2d(str));
str = str.substring(1, str.length);
} else {
data.push(h2d(str.substring(0, 2)));
str = str.substring(2, str.length);
}
}
// pad with 0 bytes
while (data.length < 8) {
data.push(0);
}
return data;
};
//---- Helper functions -----
function parseData(data, smallbyte, bigbyte, mul, add) {
// data is an array of 8 bytes
// smallbyte and bigbyte specify the region of bytes to parse.
// i.e. smallbyte=0, bigbyte=2 would parse the first 3 bytes in the data word
// mul and add represent factors to multiply and add to the parsed byte sequence
// function makes no assumption about endianness of data
// Note this function will fail if caller tries to retrieve too many bytes (js uses only 53 bits in numbers).
var ret = 0x0;
for (var i = smallbyte; i <= bigbyte; i++) {
ret += data[i] * Math.pow(256, i - smallbyte);
}
ret *= mul;
ret += add;
return ret;
}
// Convert hex string into decimal number
function h2d(h) {return parseInt(h,16);}
return {
configure: _configure,
parse: parseArray,
getBytes: _getBytes
};
}());
module.exports = ISOBUS_PARSE;