Skip to content

Commit

Permalink
Merge pull request #37 from yakumo-saki/20_alerting
Browse files Browse the repository at this point in the history
20 alerting
  • Loading branch information
yakumo-saki authored Jan 29, 2021
2 parents 700a438 + 55fe575 commit 439d5c5
Show file tree
Hide file tree
Showing 47 changed files with 690 additions and 274 deletions.
39 changes: 0 additions & 39 deletions include/README

This file was deleted.

1 change: 1 addition & 0 deletions include/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ void disp_wifi_info(String ip, String mDNS);
void disp_normal_startup_screen(String product_long);
void disp_wait_for_reconfig();
void disp_wifi_starting(int wait_print_row);
void disp_wifi_error();
void disp_all_initialize_complete(String ip, String mdns);
void disp_sensor_value(String ip, String mdns);
void disp_power_off();
Expand Down
3 changes: 2 additions & 1 deletion include/display_ssd1306.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ void disp_ssd1306_setup_startup_screen(String ipAddr);
void disp_ssd1306_wait_for_reconfig_init();
void disp_ssd1306_wait_for_reconfig_bar(int now, const int max);
void disp_ssd1306_wifi_starting(int wait_print_row);
void disp_ssd1306_wifi_error();
void disp_ssd1306_all_initialize_complete();
void disp_ssd1306_sensor_value(disp_values_t val);
void disp_ssd1306_sensor_value(disp_values_t values, value_alerts_t alerts);
void disp_ssd1306_power_off();

void disp_ssd1306_wifi_error();
Expand Down
2 changes: 1 addition & 1 deletion include/display_st7789.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void disp_st7789_wait_for_reconfig_init();
void disp_st7789_wait_for_reconfig_bar(int now, const int max);
void disp_st7789_wifi_starting(int wait_print_row);
void disp_st7789_all_initialize_complete(String ip, String mdns);
void disp_st7789_sensor_value(disp_values_t new_values, disp_values_t last_values);
void disp_st7789_sensor_value(disp_values_t values, value_alerts_t alerts);
void disp_st7789_power_off();

void disp_st7789_wifi_error();
Expand Down
2 changes: 1 addition & 1 deletion include/display_st7789_big.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

void _clear_screen_big();
void _disp_header_big(String ip, String mDNS);
void _disp_sensor_value_big(disp_values_t val);
void _disp_sensor_value_big(disp_values_t values, value_alerts_t alerts);
2 changes: 1 addition & 1 deletion include/display_st7789_normal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

void _clear_screen_normal();
void _disp_header_normal(String ip, String mDNS);
void _disp_sensor_value_normal(disp_values_t val);
void _disp_sensor_value_normal(disp_values_t values, value_alerts_t alerts);
4 changes: 4 additions & 0 deletions include/display_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "structs.h"

bool has_caution(value_alerts_t alerts);
bool has_warning(value_alerts_t alerts);
3 changes: 3 additions & 0 deletions include/scan_alert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "structs.h"

value_alerts_t check_for_alerts();
32 changes: 31 additions & 1 deletion include/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ typedef struct {
String co2ppm;
sensor_values_t rawValues;
} disp_values_t;

typedef struct {
bool warning = false;
bool caution = false;
} value_alert_t;

typedef struct {
value_alert_t temperature;
value_alert_t humidity;
value_alert_t pressure;
value_alert_t lux;
value_alert_t co2;
} value_alerts_t;

typedef struct {
String low;
String high;
} config_alert_range_t;

typedef struct {
config_alert_range_t caution1;
config_alert_range_t caution2;
config_alert_range_t warning1;
config_alert_range_t warning2;
} config_alert_t;

typedef struct {
String settingId;
String ssid;
Expand All @@ -40,8 +66,12 @@ typedef struct {
String st7789Mode;
String mqttBroker;
String mqttName;
config_alert_t temperatureAlerts;
config_alert_t humidityAlerts;
config_alert_t pressureAlerts;
config_alert_t luxAlerts;
config_alert_t co2Alerts;
} config_t;



#endif
163 changes: 158 additions & 5 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "log.h"
#include "global.h"

#define JSON_BUF 2048
#define CONF_JSON_SIZE 1400

/**
* とりあえずのデフォルト値をグローバル変数にセットする
Expand Down Expand Up @@ -35,14 +35,69 @@ void set_default_config_value()

config.mqttBroker = "";
config.mqttName = "";

config.temperatureAlerts.warning1.low = "-99";
config.temperatureAlerts.warning1.high = "10";
config.temperatureAlerts.caution1.low = "10";
config.temperatureAlerts.caution1.high = "15";
config.temperatureAlerts.caution2.low = "250";
config.temperatureAlerts.caution2.high = "30";
config.temperatureAlerts.warning2.low = "30";
config.temperatureAlerts.warning2.high = "99";

config.humidityAlerts.warning1.low = "0";
config.humidityAlerts.warning1.high = "20";
config.humidityAlerts.caution1.low = "20";
config.humidityAlerts.caution1.high = "35";
config.humidityAlerts.caution2.low = "65";
config.humidityAlerts.caution2.high = "75";
config.humidityAlerts.warning2.low = "75";
config.humidityAlerts.warning2.high = "100";

config.pressureAlerts.warning1.low = "0";
config.pressureAlerts.warning1.high = "995";
config.pressureAlerts.caution1.low = "995";
config.pressureAlerts.caution1.high = "1000";
config.pressureAlerts.caution2.low = "3000";
config.pressureAlerts.caution2.high = "3000";
config.pressureAlerts.warning2.low = "1025";
config.pressureAlerts.warning2.high = "3000";

config.luxAlerts.warning1.low = "0";
config.luxAlerts.warning1.high = "1";
config.luxAlerts.caution1.low = "-1";
config.luxAlerts.caution1.high = "-1";
config.luxAlerts.caution2.low = "-1";
config.luxAlerts.caution2.high = "-1";
config.luxAlerts.warning2.low = "2000";
config.luxAlerts.warning2.high = "99999";

config.co2Alerts.warning1.low = "0";
config.co2Alerts.warning1.high = "390";
config.co2Alerts.caution1.low = "390";
config.co2Alerts.caution1.high = "398";
config.co2Alerts.caution2.low = "800";
config.co2Alerts.caution2.high = "1000";
config.co2Alerts.warning2.low = "1000";
config.co2Alerts.warning2.high = "9999";
}

String alerts_to_log_string(const config_alert_t& alerts) {
String log = "";
cfglog(" WARNING1 '" + alerts.warning1.low + "' ~ '" + alerts.warning1.high + "'");
cfglog(" CAUTION1 '" + alerts.caution1.low + "' ~ '" + alerts.caution1.high + "'");
cfglog(" CAUTION2 '" + alerts.caution2.low + "' ~ '" + alerts.caution2.high + "'");
cfglog(" WARNING2 '" + alerts.warning2.low + "' ~ '" + alerts.warning2.high + "'");

return log;
}

void print_config() {
cfglog("SSID: " + config.ssid);
cfglog("PASS: " + config.password);
cfglog("mDNS: " + config.mDNS);
cfglog("opMode: " + config.opMode);
cfglog("DISPLAY:");
cfglog(F("DISPLAY:"));
cfglog(" Flip: " + config.displayFlip);
cfglog(" Brightness: " + config.displayBrightness);
cfglog("ST7789: " + config.st7789);
Expand All @@ -53,6 +108,34 @@ void print_config() {
cfglog(" TX PIN: " + config.mhz19bTxPin);
cfglog("MQTT Broker: " + config.mqttBroker);
cfglog("MQTT Name : " + config.mqttName);
cfglog(F("Alerts:"));

cfglog(F(" Temperature:"));
alerts_to_log_string(config.temperatureAlerts);

cfglog(F(" Humidity :"));
alerts_to_log_string(config.humidityAlerts);

cfglog(F(" Pressure :"));
alerts_to_log_string(config.pressureAlerts);

cfglog(F(" Luminous :"));
alerts_to_log_string(config.luxAlerts);

cfglog(F(" CO2 :"));
alerts_to_log_string(config.co2Alerts);
}

void trim_alert_range(config_alert_range_t& range) {
range.low.trim();
range.high.trim();
}

void trim_alerts(config_alert_t& alerts) {
trim_alert_range(alerts.warning1);
trim_alert_range(alerts.warning2);
trim_alert_range(alerts.caution1);
trim_alert_range(alerts.caution2);
}

void trim_config() {
Expand All @@ -71,13 +154,32 @@ void trim_config() {
config.mhz19bTxPin.trim();
config.mqttBroker.trim();
config.mqttName.trim();

trim_alerts(config.temperatureAlerts);
trim_alerts(config.humidityAlerts);
trim_alerts(config.pressureAlerts);
trim_alerts(config.luxAlerts);
trim_alerts(config.co2Alerts);
}

DynamicJsonDocument alerts_to_json(const config_alert_t& alerts) {
DynamicJsonDocument json(100);
json["warn1.L"] = alerts.warning1.low;
json["warn1.H"] = alerts.warning1.high;
json["warn2.L"] = alerts.warning2.low;
json["warn2.H"] = alerts.warning2.high;
json["caut1.L"] = alerts.caution1.low;
json["caut1.H"] = alerts.caution1.high;
json["caut2.L"] = alerts.caution2.low;
json["caut2.H"] = alerts.caution2.high;
return json;
}

void write_config_file(File f) {

trim_config();

StaticJsonDocument<JSON_BUF> doc;
DynamicJsonDocument doc(CONF_JSON_SIZE);
doc["settingId"] = SETTING_ID; // これから書くConfigなので必ず想定しているconfig versionを書く
doc["ssid"] = config.ssid;
doc["password"] = config.password;
Expand All @@ -94,12 +196,20 @@ void write_config_file(File f) {
doc["mqttBroker"] = config.mqttBroker;
doc["mqttName"] = config.mqttName;

doc["temperatureAlerts"] = alerts_to_json(config.temperatureAlerts);
doc["humidityAlerts"] = alerts_to_json(config.humidityAlerts);
doc["luxAlerts"] = alerts_to_json(config.luxAlerts);
doc["pressureAlerts"] = alerts_to_json(config.pressureAlerts);
doc["co2Alerts"] = alerts_to_json(config.co2Alerts);

cfglog(F("Writing config"));
if (serializeJson(doc, f) == 0) {
cfglog(F("Failed to write to file"));
}
}

void set_config_value(String& cfg, StaticJsonDocument<JSON_BUF> &json, String key) {
// 指定されたキーが存在していれば値をセットする。存在しなければセットしない
void set_config_value(String& cfg, DynamicJsonDocument &json, String key) {
// https://arduinojson.org/v6/api/jsonobject/containskey/
JsonVariant value = json[key];
if (value.isNull()) {
Expand All @@ -109,12 +219,49 @@ void set_config_value(String& cfg, StaticJsonDocument<JSON_BUF> &json, String ke
cfg = value.as<String>();
}

void set_config_value(String& cfg, DynamicJsonDocument &json, String key1, String key2) {

JsonVariant middleObj = json[key1];

if (middleObj.isNull()) {

cfglog("Config file not contains first key:" + key1);
return;
} else {
JsonVariant value = middleObj[key2];

if (value.isNull()) {
cfglog("Config file not contains second key:" + key2);
return;
}

cfg = value.as<String>();
}
}

void read_config_alerts(config_alert_t& alerts, DynamicJsonDocument doc, String key1) {
set_config_value(alerts.warning1.low ,doc, key1, "warn1.L");
set_config_value(alerts.warning1.high ,doc, key1, "warn1.H");
set_config_value(alerts.caution1.low ,doc, key1, "caut1.L");
set_config_value(alerts.caution1.high ,doc, key1, "caut1.H");
set_config_value(alerts.warning2.low ,doc, key1, "warn2.L");
set_config_value(alerts.warning2.high ,doc, key1, "warn2.H");
set_config_value(alerts.caution2.low ,doc, key1, "caut2.L");
set_config_value(alerts.caution2.high ,doc, key1, "caut2.H");
}

void read_config_file(File f) {

set_default_config_value(); // とりあえずデフォルト値をロードしておく。

StaticJsonDocument<JSON_BUF> doc;
DynamicJsonDocument doc(CONF_JSON_SIZE);

cfglog(F("Json deserialize start"));

DeserializationError error = deserializeJson(doc, f);

cfglog(F("Json deserialize done :)"));

if (error) {
config.settingId = "INVALID";
cfglog(F("Failed to read file or Parse as json failed"));
Expand All @@ -137,5 +284,11 @@ void read_config_file(File f) {
set_config_value(config.mqttBroker ,doc ,"mqttBroker");
set_config_value(config.mqttName ,doc ,"mqttName");

read_config_alerts(config.temperatureAlerts, doc, "temperatureAlerts");
read_config_alerts(config.humidityAlerts, doc, "humidityAlerts");
read_config_alerts(config.pressureAlerts, doc, "pressureAlerts");
read_config_alerts(config.luxAlerts, doc, "luxAlerts");
read_config_alerts(config.co2Alerts, doc, "co2Alerts");

print_config();
}
Loading

0 comments on commit 439d5c5

Please sign in to comment.