Skip to content

Commit 433b43f

Browse files
committed
fix: DateTime type decoding, add file and bytes metric type defs
1 parent 180735e commit 433b43f

File tree

5 files changed

+70
-20
lines changed

5 files changed

+70
-20
lines changed

spBv1.0.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,25 @@ export type TemplateMetric = MetricBase & {
146146
value: Template
147147
}
148148

149+
export type BytesMetric = MetricBase & {
150+
type: 'Bytes'
151+
value: Bytes
152+
}
153+
154+
export type FileMetric = MetricBase & {
155+
type: 'File'
156+
value: Bytes
157+
}
158+
149159
export type Metric =
150160
| NumberMetric
151161
| BooleanMetric
152162
| StringMetric
153163
| DateTimeMetric
154164
| DataSetMetric
155165
| TemplateMetric
166+
| BytesMetric
167+
| FileMetric
156168

157169
export type NumberParameter = {
158170
name?: string
@@ -194,3 +206,6 @@ export type Payload = {
194206

195207
export function encodePayload(payload: Payload): Uint8Array
196208
export function decodePayload(payload: Uint8Array): Payload
209+
210+
export function encodeType(typeString: string): number
211+
export function decodeType(typeInt: number): string | null | undefined

spBv1.0.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ function decodeType(typeInt) {
275275
return 'Boolean'
276276
case 12:
277277
return 'String'
278+
case 13:
279+
return 'DateTime'
278280
case 14:
279281
return 'Text'
280282
case 15:

spBv1.0.js.flow

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ export type TemplateMetric = {|
176176
value: Template,
177177
|}
178178

179+
export type BytesMetric = {|
180+
...MetricBase,
181+
type: 'Bytes',
182+
value: Bytes,
183+
|}
184+
185+
export type FileMetric = {|
186+
...MetricBase,
187+
type: 'File',
188+
value: Bytes,
189+
|}
190+
179191
export type Metric =
180192
| NumberMetric
181193
| BigIntMetric
@@ -184,6 +196,8 @@ export type Metric =
184196
| DateTimeMetric
185197
| DataSetMetric
186198
| TemplateMetric
199+
| BytesMetric
200+
| FileMetric
187201

188202
export type NumberParameter = {|
189203
name?: ?string,

test/spBv1.0.test.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ const { expect } = require('chai')
33
const { encodePayload, decodePayload } = require('../spBv1.0')
44

55
function checkRoundTrip(value, type, expected) {
6-
const actual = decodePayload(
6+
const decoded = decodePayload(
77
encodePayload({
88
timestamp: Date.now(),
99
metrics: [{ value, type }],
1010
})
11-
).metrics[0].value
11+
).metrics[0]
1212

13-
if (expected !== expected) expect(actual).to.be.NaN
14-
else expect(actual).to.equal(expected !== undefined ? expected : value)
13+
if (expected !== expected) expect(decoded.value).to.be.NaN
14+
else
15+
expect(decoded.value).to.deep.equal(
16+
expected !== undefined ? expected : value
17+
)
18+
expect(decoded.type).to.equal(type)
1519
}
1620

1721
const floatBuf = Buffer.alloc(4)
@@ -70,4 +74,18 @@ describe(`spBv1.0`, function () {
7074
checkRoundTrip(float(0x7f7fffff), 'Float')
7175
checkRoundTrip(float(0x7fc00000), 'Float', NaN)
7276
})
77+
it(`DateTime`, function () {
78+
checkRoundTrip(123412341n, 'DateTime')
79+
checkRoundTrip(
80+
new Date('Jan 1 2021'),
81+
'DateTime',
82+
BigInt(new Date('Jan 1 2021').getTime()) // eslint-disable-line no-undef
83+
)
84+
})
85+
it(`Bytes`, function () {
86+
checkRoundTrip(Buffer.from([0, 1, 2, 3]), 'Bytes')
87+
})
88+
it(`File`, function () {
89+
checkRoundTrip(Buffer.from([0, 1, 2, 3]), 'File')
90+
})
7391
})

test/spBv1.0typeTest.js.flow

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@
33
import * as sparkplug from '../spBv1.0'
44

55
// $FlowFixMe
6-
sparkplug.encodePayload({ })
6+
sparkplug.encodePayload({})
77

88
sparkplug.encodePayload({
9-
timestamp: Date.now()
9+
timestamp: Date.now(),
1010
})
1111

1212
sparkplug.encodePayload({
1313
timestamp: Date.now(),
1414
metrics: [
15-
{value: null, type: 'UInt8'},
16-
{value: 2, type: 'UInt8'},
17-
{value: 2, type: 'Int8'},
18-
{value: 2, type: 'Float'},
19-
{value: 2, type: 'DateTime'},
20-
{value: new Date(), type: 'DateTime'},
21-
{value: null, type: 'String'},
22-
{value: 'blah', type: 'String'},
23-
{value: null, type: 'Boolean'},
24-
{value: true, type: 'Boolean'},
15+
{ value: null, type: 'UInt8' },
16+
{ value: 2, type: 'UInt8' },
17+
{ value: 2, type: 'Int8' },
18+
{ value: 2, type: 'Float' },
19+
{ value: 2, type: 'DateTime' },
20+
{ value: new Date(), type: 'DateTime' },
21+
{ value: null, type: 'String' },
22+
{ value: 'blah', type: 'String' },
23+
{ value: null, type: 'Boolean' },
24+
{ value: true, type: 'Boolean' },
2525
// $FlowFixMe
26-
{value: 'hello', type: 'UInt8'},
26+
{ value: 'hello', type: 'UInt8' },
2727
// $FlowFixMe
28-
{value: 2, type: 'String'},
28+
{ value: 2, type: 'String' },
2929
// $FlowFixMe
30-
{value: 2, type: 'Boolean'},
31-
]
30+
{ value: 2, type: 'Boolean' },
31+
{ value: Buffer.from([0, 1, 2]), type: 'Bytes' },
32+
],
3233
})

0 commit comments

Comments
 (0)