Implement Integral Data Types #13

Merged
sergeysav merged 2 commits from sergeysav/integral_data_types into main 2026-01-03 08:37:27 -08:00
17 changed files with 307 additions and 52 deletions
Showing only changes of commit 791518eb3d - Show all commits

1
Cargo.lock generated
View File

@@ -1953,6 +1953,7 @@ dependencies = [
"fern",
"futures-util",
"log",
"num-traits",
"papaya",
"serde",
"serde_json",

View File

@@ -7,6 +7,32 @@ pub enum DataType {
Float32,
Float64,
Boolean,
Int8,
Int16,
Int32,
Int64,
Unsigned8,
Unsigned16,
Unsigned32,
Unsigned64,
}
impl DataType {
pub fn get_data_length(self) -> u64 {
match self {
DataType::Float32 => 4,
DataType::Float64 => 8,
DataType::Boolean => 1,
DataType::Int8 => 1,
DataType::Int16 => 2,
DataType::Int32 => 4,
DataType::Int64 => 8,
DataType::Unsigned8 => 1,
DataType::Unsigned16 => 2,
DataType::Unsigned32 => 4,
DataType::Unsigned64 => 8,
}
}
}
pub trait ToDataType: Into<DataValue> {
@@ -24,3 +50,11 @@ macro_rules! impl_to_data_type {
impl_to_data_type!(f32, DataType::Float32);
impl_to_data_type!(f64, DataType::Float64);
impl_to_data_type!(bool, DataType::Boolean);
impl_to_data_type!(i8, DataType::Int8);
impl_to_data_type!(i16, DataType::Int16);
impl_to_data_type!(i32, DataType::Int32);
impl_to_data_type!(i64, DataType::Int64);
impl_to_data_type!(u8, DataType::Unsigned8);
impl_to_data_type!(u16, DataType::Unsigned16);
impl_to_data_type!(u32, DataType::Unsigned32);
impl_to_data_type!(u64, DataType::Unsigned64);

View File

@@ -7,6 +7,14 @@ pub enum DataValue {
Float32(f32),
Float64(f64),
Boolean(bool),
Int8(i8),
Int16(i16),
Int32(i32),
Int64(i64),
Unsigned8(u8),
Unsigned16(u16),
Unsigned32(u32),
Unsigned64(u64),
}
impl DataValue {
@@ -15,6 +23,14 @@ impl DataValue {
DataValue::Float32(_) => DataType::Float32,
DataValue::Float64(_) => DataType::Float64,
DataValue::Boolean(_) => DataType::Boolean,
DataValue::Int8(_) => DataType::Int8,
DataValue::Int16(_) => DataType::Int16,
DataValue::Int32(_) => DataType::Int32,
DataValue::Int64(_) => DataType::Int64,
DataValue::Unsigned8(_) => DataType::Unsigned8,
DataValue::Unsigned16(_) => DataType::Unsigned16,
DataValue::Unsigned32(_) => DataType::Unsigned32,
DataValue::Unsigned64(_) => DataType::Unsigned64,
}
}
}

View File

@@ -3,8 +3,10 @@ import { computed, onMounted } from 'vue';
import {
type AnyTypeId,
type DynamicDataType,
getLimits,
isBooleanType,
isNumericType,
type NumericLimits,
} from '@/composables/dynamic.ts';
const props = defineProps<{
@@ -17,6 +19,18 @@ const is_numeric = computed(() => {
return isNumericType(props.type);
});
const numeric_limit = computed(() => {
if (isNumericType(props.type)) {
return getLimits(props.type);
}
return {
min: 0,
max: 0,
integer: false,
is_big: false,
} as NumericLimits;
});
const is_boolean = computed(() => {
return isBooleanType(props.type);
});
@@ -25,18 +39,81 @@ const is_boolean = computed(() => {
onMounted(() => {
if (model.value === undefined) {
if (is_numeric.value) {
if (numeric_limit.value.integer) {
if (numeric_limit.value.is_big) {
model.value = 0n;
} else {
model.value = 0;
}
} else {
model.value = 0.0;
}
} else if (is_boolean.value) {
debugger;
model.value = false;
}
}
});
const value = computed({
get: () => {
if (is_numeric.value) {
if (numeric_limit.value.is_big) {
return ((model.value || 0n) as bigint).toString();
} else {
return model.value as number;
}
} else if (is_boolean.value) {
// Force it into a boolean
return !!model.value;
}
return undefined;
},
set: (value) => {
if (is_numeric.value) {
if (numeric_limit.value.is_big) {
let result = BigInt(value as string);
if (result > (numeric_limit.value.max as bigint)) {
result = numeric_limit.value.max as bigint;
} else if (result < (numeric_limit.value.min as bigint)) {
result = numeric_limit.value.min as bigint;
}
model.value = result;
} else {
let result_value = value as number;
if (numeric_limit.value.integer) {
result_value = Math.round(value as number);
}
if (result_value > (numeric_limit.value.max as number)) {
result_value = numeric_limit.value.max as number;
} else if (result_value < (numeric_limit.value.min as number)) {
result_value = numeric_limit.value.min as number;
}
model.value = result_value;
}
} else if (is_boolean.value) {
model.value = value as boolean;
}
},
});
</script>
<template>
<input v-if="is_numeric" type="number" v-model="model" />
<input v-else-if="is_boolean" type="checkbox" v-model="model" />
<input
v-if="is_numeric && !numeric_limit.is_big"
type="number"
v-model="value"
:max="numeric_limit.max as number"
:min="numeric_limit.min as number"
:step="numeric_limit.integer ? 1 : undefined"
/>
<input
v-else-if="is_numeric && numeric_limit.is_big"
type="text"
v-model="value"
pattern="-?\d+"
/>
<input v-else-if="is_boolean" type="checkbox" v-model="value" />
<span v-else>UNKNOWN INPUT</span>
</template>

View File

@@ -2,6 +2,7 @@
import {
type CommandParameterData,
type DynamicDataType,
getLimits,
isBooleanType,
isNumericType,
} from '@/composables/dynamic.ts';
@@ -40,7 +41,8 @@ watch([command_info], ([cmd_info]) => {
switch (model_param_value.type) {
case 'constant':
if (
typeof model_param_value.value == 'number' &&
(typeof model_param_value.value == 'number' ||
typeof model_param_value.value == 'bigint') &&
!isNumericType(param.data_type)
) {
model_param_value = undefined;
@@ -59,7 +61,11 @@ watch([command_info], ([cmd_info]) => {
if (model_param_value === undefined) {
let default_value: DynamicDataType = 0;
if (isNumericType(param.data_type)) {
if (getLimits(param.data_type).is_big) {
default_value = 0n;
} else {
default_value = 0;
}
} else if (isBooleanType(param.data_type)) {
default_value = false;
}

View File

@@ -4,6 +4,7 @@ import { ref } from 'vue';
import CommandParameter from '@/components/CommandParameter.vue';
import FlexDivider from '@/components/FlexDivider.vue';
import type { DynamicDataType } from '@/composables/dynamic.ts';
import { toJsonString } from '@/composables/json.ts';
const props = defineProps<{
command: CommandDefinition | null;
@@ -27,7 +28,7 @@ async function sendCommand() {
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(params),
body: toJsonString(params),
});
if (response.ok) {
result.value = await response.json();

View File

@@ -7,6 +7,7 @@ import {
} from '@/composables/dynamic.ts';
import { computed, defineAsyncComponent, inject, type Ref, ref } from 'vue';
import CommandParameterListConfigurator from '@/components/CommandParameterListConfigurator.vue';
import { toJsonString } from '@/composables/json.ts';
const TelemetryValue = defineAsyncComponent(
() => import('@/components/TelemetryValue.vue'),
@@ -166,7 +167,7 @@ async function sendCommand(command: {
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(params),
body: toJsonString(params),
});
busy.value = false;
}

View File

@@ -3,7 +3,7 @@ import { computed, watch } from 'vue';
import CopyableDynamicSpan from '@/components/CopyableDynamicSpan.vue';
const props = defineProps<{
value: number;
value: number | bigint;
max_width: number;
copyable?: boolean;
include_span?: boolean;
@@ -23,6 +23,9 @@ const display_value = computed(() => {
if (props.value == 0) {
return '0';
}
if (typeof props.value === 'bigint') {
return props.value.toString();
}
let precision = props.value.toPrecision(props.max_width - 3);
// Chop off the last character as long as it is a 0
while (

View File

@@ -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 =
| {

View File

@@ -0,0 +1,20 @@
// 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') {
return JSON.rawJSON(value.toString());
}
return value;
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function parseJsonString(data: string): any {
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;
});
}

View File

@@ -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,
},

View File

@@ -2,6 +2,7 @@
import DynamicComponent from '@/components/DynamicComponent.vue';
import type { OptionalDynamicComponentData } from '@/composables/dynamic.ts';
import { ref, watchEffect } from 'vue';
import { parseJsonString, toJsonString } from '@/composables/json.ts';
const data = ref<OptionalDynamicComponentData>({ type: 'none' });
@@ -29,7 +30,7 @@ async function load(id: string) {
loading.value = true;
const panel_data = await fetch(`/api/panel/${id}`);
const panel_json_value = await panel_data.json();
data.value = JSON.parse(
data.value = parseJsonString(
panel_json_value['data'],
) as OptionalDynamicComponentData;
panel_name.value = panel_json_value['name'];
@@ -50,9 +51,9 @@ async function save() {
if (panel_id_value) {
const res = await fetch(`/api/panel/${panel_id_value}`, {
method: 'PUT',
body: JSON.stringify({
body: toJsonString({
name: panel_name.value,
data: JSON.stringify(data.value),
data: toJsonString(data.value),
}),
headers: {
'Content-Type': 'application/json',
@@ -62,9 +63,9 @@ async function save() {
} else {
const res = await fetch('/api/panel', {
method: 'POST',
body: JSON.stringify({
body: toJsonString({
name: panel_name.value,
data: JSON.stringify(data.value),
data: toJsonString(data.value),
}),
headers: {
'Content-Type': 'application/json',

View File

@@ -6,6 +6,7 @@ import type {
} from '@/composables/dynamic.ts';
import { computed, provide, ref, watchEffect } from 'vue';
import { useRoute } from 'vue-router';
import { parseJsonString } from '@/composables/json.ts';
const route = useRoute();
@@ -22,7 +23,7 @@ provide('inputs', inputs);
watchEffect(async () => {
const panel_data = await fetch(`/api/panel/${id.value}`);
const panel_json_value = await panel_data.json();
panel.value = JSON.parse(
panel.value = parseJsonString(
panel_json_value['data'],
) as OptionalDynamicComponentData;
});

View File

@@ -15,6 +15,7 @@ derive_more = { workspace = true, features = ["from"] }
fern = { workspace = true, features = ["colored"] }
futures-util = { workspace = true }
log = { workspace = true }
num-traits = { workspace = true }
papaya = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

View File

@@ -11,6 +11,7 @@ use api::messages::command::{Command, CommandDefinition, CommandHeader, CommandR
use api::messages::ResponseMessage;
use chrono::Utc;
use log::error;
use num_traits::FromPrimitive;
use papaya::HashMap;
use std::collections::HashMap as StdHashMap;
use tokio::sync::oneshot;
@@ -102,10 +103,34 @@ impl CommandManagementService {
let Some(param_value) = parameters.get(&parameter.name) else {
return Err(MisingParameter(parameter.name.clone()));
};
let Some(param_value) = (match parameter.data_type {
DataType::Float32 => param_value.as_f64().map(|v| DataValue::Float32(v as f32)),
DataType::Float64 => param_value.as_f64().map(DataValue::Float64),
DataType::Boolean => param_value.as_bool().map(DataValue::Boolean),
let Some(Some(param_value)) = (match parameter.data_type {
DataType::Float32 => param_value
.as_f64()
.map(|v| Some(DataValue::Float32(v as f32))),
DataType::Float64 => param_value.as_f64().map(DataValue::Float64).map(Some),
DataType::Boolean => param_value.as_bool().map(DataValue::Boolean).map(Some),
DataType::Int8 => param_value
.as_i64()
.map(|v| Some(DataValue::Int8(i8::from_i64(v)?))),
DataType::Int16 => param_value
.as_i64()
.map(|v| Some(DataValue::Int16(i16::from_i64(v)?))),
DataType::Int32 => param_value
.as_i64()
.map(|v| Some(DataValue::Int32(i32::from_i64(v)?))),
DataType::Int64 => param_value.as_i64().map(|v| Some(DataValue::Int64(v))),
DataType::Unsigned8 => param_value
.as_u64()
.map(|v| Some(DataValue::Unsigned8(u8::from_u64(v)?))),
DataType::Unsigned16 => param_value
.as_u64()
.map(|v| Some(DataValue::Unsigned16(u16::from_u64(v)?))),
DataType::Unsigned32 => param_value
.as_u64()
.map(|v| Some(DataValue::Unsigned32(u32::from_u64(v)?))),
DataType::Unsigned64 => {
param_value.as_u64().map(|v| Some(DataValue::Unsigned64(v)))
}
}) else {
return Err(WrongParameterType {
name: parameter.name.clone(),

View File

@@ -196,9 +196,17 @@ impl HistorySegmentFile {
// Write all the values
for value in &data.values {
match value {
DataValue::Float32(value) => file.write_data::<f32>(*value)?,
DataValue::Float64(value) => file.write_data::<f64>(*value)?,
DataValue::Boolean(value) => file.write_data::<bool>(*value)?,
DataValue::Float32(value) => file.write_data(*value)?,
DataValue::Float64(value) => file.write_data(*value)?,
DataValue::Boolean(value) => file.write_data(*value)?,
DataValue::Int8(value) => file.write_data(*value)?,
DataValue::Int16(value) => file.write_data(*value)?,
DataValue::Int32(value) => file.write_data(*value)?,
DataValue::Int64(value) => file.write_data(*value)?,
DataValue::Unsigned8(value) => file.write_data(*value)?,
DataValue::Unsigned16(value) => file.write_data(*value)?,
DataValue::Unsigned32(value) => file.write_data(*value)?,
DataValue::Unsigned64(value) => file.write_data(*value)?,
}
}
@@ -332,20 +340,20 @@ impl HistorySegmentFile {
}
fn read_telemetry_item(&mut self, telemetry_data_type: DataType) -> anyhow::Result<DataValue> {
match telemetry_data_type {
DataType::Float32 => {
self.file_position += 4;
Ok(DataValue::Float32(self.file.read_data::<f32>()?))
}
DataType::Float64 => {
self.file_position += 8;
Ok(DataValue::Float64(self.file.read_data::<f64>()?))
}
DataType::Boolean => {
self.file_position += 1;
Ok(DataValue::Boolean(self.file.read_data::<bool>()?))
}
}
self.file_position += telemetry_data_type.get_data_length() as i64;
Ok(match telemetry_data_type {
DataType::Float32 => self.file.read_data::<f32>()?.into(),
DataType::Float64 => self.file.read_data::<f64>()?.into(),
DataType::Boolean => self.file.read_data::<bool>()?.into(),
DataType::Int8 => self.file.read_data::<i8>()?.into(),
DataType::Int16 => self.file.read_data::<i16>()?.into(),
DataType::Int32 => self.file.read_data::<i32>()?.into(),
DataType::Int64 => self.file.read_data::<i64>()?.into(),
DataType::Unsigned8 => self.file.read_data::<u8>()?.into(),
DataType::Unsigned16 => self.file.read_data::<u16>()?.into(),
DataType::Unsigned32 => self.file.read_data::<u32>()?.into(),
DataType::Unsigned64 => self.file.read_data::<u64>()?.into(),
})
}
fn get_telemetry_item(
@@ -353,11 +361,7 @@ impl HistorySegmentFile {
index: u64,
telemetry_data_type: DataType,
) -> anyhow::Result<DataValue> {
let item_length = match telemetry_data_type {
DataType::Float32 => 4,
DataType::Float64 => 8,
DataType::Boolean => 1,
};
let item_length = telemetry_data_type.get_data_length();
let desired_position =
Self::HEADER_LENGTH + self.length * Self::TIMESTAMP_LENGTH + index * item_length;
let seek_amount = desired_position as i64 - self.file_position;

View File

@@ -3,8 +3,6 @@ use crate::telemetry::data_item::TelemetryDataItem;
use crate::telemetry::definition::TelemetryDefinition;
use crate::telemetry::history::{TelemetryHistory, TelemetryHistoryService};
use anyhow::bail;
use api::data_type::DataType;
use api::data_value::DataValue;
use api::messages::telemetry_definition::{
TelemetryDefinitionRequest, TelemetryDefinitionResponse,
};
@@ -144,11 +142,7 @@ impl TelemetryManagementService {
bail!("Telemetry Item Not Found");
};
let expected_type = match &tlm_item.value {
DataValue::Float32(_) => DataType::Float32,
DataValue::Float64(_) => DataType::Float64,
DataValue::Boolean(_) => DataType::Boolean,
};
let expected_type = tlm_item.value.to_data_type();
if expected_type != tlm_data.data.definition.data_type {
bail!("Data Type Mismatch");
};