Skip to content

Commit 3937286

Browse files
committed
made some changes to files
1 parent 8b21d63 commit 3937286

3 files changed

Lines changed: 114 additions & 259 deletions

File tree

RocketControlUnitGUI/src/data.ts

Lines changed: 93 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,110 @@
1-
// data.ts
2-
import PocketBase from 'pocketbase';
1+
// data.ts
2+
import PocketBase from 'pocketbase';
3+
import dotenv from "dotenv";
34

4-
const pb = new PocketBase('http://127.0.0.1:8090');
5-
pb.autoCancellation(false);
5+
66

7-
let isAuthenticated = false;
7+
const ADMIN_EMAIL: string = import.meta.env.VITE_EMAIL;
8+
const ADMIN_PASSWORD: string = import.meta.env.VITE_PASSWORD;
89

9-
const ADMIN_EMAIL = 'test';
10-
const ADMIN_PASSWORD = 'test';
10+
console.log(ADMIN_EMAIL);
11+
12+
const pb = new PocketBase('http://127.0.0.1:8090');
13+
pb.autoCancellation(false);
14+
15+
let isAuthenticated = false;
1116

12-
// Function to authenticate the admin user
13-
export async function authenticate() {
14-
if (!isAuthenticated) {
15-
await pb.admins.authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD);
16-
isAuthenticated = true;
17-
}
18-
}
19-
20-
type RecordData = { [key: string]: any };
21-
export type AllData = { [collectionName: string]: RecordData[] };
22-
23-
// Fetch paginated data with existing logic
24-
export async function fetchPaginatedData(
25-
collectionName: string,
26-
sendToChart: (data: RecordData[]) => void,
27-
batchSize: number = 10
28-
) {
29-
console.log(`Fetching data from collection: ${collectionName}`);
30-
let page = 1;
31-
let hasMoreData = true;
32-
33-
try {
34-
await authenticate(); // Authenticate once before fetching data
35-
36-
while (hasMoreData) {
37-
console.log('Fetching data from collection:', collectionName, 'Page:', page);
38-
const records = await pb.collection(collectionName).getList(page, batchSize);
39-
console.log('Fetched records:', records.items);
40-
41-
if (records.items.length === 0) {
42-
console.warn(`No records found for ${collectionName} on page ${page}.`);
43-
break;
44-
}
4517

46-
const dynamicKeys = Object.keys(records.items[0]).filter(key => key !== 'id' && key !== 'created');
4718

48-
const transformedBatch: RecordData[] = records.items.map((record: RecordData) => {
49-
const transformedRecord: RecordData = {};
50-
dynamicKeys.forEach(key => {
51-
transformedRecord[key] = record[key];
19+
// Function to authenticate the admin user
20+
export async function authenticate() {
21+
if (!isAuthenticated) {
22+
await pb.admins.authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD);
23+
isAuthenticated = true;
24+
}
25+
}
26+
27+
type RecordData = { [key: string]: any };
28+
export type AllData = { [collectionName: string]: RecordData[] };
29+
30+
// Fetch paginated data with existing logic
31+
export async function fetchPaginatedData(
32+
collectionName: string,
33+
sendToChart: (data: RecordData[]) => void,
34+
batchSize: number = 10
35+
) {
36+
console.log(`Fetching data from collection: ${collectionName}`);
37+
let page = 1;
38+
let hasMoreData = true;
39+
40+
try {
41+
await authenticate(); // Authenticate once before fetching data
42+
43+
while (hasMoreData) {
44+
console.log('Fetching data from collection:', collectionName, 'Page:', page);
45+
const records = await pb.collection(collectionName).getList(page, batchSize);
46+
console.log('Fetched records:', records.items);
47+
48+
if (records.items.length === 0) {
49+
console.warn(`No records found for ${collectionName} on page ${page}.`);
50+
break;
51+
}
52+
53+
const dynamicKeys = Object.keys(records.items[0]).filter(key => key !== 'id' && key !== 'created');
54+
55+
const transformedBatch: RecordData[] = records.items.map((record: RecordData) => {
56+
const transformedRecord: RecordData = {};
57+
dynamicKeys.forEach(key => {
58+
transformedRecord[key] = record[key];
59+
});
60+
return transformedRecord;
5261
});
53-
return transformedRecord;
54-
});
5562

56-
console.log('Transformed batch:', transformedBatch);
57-
sendToChart(transformedBatch);
63+
console.log('Transformed batch:', transformedBatch);
64+
sendToChart(transformedBatch);
5865

59-
if (records.items.length < batchSize) {
60-
hasMoreData = false;
61-
} else {
62-
page += 1;
66+
if (records.items.length < batchSize) {
67+
hasMoreData = false;
68+
} else {
69+
page += 1;
70+
}
6371
}
72+
console.log('All data fetched for collection:', collectionName);
73+
} catch (error) {
74+
console.error('Error fetching paginated data for collection:', collectionName, error);
6475
}
65-
console.log('All data fetched for collection:', collectionName);
66-
} catch (error) {
67-
console.error('Error fetching paginated data for collection:', collectionName, error);
6876
}
69-
}
70-
71-
// Real-time subscription to a collection
72-
export async function subscribeToCollection(
73-
collectionName: string,
74-
handleDataUpdate: (data: RecordData) => void
75-
) {
76-
try {
77-
await authenticate(); // Ensure we're authenticated before subscribing
78-
79-
// Subscribe to the collection for any changes
80-
pb.collection(collectionName).subscribe('*', function (e) {
81-
console.log(`Received real-time update for collection ${collectionName}:`, e.record);
82-
handleDataUpdate(e.record);
83-
});
84-
85-
console.log(`Subscribed to real-time updates for collection: ${collectionName}`);
86-
} catch (error) {
87-
console.error(`Error subscribing to collection ${collectionName}:`, error);
77+
78+
// Real-time subscription to a collection
79+
export async function subscribeToCollection(
80+
collectionName: string,
81+
handleDataUpdate: (data: RecordData) => void
82+
) {
83+
try {
84+
await authenticate(); // Ensure we're authenticated before subscribing
85+
86+
// Subscribe to the collection for any changes
87+
pb.collection(collectionName).subscribe('*', function (e) {
88+
console.log(`Received real-time update for collection ${collectionName}:`, e.record);
89+
handleDataUpdate(e.record);
90+
});
91+
92+
console.log(`Subscribed to real-time updates for collection: ${collectionName}`);
93+
} catch (error) {
94+
console.error(`Error subscribing to collection ${collectionName}:`, error);
95+
}
8896
}
89-
}
9097

91-
// Function to unsubscribe from a collection (optional)
92-
export async function unsubscribeFromCollection(collectionName: string) {
93-
try {
94-
await authenticate(); // Ensure we're authenticated before unsubscribing
98+
// Function to unsubscribe from a collection (optional)
99+
export async function unsubscribeFromCollection(collectionName: string) {
100+
try {
101+
await authenticate(); // Ensure we're authenticated before unsubscribing
95102

96-
// Unsubscribe from the collection
97-
pb.collection(collectionName).unsubscribe('*');
103+
// Unsubscribe from the collection
104+
pb.collection(collectionName).unsubscribe('*');
98105

99-
console.log(`Unsubscribed from real-time updates for collection: ${collectionName}`);
100-
} catch (error) {
101-
console.error(`Error unsubscribing from collection ${collectionName}:`, error);
106+
console.log(`Unsubscribed from real-time updates for collection: ${collectionName}`);
107+
} catch (error) {
108+
console.error(`Error unsubscribing from collection ${collectionName}:`, error);
109+
}
102110
}
103-
}

RocketControlUnitGUI/src/routes/data/RocketChart.svelte

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@
2323
let currentRotationY = 0;
2424
let currentRotationZ = 0;
2525
26-
// Target rotation values based on fetched data
27-
let targetRotationX = 0;
28-
let targetRotationY = 0;
29-
let targetRotationZ = 0;
26+
// Smoothed target rotation values
27+
let smoothedRotationX = 0;
28+
let smoothedRotationY = 0;
29+
let smoothedRotationZ = 0;
3030
3131
// Rotation speed for smooth animation
32-
const rotationSpeed = 1;
32+
const rotationSpeed = 0.002;
33+
34+
// Smoothing factor for EMA (between 0 and 1)
35+
const smoothingFactor = 0.1; // Adjust this value as needed
3336
3437
// Handle data updates from real-time subscription
3538
function handleDataUpdate(data: RecordData) {
3639
// Parse the gyro values safely
37-
targetRotationX = parseFloat(data.gyro_x) || 0;
38-
targetRotationY = parseFloat(data.gyro_y) || 0;
39-
targetRotationZ = parseFloat(data.gyro_z) || 0;
40+
const rawRotationX = parseFloat(data.gyro_x) || 0;
41+
const rawRotationY = parseFloat(data.gyro_y) || 0;
42+
const rawRotationZ = parseFloat(data.gyro_z) || 0;
43+
44+
// Apply Exponential Moving Average for smoothing
45+
smoothedRotationX = smoothingFactor * rawRotationX + (1 - smoothingFactor) * smoothedRotationX;
46+
smoothedRotationY = smoothingFactor * rawRotationY + (1 - smoothingFactor) * smoothedRotationY;
47+
smoothedRotationZ = smoothingFactor * rawRotationZ + (1 - smoothingFactor) * smoothedRotationZ;
4048
}
4149
4250
// Handle the first batch of fetched paginated data
@@ -52,7 +60,7 @@
5260
5361
onMount(() => {
5462
// Fetch existing paginated data from PocketBase
55-
fetchPaginatedData('Imu', handleFirstBatch, 10); // Adjust batch size if needed
63+
fetchPaginatedData('Imu', handleFirstBatch, 1); // Adjust batch size if needed
5664
5765
// Subscribe to real-time updates after initial data fetch
5866
subscribeToCollection('Imu', handleDataUpdate);
@@ -65,12 +73,10 @@
6573
6674
// Use frame to update rotation smoothly
6775
useFrame((ctx, delta) => {
68-
// 'delta' is the time since last frame in seconds
69-
70-
// Interpolate towards the target rotations for smooth animation
71-
currentRotationX += (targetRotationX - currentRotationX) * rotationSpeed * delta;
72-
currentRotationY += (targetRotationY - currentRotationY) * rotationSpeed * delta;
73-
currentRotationZ += (targetRotationZ - currentRotationZ) * rotationSpeed * delta;
76+
// Interpolate towards the smoothed rotations for smooth animation
77+
currentRotationX += (smoothedRotationX - currentRotationX) * rotationSpeed * delta;
78+
currentRotationY += (smoothedRotationY - currentRotationY) * rotationSpeed * delta;
79+
currentRotationZ += (smoothedRotationZ - currentRotationZ) * rotationSpeed * delta;
7480
});
7581
</script>
7682

0 commit comments

Comments
 (0)