summaryrefslogtreecommitdiff
path: root/src/data/csv-replay-source.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/csv-replay-source.js')
-rw-r--r--src/data/csv-replay-source.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/data/csv-replay-source.js b/src/data/csv-replay-source.js
new file mode 100644
index 0000000..c4e6a66
--- /dev/null
+++ b/src/data/csv-replay-source.js
@@ -0,0 +1,60 @@
+import { BaseSource } from './base-source.js';
+
+function clamp(value, min, max) {
+ return Math.min(max, Math.max(min, value));
+}
+
+export class CsvReplaySource extends BaseSource {
+ constructor(config = {}) {
+ super({
+ replayRate: 1,
+ dataset: [],
+ ...config,
+ });
+ this.sourceType = 'csv-replay';
+ this.nextPointIndex = 0;
+ }
+
+ start(startTimeMs = 0) {
+ super.start();
+ this.reset(startTimeMs);
+ }
+
+ reset() {
+ this.nextPointIndex = 0;
+ }
+
+ updateConfig(nextConfig) {
+ const datasetChanged = nextConfig.dataset !== this.config.dataset;
+ super.updateConfig(nextConfig);
+ if (datasetChanged) {
+ this.reset();
+ }
+ }
+
+ update(currentPlotTimeMs) {
+ if (!this.running || !Array.isArray(this.config.dataset) || this.config.dataset.length === 0) {
+ return [];
+ }
+
+ const replayRate = clamp(this.config.replayRate ?? 1, 0.1, 8);
+ const targetDatasetTimeMs = currentPlotTimeMs * replayRate;
+ const points = [];
+
+ while (this.nextPointIndex < this.config.dataset.length) {
+ const datasetPoint = this.config.dataset[this.nextPointIndex];
+ if (datasetPoint.timeMs > targetDatasetTimeMs) {
+ break;
+ }
+
+ points.push({
+ timeMs: datasetPoint.timeMs / replayRate,
+ value: datasetPoint.value,
+ sourceId: this.config.id ?? 'csv-replay',
+ });
+ this.nextPointIndex += 1;
+ }
+
+ return points;
+ }
+}