-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
130 lines (111 loc) · 3.34 KB
/
index.ts
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
import BaseCBOR, { SenML } from '@arduino/cbor-js';
import * as Utils from '../utils';
import { CloudMessageValue } from '../client/ICloudClient';
export const CBOR = BaseCBOR;
export function isPropertyValue(message: SenML | string[]): message is SenML {
return !!(message as SenML).n;
}
export function isNil<T>(v: T): boolean {
return v === null || v === undefined;
}
export function takeFrom(...values: CloudMessageValue[]): CloudMessageValue {
return values.find((v) => !isNil(v));
}
export function valueFrom(message: SenML | string[]): CloudMessageValue {
return isPropertyValue(message)
? takeFrom(message.v, message.vs, message.vb)
: takeFrom(message[2], message[3], message[4]);
}
export function nameFrom(property: SenML | string[]): string {
return isPropertyValue(property) ? property.n : property[0];
}
export function toString(value: SenML[], numericKeys?: boolean): string {
const encoded = CBOR.encode(value, numericKeys);
return Utils.arrayBufferToBase64(encoded);
}
export function toCloudProtocolV2(cborValue: SenML): SenML {
const cloudV2CBORValue = {};
let cborLabel = null;
Object.keys(cborValue).forEach((label) => {
switch (label) {
case 'bn':
cborLabel = -2;
break;
case 'bt':
cborLabel = -3;
break;
case 'bu':
cborLabel = -4;
break;
case 'bv':
cborLabel = -5;
break;
case 'bs':
cborLabel = -6;
break;
case 'bver':
cborLabel = -1;
break;
case 'n':
cborLabel = 0;
break;
case 'u':
cborLabel = 1;
break;
case 'v':
cborLabel = 2;
break;
case 'vs':
cborLabel = 3;
break;
case 'vb':
cborLabel = 4;
break;
case 'vd':
cborLabel = 8;
break;
case 's':
cborLabel = 5;
break;
case 't':
cborLabel = 6;
break;
case 'ut':
cborLabel = 7;
break;
default:
cborLabel = label;
}
cloudV2CBORValue[cborLabel] = cborValue[label];
});
return cloudV2CBORValue;
}
export function format(value: CloudMessageValue, name: string, timestamp: number, deviceId: string): SenML {
const parsed: SenML = {};
if (timestamp !== -1) parsed.bt = timestamp || new Date().getTime();
parsed.n = name;
if (deviceId) {
parsed.bn = `urn:uuid:${deviceId}`;
}
if (Utils.isNumber(value)) parsed.v = Number(value);
if (Utils.isString(value)) parsed.vs = String(value);
if (Utils.isBoolean(value)) parsed.vb = Boolean(value);
return parsed;
}
export function parse(
name: string,
value: CloudMessageValue,
timestamp: number,
useCloudProtocolV2: boolean,
deviceId: string
): SenML | SenML[] {
if (timestamp && !Number.isInteger(timestamp)) throw new Error('Timestamp must be Integer');
if (name === undefined || typeof name !== 'string') throw new Error('Name must be a valid string');
if (Utils.isObject(value))
return Object.keys(value)
.map((key, i) => format(value[key], `${name}:${key}`, i === 0 ? timestamp : -1, i === 0 ? deviceId : undefined))
.map((cborValue) => (useCloudProtocolV2 ? toCloudProtocolV2(cborValue) : cborValue));
let cborValue = format(value, name, timestamp, deviceId);
if (useCloudProtocolV2) cborValue = toCloudProtocolV2(cborValue);
return cborValue;
}