Implement Integral Data Types (#13)
**Rationale:** Integral Types were missing and are needed for Project Nautilus. **Changes:** - Implements Integral Data Types - u64 and i64 implemented through bigint Reviewed-on: #13 Co-authored-by: Sergey Savelyev <sergeysav.nn@gmail.com> Co-committed-by: Sergey Savelyev <sergeysav.nn@gmail.com>
This commit was merged in pull request #13.
This commit is contained in:
@@ -41,8 +41,13 @@ export function useCommand(name: MaybeRefOrGetter<string>) {
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/cmd/${name_value}`);
|
||||
data.value = await res.json();
|
||||
error.value = null;
|
||||
if (res.ok) {
|
||||
data.value = await res.json();
|
||||
error.value = null;
|
||||
} else {
|
||||
data.value = null;
|
||||
error.value = await res.text();
|
||||
}
|
||||
} catch (e) {
|
||||
data.value = null;
|
||||
error.value = e;
|
||||
|
||||
@@ -1,5 +1,74 @@
|
||||
export const NumericTypes = ['Float32', 'Float64'] as const;
|
||||
export const NumericTypes = [
|
||||
'Float32',
|
||||
'Float64',
|
||||
'Int8',
|
||||
'Int16',
|
||||
'Int32',
|
||||
'Int64',
|
||||
'Unsigned8',
|
||||
'Unsigned16',
|
||||
'Unsigned32',
|
||||
'Unsigned64',
|
||||
] as const;
|
||||
export type NumericTypeId = (typeof NumericTypes)[number];
|
||||
|
||||
export type NumericLimits = {
|
||||
min: number | bigint;
|
||||
max: number | bigint;
|
||||
integer: boolean;
|
||||
is_big: boolean;
|
||||
};
|
||||
|
||||
export function getLimits(numeric_type: NumericTypeId): NumericLimits {
|
||||
switch (numeric_type) {
|
||||
case 'Float32':
|
||||
return {
|
||||
integer: false,
|
||||
is_big: false,
|
||||
min: -3.40282347e38,
|
||||
max: 3.40282347e38,
|
||||
};
|
||||
case 'Float64':
|
||||
return {
|
||||
integer: false,
|
||||
is_big: false,
|
||||
min: -1.7976931348623157e308,
|
||||
max: 1.7976931348623157e308,
|
||||
};
|
||||
case 'Int8':
|
||||
return { integer: true, is_big: false, min: -128, max: 127 };
|
||||
case 'Int16':
|
||||
return { integer: true, is_big: false, min: -32768, max: 32767 };
|
||||
case 'Int32':
|
||||
return {
|
||||
integer: true,
|
||||
is_big: false,
|
||||
min: -2147483648,
|
||||
max: 2147483647,
|
||||
};
|
||||
case 'Int64':
|
||||
return {
|
||||
integer: true,
|
||||
is_big: true,
|
||||
min: -9223372036854775808n,
|
||||
max: 9223372036854775807n,
|
||||
};
|
||||
case 'Unsigned8':
|
||||
return { integer: true, is_big: false, min: 0, max: 255 };
|
||||
case 'Unsigned16':
|
||||
return { integer: true, is_big: false, min: 0, max: 65535 };
|
||||
case 'Unsigned32':
|
||||
return { integer: true, is_big: false, min: 0, max: 4294967295 };
|
||||
case 'Unsigned64':
|
||||
return {
|
||||
integer: true,
|
||||
is_big: true,
|
||||
min: 0,
|
||||
max: 18446744073709551615n,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const BooleanTypes = ['Boolean'] as const;
|
||||
export type BooleanTypeId = (typeof BooleanTypes)[number];
|
||||
export const AnyTypes = [...NumericTypes, ...BooleanTypes] as const;
|
||||
@@ -12,7 +81,7 @@ export function isBooleanType(type: AnyTypeId): type is BooleanTypeId {
|
||||
return BooleanTypes.some((it) => it == type);
|
||||
}
|
||||
|
||||
export type DynamicDataType = number | boolean;
|
||||
export type DynamicDataType = bigint | number | boolean;
|
||||
|
||||
export type CommandParameterData =
|
||||
| {
|
||||
|
||||
22
frontend/src/composables/json.ts
Normal file
22
frontend/src/composables/json.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function toJsonString(data: any): string {
|
||||
return JSON.stringify(data, (_key, value) => {
|
||||
if (typeof value == 'bigint') {
|
||||
// @ts-expect-error TS2339
|
||||
return JSON.rawJSON(value.toString());
|
||||
}
|
||||
return value;
|
||||
});
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function parseJsonString(data: string): any {
|
||||
// @ts-expect-error TS2345, TS7006
|
||||
return JSON.parse(data, (key, value, context) => {
|
||||
if (key === 'Int64' || key == 'Unsigned64') {
|
||||
// Or use the constructor of your custom high-precision number library
|
||||
return BigInt(context.source);
|
||||
}
|
||||
return value;
|
||||
});
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
} from 'vue';
|
||||
import type { TelemetryDefinition } from '@/composables/telemetry';
|
||||
import { onDocumentVisibilityChange } from '@/composables/document.ts';
|
||||
import { parseJsonString, toJsonString } from '@/composables/json.ts';
|
||||
|
||||
export interface TelemetryDataItem {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -71,7 +72,7 @@ export class WebsocketHandle {
|
||||
}
|
||||
});
|
||||
this.websocket.addEventListener('message', (event) => {
|
||||
const message = JSON.parse(event.data);
|
||||
const message = parseJsonString(event.data);
|
||||
if (message['TlmValue']) {
|
||||
const tlm_value = message['TlmValue'] as TlmValue;
|
||||
const listeners = this.on_telem_value.get(tlm_value.uuid);
|
||||
@@ -126,7 +127,7 @@ export class WebsocketHandle {
|
||||
([uuid_value, connected, enabled, min_sep, live_value]) => {
|
||||
if (connected && enabled && uuid_value && live_value) {
|
||||
this.websocket?.send(
|
||||
JSON.stringify({
|
||||
toJsonString({
|
||||
RegisterTlmListener: {
|
||||
uuid: uuid_value,
|
||||
minimum_separation_ms: min_sep,
|
||||
@@ -142,7 +143,7 @@ export class WebsocketHandle {
|
||||
this.on_telem_value.get(uuid_value)?.push(callback_fn);
|
||||
onWatcherCleanup(() => {
|
||||
this.websocket?.send(
|
||||
JSON.stringify({
|
||||
toJsonString({
|
||||
UnregisterTlmListener: {
|
||||
uuid: uuid_value,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user