Skip to content

Commit

Permalink
Fix: Time multi channel working - I could swear I would
Browse files Browse the repository at this point in the history
  • Loading branch information
Grabt234 committed Dec 10, 2023
1 parent 54bc3db commit 6dc6de9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 58 deletions.
49 changes: 28 additions & 21 deletions src/lib/SensorGroup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
legend: { display: false },
animation: {
// Disable animations
duration: 1 // Set the duration to 0 for all animations
duration: 2 // Set the duration to 0 for all animations
}
}
});
Expand Down Expand Up @@ -76,7 +76,7 @@
options: {
legend: { display: false },
animation: {
duration: 1
duration: 2
}
}
});
Expand All @@ -96,35 +96,42 @@
const colors = ['red', 'green', 'blue', 'yellow', 'orange', 'purple'];
if (mounted) {
let timeDatasets = [];
const numChannels = TimeDomainYValues.length;
console.log('Upading (START) ' + timeID);
console.log(TimeDomainYValues);
let timeDatasets = [];
// Create an empty dataset for each channel
for (let channelIndex = 0; channelIndex < numChannels; channelIndex++) {
timeDatasets.push({
data: TimeDomainYValues[channelIndex],
data: [],
borderColor: colors[channelIndex],
fill: false
});
}
console.log(TimeDomainChart);
TimeDomainChart.data.datasets = timeDatasets;
TimeDomainChart.data.labels = TimeDomainXValues;
TimeDomainChart.update();
}
if (mounted) {
let freqDatasets = [];
const numChannels = FreqDomainYValues.length;
// Update the data for each channel
for (let channelIndex = 0; channelIndex < numChannels; channelIndex++) {
freqDatasets.push({
data: FreqDomainYValues[channelIndex],
borderColor: colors[channelIndex],
fill: false
});
timeDatasets[channelIndex].data.push(...TimeDomainYValues[channelIndex]);
}
console.log(FreqDomainChart);
FreqDomainChart.data.datasets = freqDatasets;
FreqDomainChart.data.labels = FreqDomainXValues;
FreqDomainChart.update();
console.log('Upading (END) ' + timeID);
TimeDomainChart.data.datasets = timeDatasets;
TimeDomainChart.data.labels = TimeDomainXValues;
// let freqDatasets = [];
// for (let channelIndex = 0; channelIndex < numChannels; channelIndex++) {
// freqDatasets.push({
// data: FreqDomainYValues[channelIndex],
// borderColor: colors[channelIndex],
// fill: false
// });
// }
// FreqDomainChart.data.datasets = freqDatasets;
// FreqDomainChart.data.labels = FreqDomainXValues;
TimeDomainChart.update();
//FreqDomainChart.update();
}
}
</script>
Expand Down
92 changes: 55 additions & 37 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { writable } from 'svelte/store';

Check warning on line 5 in src/routes/+page.svelte

View workflow job for this annotation

GitHub Actions / lint

'writable' is defined but never used
// Create a writable store initialized as an empty object (to mimic a map)
let mapData = [];
var mapData = [];
// Function to add an item to the "map"
// function updateItemInMap(key, value) {
Expand All @@ -14,53 +14,72 @@
function updateItemInMap(key, value) {
//const existingItemIndex = mapData.findIndex((item) => item.id === newItem.id);
// console.log(key);
// console.log(value);
// console.log('----');
let index = -1;
for (let i = 0; i < mapData.length; i++) {
if (JSON.stringify(mapData[i].key) === JSON.stringify(key)) {
index = i;
break;
}
}
if (index !== -1) {
// Update the existing item
let a = Object.assign(mapData[index].value, value);
mapData[index].value = a;
// new or first entry
if (index == -1) {
if (mapData.length == 0) {
index = 0;
mapData[index] = {
key: key,
value: value
};
} else {
index = mapData.length;
mapData[index] = {
key: key,
value: value
};
}
console.log('Added at index: ' + index);
} else {
// Add the new item if it doesn't exist
let newItem = { key: key, value: value }; // Assuming key represents the 'id' of the item
mapData.push(newItem);
}
console.log('Found at index: (START)' + index);
console.log(mapData[index].value['TimeDomainYValues']);
console.log();
for (const key in value) {
mapData[index].value[key] = JSON.parse(JSON.stringify(value[key]));
}
console.log(mapData[index].value['TimeDomainYValues']);
console.log('Found at index: (END)' + index);
}
}
// Create the time domain chart and link mount it to the HTML canvas
onMount(() => {
const TimeWebSocket = new WebSocket('ws://localhost:10100/DataTypes/TimeChunk');
let timeParsedData = null;
let timeDatasets = [];
// console.log(sensorGroup);
TimeWebSocket.addEventListener('message', async (event) => {
const receivedMessage = event.data;
timeParsedData = JSON.parse(receivedMessage);
var timeParsedData = JSON.parse(event.data);
let timeDatasets = [];
// Check if we are tracking it in the mpa already
// And if not then track it
const newData = JSON.parse(event.data)['TimeChunk']['Channels'];
const numChannels = JSON.parse(event.data)['TimeChunk']['NumChannels'];
const newData = timeParsedData['TimeChunk']['Channels'];
const numChannels = timeParsedData['TimeChunk']['NumChannels'];
for (let channelIndex = 0; channelIndex < numChannels; channelIndex++) {
timeDatasets[channelIndex] = newData[channelIndex];
}
updateItemInMap(JSON.parse(event.data)['TimeChunk']['SourceIndentifier'], {
timeSampleRate: JSON.parse(event.data)['TimeChunk']['SampleRate'],
timeChunkSize: JSON.parse(event.data)['TimeChunk']['ChunkSize'],
sourceIdentifier: JSON.parse(event.data)['TimeChunk']['SourceIndentifier'],
// console.log(newData);
let a = {
timeSampleRate: timeParsedData['TimeChunk']['SampleRate'],
timeChunkSize: timeParsedData['TimeChunk']['ChunkSize'],
sourceIdentifier: timeParsedData['TimeChunk']['SourceIdentifier'],
TimeDomainYValues: timeDatasets,
TimeDomainXValues: Array.from({ length: 512 }, (_, index) => index + 1),
timeID: JSON.parse(event.data)['TimeChunk']['SourceIndentifier'] + '-time',
freqID: JSON.parse(event.data)['TimeChunk']['SourceIndentifier'] + '-freq'
});
timeID: timeParsedData['TimeChunk']['SourceIdentifier'] + '-time'
};
updateItemInMap(timeParsedData['TimeChunk']['SourceIdentifier'], a);
});
const FreqWebSocket = new WebSocket('ws://localhost:10100/DataTypes/FFTMagnitudeChunk');
Expand All @@ -72,20 +91,19 @@
// Check if we are tracking it in the mpa already
// And if not then track it
const newData = JSON.parse(event.data)['FFTMagnitudeChunk']['Channels'];
const numChannels = JSON.parse(event.data)['FFTMagnitudeChunk']['NumChannels'];
for (let channelIndex = 0; channelIndex < numChannels; channelIndex++) {
freqDatasets[channelIndex] = newData[channelIndex];
}
updateItemInMap(JSON.parse(event.data)['FFTMagnitudeChunk']['SourceIndentifier'], {
freqSampleRate: JSON.parse(event.data)['FFTMagnitudeChunk']['SampleRate'],
freqChunkSize: JSON.parse(event.data)['FFTMagnitudeChunk']['ChunkSize'],
sourceIdentifier: JSON.parse(event.data)['FFTMagnitudeChunk']['SourceIndentifier'],
FreqDomainYValues: freqDatasets,
FreqDomainXValues: Array.from({ length: 512 }, (_, index) => index + 1),
timeID: JSON.parse(event.data)['FFTMagnitudeChunk']['SourceIndentifier'] + '-time',
freqID: JSON.parse(event.data)['FFTMagnitudeChunk']['SourceIndentifier'] + '-freq'
});
// const newData = FreqParsedData['FFTMagnitudeChunk']['Channels'];
// const numChannels = FreqParsedData['FFTMagnitudeChunk']['NumChannels'];
// for (let channelIndex = 0; channelIndex < numChannels; channelIndex++) {
// freqDatasets[channelIndex] = newData[channelIndex];
// }
// updateItemInMap(FreqParsedData['FFTMagnitudeChunk']['SourceIdentifier'], {
// freqSampleRate: FreqParsedData['FFTMagnitudeChunk']['SampleRate'],
// freqChunkSize: FreqParsedData['FFTMagnitudeChunk']['ChunkSize'],
// sourceIdentifier: FreqParsedData['FFTMagnitudeChunk']['SourceIdentifier'],
// FreqDomainYValues: freqDatasets,
// FreqDomainXValues: Array.from({ length: 512 }, (_, index) => index + 1),
// timeID: FreqParsedData['FFTMagnitudeChunk']['SourceIdentifier'] + '-time'
// });
});
});
</script>
Expand Down

0 comments on commit 6dc6de9

Please sign in to comment.