@@ -646,30 +646,56 @@ const char *SensorTypeToString(wippersnapper_sensor_SensorType sensorType) {
646
646
}
647
647
}
648
648
649
- void ws_sdcard::BuildJSONDoc (JsonDocument &doc, const char * pin, float value,
649
+ void ws_sdcard::BuildJSONDoc (JsonDocument &doc, uint8_t pin, float value,
650
650
wippersnapper_sensor_SensorType read_type) {
651
+ char pin_name[12 ];
652
+ sprintf (pin_name, " A%d" , pin);
651
653
doc[" timestamp" ] = GetTimestamp ();
652
- doc[" pin" ] = pin ;
654
+ doc[" pin" ] = pin_name ;
653
655
doc[" value" ] = value;
654
656
doc[" si_unit" ] = SensorTypeToString (read_type);
655
657
}
656
658
657
- void ws_sdcard::BuildJSONDoc (JsonDocument &doc, const char * pin, uint16_t value,
659
+ void ws_sdcard::BuildJSONDoc (JsonDocument &doc, uint8_t pin, uint16_t value,
658
660
wippersnapper_sensor_SensorType read_type) {
661
+ char pin_name[12 ];
662
+ sprintf (pin_name, " A%d" , pin);
659
663
doc[" timestamp" ] = GetTimestamp ();
660
- doc[" pin" ] = pin ;
664
+ doc[" pin" ] = pin_name ;
661
665
doc[" value" ] = value;
662
666
doc[" si_unit" ] = SensorTypeToString (read_type);
663
667
}
664
668
665
- void ws_sdcard::BuildJSONDoc (JsonDocument &doc, const char * pin, bool value,
669
+ void ws_sdcard::BuildJSONDoc (JsonDocument &doc, uint8_t pin, bool value,
666
670
wippersnapper_sensor_SensorType read_type) {
671
+ char pin_name[12 ];
672
+ sprintf (pin_name, " D%d" , pin);
667
673
doc[" timestamp" ] = GetTimestamp ();
668
- doc[" pin" ] = pin ;
674
+ doc[" pin" ] = pin_name ;
669
675
doc[" value" ] = value;
670
676
doc[" si_unit" ] = SensorTypeToString (read_type);
671
677
}
672
678
679
+ bool ws_sdcard::LogJSONDoc (JsonDocument &doc) {
680
+ File32 file;
681
+ size_t szJson;
682
+ // Serialize the JSON document
683
+ #ifndef OFFLINE_MODE_DEBUG
684
+ if (!_sd.open (" log.json" , FILE_WRITE))
685
+ return false ;
686
+ BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
687
+ szJson = serializeJson (
688
+ doc, file); // Serialize the JSON to the file in 64-byte chunks
689
+ bufferedFile.print (" \n " ); // JSONL format specifier
690
+ bufferedFile.flush (); // Send the remaining bytes
691
+ #else
692
+ szJson = serializeJson (doc, Serial); // TODO: Add buffering here, too?
693
+ Serial.print (" \n " ); // JSONL format specifier
694
+ #endif
695
+ _sz_log_file = szJson + 2 ; // +2 bytes for "\n"
696
+ return true ;
697
+ }
698
+
673
699
/* *************************************************************************/
674
700
/* !
675
701
@brief Logs a GPIO sensor event to the SD card.
@@ -684,30 +710,10 @@ void ws_sdcard::BuildJSONDoc(JsonDocument &doc, const char *pin, bool value,
684
710
/* *************************************************************************/
685
711
bool ws_sdcard::LogGPIOSensorEventToSD (
686
712
uint8_t pin, float value, wippersnapper_sensor_SensorType read_type) {
687
- // Get the pin name in the correct format ("A0", "A1", etc.)
688
- // TODO: Maybe send c_pin_name to sprintf and include A/D specifier from here?
689
- char c_pin_name[12 ];
690
- sprintf (c_pin_name, " A%d" , pin);
691
-
692
- // Create the JSON document
693
713
JsonDocument doc;
694
- BuildJSONDoc (doc, c_pin_name, value, read_type);
695
-
696
- // Serialize the JSON document
697
- #ifndef OFFLINE_MODE_DEBUG
698
- // TODO: Make this an attempt to open and return false on failure
699
- File32 file = _sd.open (" log.json" , FILE_WRITE);
700
- BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
701
- serializeJson (doc, file); // Serialize the JSON to the file in 64-byte chunks
702
- // TODO: I am not sure if this works, consult PDF ch 4.7
703
- bufferedFile.print (" \n " );
704
- bufferedFile.flush (); // Send the remaining bytes
705
- #else
706
- serializeJson (doc, Serial); // TODO: Add buffering here, too?
707
- Serial.print (" \n " ); // JSON requires a newline at the end of each log line
708
- #endif
709
-
710
- // TODO: Does this need to be a boolean?
714
+ BuildJSONDoc (doc, pin, value, read_type);
715
+ if (!LogJSONDoc (doc))
716
+ return false ;
711
717
return true ;
712
718
}
713
719
@@ -725,29 +731,10 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
725
731
/* *************************************************************************/
726
732
bool ws_sdcard::LogGPIOSensorEventToSD (
727
733
uint8_t pin, uint16_t value, wippersnapper_sensor_SensorType read_type) {
728
- // Get the pin name in the correct format ("A0", "A1", etc.)
729
- char c_pin_name[12 ];
730
- sprintf (c_pin_name, " A%d" , pin);
731
-
732
- // Create the JSON document
733
734
JsonDocument doc;
734
- BuildJSONDoc (doc, c_pin_name, value, read_type);
735
-
736
- // Serialize the JSON document
737
- #ifndef OFFLINE_MODE_DEBUG
738
- // TODO: Make this an attempt to open and return false on failure
739
- File32 file = _sd.open (" log.json" , FILE_WRITE);
740
- BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
741
- serializeJson (doc, file); // Serialize the JSON to the file in 64-byte chunks
742
- // TODO: I am not sure if this works, consult PDF ch 4.7
743
- bufferedFile.print (" \n " );
744
- bufferedFile.flush (); // Send the remaining bytes
745
- #else
746
- serializeJson (doc, Serial); // TODO: Add buffering here, too?
747
- Serial.print (" \n " ); // JSON requires a newline at the end of each log line
748
- #endif
749
-
750
- // TODO: Does this need to be a boolean?
735
+ BuildJSONDoc (doc, pin, value, read_type);
736
+ if (!LogJSONDoc (doc))
737
+ return false ;
751
738
return true ;
752
739
}
753
740
@@ -765,32 +752,10 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
765
752
/* *************************************************************************/
766
753
bool ws_sdcard::LogGPIOSensorEventToSD (
767
754
uint8_t pin, bool value, wippersnapper_sensor_SensorType read_type) {
768
- // Get the pin name in the correct format ("A0", "A1", etc.)
769
- char c_pin_name[12 ];
770
- sprintf (c_pin_name, " D%d" , pin);
771
-
772
- // Create the JSON document
773
755
JsonDocument doc;
774
- BuildJSONDoc (doc, c_pin_name, value, read_type);
775
-
776
- // Serialize the JSON document
777
- #ifndef OFFLINE_MODE_DEBUG
778
- // TODO: Make this an attempt to open and return false on failure
779
- File32 file;
780
- if (!_sd.open (" log.json" , FILE_WRITE)) {
781
- WS_DEBUG_PRINTLN (" [SD] FATAL Error - Unable to open JSON log file!" );
782
- }
783
- BufferingPrint bufferedFile (file, 64 ); // Add buffering to the file
784
- serializeJson (doc, file); // Serialize the JSON to the file in 64-byte chunks
785
- // TODO: I am not sure if this works, consult PDF ch 4.7
786
- bufferedFile.print (" \n " );
787
- bufferedFile.flush (); // Send the remaining bytes
788
- #else
789
- serializeJson (doc, Serial); // TODO: Add buffering here, too?
790
- Serial.print (" \n " ); // JSON requires a newline at the end of each log line
791
- #endif
792
-
793
- // TODO: Does this need to be a boolean?
756
+ BuildJSONDoc (doc, pin, value, read_type);
757
+ if (!LogJSONDoc (doc))
758
+ return false ;
794
759
return true ;
795
760
}
796
761
0 commit comments