Added support for binary data types
- Select data type (string, json, hex, uint, int, float) for each topic individually - Default data type is 'string' - Show milliseconds in message received timestamp
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { TopicDataType } from "./TreeNode"
|
||||
|
||||
const { Base64 } = require('js-base64')
|
||||
|
||||
export class Base64Message {
|
||||
@@ -24,6 +26,124 @@ export class Base64Message {
|
||||
return new Base64Message(Base64.encode(str))
|
||||
}
|
||||
|
||||
/* Raw message conversions (hex, uint, int, float) */
|
||||
public static format(message: Base64Message | null, type: TopicDataType = 'string'): [string, 'json' | undefined] {
|
||||
if (!message) {
|
||||
return ['', undefined]
|
||||
}
|
||||
|
||||
try {
|
||||
switch (type) {
|
||||
case 'json':
|
||||
{
|
||||
const json = JSON.parse(Base64Message.toUnicodeString(message))
|
||||
return [JSON.stringify(json, undefined, ' '), 'json']
|
||||
}
|
||||
case 'hex':
|
||||
{
|
||||
const hex = Base64Message.toHex(message)
|
||||
return [hex, undefined]
|
||||
}
|
||||
case 'integer':
|
||||
{
|
||||
const int = Base64Message.toInt(message)
|
||||
return [int ? int : '', undefined]
|
||||
}
|
||||
case 'unsigned int':
|
||||
{
|
||||
const uint = Base64Message.toUInt(message)
|
||||
return [uint ? uint : '', undefined]
|
||||
}
|
||||
case 'floating point':
|
||||
{
|
||||
const float = Base64Message.toFloat(message)
|
||||
return [float ? float : '', undefined]
|
||||
}
|
||||
default:
|
||||
{
|
||||
const str = Base64Message.toUnicodeString(message)
|
||||
return [str, undefined]
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
const str = Base64Message.toUnicodeString(message)
|
||||
return [str, undefined]
|
||||
}
|
||||
}
|
||||
|
||||
public static toHex(message: Base64Message) {
|
||||
const buf = Buffer.from(message.base64Message, 'base64')
|
||||
|
||||
let str: string = '';
|
||||
buf.forEach(element => {
|
||||
str += `0x${element.toString(16)} `
|
||||
})
|
||||
return str.trimRight()
|
||||
}
|
||||
|
||||
public static toUInt(message: Base64Message) {
|
||||
const buf = Buffer.from(message.base64Message, 'base64')
|
||||
|
||||
let num: Number = 0;
|
||||
switch (buf.length) {
|
||||
case 1:
|
||||
num = buf.readUInt8(0)
|
||||
break
|
||||
case 2:
|
||||
num = buf.readUInt16LE(0)
|
||||
break
|
||||
case 4:
|
||||
num = buf.readUInt32LE(0)
|
||||
break
|
||||
case 8:
|
||||
num = Number(buf.readBigUInt64LE(0))
|
||||
break
|
||||
default:
|
||||
return undefined
|
||||
}
|
||||
return num.toString()
|
||||
}
|
||||
|
||||
public static toInt(message: Base64Message) {
|
||||
const buf = Buffer.from(message.base64Message, 'base64')
|
||||
|
||||
let num: Number = 0;
|
||||
switch (buf.length) {
|
||||
case 1:
|
||||
num = buf.readInt8(0)
|
||||
break
|
||||
case 2:
|
||||
num = buf.readInt16LE(0)
|
||||
break
|
||||
case 4:
|
||||
num = buf.readInt32LE(0)
|
||||
break
|
||||
case 8:
|
||||
num = Number(buf.readBigInt64LE(0))
|
||||
break
|
||||
default:
|
||||
return undefined
|
||||
}
|
||||
return num.toString()
|
||||
}
|
||||
|
||||
public static toFloat(message: Base64Message) {
|
||||
const buf = Buffer.from(message.base64Message, 'base64')
|
||||
|
||||
let num: Number = 0;
|
||||
switch (buf.length) {
|
||||
case 4:
|
||||
num = buf.readFloatLE(0)
|
||||
break
|
||||
case 8:
|
||||
num = buf.readDoubleLE(0)
|
||||
break
|
||||
default:
|
||||
return undefined
|
||||
}
|
||||
return num.toString()
|
||||
}
|
||||
|
||||
public static toDataUri(message: Base64Message, mimeType: string) {
|
||||
return `data:${mimeType};base64,${message.base64Message}`
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ import { Destroyable } from './Destroyable'
|
||||
import { Edge, Message, RingBuffer, MessageHistory } from './'
|
||||
import { EventDispatcher } from '../../../events'
|
||||
|
||||
export type TopicDataType = 'string' | 'json' | 'hex' | 'integer' | 'unsigned int' | 'floating point'
|
||||
|
||||
export class TreeNode<ViewModel extends Destroyable> {
|
||||
public sourceEdge?: Edge<ViewModel>
|
||||
public message?: Message
|
||||
@@ -17,6 +19,7 @@ export class TreeNode<ViewModel extends Destroyable> {
|
||||
public onMessage = new EventDispatcher<Message>()
|
||||
public onDestroy = new EventDispatcher<TreeNode<ViewModel>>()
|
||||
public isTree = false
|
||||
public type: TopicDataType = 'string'
|
||||
|
||||
private cachedPath?: string
|
||||
private cachedChildTopics?: Array<TreeNode<ViewModel>>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export { Edge } from './Edge'
|
||||
export { TreeNode } from './TreeNode'
|
||||
export { TreeNode, TopicDataType } from './TreeNode'
|
||||
export { Message } from './Message'
|
||||
export { TreeNodeFactory } from './TreeNodeFactory'
|
||||
export { Tree } from './Tree'
|
||||
|
||||
Reference in New Issue
Block a user