Skip to content

Commit

Permalink
sensors-logging: Use setTimeout instead of setInterval on log rou…
Browse files Browse the repository at this point in the history
…tine

This allows for changing the log interval during a log.
  • Loading branch information
rafaellehmkuhl authored and patrickelectric committed Feb 27, 2024
1 parent d2767fb commit 5b19ace
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/libs/sensors-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type CockpitStandardLog = CockpitStandardLogPoint[]
* Manager logging vehicle data and others
*/
class DataLogger {
currentLoggerInterval: ReturnType<typeof setInterval> | undefined = undefined
shouldBeLogging = false
currentCockpitLog: CockpitStandardLog = []
variablesBeingUsed: DatalogVariable[] = []
selectedVariablesToShow = useStorage<DatalogVariable[]>('cockpit-datalogger-overlay-variables', [])
Expand All @@ -85,6 +85,8 @@ class DataLogger {
return
}

this.shouldBeLogging = true

const vehicleStore = useMainVehicleStore()
const cockpitLogsDB = localforage.createInstance({
driver: localforage.INDEXEDDB,
Expand All @@ -98,7 +100,7 @@ class DataLogger {
const fileName = `Cockpit (${format(initialTime, 'LLL dd, yyyy - HH꞉mm꞉ss O')}).clog`
this.currentCockpitLog = []

this.currentLoggerInterval = setInterval(async () => {
const logRoutine = async (): Promise<void> => {
const timeNow = new Date()
const secondsNow = differenceInSeconds(timeNow, initialTime)

Expand Down Expand Up @@ -127,7 +129,13 @@ class DataLogger {
})

await cockpitLogsDB.setItem(fileName, this.currentCockpitLog)
}, interval)

if (this.shouldBeLogging) {
setTimeout(logRoutine, this.logInterval.value)
}
}

logRoutine()
}

/**
Expand All @@ -139,15 +147,15 @@ class DataLogger {
return
}

clearInterval(this.currentLoggerInterval)
this.shouldBeLogging = false
}

/**
* Wether the logger is currently logging or not
* @returns {boolean}
*/
logging(): boolean {
return this.currentLoggerInterval !== undefined
return this.shouldBeLogging
}

/**
Expand Down

0 comments on commit 5b19ace

Please sign in to comment.