Skip to content

Commit

Permalink
Fix: add offset to resync if data is lost
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebonnici committed Apr 15, 2024
1 parent dd280dd commit e9139cd
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ export interface GlobalOptions {
onSuccess: (writeBuffer: WriteBuffer, absoluteTime: number) => void;
onFail: (error: Error) => void;
}[];
inSyncOffset: number;
lastInSyncTime: number;
}

const options: GlobalOptions = {
samplesPerSecond: initialSamplesPerSecond,
timeReachedTriggers: [],
inSyncOffset: 0,
lastInSyncTime: 0,
};

class FileData {
Expand Down Expand Up @@ -175,11 +179,14 @@ export const DataManager = () => ({

getTimestamp,
isInSync: () => {
const actualTimePassed =
Date.now() -
(options.writeBuffer?.getFirstWriteTime() ??
options.fileBuffer?.getFirstWriteTime() ??
0);
const firstWriteTime =
options.writeBuffer?.getFirstWriteTime() ??
options.fileBuffer?.getFirstWriteTime() ??
0;

if (firstWriteTime === 0) return true;

const actualTimePassed = Date.now() - firstWriteTime;

const processedBytes =
options.writeBuffer?.getBytesWritten() ??
Expand All @@ -194,7 +201,21 @@ export const DataManager = () => ({

// We get serial data every 30 ms regardless of sampling rate.
// If PC is ahead by more then 1.5 samples we are not in sync
return pcAheadDelta <= 45;
let inSync = pcAheadDelta - options.inSyncOffset <= 45;

if (inSync) {
options.lastInSyncTime = Date.now();
}

// If Data is lost in the serial and this was not detected we need to resync the timers so we do not get stuck rendering at 1 FPS
// NOTE this is temporary fix until PPK protocol can handel data loss better
if (Date.now() - options.lastInSyncTime >= 1000) {
options.lastInSyncTime = Date.now();
options.inSyncOffset = actualTimePassed - simulationDelta;
inSync = true;
}

return inSync;
},
getStartSystemTime: () => options.fileBuffer?.getFirstWriteTime(),

Expand Down Expand Up @@ -258,6 +279,7 @@ export const DataManager = () => ({
options.writeBuffer = undefined;
options.foldingBuffer = undefined;
options.samplesPerSecond = initialSamplesPerSecond;
options.inSyncOffset = 0;
},
initializeLiveSession: (sessionRootPath: string) => {
const sessionPath = path.join(sessionRootPath, v4());
Expand Down

0 comments on commit e9139cd

Please sign in to comment.