stage gitlab1

This commit is contained in:
Laurent Deleers
2020-04-10 10:40:11 +02:00
parent cdce5cbf3c
commit 7b6dfd11ec
3 changed files with 170 additions and 0 deletions

101
covid19.cpp Normal file
View File

@@ -0,0 +1,101 @@
#include "covid19.h"
Covid19::Covid19() {
lastWorld = 0;
}
String Covid19::getDataFor(String myuri) {
String payload;
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
HTTPClient https;
https.setTimeout(5000);
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, "https://corona.lmao.ninja/"+myuri)) {
int httpCode = https.GET();
if (httpCode > 0) {
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
payload = https.getString();
}
} else {
payload = "{'error " + (String)https.errorToString(httpCode).c_str() + "'}";
//Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
payload = "{'error'}";
}
return payload;
}
void Covid19::displayCountry(String country, Adafruit_SSD1306 &OLED) {
String json = getDataFor("countries/"+country);
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
int cases = doc["cases"];
int todayCases = doc["todayCases"];
int deaths = doc["deaths"];
int todayDeaths = doc["todayDeaths"];
int recovered = doc["recovered"];
Serial.print("|=============================\n");
Serial.print("| STATS FOR : " + country + "\n");
Serial.print("|=============================\n");
Serial.print("| Cases : " + String(cases) + " \n");
Serial.print("| todayCases : " + String(todayCases) + " \n");
Serial.print("| deaths : " + String(deaths) + " \n");
Serial.print("| todayDeaths : " + String(todayDeaths) + " \n");
Serial.print("| recovered : " + String(recovered) + " \n");
OLED.clearDisplay();
OLED.setTextWrap(false);
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(0,0);
OLED.println(" === " + country + " === " );
OLED.println("Cases : " + String(cases) + " (" + todayCases + ")" );
OLED.println("Death : " + String(deaths) );
OLED.println("Recov : " + String(recovered) );
OLED.display();
}
void Covid19::displayWorld(Adafruit_SSD1306 &OLED) {
String json = getDataFor("all");
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
int cases = doc["cases"];
int deaths = doc["deaths"];
int recovered = doc["recovered"];
if(lastWorld==0) lastWorld = cases;
int delta = cases - lastWorld;
Serial.print("|=============================\n");
Serial.print("| STATS FOR : WORLD \n");
Serial.print("|=============================\n");
Serial.print("| Cases : " + String(cases) + " ("+delta+")\n");
Serial.print("| deaths : " + String(deaths) + " \n");
Serial.print("| recovered : " + String(recovered) + " \n");
OLED.clearDisplay();
OLED.setTextWrap(false);
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(0,0);
OLED.println(" === WORLDWIDE === " );
OLED.println("Cases : " + String(cases) + "("+delta+")" );
OLED.println("Death : " + String(deaths) );
OLED.println("Recov : " + String(recovered) );
OLED.display();
lastWorld = cases;
}