fix: repair types
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { Base64Message } from '../../../backend/src/Model/Base64Message'
|
||||
import { Decoder } from '../../../backend/src/Model/Decoder'
|
||||
import { DecoderEnvelope } from './DecoderEnvelope'
|
||||
import { MessageDecoder } from './MessageDecoder'
|
||||
|
||||
type BinaryFormats =
|
||||
@@ -18,7 +20,7 @@ type BinaryFormats =
|
||||
*/
|
||||
export const BinaryDecoder: MessageDecoder<BinaryFormats> = {
|
||||
formats: ['int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float', 'double'],
|
||||
decode(input: Base64Message, format: BinaryFormats): Base64Message {
|
||||
decode(input: Base64Message, format: BinaryFormats): DecoderEnvelope {
|
||||
const decodingOption = {
|
||||
int8: [Buffer.prototype.readInt8, 1],
|
||||
int16: [Buffer.prototype.readInt16LE, 2],
|
||||
@@ -37,12 +39,18 @@ export const BinaryDecoder: MessageDecoder<BinaryFormats> = {
|
||||
const buf = input.toBuffer()
|
||||
let str: String[] = []
|
||||
if (buf.length % bytesToRead !== 0) {
|
||||
return new Base64Message(undefined, 'Data type does not align with message')
|
||||
return {
|
||||
error: 'Data type does not align with message',
|
||||
decoder: Decoder.NONE,
|
||||
}
|
||||
}
|
||||
for (let index = 0; index < buf.length; index += bytesToRead) {
|
||||
str.push((readNumber as any).apply(buf, [index]).toString())
|
||||
}
|
||||
|
||||
return Base64Message.fromString(JSON.stringify(str.length === 1 ? str[0] : str))
|
||||
return {
|
||||
message: Base64Message.fromString(JSON.stringify(str.length === 1 ? str[0] : str)),
|
||||
decoder: Decoder.NONE,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
8
app/src/decoders/DecoderEnvelope.ts
Normal file
8
app/src/decoders/DecoderEnvelope.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Base64Message } from '../../../backend/src/Model/Base64Message'
|
||||
import { Decoder } from '../../../backend/src/Model/Decoder'
|
||||
|
||||
export interface DecoderEnvelope {
|
||||
message?: Base64Message
|
||||
error?: string
|
||||
decoder: Decoder
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Base64Message } from '../../../backend/src/Model/Base64Message'
|
||||
import { DecoderEnvelope } from './DecoderEnvelope'
|
||||
|
||||
export interface MessageDecoder<T = string> {
|
||||
/**
|
||||
@@ -8,5 +9,5 @@ export interface MessageDecoder<T = string> {
|
||||
formats: T[]
|
||||
canDecodeTopic?(topic: string): boolean
|
||||
canDecodeData?(data: Base64Message): boolean
|
||||
decode(input: Base64Message, format: T | string | undefined): Base64Message
|
||||
decode(input: Base64Message, format: T | string | undefined): DecoderEnvelope
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ export const SparkplugDecoder: MessageDecoder = {
|
||||
canDecodeTopic(topic: string) {
|
||||
return !!topic.match(/^spBv1\.0\/[^/]+\/[ND](DATA|CMD|DEATH|BIRTH)\/[^/]+(\/[^/]+)?$/u)
|
||||
},
|
||||
decode(input: Base64Message): Base64Message {
|
||||
decode(input) {
|
||||
try {
|
||||
const message = Base64Message.fromString(
|
||||
JSON.stringify(
|
||||
@@ -17,12 +17,12 @@ export const SparkplugDecoder: MessageDecoder = {
|
||||
sparkplug.decodePayload(new Uint8Array(input.toBuffer()))
|
||||
)
|
||||
)
|
||||
message.decoder = Decoder.SPARKPLUG
|
||||
return message
|
||||
return { message, decoder: Decoder.SPARKPLUG }
|
||||
} catch {
|
||||
const message = new Base64Message(undefined, 'Failed to decode sparkplugb payload')
|
||||
message.decoder = Decoder.NONE
|
||||
return message
|
||||
return {
|
||||
error: 'Failed to decode sparkplugb payload',
|
||||
decoder: Decoder.NONE,
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { Base64Message } from '../../../backend/src/Model/Base64Message'
|
||||
import { Decoder } from '../../../backend/src/Model/Decoder'
|
||||
import { MessageDecoder } from './MessageDecoder'
|
||||
|
||||
export const StringDecoder: MessageDecoder = {
|
||||
formats: ['string'],
|
||||
decode(input: Base64Message): Base64Message {
|
||||
return input
|
||||
decode(input: Base64Message) {
|
||||
return { message: input, decoder: Decoder.NONE }
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user