From 7b2d4612dfb0cf5b94265c599cc3fab4bcebeb7b Mon Sep 17 00:00:00 2001 From: Luc Date: Sun, 8 Sep 2019 10:48:28 +0200 Subject: [PATCH] Add auto notification setting Alllow to automaticatilly send notification when device is online --- esp3d/command.cpp | 18 ++++++++++++++++++ esp3d/config.cpp | 3 +++ esp3d/config.h | 7 +++++-- esp3d/notifications_service.cpp | 17 ++++++++++++++++- esp3d/notifications_service.h | 4 ++++ esp3d/wificonf.cpp | 1 + 6 files changed, 47 insertions(+), 3 deletions(-) diff --git a/esp3d/command.cpp b/esp3d/command.cpp index 8b90adc0..f968e7d9 100644 --- a/esp3d/command.cpp +++ b/esp3d/command.cpp @@ -1259,6 +1259,19 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a ESPCOM::print ( F ("\",\"H\":\"Notifications Settings\",\"M\":\""), output, espresponse); ESPCOM::print ( (const char *) CONFIG::intTostr (MIN_NOTIFICATION_SETTINGS_LENGTH), output, espresponse); ESPCOM::print ( F("\"}"), output, espresponse); + ESPCOM::println (F (","), output, espresponse); + //Auto Notification + ESPCOM::print (F ("{\"F\":\"network\",\"P\":\""), output, espresponse); + ESPCOM::print ( (const char *) CONFIG::intTostr (ESP_AUTO_NOTIFICATION), output, espresponse); + ESPCOM::print (F ("\",\"T\":\"B\",\"V\":\""), output, espresponse); + if (!CONFIG::read_byte (ESP_AUTO_NOTIFICATION, &bbuf ) ) { + ESPCOM::print ("???", output, espresponse); + } else { + ESPCOM::print ( (const char *) CONFIG::intTostr (bbuf), output, espresponse); + } + ESPCOM::print (F ("\",\"H\":\"Auto notification\",\"O\":[{\"No\":\"0\"},{\"Yes\":\"1\"}]}"), output, espresponse); + + #endif //NOTIFICATION_FEATURE } @@ -1419,6 +1432,11 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a CONFIG::InitDHT(true); } #endif +#ifdef NOTIFICATION_FEATURE + if (pos == ESP_AUTO_NOTIFICATION) { + notificationsservice.setAutonotification ((bbuf == 0)? false: true); + } +#endif #if defined(TIMESTAMP_FEATURE) if ( (pos == EP_TIMEZONE) || (pos == EP_TIME_ISDST) || (pos == EP_TIME_SERVER1) || (pos == EP_TIME_SERVER2) || (pos == EP_TIME_SERVER3) ) { CONFIG::init_time_client(); diff --git a/esp3d/config.cpp b/esp3d/config.cpp index 496c3f58..abeb022d 100644 --- a/esp3d/config.cpp +++ b/esp3d/config.cpp @@ -911,6 +911,9 @@ bool CONFIG::reset_config() if (!CONFIG::write_string (ESP_NOTIFICATION_SETTINGS, DEFAULT_NOTIFICATION_SETTINGS ) ) { return false; } + if (!CONFIG::write_byte (ESP_AUTO_NOTIFICATION, DEFAULT_AUTO_NOTIFICATION_STATE) ) { + return false; + } #endif return set_EEPROM_version(EEPROM_CURRENT_VERSION); diff --git a/esp3d/config.h b/esp3d/config.h index 5649580b..cc29f1f3 100644 --- a/esp3d/config.h +++ b/esp3d/config.h @@ -19,7 +19,7 @@ */ //version and sources location -#define FW_VERSION "2.1.0.b37" +#define FW_VERSION "2.1.0.b38" #define REPOSITORY "https://github.com/luc-github/ESP3D" //Customize ESP3D //////////////////////////////////////////////////////////////////////// @@ -316,7 +316,8 @@ typedef enum { #define EP_HOSTNAME 130//33 bytes 32+1 = string ; warning does not support multibyte char like chinese #define EP_DHT_INTERVAL 164//4 bytes = int #define ESP_NOTIFICATION_TYPE 168 //1 byte = flag -#define EP_FREE_INT2 169//3 bytes = int +#define ESP_AUTO_NOTIFICATION 170//1 bytes = flag +#define EP_FREE_BYTE1 171//1 bytes = flag #define EP_FREE_INT3 172//4 bytes = int #define EP_ADMIN_PWD 176//21 bytes 20+1 = string ; warning does not support multibyte char like chinese #define EP_USER_PWD 197//21 bytes 20+1 = string ; warning does not support multibyte char like chinese @@ -396,6 +397,8 @@ const int DEFAULT_DHT_INTERVAL = 30; #define DEFAULT_NOTIFICATION_TOKEN1 "" #define DEFAULT_NOTIFICATION_TOKEN2 "" #define DEFAULT_NOTIFICATION_SETTINGS "" +#define DEFAULT_AUTO_NOTIFICATION_STATE 1 +#define NOTIFICATION_ESP_ONLINE "Hi, %ESP_NAME% is now online at %ESP_IP%" //Notifications #define ESP_PUSHOVER_NOTIFICATION 1 diff --git a/esp3d/notifications_service.cpp b/esp3d/notifications_service.cpp index a5627128..af3adf0f 100644 --- a/esp3d/notifications_service.cpp +++ b/esp3d/notifications_service.cpp @@ -92,9 +92,22 @@ bool Wait4Answer(TSecureClient & client, const char * linetrigger, const char * return false; } +bool NotificationsService::sendAutoNotification(const char * msg) +{ + if ((WiFi.getMode() == WIFI_AP) || (!_started) || (!_autonotification))return false; + String msgtpl = msg; + //check if has variable to change + if (msgtpl.indexOf("%") != -1) { + msgtpl.replace("%ESP_IP%", WiFi.localIP().toString().c_str()); + msgtpl.replace("%ESP_NAME%", wifi_config.get_hostname()); + } + return sendMSG("ESP3D Notification", msgtpl.c_str()); +} + NotificationsService::NotificationsService() { _started = false; + _autonotification = false; _notificationType = 0; _token1 = ""; _token1 = ""; @@ -411,7 +424,9 @@ bool NotificationsService::begin() return false; break; } - + if (CONFIG::read_byte (ESP_NOTIFICATION_TYPE, &bbuf ) ) { + _autonotification = (bbuf == 0) ? false: true; + } if (!res) { end(); } diff --git a/esp3d/notifications_service.h b/esp3d/notifications_service.h index 5f6c6b98..54909b1e 100644 --- a/esp3d/notifications_service.h +++ b/esp3d/notifications_service.h @@ -35,9 +35,13 @@ public: bool sendMSG(const char * title, const char * message); const char * getTypeString(); bool started(); + bool isAutonotification() { return _autonotification;}; + void setAutonotification(bool value) { _autonotification = value;}; + bool sendAutoNotification(const char * msg); private: bool _started; uint8_t _notificationType; + bool _autonotification; String _token1; String _token2; String _settings; diff --git a/esp3d/wificonf.cpp b/esp3d/wificonf.cpp index 4b29b47d..210c95ff 100644 --- a/esp3d/wificonf.cpp +++ b/esp3d/wificonf.cpp @@ -673,6 +673,7 @@ bool WIFI_CONFIG::Enable_servers() #endif #if defined(NOTIFICATION_FEATURE) notificationsservice.begin(); + notificationsservice.sendAutoNotification(NOTIFICATION_ESP_ONLINE); #endif return true;