Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adjusting telemetry interval #746

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions src/libs/sensors-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,23 @@ 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', [])
logInterval = useStorage<number>('cockpit-datalogger-log-interval', 1000)

/**
* Start an intervaled logging
* @param {number} interval - The time to wait between two logs
*/
startLogging(interval = 1000): void {
startLogging(): void {
if (this.logging()) {
Swal.fire({ title: 'Error', text: 'A log is already being generated.', icon: 'error', timer: 3000 })
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,41 @@ 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
}

/**
* Set the interval between log points
* @param {number} interval The interval in milliseconds. Default is 1000 and minimum is 1
*/
setInterval(interval: number): void {
if (interval < 1) {
Swal.fire({ text: 'Minimum log interval is 1 millisecond (1000 Hz).', icon: 'error' })
return
}

this.logInterval.value = interval
}

/**
* Set the frequency of log points
* @param {number} frequency The frequency in hertz. Default is 1 Hz, minimum is 0.1 Hz and maximum is 1000 Hz
*/
setFrequency(frequency: number): void {
if (frequency > 1000 || frequency < 0.1) {
Swal.fire({ text: 'Log frequency should stay between 0.1 Hz and 1000 Hz.', icon: 'error' })
return
}

this.setInterval(1000 / frequency)
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/views/ConfigurationLogsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,41 @@
:value="variable"
></v-checkbox>
</div>
<h1 class="text-lg font-bold text-slate-600">Frequency of the telemetry log</h1>
<span class="text-sm text-slate-400 w-[50%] text-center">
Values between 1 and 100Hz are more common and can be set with the slider.
</span>
<span class="text-sm text-slate-400">
You can go as low as 0.1 Hz and as far as 1000 Hz using the text input.
</span>
<div class="flex flex-col justify-center m-3 align-center">
<fwb-range v-model="newFrequency" class="m-2" :min="1" :max="100" :steps="1" label="" />
<fwb-input v-model="newFrequencyString" class="w-24 m-1 text-center align-middle">
<template #suffix>
<span class="flex justify-center h-7 align-center">Hz</span>
</template>
</fwb-input>
</div>
</template>
</BaseConfigurationView>
</template>

<script setup lang="ts">
import { FwbInput, FwbRange } from 'flowbite-vue'
import { computed, ref, watch } from 'vue'
import { datalogger, DatalogVariable } from '@/libs/sensors-logging'
import BaseConfigurationView from './BaseConfigurationView.vue'
const newFrequency = ref(1000 / datalogger.logInterval.value)
watch(newFrequency, (newVal) => {
datalogger.setFrequency(newVal)
})
const newFrequencyString = computed({
get: () => newFrequency.value.toString(),
set: (value) => (newFrequency.value = parseFloat(value)),
})
</script>
Loading