-
Notifications
You must be signed in to change notification settings - Fork 44
/
kurapayload.js
207 lines (191 loc) · 7.67 KB
/
kurapayload.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
/**
* Copyright (c) 2012, 2016 Cirrus Link Solutions
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Cirrus Link Solutions
*/
/**
* Provides support for generating Kura payloads.
*/
(function () {
var ProtoBuf = require("protobufjs"),
ByteBuffer = require("bytebuffer"),
tempBuffer = ByteBuffer.allocate(1024);
var builder = ProtoBuf.loadProtoFile('../../kurapayload.proto'),
KuraDataTypes = builder.build('kuradatatypes');
KuraPayload = KuraDataTypes.KuraPayload,
KuraMetric = KuraPayload.KuraMetric,
KuraPosition = KuraPayload.KuraPosition,
ValueType = KuraMetric.ValueType;
exports.generateKuraPayload = function(object) {
var newPayload = new KuraPayload(object.timestamp);
// Build up the position
if (object.position !== undefined && object.position !== null) {
var position = object.position,
newPosition = new KuraPosition(position.latitude, position.longitude);
if (position.altitude !== undefined && position.altitude !== null) {
newPosition.altitude = position.altitude;
}
if (position.precision !== undefined && position.precision !== null) {
newPosition.precision = position.precision;
}
if (position.heading !== undefined && position.heading !== null) {
newPosition.heading = position.heading;
}
if (position.speed !== undefined && position.speed !== null) {
newPosition.speed = position.speed;
}
if (position.timestamp !== undefined && position.timestamp !== null) {
newPosition.timestamp = position.timestamp;
}
if (position.satellites !== undefined && position.satellites !== null) {
newPosition.satellites = position.satellites;
}
if (position.status !== undefined && position.status !== null) {
newPosition.status = position.status;
}
// Add to KuraPayload
newPayload.position = newPosition;
}
// Build up the metric
if (object.metric !== undefined && object.metric !== null) {
// loop over array of metric
for (var i = 0; i < object.metric.length; i++) {
var metric = object.metric[i],
newMetric = new KuraMetric(metric.name),
value = metric.value,
type = metric.type;
// Get metric type and value
switch (type) {
case 'int':
newMetric.type = ValueType.INT32;
newMetric.int_value = value;
break;
case 'long':
newMetric.type = ValueType.INT64;
newMetric.long_value = value;
break;
case 'float':
newMetric.type = ValueType.FLOAT;
newMetric.float_value = value;
break;
case 'double':
newMetric.type = ValueType.DOUBLE;
newMetric.double_value = value;
break;
case 'boolean':
newMetric.type = ValueType.BOOL;
newMetric.bool_value = value;
break;
case 'string':
newMetric.type = ValueType.STRING;
newMetric.string_value = value;
break;
case 'bytes':
if (value instanceof ByteBuffer) {
newMetric.type = ValueType.BYTES;
newMetric.bytes_value = value;
} else {
throw new Error("Invalid object type for KuraMetric value");
}
}
newPayload.metric.push(newMetric);
}
}
// set body
if (object.body !== undefined && object.body !== null) {
if (object.body instanceof ByteBuffer) {
newPayload.bytes = object.body
} else {
throw new Error("Invalid object type for KuraPayload body");
}
}
// Return the new KuraPayload as a Buffer
return newPayload.toBuffer();
}
exports.parseKuraPayload = function(proto) {
var kuraPayload = KuraPayload.decode(proto),
timestamp = kuraPayload.timestamp,
kuraPosition = kuraPayload.position,
kuraMetrics = kuraPayload.metric,
body = kuraPayload.body,
object = {};
object.timestamp = timestamp.toNumber();
if (position !== undefined) {
var position = {};
position.latitude = kuraPosition.latitude;
position.longitude = kuraPosition.longitude;
if (kuraPosition.altitude !== undefined) {
position.altitude = kuraPostition.altitude;
}
if (kuraPosition.precision !== undefined) {
position.precision = kuraPostition.precision;
}
if (kuraPosition.heading !== undefined) {
position.heading = kuraPostition.heading;
}
if (kuraPosition.speed !== undefined) {
position.speed = kuraPostition.speed;
}
if (kuraPosition.timestamp !== undefined) {
position.timestamp = kuraPostition.timestamp;
}
if (kuraPosition.satellites !== undefined) {
position.satellites = kuraPostition.satellites;
}
if (kuraPosition.status !== undefined) {
position.status = kuraPostition.status;
}
object.position = position;
}
if (body !== undefined && body !== null) {
object.body = body;
}
if (kuraMetrics != undefined) {
metrics = [];
// loop over array of metric
for (var i = 0; i < kuraMetrics.length; i++) {
var kuraMetric = kuraMetrics[i],
metric = {};
metric.name = kuraMetric.name;
switch (kuraMetric.type) {
case 0:
metric.value = kuraMetric.double_value;
metric.type = "double";
break;
case 1:
metric.value = kuraMetric.float_value;
metric.type = "float";
break;
case 2:
metric.value = kuraMetric.long_value.toNumber();
metric.type = "long";
break;
case 3:
metric.value = kuraMetric.int_value;
metric.type = "int";
break;
case 4:
metric.value = kuraMetric.bool_value;
metric.type = "boolean";
break;
case 5:
metric.value = kuraMetric.string_value;
metric.type = "string";
break;
case 6:
metric.value = kuraMetric.bytes_value;
metric.type = "bytes";
}
metrics.push(metric);
}
object.metric = metrics;
}
return object;
}
}());