move sparkplug decoding to backend

This commit is contained in:
Thomas Nordquist
2022-02-27 22:49:54 +01:00
parent 72400af679
commit b17b54490e
12 changed files with 3423 additions and 3687 deletions

View File

@@ -1,10 +1,24 @@
const protobuf = require('protobufjs')
const sparkplugBProto = require('../../../res/sparkplug_b.proto')
import { readFileSync } from 'fs'
import * as protobuf from 'protobufjs'
import { Base64Message } from './Base64Message';
import { Decoder } from './Decoder';
export let Payload = undefined
const buffer = readFileSync(require.resolve('../../../../res/sparkplug_b.proto'));
const root = protobuf.parse(buffer.toString()).root
export let SparkplugPayload = root.lookupType('com.cirruslink.sparkplug.protobuf.Payload')
export function loadSparkplugBPayload() {
protobuf.load(sparkplugBProto).then((root: any) => {
Payload = root.lookupType('com.cirruslink.sparkplug.protobuf.Payload')
})
export const SparkplugDecoder = {
decode(input: Buffer): Base64Message | undefined {
try {
let message = Base64Message.fromString(
JSON.stringify(
SparkplugPayload.toObject(SparkplugPayload.decode(new Uint8Array(input)))
)
)
message.decoder = Decoder.SPARKPLUG
return message
} catch {
// ignore
}
}
}