Skip to content

Commit

Permalink
Cleanup error-logging
Browse files Browse the repository at this point in the history
Signed-off-by: simonmicro <[email protected]>
  • Loading branch information
simonmicro committed Apr 6, 2024
1 parent 8d7a3ce commit 9a962f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
35 changes: 17 additions & 18 deletions src/apps/_experiments/OswAppWeather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ String WeatherParser::encodeWeather(DynamicJsonDocument& doc) {
update.weather = this->_getWCond(doc["list"][i]["weather"][0]["id"]);
res = encoder.setUpdate(update);
if (!res) {
OSW_LOG_W("ERROR_INPUT" );
return "ERROR_INPUT" ;
OSW_LOG_E("Failed to pass update to encoder!");
return "ERROR_INPUT";
}
}
return encoder.getEncoded();
Expand Down Expand Up @@ -305,12 +305,12 @@ bool OswAppWeather::_request() {
strcpy(encoded_arr, encoded.c_str());
String encoded_S = String(encoded_arr);
if (!this->pref.putString("wtr",encoded_S)) {
OSW_LOG_E("Error: unable to write to NVS");
OSW_LOG_E("Unable to write to NVS?!");
this->dataLoaded = false;
return false;
}
} else {
OSW_LOG_E("Error: API response: ", code);
OSW_LOG_E("Unexpected API response: ", code);
this->dataLoaded = false;
return false;
}
Expand All @@ -331,7 +331,7 @@ void OswAppWeather::weatherRequest() {
this->requestMode = true;
}
bool OswAppWeather::_request() {

// in emulator instantly "fullfill" the request
this->requestMode=false;
this->dataLoaded=true;
return true;
Expand Down Expand Up @@ -412,23 +412,22 @@ bool OswAppWeather::loadData() {
std::ifstream inFile;
inFile.open("file_weather.json"); //open the input file
if(!inFile.is_open()) {
OSW_LOG_E("Emulator Error: Unable to open 'file_weather.json' in the './build' directory");
OSW_LOG_E("Unable to open 'file_weather.json' in the current working directory");
// error handling will be done below as the string is empty
}
std::stringstream strStream;
strStream << inFile.rdbuf();
std::string strW = strStream.str();
OSW_LOG_D("json file raw:");
OSW_LOG_D(strW);
DynamicJsonDocument doc(16432*2);// when in emulator more space is needed
OSW_LOG_I("json file:");
OSW_LOG_I(strW);
deserializeJson(doc,strW);
WeatherParser pars;
String encoded = pars.encodeWeather(doc);
OSW_LOG_I("encoded");
OSW_LOG_I(encoded);
int encoded_len = encoded.length();
char encoded_arr[encoded_len + 1];
strcpy(encoded_arr, encoded.c_str());
String wstr = String(encoded_arr);
OSW_LOG_D("as encoded:");
OSW_LOG_D(encoded);
String wstr;
wstr += encoded; // this copies the value of "encoded" to "wstr" (just assignment does not take ownership of the value itself)
#else
String wstr = this->pref.getString("wtr");
#endif
Expand All @@ -445,11 +444,11 @@ bool OswAppWeather::loadData() {
WeatherDecoder decoder(wstr.c_str());
this->initTimestamp = decoder.getTime();
this->getDayList();
if(strftime(this->initTimeDMY, sizeof(this->initTimeDMY), "%d/%m/%Y", localtime(&this->initTimestamp))) {
//OSW_LOG_I(this->inittimeDMY);
if(!strftime(this->initTimeDMY, sizeof(this->initTimeDMY), "%d/%m/%Y", localtime(&this->initTimestamp))) {
OSW_LOG_W("Failed to parse timestamp: ", this->initTimeDMY);
}
if(strftime(this->initTimeMD, sizeof(this->initTimeMD), "%d/%m", localtime(&this->initTimestamp))) {
//OSW_LOG_I(this->initTimeMD);
if(!strftime(this->initTimeMD, sizeof(this->initTimeMD), "%d/%m", localtime(&this->initTimestamp))) {
OSW_LOG_W("Failed to parse timestamp: ", this->initTimeMD);
}
tmInit = localtime(&this->initTimestamp);
forecast = decoder.getUpdates();
Expand Down
14 changes: 5 additions & 9 deletions src/apps/_experiments/OswAppWeatherEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@ OswAppWeatherEncoder::OswAppWeatherEncoder() {}
bool OswAppWeatherEncoder::setUpdate(OswAppWeather::weather_update_t update) {
bool update_ok = true;
if(update.temp > 99 || update.temp < -99 ) {
OSW_LOG_W("ERROR TEMP");
OSW_LOG_W(update.temp);
update_ok = false;
OSW_LOG_W("Invalid TEMPERATURE: ", update.temp);
update_ok = false;
}
if(update.humidity > 100 || update.humidity < 0) {
OSW_LOG_W("ERROR HUMIDITY");
OSW_LOG_W(update.humidity);
OSW_LOG_W("Invalid HUMIDITY: ", update.humidity);
update_ok = false;
}
if(update.pressure < 0 || update.pressure > 2000 ) {
OSW_LOG_W("ERROR PRESSURE");
OSW_LOG_W(update.pressure);
OSW_LOG_W("Invalid PRESSURE: ", update.pressure);
update_ok = false;
}
if(update.weather < 0 || update.weather > 15) {
OSW_LOG_W(update.weather);
OSW_LOG_W("ERROR WEATHER");
OSW_LOG_W("Invalid WEATHER: ", update.weather);
update_ok = false;
}
if(!update_ok) {
Expand Down

0 comments on commit 9a962f3

Please sign in to comment.