fix: repair types

This commit is contained in:
Thomas Nordquist
2024-05-23 16:29:58 +02:00
parent c88978f0dd
commit a2c4388c78
19 changed files with 180 additions and 110 deletions

View File

@@ -98,7 +98,7 @@ export class MqttSource implements DataSource<MqttOptions> {
public publish(msg: MqttMessage) {
if (this.client) {
this.client.publish(msg.topic, msg.payload?.toBuffer() ?? '', {
this.client.publish(msg.topic, (msg.payload && new Base64Message(msg.payload))?.toBuffer() ?? '', {
qos: msg.qos,
retain: msg.retain,
})

View File

@@ -1,20 +1,42 @@
import { Base64 } from 'js-base64'
import { Decoder } from './Decoder'
import { TopicDataType } from './TreeNode'
export type Base64MessageDTO = Pick<Base64Message, 'base64Message'>
export class Base64Message {
public base64Message: string
private unicodeValue: string
public error?: string
public decoder: Decoder
public length: number
private _unicodeValue: string | undefined
constructor(base64Str?: string, error?: string) {
this.base64Message = base64Str ?? ''
this.error = error
this.unicodeValue = Base64.decode(base64Str ?? '')
this.length = base64Str?.length ?? 0
this.decoder = Decoder.NONE
// Todo: Rename to `encodedLength`
public get length(): number {
return this.base64Message.length
}
private get unicodeValue(): string {
if (!this._unicodeValue) {
this._unicodeValue = Base64.decode(this.base64Message ?? '')
}
return this._unicodeValue
}
constructor(base64Str?: string | Base64MessageDTO, error?: string) {
if (typeof base64Str === 'string' || typeof base64Str === 'undefined') {
this.base64Message = base64Str ?? ''
} else {
if (typeof base64Str.base64Message !== 'string') {
throw new Error('Received unexpected type in copy constructor')
}
this.base64Message = base64Str.base64Message
}
}
/**
* Override default JSON serialization behavior to only return the DTO
* @returns
*/
public toJSON(): Base64MessageDTO {
return { base64Message: this.base64Message }
}
public toUnicodeString() {

View File

@@ -15,7 +15,7 @@ export class ChangeBuffer {
public push(val: MqttMessage) {
if (!this.isFull()) {
this.buffer.push({ message: val, received: new Date() })
this.size += this.estimatedMessageOverhead + (val.payload ? val.payload.length : 0)
this.size += this.estimatedMessageOverhead + (val.payload?.base64Message.length ?? 0)
this.length += 1
}
}

View File

@@ -1,7 +1,8 @@
import { Base64Message } from './Base64Message'
import { QoS } from '../DataSource/MqttSource'
import { MemoryConsumptionExpressedByLength } from './RingBuffer'
export interface Message {
export interface Message extends MemoryConsumptionExpressedByLength {
// mqtt based info
payload: Base64Message | null
messageId?: number

View File

@@ -32,7 +32,7 @@ export abstract class TreeNodeFactory {
node.setMessage({
...mqttMessage,
payload: mqttMessage.payload && new Base64Message(mqttMessage.payload?.base64Message),
length: mqttMessage.payload?.length ?? 0,
length: mqttMessage.payload?.base64Message.length ?? 0,
received: receiveDate,
messageNumber: this.messageCounter,
})