make new folder for reuse ngLib add new features for datatree like change add remove rename datapoints improve pingpong

This commit is contained in:
Adrian Zuercher
2025-07-25 18:37:18 +02:00
parent ffb8e4994e
commit 81b7f96abc
52 changed files with 2145 additions and 960 deletions

View File

@@ -0,0 +1,5 @@
export interface Driver {
type: string;
addess: number;
value: number;
}

15
src/vueLib/models/Get.ts Normal file
View File

@@ -0,0 +1,15 @@
type Query = {
depth: number;
};
type Get = {
uuid?: string;
path?: string;
type?: string;
rights?: string;
value?: string | number | boolean | null;
query?: Query;
hasChild?: boolean;
};
export type Gets = Get[];

View File

@@ -0,0 +1,3 @@
export interface PongMessage {
type: 'pong';
}

View File

@@ -0,0 +1,50 @@
export type Publish = {
event: string;
uuid: string;
path: string;
type: string;
value: string | number | boolean | null;
hasChild: boolean;
};
export type Pubs = Publish[];
import { updateSubscriptionValue, removeRawSubscriptions } from './Subscriptions';
import { buildTree, buildTreeWithRawSubs, removeNodes } from '../dbm/dbmTree';
import type { RawSubs, RawSubscribe } from '../models/Subscribe';
import { ref } from 'vue';
import { UpdateTable } from '../dbm/updateTable';
import { pathIsExpanded } from '../dbm/dbmTree';
export function publishToSubscriptions(pubs: Pubs) {
let event = '';
const rawSubs = ref<RawSubs>([]);
pubs.forEach((pub) => {
switch (pub.event) {
case 'onCreate':
event = 'onCreate';
if (!pathIsExpanded(pub.path)) break;
pub.hasChild = pubs.length > 0;
rawSubs.value.push(pub as RawSubscribe);
break;
case 'onChange':
break;
case 'onDelete':
event = 'onDelete';
rawSubs.value.push(pub as RawSubscribe);
break;
}
updateSubscriptionValue(pub.uuid, pub.value);
});
switch (event) {
case 'onCreate':
buildTreeWithRawSubs(rawSubs.value);
break;
case 'onDelete':
buildTree(null);
removeRawSubscriptions(rawSubs.value);
UpdateTable();
removeNodes(pubs);
break;
}
}

View File

@@ -0,0 +1,121 @@
import type { Gets } from './Get';
import type { Sets } from './Set';
import type { Subs } from './Subscribe';
import { api } from 'src/boot/axios';
export type Request = {
get?: Gets;
set?: Sets;
subscribe?: Subs;
unsubscribe?: Subs;
};
const query = '/json_data';
export async function getRequest(
uuid: string,
path: string = '',
depth: number = 1,
): Promise<Gets> {
let payload = {};
if (uuid !== '') {
payload = { uuid: uuid, path: path, query: { depth: depth } };
} else {
payload = { path: path, query: { depth: depth } };
}
const resp = await api.post(query, {
get: [payload],
});
if (resp.data.get && resp.data.get.length > 0) {
return resp.data.get;
} else {
throw new Error('No data returned');
}
}
export async function getRequests(gets: Gets): Promise<Gets> {
const resp = await api.post(query, {
get: gets,
});
if (resp.data.get && resp.data.get.length > 0) {
return resp.data.get;
} else {
throw new Error('No data returned');
}
}
export async function rawSetsRequest(sets: Sets): Promise<Sets> {
const resp = await api.post(query, {
set: sets,
});
if (resp.data.set && resp.data.set.length > 0) {
return resp.data.set;
} else {
throw new Error('No data returned');
}
}
export async function setRequest(
path: string,
type: string,
value: string | number | boolean,
rights?: string,
uuid?: string,
rename?: boolean,
): Promise<Sets> {
const payload = {
path: path,
type: type,
value: value,
rights: rights,
uuid: uuid,
rename: rename,
};
const resp = await api.post(query, {
set: [payload],
});
if (resp.data.set && resp.data.set.length > 0) {
return resp.data.set;
} else {
throw new Error('No data returned');
}
}
export async function setsRequest(sets: Sets): Promise<Sets> {
const resp = await api.post(query, {
set: sets,
});
if (resp.data.set && resp.data.set.length > 0) {
return resp.data.set;
} else {
throw new Error('No data returned');
}
}
export async function deleteRequest(uuid?: string, path?: string, rename?: boolean): Promise<Sets> {
let payload = {};
if (uuid) {
payload = { uuid: uuid, rename: rename };
} else if (path) {
payload = { path: path };
}
const resp = await api.delete('/json_data', {
data: {
set: [payload],
},
});
if (resp.data.set && resp.data.set.length > 0) {
return resp.data.set;
} else {
throw new Error('No data returned');
}
}

View File

@@ -0,0 +1,14 @@
import type { Gets } from './Get';
import type { Sets } from './Set';
import type { RawSubs } from './Subscribe';
import type { Pubs } from './Publish';
export type Response = {
get?: Gets;
set?: Sets;
subscribe?: RawSubs;
unsubscribe?: RawSubs;
publish?: Pubs;
error?: boolean;
message?: string;
};

11
src/vueLib/models/Set.ts Normal file
View File

@@ -0,0 +1,11 @@
export type Set = {
uuid?: string | undefined;
path?: string;
type?: string;
value: string | number | boolean | null | undefined;
rights?: string;
create?: boolean;
hasChild?: boolean;
};
export type Sets = Set[];

View File

@@ -0,0 +1,54 @@
import { ref } from 'vue';
import type { Ref } from 'vue';
import type { Driver } from './Drivers';
import type { Set } from './Set';
export type Subscribe = {
uuid?: string;
path?: string;
depth?: number;
type?: string;
drivers?: Record<string, Driver>;
value?: Ref<string | number | boolean | null | undefined>;
hasChild?: boolean;
};
export type Subs = Subscribe[];
export type RawSubscribe = {
uuid?: string;
path?: string;
depth?: number;
value?: string | number | boolean | null;
rights?: string;
hasChild?: boolean;
};
export type RawSubs = RawSubscribe[];
export function convertToSubscribe(raw: RawSubscribe | Set): Subscribe {
return {
...raw,
uuid: raw.uuid ?? '',
value: ref(raw.value ?? null),
};
}
export function convertToSubscribes(rawList: RawSubs): Subs {
const subs = rawList.map(convertToSubscribe).sort((a, b) => {
const aPath = a.path ?? '';
const bPath = b.path ?? '';
return aPath.localeCompare(bPath);
});
return subs as Subs;
}
export function convertToRaw(sub: Subscribe): RawSubscribe {
return {
...(sub.uuid !== undefined ? { uuid: sub.uuid } : {}),
...(sub.path !== undefined ? { path: sub.path } : {}),
...(sub.depth !== undefined ? { depth: sub.depth } : {}),
...(sub.value?.value !== undefined ? { value: sub.value.value } : {}),
...(sub.hasChild !== undefined ? { hasChild: sub.hasChild } : {}),
};
}

View File

@@ -0,0 +1,73 @@
import type { Ref } from 'vue';
import { reactive, ref } from 'vue';
import { convertToSubscribe } from '../models/Subscribe';
import type { Subscribe, RawSubs, RawSubscribe } from '../models/Subscribe';
import type { Set } from './Set';
const EMPTYUUID = '00000000-0000-0000-0000-000000000000';
export const Subscriptions = reactive<Record<string, Subscribe>>({});
export type TableSubscription = {
path: string;
value: Ref<string | number | boolean | null | undefined>;
};
export function addRawSubscription(sub: RawSubscribe | Set | undefined) {
if (sub === undefined) return;
addSubscription(convertToSubscribe(sub as RawSubscribe));
}
export function addRawSubscriptions(subs: RawSubs) {
subs.forEach((sub) => addSubscription(convertToSubscribe(sub)));
}
function addSubscription(sub: Subscribe) {
if (EMPTYUUID === sub.uuid) {
sub.path = 'DBM';
}
if (!sub.uuid) return;
Subscriptions[sub.uuid] = sub;
}
export function updateSubscription(sub: Subscribe) {
if (!sub.uuid) return;
Subscriptions[sub.uuid] = sub;
}
export function updateSubscriptionValue(
uuid: string,
value: string | number | boolean | null | undefined,
) {
if (!uuid) return;
if (!Subscriptions[uuid]) return;
Subscriptions[uuid].value = ref(value);
}
export function removeRawSubscription(sub: RawSubscribe | string) {
removeSubscription(typeof sub === 'string' ? sub : sub.uuid);
}
export function removeRawSubscriptions(subs: RawSubs) {
subs.forEach((sub) => {
removeSubscription(sub.uuid);
});
}
export function removeAllSubscriptions() {
Object.keys(Subscriptions).forEach((key) => delete Subscriptions[key]);
}
export function removeSubscription(uuid: string | undefined) {
if (uuid === undefined) return;
if (!Subscriptions || Subscriptions[uuid] === undefined) return;
delete Subscriptions[uuid];
}
export function findSubscriptionByPath(path: string): Subscribe | undefined {
return Object.values(Subscriptions).find((sub) => sub.path === path);
}
export function findSubscriptionByUuid(uuid: string): Subscribe | undefined {
if (!Subscriptions[uuid]) return;
return Subscriptions[uuid];
}

View File

@@ -0,0 +1,7 @@
import type { UUID } from 'crypto';
export interface Value {
uuid?: UUID;
path: string;
value: number | string | undefined;
}

View File

@@ -0,0 +1,13 @@
import type { Response } from './Response';
export function catchError(data: Error | Response): string {
if (data instanceof Response) {
if (data.message) return data.message;
else console.error(data);
} else if (data instanceof Error) {
return data.message;
} else {
console.error(data);
}
return '';
}