Skip to content

Commit

Permalink
made some changes to files
Browse files Browse the repository at this point in the history
  • Loading branch information
alymasani committed Oct 19, 2024
1 parent 8b21d63 commit 3937286
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 259 deletions.
179 changes: 93 additions & 86 deletions RocketControlUnitGUI/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,110 @@
// data.ts
import PocketBase from 'pocketbase';
// data.ts
import PocketBase from 'pocketbase';
import dotenv from "dotenv";

const pb = new PocketBase('http://127.0.0.1:8090');
pb.autoCancellation(false);


let isAuthenticated = false;
const ADMIN_EMAIL: string = import.meta.env.VITE_EMAIL;
const ADMIN_PASSWORD: string = import.meta.env.VITE_PASSWORD;

const ADMIN_EMAIL = 'test';
const ADMIN_PASSWORD = 'test';
console.log(ADMIN_EMAIL);

const pb = new PocketBase('http://127.0.0.1:8090');
pb.autoCancellation(false);

let isAuthenticated = false;

// Function to authenticate the admin user
export async function authenticate() {
if (!isAuthenticated) {
await pb.admins.authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD);
isAuthenticated = true;
}
}

type RecordData = { [key: string]: any };
export type AllData = { [collectionName: string]: RecordData[] };

// Fetch paginated data with existing logic
export async function fetchPaginatedData(
collectionName: string,
sendToChart: (data: RecordData[]) => void,
batchSize: number = 10
) {
console.log(`Fetching data from collection: ${collectionName}`);
let page = 1;
let hasMoreData = true;

try {
await authenticate(); // Authenticate once before fetching data

while (hasMoreData) {
console.log('Fetching data from collection:', collectionName, 'Page:', page);
const records = await pb.collection(collectionName).getList(page, batchSize);
console.log('Fetched records:', records.items);

if (records.items.length === 0) {
console.warn(`No records found for ${collectionName} on page ${page}.`);
break;
}

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

const transformedBatch: RecordData[] = records.items.map((record: RecordData) => {
const transformedRecord: RecordData = {};
dynamicKeys.forEach(key => {
transformedRecord[key] = record[key];
// Function to authenticate the admin user
export async function authenticate() {
if (!isAuthenticated) {
await pb.admins.authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD);
isAuthenticated = true;
}
}

type RecordData = { [key: string]: any };
export type AllData = { [collectionName: string]: RecordData[] };

// Fetch paginated data with existing logic
export async function fetchPaginatedData(
collectionName: string,
sendToChart: (data: RecordData[]) => void,
batchSize: number = 10
) {
console.log(`Fetching data from collection: ${collectionName}`);
let page = 1;
let hasMoreData = true;

try {
await authenticate(); // Authenticate once before fetching data

while (hasMoreData) {
console.log('Fetching data from collection:', collectionName, 'Page:', page);
const records = await pb.collection(collectionName).getList(page, batchSize);
console.log('Fetched records:', records.items);

if (records.items.length === 0) {
console.warn(`No records found for ${collectionName} on page ${page}.`);
break;
}

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

const transformedBatch: RecordData[] = records.items.map((record: RecordData) => {
const transformedRecord: RecordData = {};
dynamicKeys.forEach(key => {
transformedRecord[key] = record[key];
});
return transformedRecord;
});
return transformedRecord;
});

console.log('Transformed batch:', transformedBatch);
sendToChart(transformedBatch);
console.log('Transformed batch:', transformedBatch);
sendToChart(transformedBatch);

if (records.items.length < batchSize) {
hasMoreData = false;
} else {
page += 1;
if (records.items.length < batchSize) {
hasMoreData = false;
} else {
page += 1;
}
}
console.log('All data fetched for collection:', collectionName);
} catch (error) {
console.error('Error fetching paginated data for collection:', collectionName, error);
}
console.log('All data fetched for collection:', collectionName);
} catch (error) {
console.error('Error fetching paginated data for collection:', collectionName, error);
}
}

// Real-time subscription to a collection
export async function subscribeToCollection(
collectionName: string,
handleDataUpdate: (data: RecordData) => void
) {
try {
await authenticate(); // Ensure we're authenticated before subscribing

// Subscribe to the collection for any changes
pb.collection(collectionName).subscribe('*', function (e) {
console.log(`Received real-time update for collection ${collectionName}:`, e.record);
handleDataUpdate(e.record);
});

console.log(`Subscribed to real-time updates for collection: ${collectionName}`);
} catch (error) {
console.error(`Error subscribing to collection ${collectionName}:`, error);

// Real-time subscription to a collection
export async function subscribeToCollection(
collectionName: string,
handleDataUpdate: (data: RecordData) => void
) {
try {
await authenticate(); // Ensure we're authenticated before subscribing

// Subscribe to the collection for any changes
pb.collection(collectionName).subscribe('*', function (e) {
console.log(`Received real-time update for collection ${collectionName}:`, e.record);
handleDataUpdate(e.record);
});

console.log(`Subscribed to real-time updates for collection: ${collectionName}`);
} catch (error) {
console.error(`Error subscribing to collection ${collectionName}:`, error);
}
}
}

// Function to unsubscribe from a collection (optional)
export async function unsubscribeFromCollection(collectionName: string) {
try {
await authenticate(); // Ensure we're authenticated before unsubscribing
// Function to unsubscribe from a collection (optional)
export async function unsubscribeFromCollection(collectionName: string) {
try {
await authenticate(); // Ensure we're authenticated before unsubscribing

// Unsubscribe from the collection
pb.collection(collectionName).unsubscribe('*');
// Unsubscribe from the collection
pb.collection(collectionName).unsubscribe('*');

console.log(`Unsubscribed from real-time updates for collection: ${collectionName}`);
} catch (error) {
console.error(`Error unsubscribing from collection ${collectionName}:`, error);
console.log(`Unsubscribed from real-time updates for collection: ${collectionName}`);
} catch (error) {
console.error(`Error unsubscribing from collection ${collectionName}:`, error);
}
}
}
36 changes: 21 additions & 15 deletions RocketControlUnitGUI/src/routes/data/RocketChart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,28 @@
let currentRotationY = 0;
let currentRotationZ = 0;
// Target rotation values based on fetched data
let targetRotationX = 0;
let targetRotationY = 0;
let targetRotationZ = 0;
// Smoothed target rotation values
let smoothedRotationX = 0;
let smoothedRotationY = 0;
let smoothedRotationZ = 0;
// Rotation speed for smooth animation
const rotationSpeed = 1;
const rotationSpeed = 0.002;
// Smoothing factor for EMA (between 0 and 1)
const smoothingFactor = 0.1; // Adjust this value as needed
// Handle data updates from real-time subscription
function handleDataUpdate(data: RecordData) {
// Parse the gyro values safely
targetRotationX = parseFloat(data.gyro_x) || 0;
targetRotationY = parseFloat(data.gyro_y) || 0;
targetRotationZ = parseFloat(data.gyro_z) || 0;
const rawRotationX = parseFloat(data.gyro_x) || 0;
const rawRotationY = parseFloat(data.gyro_y) || 0;
const rawRotationZ = parseFloat(data.gyro_z) || 0;
// Apply Exponential Moving Average for smoothing
smoothedRotationX = smoothingFactor * rawRotationX + (1 - smoothingFactor) * smoothedRotationX;
smoothedRotationY = smoothingFactor * rawRotationY + (1 - smoothingFactor) * smoothedRotationY;
smoothedRotationZ = smoothingFactor * rawRotationZ + (1 - smoothingFactor) * smoothedRotationZ;
}
// Handle the first batch of fetched paginated data
Expand All @@ -52,7 +60,7 @@
onMount(() => {
// Fetch existing paginated data from PocketBase
fetchPaginatedData('Imu', handleFirstBatch, 10); // Adjust batch size if needed
fetchPaginatedData('Imu', handleFirstBatch, 1); // Adjust batch size if needed
// Subscribe to real-time updates after initial data fetch
subscribeToCollection('Imu', handleDataUpdate);
Expand All @@ -65,12 +73,10 @@
// Use frame to update rotation smoothly
useFrame((ctx, delta) => {
// 'delta' is the time since last frame in seconds
// Interpolate towards the target rotations for smooth animation
currentRotationX += (targetRotationX - currentRotationX) * rotationSpeed * delta;
currentRotationY += (targetRotationY - currentRotationY) * rotationSpeed * delta;
currentRotationZ += (targetRotationZ - currentRotationZ) * rotationSpeed * delta;
// Interpolate towards the smoothed rotations for smooth animation
currentRotationX += (smoothedRotationX - currentRotationX) * rotationSpeed * delta;
currentRotationY += (smoothedRotationY - currentRotationY) * rotationSpeed * delta;
currentRotationZ += (smoothedRotationZ - currentRotationZ) * rotationSpeed * delta;
});
</script>

Expand Down
Loading

0 comments on commit 3937286

Please sign in to comment.