Skip to content

Commit e795301

Browse files
committed
[SD] Refactor out LogSensorEventTOSD funcs
1 parent 4d9bed8 commit e795301

File tree

2 files changed

+48
-81
lines changed

2 files changed

+48
-81
lines changed

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -646,30 +646,56 @@ const char *SensorTypeToString(wippersnapper_sensor_SensorType sensorType) {
646646
}
647647
}
648648

649-
void ws_sdcard::BuildJSONDoc(JsonDocument &doc, const char *pin, float value,
649+
void ws_sdcard::BuildJSONDoc(JsonDocument &doc, uint8_t pin, float value,
650650
wippersnapper_sensor_SensorType read_type) {
651+
char pin_name[12];
652+
sprintf(pin_name, "A%d", pin);
651653
doc["timestamp"] = GetTimestamp();
652-
doc["pin"] = pin;
654+
doc["pin"] = pin_name;
653655
doc["value"] = value;
654656
doc["si_unit"] = SensorTypeToString(read_type);
655657
}
656658

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,
658660
wippersnapper_sensor_SensorType read_type) {
661+
char pin_name[12];
662+
sprintf(pin_name, "A%d", pin);
659663
doc["timestamp"] = GetTimestamp();
660-
doc["pin"] = pin;
664+
doc["pin"] = pin_name;
661665
doc["value"] = value;
662666
doc["si_unit"] = SensorTypeToString(read_type);
663667
}
664668

665-
void ws_sdcard::BuildJSONDoc(JsonDocument &doc, const char *pin, bool value,
669+
void ws_sdcard::BuildJSONDoc(JsonDocument &doc, uint8_t pin, bool value,
666670
wippersnapper_sensor_SensorType read_type) {
671+
char pin_name[12];
672+
sprintf(pin_name, "D%d", pin);
667673
doc["timestamp"] = GetTimestamp();
668-
doc["pin"] = pin;
674+
doc["pin"] = pin_name;
669675
doc["value"] = value;
670676
doc["si_unit"] = SensorTypeToString(read_type);
671677
}
672678

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+
673699
/**************************************************************************/
674700
/*!
675701
@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,
684710
/**************************************************************************/
685711
bool ws_sdcard::LogGPIOSensorEventToSD(
686712
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
693713
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;
711717
return true;
712718
}
713719

@@ -725,29 +731,10 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
725731
/**************************************************************************/
726732
bool ws_sdcard::LogGPIOSensorEventToSD(
727733
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
733734
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;
751738
return true;
752739
}
753740

@@ -765,32 +752,10 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
765752
/**************************************************************************/
766753
bool ws_sdcard::LogGPIOSensorEventToSD(
767754
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
773755
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;
794759
return true;
795760
}
796761

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "StreamUtils.h"
2020
#include "Wippersnapper_V2.h"
2121

22-
#define SD_FAT_TYPE 3
22+
#define SD_FAT_TYPE 3 ///< SdFat type (3 = SdFs)
2323
#define MAX_LOG_FILE_SZ 500 ///< Maximum log file size of 500 bytes
2424

2525
// forward decl.
@@ -74,20 +74,22 @@ class ws_sdcard {
7474
int num_sensors, const char *sensor_type_1,
7575
const char *sensor_type_2);
7676

77-
void BuildJSONDoc(JsonDocument &doc, const char *pin, float value,
77+
void BuildJSONDoc(JsonDocument &doc, uint8_t pin, float value,
7878
wippersnapper_sensor_SensorType read_type);
79-
void BuildJSONDoc(JsonDocument &doc, const char *pin, uint16_t value,
79+
void BuildJSONDoc(JsonDocument &doc, uint8_t pin, uint16_t value,
8080
wippersnapper_sensor_SensorType read_type);
81-
void BuildJSONDoc(JsonDocument &doc, const char *pin, bool value,
81+
void BuildJSONDoc(JsonDocument &doc, uint8_t pin, bool value,
8282
wippersnapper_sensor_SensorType read_type);
83+
bool LogJSONDoc(JsonDocument &doc);
84+
8385
bool
8486
PushSignalToSharedBuffer(wippersnapper_signal_BrokerToDevice &msg_signal);
8587
SdFat _sd; ///< SD object from Adafruit SDFat library
8688
bool is_mode_offline; ///< True if offline mode is enabled, False otherwise
8789
String _serialInput; ///< Serial input buffer
8890
const char *json_test_data; ///< Json test data
8991
const char *_log_filename; ///< Path to the log file
90-
int _sz_log_file; ///< Size of the current log file, in Bytes
92+
size_t _sz_log_file; ///< Size of the current log file, in Bytes
9193
RTC_DS3231 *_rtc_ds3231 = nullptr; ///< DS3231 RTC object
9294
RTC_DS1307 *_rtc_ds1307 = nullptr; ///< DS1307 RTC object
9395
RTC_PCF8523 *_rtc_pcf8523 = nullptr; ///< PCF8523 RTC object

0 commit comments

Comments
 (0)