Skip to content

Commit 3483345

Browse files
committed
[SD] Refactoring ParseFile1
1 parent 26c62ab commit 3483345

File tree

5 files changed

+195
-126
lines changed

5 files changed

+195
-126
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,9 @@ void Wippersnapper_V2::provisionV2() {
104104
_littleFSV2 = new WipperSnapper_LittleFS();
105105
#endif
106106

107-
if (WsV2._sdCardV2->initSDCard()) {
108-
setStatusLEDColor(GREEN);
107+
// Are we using sd-card/offline mode?
108+
if (WsV2._sdCardV2->InitSDCard())
109109
return;
110-
} else {
111-
setStatusLEDColor(RED);
112-
}
113110

114111
#ifdef USE_DISPLAY
115112
// Initialize the display
@@ -1190,10 +1187,6 @@ void Wippersnapper_V2::connectV2() {
11901187
// NOTE: After this, bail out of this function and run the app loop!!!
11911188
if (WsV2._sdCardV2->mode_offline == true) {
11921189
WS_DEBUG_PRINTLN("[Offline] Running device configuration...");
1193-
// Enable logging
1194-
// TODO: Change func. name to ConfigureLogging
1195-
WS_DEBUG_PRINTLN("[Offline] Enabling logging...");
1196-
WsV2._sdCardV2->EnableLogging();
11971190
// Wait for incoming JSON string from Serial
11981191
#ifdef OFFLINE_MODE_DEBUG
11991192
if (!WsV2._sdCardV2->waitForSerialConfig())

src/components/ds18x20/controller.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,23 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
102102
is_initialized = false;
103103
}
104104

105-
// If we're not in offline mode, publish a Ds18x20Added message back to the broker
106-
if (! WsV2._sdCardV2->mode_offline) {
105+
// If we're not in offline mode, publish a Ds18x20Added message back to the
106+
// broker
107+
if (!WsV2._sdCardV2->mode_offline) {
107108
// Encode and publish a Ds18x20Added message back to the broker
108109
if (!_DS18X20_model->EncodeDS18x20Added(
109110
_DS18X20_model->GetDS18x20AddMsg()->onewire_pin, is_initialized)) {
110-
WS_DEBUG_PRINTLN("ERROR | DS18x20: Unable to encode Ds18x20Added message!");
111-
return false;
111+
WS_DEBUG_PRINTLN(
112+
"ERROR | DS18x20: Unable to encode Ds18x20Added message!");
113+
return false;
112114
}
113115

114-
if (!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_ds18x20_added_tag,
115-
_DS18X20_model->GetDS18x20AddedMsg())) {
116-
WS_DEBUG_PRINTLN(
117-
"ERROR | DS18x20: Unable to publish Ds18x20Added message!");
118-
return false;
116+
if (!WsV2.PublishSignal(
117+
wippersnapper_signal_DeviceToBroker_ds18x20_added_tag,
118+
_DS18X20_model->GetDS18x20AddedMsg())) {
119+
WS_DEBUG_PRINTLN(
120+
"ERROR | DS18x20: Unable to publish Ds18x20Added message!");
121+
return false;
119122
}
120123
}
121124

@@ -224,29 +227,30 @@ void DS18X20Controller::update() {
224227
_DS18X20_model->GetDS18x20EventMsg();
225228
pb_size_t event_count = event_msg->sensor_events_count;
226229

227-
if (! WsV2._sdCardV2->mode_offline)
228-
{
229-
// Encode the Ds18x20Event message
230-
if (!_DS18X20_model->EncodeDs18x20Event()) {
231-
WS_DEBUG_PRINTLN(
232-
"ERROR | DS18x20: Failed to encode Ds18x20Event message");
233-
continue;
234-
}
235-
// Publish the Ds18x20Event message to the broker
236-
WS_DEBUG_PRINT("DS18x20: Publishing event to broker...");
237-
if (!WsV2.PublishSignal(
238-
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag,
239-
_DS18X20_model->GetDS18x20EventMsg())) {
240-
WS_DEBUG_PRINTLN(
241-
"ERROR | DS18x20: Failed to publish Ds18x20Event message");
242-
continue;
243-
}
244-
WS_DEBUG_PRINTLN("Published!");
230+
if (!WsV2._sdCardV2->mode_offline) {
231+
// Encode the Ds18x20Event message
232+
if (!_DS18X20_model->EncodeDs18x20Event()) {
233+
WS_DEBUG_PRINTLN(
234+
"ERROR | DS18x20: Failed to encode Ds18x20Event message");
235+
continue;
236+
}
237+
// Publish the Ds18x20Event message to the broker
238+
WS_DEBUG_PRINT("DS18x20: Publishing event to broker...");
239+
if (!WsV2.PublishSignal(
240+
wippersnapper_signal_DeviceToBroker_ds18x20_event_tag,
241+
_DS18X20_model->GetDS18x20EventMsg())) {
242+
WS_DEBUG_PRINTLN(
243+
"ERROR | DS18x20: Failed to publish Ds18x20Event message");
244+
continue;
245+
}
246+
WS_DEBUG_PRINTLN("Published!");
245247
} else {
246-
if (!WsV2._sdCardV2->LogDS18xSensorEventToSD(_DS18X20_model->GetDS18x20EventMsg())) {
247-
WS_DEBUG_PRINTLN("ERROR | DS18x20: Failed to log DS18x20 event to SD card");
248-
continue;
249-
}
248+
if (!WsV2._sdCardV2->LogDS18xSensorEventToSD(
249+
_DS18X20_model->GetDS18x20EventMsg())) {
250+
WS_DEBUG_PRINTLN(
251+
"ERROR | DS18x20: Failed to log DS18x20 event to SD card");
252+
continue;
253+
}
250254
}
251255

252256
#ifdef DEBUG_PROFILE

src/components/ds18x20/model.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class DS18X20Model {
4141
void InitDS18x20EventMsg(const char *ow_pin_name);
4242
void addSensorEvent(wippersnapper_sensor_SensorType sensor_type,
4343
float sensor_value);
44+
4445
private:
4546
wippersnapper_ds18x20_Ds18x20Add
4647
_msg_DS18x20Add; ///< wippersnapper_ds18x20_Ds18x20Add message

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 136 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -38,77 +38,118 @@ ws_sdcard::~ws_sdcard() {
3838
}
3939
}
4040

41-
bool ws_sdcard::initSDCard() {
41+
bool ws_sdcard::InitSDCard() {
4242
#ifdef SD_CS_PIN
4343
// Attempt to initialize the SD card
4444
if (_sd.begin(SD_CS_PIN)) {
4545
mode_offline = true;
4646
}
4747
#endif
48-
return mode_offline;
48+
return mode_offline;
4949
}
5050

5151
/**************************************************************************/
5252
/*!
53-
@brief Enables logging via a physical RTC to a SD-card, if available.
54-
Otherwise, enables logging using millis() timestamps.
53+
@brief Initializes a DS1307 RTC
54+
@returns True if the RTC was successfully initialized, False otherwise.
5555
*/
5656
/**************************************************************************/
57-
void ws_sdcard::EnableLogging() {
58-
// Attempt to search for a DS3231 RTC
59-
WS_DEBUG_PRINTLN("Searching for DS1307 RTC...");
57+
bool ws_sdcard::InitDS1307() {
6058
_rtc_ds1307 = new RTC_DS1307();
61-
if (_rtc_ds1307->begin()) {
62-
WS_DEBUG_PRINTLN("Found DS1307 RTC!");
63-
if (!_rtc_ds1307->isrunning()) {
64-
Serial.println("RTC is not running, let's set the time!");
65-
// When time needs to be set on a new device, or after a power loss, the
66-
// following line sets the RTC to the date & time this sketch was compiled
67-
_rtc_ds1307->adjust(DateTime(F(__DATE__), F(__TIME__)));
68-
}
69-
} else {
70-
WS_DEBUG_PRINT("Unable to find DS1307 RTC, ");
59+
if (!_rtc_ds1307->begin()) {
60+
WS_DEBUG_PRINTLN("[SD] Failed to initialize DS1307 RTC");
7161
delete _rtc_ds1307;
72-
_rtc_ds1307 = nullptr;
73-
// Attempt to search for a DS3231 RTC if DS1307 is not found
74-
WS_DEBUG_PRINTLN("searching for DS3231 RTC...");
75-
_rtc_ds3231 = new RTC_DS3231();
76-
if (_rtc_ds3231->begin()) {
77-
WS_DEBUG_PRINTLN("Found DS3231 RTC!");
78-
if (_rtc_ds3231->lostPower()) {
79-
Serial.println("RTC lost power, let's set the time!");
80-
// When time needs to be set on a new device, or after a power loss, the
81-
// following line sets the RTC to the date & time this sketch was
82-
// compiled
83-
_rtc_ds3231->adjust(DateTime(F(__DATE__), F(__TIME__)));
84-
}
85-
} else {
86-
WS_DEBUG_PRINT("Unable to find DS3231 RTC, ");
87-
delete _rtc_ds3231;
88-
_rtc_ds3231 = nullptr;
89-
// Attempt to search for a DS3231 RTC if DS1307 is not found
90-
WS_DEBUG_PRINTLN("searching for PCF8523 RTC...");
91-
_rtc_pcf8523 = new RTC_PCF8523();
92-
if (_rtc_pcf8523->begin()) {
93-
WS_DEBUG_PRINTLN("Found PCF8523 RTC!");
94-
if (_rtc_pcf8523->lostPower()) {
95-
Serial.println("RTC lost power, let's set the time!");
96-
// When time needs to be set on a new device, or after a power loss,
97-
// the following line sets the RTC to the date & time this sketch was
98-
// compiled
99-
_rtc_pcf8523->adjust(DateTime(F(__DATE__), F(__TIME__)));
100-
}
101-
}
102-
}
62+
return false;
10363
}
64+
if (!_rtc_ds1307->isrunning())
65+
_rtc_ds1307->adjust(DateTime(F(__DATE__), F(__TIME__)));
66+
return true;
67+
}
10468

105-
// Fallback to millis() if no RTC is found
106-
if (_rtc_ds1307 == nullptr && _rtc_ds3231 == nullptr) {
107-
WS_DEBUG_PRINTLN(
108-
"[SD] No RTC found, defaulting to use millis() timestamps!")
109-
} else {
110-
WS_DEBUG_PRINTLN("[SD] RTC found, using RTC timestamps!");
69+
/**************************************************************************/
70+
/*!
71+
@brief Initializes a DS3231 RTC.
72+
@returns True if the RTC was successfully initialized, False
73+
otherwise.
74+
*/
75+
/**************************************************************************/
76+
bool ws_sdcard::InitDS3231() {
77+
_rtc_ds3231 = new RTC_DS3231();
78+
if (!_rtc_ds3231->begin()) {
79+
WS_DEBUG_PRINTLN("[SD] Failed to initialize DS3231 RTC");
80+
delete _rtc_ds3231;
81+
return false;
82+
}
83+
if (_rtc_ds3231->lostPower())
84+
_rtc_ds3231->adjust(DateTime(F(__DATE__), F(__TIME__)));
85+
WS_DEBUG_PRINTLN("[SD] Enabled DS3231 RTC");
86+
return true;
87+
}
88+
89+
/**************************************************************************/
90+
/*!
91+
@brief Initializes a PCF8523 RTC.
92+
@returns True if the RTC was successfully initialized, False
93+
otherwise.
94+
*/
95+
/**************************************************************************/
96+
bool ws_sdcard::InitPCF8523() {
97+
_rtc_pcf8523 = new RTC_PCF8523();
98+
if (!_rtc_pcf8523->begin()) {
99+
WS_DEBUG_PRINTLN("[SD] Failed to initialize PCF8523 RTC");
100+
delete _rtc_pcf8523;
101+
return false;
102+
}
103+
if (_rtc_pcf8523->lostPower())
104+
_rtc_pcf8523->adjust(DateTime(F(__DATE__), F(__TIME__)));
105+
return true;
106+
}
107+
108+
/**************************************************************************/
109+
/*!
110+
@brief Initializes a software RTC.
111+
@returns True if the RTC was successfully initialized, False
112+
otherwise.
113+
*/
114+
/**************************************************************************/
115+
bool ws_sdcard::InitSoftRTC() {
116+
_rtc_soft->begin(DateTime(F(__DATE__), F(__TIME__)));
117+
return true;
118+
}
119+
120+
/**************************************************************************/
121+
/*!
122+
@brief Initializes and configures a RTC for logging.
123+
@param type
124+
The desired type of RTC to configure.
125+
@returns True if the RTC was successfully configured, False otherwise.
126+
*/
127+
/**************************************************************************/
128+
bool ws_sdcard::ConfigureRTC(sdcard_rtc type) {
129+
bool did_init = false;
130+
switch (type) {
131+
case DS1307:
132+
did_init = InitDS1307();
133+
WS_DEBUG_PRINTLN("[SD] Enabled DS1307 RTC");
134+
break;
135+
case DS3231:
136+
did_init = InitDS3231();
137+
WS_DEBUG_PRINTLN("[SD] Enabled DS3231 RTC");
138+
break;
139+
case PCF8523:
140+
did_init = InitPCF8523();
141+
WS_DEBUG_PRINTLN("[SD] Enabled PCF8523 RTC");
142+
break;
143+
case SOFT_RTC:
144+
did_init = InitSoftRTC();
145+
WS_DEBUG_PRINTLN("[SD] Enabled software RTC");
146+
break;
147+
default:
148+
WS_DEBUG_PRINTLN("[SD] Unknown RTC type");
149+
did_init = false;
150+
break;
111151
}
152+
return did_init;
112153
}
113154

114155
/**************************************************************************/
@@ -162,6 +203,7 @@ bool ws_sdcard::parseConfigFile() {
162203
// the MQTT broker while in offline mode, we can still configure
163204
// the hardware by parsing the JSON object's "exportedFromDevice"
164205
// contents and setting up the hardware
206+
165207
WS_DEBUG_PRINT("[SD] Performing check-in process...");
166208
JsonObject exportedFromDevice = doc["exportedFromDevice"];
167209
if (exportedFromDevice.isNull()) {
@@ -177,6 +219,27 @@ bool ws_sdcard::parseConfigFile() {
177219
exportedFromDevice["totalAnalogPins"]);
178220
WS_DEBUG_PRINTLN("OK!");
179221

222+
// Configure RTC based on the RTC type
223+
sdcard_rtc type;
224+
if (strcmp(exportedFromDevice["rtc"], "DS1307")) {
225+
type = DS1307;
226+
} else if (strcmp(exportedFromDevice["rtc"], "DS3231")) {
227+
type = DS3231;
228+
} else if (strcmp(exportedFromDevice["rtc"], "PCF8523")) {
229+
type = PCF8523;
230+
} else if (strcmp(exportedFromDevice["rtc"], "SOFT_RTC")) {
231+
type = SOFT_RTC;
232+
} else {
233+
WS_DEBUG_PRINTLN(
234+
"[SD] FATAL Parsing error - Unknown RTC type found in JSON string!");
235+
type = UNKNOWN;
236+
}
237+
238+
if (!ConfigureRTC(type)) {
239+
WS_DEBUG_PRINTLN("[SD] Failed to to configure RTC!");
240+
return false;
241+
}
242+
180243
// Parse the "components" array into a JsonObject
181244
JsonArray components_ar = doc["components"].as<JsonArray>();
182245
int count = components_ar.size();
@@ -240,7 +303,7 @@ bool ws_sdcard::parseConfigFile() {
240303
String(component["direction"]));
241304
return false;
242305
}
243-
306+
244307
msg_signal_b2d = wippersnapper_signal_BrokerToDevice_init_zero;
245308
msg_signal_b2d.which_payload =
246309
wippersnapper_signal_BrokerToDevice_digitalio_add_tag;
@@ -491,29 +554,28 @@ bool ws_sdcard::waitForSerialConfig() {
491554
"}\\n\r\n";
492555

493556
_serialInput = ""; // Clear the serial input buffer
494-
if (!_use_test_data) {
557+
if (!_use_test_data) {
495558
WS_DEBUG_PRINTLN("[SD] Waiting for incoming JSON string...");
496559
while (true) {
497-
// Check if there is data available to read
498-
if (Serial.available() > 0) {
499-
// Read and append to _serialInput
500-
char c = Serial.read();
501-
_serialInput += c;
502-
503-
// DEBUG - Check JSON output as an Int and total output
504-
// WS_DEBUG_PRINT("[SD] Character read: ");
505-
// WS_DEBUG_PRINTLN((int)c);
506-
// WS_DEBUG_PRINTLN(_serialInput);
507-
508-
// Check for end of JSON string using \n sequence
509-
if (_serialInput.endsWith("\\n")) {
510-
WS_DEBUG_PRINTLN("[SD] End of JSON string detected!");
511-
break;
512-
}
560+
// Check if there is data available to read
561+
if (Serial.available() > 0) {
562+
// Read and append to _serialInput
563+
char c = Serial.read();
564+
_serialInput += c;
565+
566+
// DEBUG - Check JSON output as an Int and total output
567+
// WS_DEBUG_PRINT("[SD] Character read: ");
568+
// WS_DEBUG_PRINTLN((int)c);
569+
// WS_DEBUG_PRINTLN(_serialInput);
570+
571+
// Check for end of JSON string using \n sequence
572+
if (_serialInput.endsWith("\\n")) {
573+
WS_DEBUG_PRINTLN("[SD] End of JSON string detected!");
574+
break;
513575
}
576+
}
514577
}
515-
}
516-
578+
}
517579

518580
// Strip the '\n' off the end of _serialInput
519581
_serialInput.trim();

0 commit comments

Comments
 (0)