31 #include "../../include/esp3d_config.h"
32 #ifdef NOTIFICATION_FEATURE
34 #include "../../core/settings_esp3d.h"
35 #include "../../core/esp3doutput.h"
36 #include "../network/netconfig.h"
38 #if defined( ARDUINO_ARCH_ESP8266)
40 #if defined(USING_AXTLS)
41 #include "WiFiClientSecureAxTLS.h"
42 using namespace axTLS;
43 typedef axTLS::WiFiClientSecure TSecureClient;
45 #include <WiFiClientSecure.h>
46 typedef WiFiClientSecure TSecureClient;
48 #endif //ARDUINO_ARCH_ESP8266
50 #if defined(ARDUINO_ARCH_ESP32)
51 #include <WiFiClientSecure.h>
52 typedef WiFiClientSecure TSecureClient;
53 #endif //ARDUINO_ARCH_ESP32
57 #define PUSHOVERTIMEOUT 5000
58 #define PUSHOVERSERVER "api.pushover.net"
59 #define PUSHOVERPORT 443
61 #define LINETIMEOUT 5000
62 #define LINESERVER "notify-api.line.me"
65 #define EMAILTIMEOUT 5000
69 bool Wait4Answer(TSecureClient & client,
const char * linetrigger,
const char * expected_answer, uint32_t timeout)
71 if(client.connected()) {
73 uint32_t starttimeout = millis();
74 while (client.connected() && ((millis() -starttimeout) < timeout)) {
75 answer = client.readStringUntil(
'\n');
77 if ((answer.indexOf(linetrigger) != -1) || (strlen(linetrigger) == 0)) {
82 if (strlen(expected_answer) == 0) {
86 if(answer.indexOf(expected_answer) == -1) {
106 if (msgtpl.indexOf(
"%") != -1) {
107 msgtpl.replace(
"%ESP_IP%", WiFi.localIP().toString().c_str());
122 _notificationType = 0;
139 switch(_notificationType) {
157 if (!((strlen(title) == 0) && (strlen(message) == 0))) {
158 switch(_notificationType) {
160 return sendPushoverMSG(title,message);
163 return sendEmailMSG(title,message);
166 return sendLineMSG(title,message);
176 bool NotificationsService::sendPushoverMSG(
const char * title,
const char * message)
181 #pragma GCC diagnostic push
182 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
183 TSecureClient Notificationclient;
184 #pragma GCC diagnostic pop
185 #if defined(ARDUINO_ARCH_ESP8266) && !defined(USING_AXTLS)
186 Notificationclient.setInsecure();
187 #endif //ARDUINO_ARCH_ESP8266 && !USING_AXTLS
188 if (!Notificationclient.connect(_serveraddress.c_str(), _port)) {
189 log_esp3d(
"Error connecting server %s:%d", _serveraddress.c_str(), _port);
204 postcmd =
"POST /1/messages.json HTTP/1.1\r\nHost: api.pushover.net\r\nConnection: close\r\nCache-Control: no-cache\r\nUser-Agent: ESP3D\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nContent-Length: ";
205 postcmd += data.length();
206 postcmd +=
"\r\n\r\n";
210 Notificationclient.print(postcmd);
212 Notificationclient.stop();
215 bool NotificationsService::sendEmailMSG(
const char * title,
const char * message)
217 #pragma GCC diagnostic push
218 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
219 TSecureClient Notificationclient;
220 #pragma GCC diagnostic pop
221 #if defined(ARDUINO_ARCH_ESP8266) && !defined(USING_AXTLS)
222 Notificationclient.setInsecure();
223 #endif //ARDUINO_ARCH_ESP8266 && !USING_AXTLS
225 if (!Notificationclient.connect(_serveraddress.c_str(), _port)) {
226 log_esp3d(
"Error connecting server %s:%d", _serveraddress.c_str(), _port);
236 Notificationclient.print(
"HELO friend\r\n");
243 Notificationclient.print(
"AUTH LOGIN\r\n");
250 Notificationclient.printf(
"%s\r\n",_token1.c_str());
257 Notificationclient.printf(
"%s\r\n",_token2.c_str());
264 Notificationclient.printf(
"MAIL FROM: <%s>\r\n",_settings.c_str());
271 Notificationclient.printf(
"RCPT TO: <%s>\r\n",_settings.c_str());
278 Notificationclient.print(
"DATA\r\n");
285 Notificationclient.printf(
"From:ESP3D<%s>\r\n",_settings.c_str());
286 Notificationclient.printf(
"To: <%s>\r\n",_settings.c_str());
287 Notificationclient.printf(
"Subject: %s\r\n\r\n",title);
288 Notificationclient.println(message);
292 Notificationclient.print(
".\r\n");
299 Notificationclient.print(
"QUIT\r\n");
305 Notificationclient.stop();
308 bool NotificationsService::sendLineMSG(
const char * title,
const char * message)
313 #pragma GCC diagnostic push
314 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
315 TSecureClient Notificationclient;
316 #pragma GCC diagnostic pop
317 #if defined(ARDUINO_ARCH_ESP8266) && !defined(USING_AXTLS)
318 Notificationclient.setInsecure();
319 #endif //ARDUINO_ARCH_ESP8266 && !USING_AXTLS
321 if (!Notificationclient.connect(_serveraddress.c_str(), _port)) {
322 log_esp3d(
"Error connecting server %s:%d", _serveraddress.c_str(), _port);
329 postcmd =
"POST /api/notify HTTP/1.1\r\nHost: notify-api.line.me\r\nConnection: close\r\nCache-Control: no-cache\r\nUser-Agent: ESP3D\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nContent-Type: application/x-www-form-urlencoded\r\n";
330 postcmd +=
"Authorization: Bearer ";
331 postcmd += _token1 +
"\r\n";
332 postcmd +=
"Content-Length: ";
333 postcmd += data.length();
334 postcmd +=
"\r\n\r\n";
338 Notificationclient.print(postcmd);
340 Notificationclient.stop();
344 bool NotificationsService::getPortFromSettings()
347 int pos = tmp.lastIndexOf(
':');
351 _port= tmp.substring(pos+1).toInt();
360 bool NotificationsService::getServerAddressFromSettings()
363 int pos1 = tmp.indexOf(
'#');
364 int pos2 = tmp.lastIndexOf(
':');
365 if ((pos1 == -1) || (pos2 == -1)) {
370 _serveraddress = tmp.substring(pos1+1, pos2);
371 log_esp3d(
"server : %s", _serveraddress.c_str());
375 bool NotificationsService::getEmailFromSettings()
378 int pos = tmp.indexOf(
'#');
382 _settings = tmp.substring(0, pos);
383 log_esp3d(
"email : %s", _settings.c_str());
394 switch(_notificationType) {
413 if(!getEmailFromSettings() || !getPortFromSettings()|| !getServerAddressFromSettings()) {
434 _notificationType = 0;
448 #endif //NOTIFICATION_FEATURE