+
Start Time: {startTimeString}
+
End Time: {endTimeString}
+
Recording Duration: {formatDuration(durationInSeconds)}
+
Samples Recorded: {recordedFilesCount}
+
+ ),
+ });
+ } else {
+ console.error("Start time is null. Unable to calculate duration.");
+ toast.error("Recording start time was not captured.");
}
- setrecData(false); // Reset recording data state
-
- // Display a success message with recording details
- toast.success("Recording completed Successfully", {
- description: (
-
-
Start Time: {startTimeString}
-
End Time: {endTimeString}
-
Recording Duration: {formatDuration(durationInSeconds)}
-
Samples Recorded: {recordedFilesCount}
-
Data saved to IndexedDB
-
- ),
- });
-
- // Reset recording states
- isRecordingRef.current = false; // Indicate recording has stopped
- startTimeRef.current = null; // Clear the start time
- endTimeRef.current = null; // Clear the end time
- setElapsedTime(0); // Reset the elapsed time display
-
- setIsRecordButtonDisabled(true); // Disable the record button
- };
-
- const checkDataAvailability = async () => {
- const allData = await getAllDataFromIndexedDB();
- setHasData(allData.length > 0);
+ // Reset the recording state
+ isRecordingRef.current = false;
+ setElapsedTime(0);
+ setrecData(false);
+ setIsRecordButtonDisabled(true);
};
// Call this function when your component mounts or when you suspect the data might change
useEffect(() => {
- checkDataAvailability();
- }, []);
+ const checkDataAndConnection = async () => {
+ // Check if data exists in IndexedDB
+ const allData = await getAllDataFromIndexedDB();
+ setHasData(allData.length > 0);
+
+ // Disable the record button if there is data in IndexedDB and device is connected
+ setIsRecordButtonDisabled(allData.length > 0 || !isConnected);
+ };
+
+ checkDataAndConnection();
+ }, [isConnected]);
// Add this function to save data to IndexedDB during recording
const saveDataDuringRecording = async (data: string[][]) => {
if (!isRecordingRef.current || !indexedDBRef.current) return;
-
+
try {
const tx = indexedDBRef.current.transaction(["adcReadings"], "readwrite");
const store = tx.objectStore("adcReadings");
-
- // Dynamically create channel data based on the current canvas count
+
+ console.log(`Saving data for ${canvasCount} channels.`);
+
for (const row of data) {
- const channels = row.slice(0, canvasCount); // Only include channels up to the current canvasCount
+ // Ensure all channels are present by filling missing values with null
+ const channels = row.slice(0, canvasCount).map((value) =>
+ value !== undefined ? Number(value) : null
+ );
+
await store.add({
timestamp: new Date().toISOString(),
- channels: channels.map(Number), // Save channel data as an array of numbers
- counter: Number(row[6]), // If you have a counter column
+ channels, // Save the array of channels
+ counter: Number(row[6]), // Adjust based on counter location
});
}
} catch (error) {
console.error("Error saving data during recording:", error);
}
};
-
+
// Function to format time from seconds into a "MM:SS" string format
const formatTime = (seconds: number): string => {
// Calculate the number of minutes by dividing seconds by 60
@@ -587,7 +602,6 @@ const Connection: React.FC