@@ -120,38 +120,42 @@ bool ws_sdcard::InitSoftRTC() {
120120/* *************************************************************************/
121121/* !
122122 @brief Initializes and configures a RTC for logging.
123- @param type
123+ @param rtc_type
124124 The desired type of RTC to configure.
125125 @returns True if the RTC was successfully configured, False otherwise.
126126*/
127127/* *************************************************************************/
128- bool ws_sdcard::ConfigureRTC (sdcard_rtc type ) {
128+ bool ws_sdcard::ConfigureRTC (const char *rtc_type ) {
129129 bool did_init = false ;
130- switch (type) {
131- case DS1307:
130+ // Initialize the RTC based on the rtc_type
131+ if ( strcmp (rtc_type, " DS1307" )) {
132132 did_init = InitDS1307 ();
133133 WS_DEBUG_PRINTLN (" [SD] Enabled DS1307 RTC" );
134- break ;
135- case DS3231:
134+ } else if (strcmp (rtc_type, " DS3231" )) {
136135 did_init = InitDS3231 ();
137136 WS_DEBUG_PRINTLN (" [SD] Enabled DS3231 RTC" );
138- break ;
139- case PCF8523:
137+ } else if (strcmp (rtc_type, " PCF8523" )) {
140138 did_init = InitPCF8523 ();
141139 WS_DEBUG_PRINTLN (" [SD] Enabled PCF8523 RTC" );
142- break ;
143- case SOFT_RTC:
140+ } else if (strcmp (rtc_type, " SOFT_RTC" )) {
144141 did_init = InitSoftRTC ();
145142 WS_DEBUG_PRINTLN (" [SD] Enabled software RTC" );
146- break ;
147- default :
148- WS_DEBUG_PRINTLN ( " [SD] Unknown RTC type" );
143+ } else {
144+ WS_DEBUG_PRINTLN (
145+ " [SD] FATAL Parsing error - Unknown RTC type found in JSON string! " );
149146 did_init = false ;
150- break ;
151147 }
148+
152149 return did_init;
153150}
154151
152+ void ws_sdcard::CheckIn (uint8_t max_digital_pins, uint8_t max_analog_pins,
153+ float ref_voltage) {
154+ WsV2.digital_io_controller ->SetMaxDigitalPins (max_digital_pins);
155+ WsV2.analogio_controller ->SetTotalAnalogPins (max_analog_pins);
156+ WsV2.analogio_controller ->SetRefVoltage (ref_voltage);
157+ }
158+
155159/* *************************************************************************/
156160/* !
157161 @brief Searches for and parses the JSON configuration file and sets up
@@ -161,81 +165,55 @@ bool ws_sdcard::ConfigureRTC(sdcard_rtc type) {
161165*/
162166/* *************************************************************************/
163167bool ws_sdcard::parseConfigFile () {
164- File32 file_config; // TODO: MAke this global?
165- #ifndef OFFLINE_MODE_DEBUG
166- file_config = _sd.open (" config.json" , FILE_READ);
167- #endif
168-
168+ File32 file_config;
169169 JsonDocument doc;
170- // TODO: Change max input length to fit an expected/max json size
171- int max_input_len = 2048 ;
170+ int max_json_len =
171+ 2048 ; // TODO: It's possible this is too small - what's the max size?
172172
173- // Attempt to de-serialize the JSON document
173+ // Attempt to open and deserialize the JSON config file
174174 DeserializationError error;
175- #ifdef OFFLINE_MODE_DEBUG
176- if (!_use_test_data) {
177- // Read the config file from the serial input buffer
178- WS_DEBUG_PRINTLN (" [SD] Reading JSON config file..." );
179- error = deserializeJson (doc, _serialInput.c_str (), max_input_len);
180- } else {
181- // Read the config file from the test JSON string
182- WS_DEBUG_PRINTLN (" [SD] Reading test JSON data..." );
183- error = deserializeJson (doc, json_test_data, max_input_len);
184- }
185- #else
175+ #ifndef OFFLINE_MODE_DEBUG
186176 // Read the config file from the SD card
187- WS_DEBUG_PRINTLN (" [SD] Reading config file..." );
188- // TODO - implement this
189- // error = deserializeJson(doc, file_config, max_input_len);
177+ file_config = _sd.open (" config.json" , FILE_READ);
178+ error = deserializeJson (doc, file_config, max_json_len);
179+ #else
180+ // Test Mode - do not use the SD card, use test data instead!
181+ if (_use_test_data)
182+ error = deserializeJson (doc, _serialInput.c_str (), max_json_len);
183+ else
184+ error = deserializeJson (doc, json_test_data, max_json_len);
190185#endif
191186
192187 // If the JSON document failed to deserialize - halt the running device and
193188 // print the error because it is not possible to continue running in offline
194189 // mode without a valid config file
195190 if (error) {
196- WS_DEBUG_PRINTLN (" [SD] deserializeJson() failed , error code: " +
191+ WS_DEBUG_PRINTLN (" [SD] Unable to deserialize config JSON , error code: " +
197192 String (error.c_str ()));
198193 return false ;
199194 }
200195
201- // TODO: Let's refactor this outwards to a function called `CheckInJSON()`
202- // NOTE: While we can't do a "proper" check-in procedure with
203- // the MQTT broker while in offline mode, we can still configure
204- // the hardware by parsing the JSON object's "exportedFromDevice"
205- // contents and setting up the hardware
206-
207- WS_DEBUG_PRINT (" [SD] Performing check-in process..." );
196+ // Parse the exportedFromDevice array
208197 JsonObject exportedFromDevice = doc[" exportedFromDevice" ];
209198 if (exportedFromDevice.isNull ()) {
210199 WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - No exportedFromDevice object "
211200 " found in JSON string! Unable to configure hardware!" );
212201 return false ;
213202 }
214- WsV2.digital_io_controller ->SetMaxDigitalPins (
215- exportedFromDevice[" totalGPIOPins" ]);
216- WsV2.analogio_controller ->SetRefVoltage (
217- exportedFromDevice[" referenceVoltage" ]);
218- WsV2.analogio_controller ->SetTotalAnalogPins (
219- exportedFromDevice[" totalAnalogPins" ]);
220- WS_DEBUG_PRINTLN (" OK!" );
221-
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- }
237203
238- if (!ConfigureRTC (type)) {
204+ // Mock the check-in process using the JSON's values
205+ CheckIn (exportedFromDevice[" totalGPIOPins" ],
206+ exportedFromDevice[" totalAnalogPins" ],
207+ exportedFromDevice[" referenceVoltage" ]);
208+
209+ // Initialize and configure RTC
210+ const char *json_rtc = exportedFromDevice[" rtc" ];
211+ if (json_rtc == nullptr ) {
212+ WS_DEBUG_PRINTLN (" [SD] FATAL Parsing error - rtc field not found in "
213+ " configuration JSON, unable to configure RTC!" );
214+ return false ;
215+ }
216+ if (!ConfigureRTC (json_rtc)) {
239217 WS_DEBUG_PRINTLN (" [SD] Failed to to configure RTC!" );
240218 return false ;
241219 }
0 commit comments