Specific int/uint byte size

- possible data types are: 'json', 'string', 'hex', 'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float', 'double'
- default is 'json'
This commit is contained in:
mhorsche
2022-06-21 21:14:59 +02:00
parent 567f6d2d50
commit 626b9cab7d
3 changed files with 94 additions and 36 deletions

View File

@@ -9,7 +9,8 @@ import Popper from '@material-ui/core/Popper'
import MenuItem from '@material-ui/core/MenuItem' import MenuItem from '@material-ui/core/MenuItem'
import MenuList from '@material-ui/core/MenuList' import MenuList from '@material-ui/core/MenuList'
const options: q.TopicDataType[] = ['string', 'json', 'hex', 'integer', 'unsigned int', 'floating point']; // const options: q.TopicDataType[] = ['json', 'string', 'hex', 'integer', 'unsigned int', 'floating point']
const options: q.TopicDataType[] = ['json', 'string', 'hex', 'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float', 'double']
export const TopicTypeButton = (props: { export const TopicTypeButton = (props: {
node?: q.TreeNode<any> node?: q.TreeNode<any>

View File

@@ -26,7 +26,7 @@ export class Base64Message {
return new Base64Message(Base64.encode(str)) return new Base64Message(Base64.encode(str))
} }
/* Raw message conversions (hex, uint, int, float) */ /* Raw message conversions ('uint8' | 'uint16' | 'uint32' | 'uint64' | 'int8' | 'int16' | 'int32' | 'int64' | 'float' | 'double') */
public static format(message: Base64Message | null, type: TopicDataType = 'string'): [string, 'json' | undefined] { public static format(message: Base64Message | null, type: TopicDataType = 'string'): [string, 'json' | undefined] {
if (!message) { if (!message) {
return ['', undefined] return ['', undefined]
@@ -44,19 +44,54 @@ export class Base64Message {
const hex = Base64Message.toHex(message) const hex = Base64Message.toHex(message)
return [hex, undefined] return [hex, undefined]
} }
case 'integer': case 'uint8':
{ {
const int = Base64Message.toInt(message) const uint = Base64Message.toUInt(message, 1)
return [int ? int : '', undefined]
}
case 'unsigned int':
{
const uint = Base64Message.toUInt(message)
return [uint ? uint : '', undefined] return [uint ? uint : '', undefined]
} }
case 'floating point': case 'uint16':
{ {
const float = Base64Message.toFloat(message) const uint = Base64Message.toUInt(message, 2)
return [uint ? uint : '', undefined]
}
case 'uint32':
{
const uint = Base64Message.toUInt(message, 4)
return [uint ? uint : '', undefined]
}
case 'uint64':
{
const uint = Base64Message.toUInt(message, 8)
return [uint ? uint : '', undefined]
}
case 'int8':
{
const int = Base64Message.toInt(message, 1)
return [int ? int : '', undefined]
}
case 'int16':
{
const int = Base64Message.toInt(message, 2)
return [int ? int : '', undefined]
}
case 'int32':
{
const int = Base64Message.toInt(message, 4)
return [int ? int : '', undefined]
}
case 'int64':
{
const int = Base64Message.toInt(message, 8)
return [int ? int : '', undefined]
}
case 'float':
{
const float = Base64Message.toFloat(message, 4)
return [float ? float : '', undefined]
}
case 'double':
{
const float = Base64Message.toFloat(message, 8)
return [float ? float : '', undefined] return [float ? float : '', undefined]
} }
default: default:
@@ -76,72 +111,93 @@ export class Base64Message {
let str: string = ''; let str: string = '';
buf.forEach(element => { buf.forEach(element => {
str += `0x${element.toString(16)} ` let hex = element.toString(16).toUpperCase();
str += `0x${hex.length < 2 ? "0" + hex : hex} `
}) })
return str.trimRight() return str.trimRight()
} }
public static toUInt(message: Base64Message) { public static toUInt(message: Base64Message, bytes: number) {
const buf = Buffer.from(message.base64Message, 'base64') const buf = Buffer.from(message.base64Message, 'base64')
let num: Number = 0; let str: String[] = [];
switch (buf.length) { switch (bytes) {
case 1: case 1:
num = buf.readUInt8(0) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readUInt8(index).toString())
}
break break
case 2: case 2:
num = buf.readUInt16LE(0) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readUInt16LE(index).toString())
}
break break
case 4: case 4:
num = buf.readUInt32LE(0) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readUInt32LE(index).toString())
}
break break
case 8: case 8:
num = Number(buf.readBigUInt64LE(0)) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readBigUInt64LE(index).toString())
}
break break
default: default:
return undefined return undefined
} }
return num.toString() return str.join(', ')
} }
public static toInt(message: Base64Message) { public static toInt(message: Base64Message, bytes: number) {
const buf = Buffer.from(message.base64Message, 'base64') const buf = Buffer.from(message.base64Message, 'base64')
let num: Number = 0; let str: String[] = [];
switch (buf.length) { switch (bytes) {
case 1: case 1:
num = buf.readInt8(0) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readInt8(index).toString())
}
break break
case 2: case 2:
num = buf.readInt16LE(0) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readInt16LE(index).toString())
}
break break
case 4: case 4:
num = buf.readInt32LE(0) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readInt32LE(index).toString())
}
break break
case 8: case 8:
num = Number(buf.readBigInt64LE(0)) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readBigInt64LE(index).toString())
}
break break
default: default:
return undefined return undefined
} }
return num.toString() return str.join(', ')
} }
public static toFloat(message: Base64Message) { public static toFloat(message: Base64Message, bytes: number) {
const buf = Buffer.from(message.base64Message, 'base64') const buf = Buffer.from(message.base64Message, 'base64')
let num: Number = 0; let str: String[] = [];
switch (buf.length) { switch (bytes) {
case 4: case 4:
num = buf.readFloatLE(0) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readFloatLE(index).toString())
}
break break
case 8: case 8:
num = buf.readDoubleLE(0) for (let index = 0; index < buf.length; index += bytes) {
str.push(buf.readDoubleLE(index).toString())
}
break break
default: default:
return undefined return undefined
} }
return num.toString() return str.join(', ')
} }
public static toDataUri(message: Base64Message, mimeType: string) { public static toDataUri(message: Base64Message, mimeType: string) {

View File

@@ -2,7 +2,8 @@ import { Destroyable } from './Destroyable'
import { Edge, Message, RingBuffer, MessageHistory } from './' import { Edge, Message, RingBuffer, MessageHistory } from './'
import { EventDispatcher } from '../../../events' import { EventDispatcher } from '../../../events'
export type TopicDataType = 'string' | 'json' | 'hex' | 'integer' | 'unsigned int' | 'floating point' // export type TopicDataType = 'json' | 'string' | 'hex' | 'integer' | 'unsigned int' | 'floating point'
export type TopicDataType = 'json' | 'string' | 'hex' | 'uint8' | 'uint16' | 'uint32' | 'uint64' | 'int8' | 'int16' | 'int32' | 'int64' | 'float' | 'double'
export class TreeNode<ViewModel extends Destroyable> { export class TreeNode<ViewModel extends Destroyable> {
public sourceEdge?: Edge<ViewModel> public sourceEdge?: Edge<ViewModel>
@@ -19,7 +20,7 @@ export class TreeNode<ViewModel extends Destroyable> {
public onMessage = new EventDispatcher<Message>() public onMessage = new EventDispatcher<Message>()
public onDestroy = new EventDispatcher<TreeNode<ViewModel>>() public onDestroy = new EventDispatcher<TreeNode<ViewModel>>()
public isTree = false public isTree = false
public type: TopicDataType = 'string' public type: TopicDataType = 'json'
private cachedPath?: string private cachedPath?: string
private cachedChildTopics?: Array<TreeNode<ViewModel>> private cachedChildTopics?: Array<TreeNode<ViewModel>>