-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder-test.js
More file actions
145 lines (116 loc) · 3.61 KB
/
Copy pathdecoder-test.js
File metadata and controls
145 lines (116 loc) · 3.61 KB
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
const elpp = require('./decoder')
const encoder = require('./encoder')
const log = console.log
const log_obj = function (obj) { console.dir(obj, { depth: null }) }
log('starting decoder tests')
var DEBUG = true
function TRACE_D(msg) {
if (DEBUG) {
console.log(msg)
}
}
function temp_processor(out, data) {
/* do something with decoded temp data */
data['temp'] = out[0]
}
function pm_processor(out, data) {
data['pm'] = {
'1.0': out[0],
'2.5': out[1],
'4.0': out[2],
'10.0': out[3],
'temp': out[4]
}
}
function time_processor(out, data) {
data['time'] = {
'flags': out[0],
'epoch': out[1]
}
}
function accel_processor(out, data) {
data['accel'] = {
'x' : out[0],
'y' : out[1],
'z' : out[2],
}
}
function devstartup_processor(out, data) {
/* 'data' is a namespace for the processor to write into; it will be post-processed and returned by the platform */
/* out is the decoded data items as an array:
0 : decoded varuint32 field (array length of firmware version digits)
1 : decoded array bytes of firmware version digits
2 : reset flags
*/
ver_digits = out[0]
data['devstartup'] = {
'fw_ver' : 'v' + out[1].map(x => x.toString()).join('.'),
'reset_flags' : '0x' + out[2].toString(16)
}
}
/* The channel map defines the application's mapping of channels to data type decoders.
* The data type decoders form a root of a possibly heirarch of decoders and primitive decoders.
* A processor function is required to convert the output into something the cloud platform can
* work with. The processor gets a list of decoded output values.
*
* A Measurement{Earth} Trusted Sensor Platform module will specify this mapping.
*/
var channel_map = {
0: { decoder: elpp.temperature_decoder, processor: temp_processor },
1: { decoder: elpp.particle_decoder, processor: pm_processor },
2: { decoder: elpp.accel_decoder, processor: accel_processor },
10: { decoder: elpp.time_decoder, processor: time_processor },
30: { decoder: elpp.devstartup_decoder, processor: devstartup_processor }
}
var platform = {
/* Setup the object in a platform specific way */
pre_process: function (obj) {
},
/* Post process and return the data specific to the platform */
post_process: function (obj) {
return obj
}
}
function temperature_provider() {
return [-12.7]
}
function pm_provider() {
var data = [
1023,//36,
1023,//18,
1024,//76,
1023,//557,
0,
-2090.8,
'111111111111'
]
return data
}
function time_provider() {
return [0x1, (Date.now() / 1000)]
}
function accel_provider() {
return [12,-1234,12345678]
}
function devstartup_provider() {
/* encoding an array of version digits, and a uint16 reset flags */
var data = [
[2,1,6,5], /* v2.1.6-5 */
0xAF03
]
return data
}
var encoder_map = {
0: { encoder: encoder.temperature_encoder, provider: temperature_provider },
1: { encoder: encoder.particle_encoder, provider: pm_provider },
2: { encoder: encoder.accel_encoder, provider: accel_provider },
10: {encoder: encoder.time_encoder, provider: time_provider},
30: {encoder: encoder.devstartup_encoder, provider: devstartup_provider }
}
/* vector is a list of channel numbers to encode, in order. */
var test_vec = encoder.encoder([10, 0, 1, 2, 30], encoder_map)
log('== encoded ==')
log(Buffer.from(test_vec).toString('hex'))
log('== decoded ==')
var result = elpp.decoder(test_vec, channel_map, platform)
log_obj(result)