Optimization

This commit is contained in:
Dmitry Borisenko
2020-10-20 23:58:23 +03:00
parent a5171f1178
commit 29e5a55ad3
14 changed files with 63 additions and 121 deletions

6
data/widgets/btn.json Normal file
View File

@@ -0,0 +1,6 @@
{
"widget": "btn",
"icon": "switch",
"fill": "outline",
"color": "orange"
}

View File

@@ -1,17 +1,17 @@
#pragma once #pragma once
#include "Utils/TimeUtils.h"
#include "Utils/PrintMessage.h"
#include "Global.h"
#include "Clock.h" #include "Clock.h"
#include "Global.h"
#include "Utils/TimeUtils.h"
#include "Utils\SerialPrint.h"
extern void clock_init();
#ifdef ESP8266 #ifdef ESP8266
#include "sntp.h" #include "sntp.h"
#endif #endif
class Clock { class Clock {
private: private:
Time_t _time_local; Time_t _time_local;
Time_t _time_utc; Time_t _time_utc;
@@ -39,7 +39,6 @@ class Clock {
if (drift > 1) { if (drift > 1) {
// Обработать ситуации c дрифтом времени на значительные величины // Обработать ситуации c дрифтом времени на значительные величины
} }
// TODO сохранять время на флеше
_unixtime = now; _unixtime = now;
@@ -71,27 +70,26 @@ class Clock {
void startSync() { void startSync() {
if (!_configured) { if (!_configured) {
//SerialPrint("I","module","sync to: " + _ntp + " timezone: " + String(_timezone)); SerialPrint("I", "NTP", "sync to: " + _ntp + " timezone: " + String(_timezone));
setupSntp(); setupSntp();
_configured = true; _configured = true;
// лучше не ждать, проверим в следующий раз
return; return;
} }
_hasSynced = hasTimeSynced(); _hasSynced = hasTimeSynced();
if (_hasSynced) { if (_hasSynced) {
//SerialPrint("I","module","synced " + getDateDotFormated() + " " + getTime()); SerialPrint("I", "NTP", "synced " + getDateDotFormated() + " " + getTime());
} else { } else {
//SerialPrint("[E]","module","failed to obtain"); SerialPrint("E", "NTP", "failed to obtain time");
} }
} }
void setupSntp() { void setupSntp() {
#ifdef ESP2866 #ifdef ESP8266
sntp_setservername(0, _ntp.c_str()); sntp_setservername(0, _ntp.c_str());
sntp_setservername(1, "ru.pool.ntp.org"); sntp_setservername(1, "ru.pool.ntp.org");
sntp_setservername(2, "pool.ntp.org"); sntp_setservername(2, "pool.ntp.org");
sntp_stop(); sntp_stop();
sntp_set_timezone(0); // UTC time sntp_set_timezone(0);
sntp_init(); sntp_init();
#else #else
configTime(0, 0, _ntp.c_str(), "ru.pool.ntp.org", "pool.ntp.org"); configTime(0, 0, _ntp.c_str(), "ru.pool.ntp.org", "pool.ntp.org");
@@ -164,4 +162,5 @@ class Clock {
const String getUptime() { const String getUptime() {
return prettyMillis(_uptime); return prettyMillis(_uptime);
} }
}; };
extern Clock* timeNow;

View File

@@ -86,7 +86,7 @@ struct Time_t {
enum TimerTask_t { WIFI_SCAN, enum TimerTask_t { WIFI_SCAN,
WIFI_MQTT_CONNECTION_CHECK, WIFI_MQTT_CONNECTION_CHECK,
SENSORS10SEC, SENSORS10SEC,
SENSORS60SEC, SENSORS30SEC,
STEPPER1, STEPPER1,
STEPPER2, STEPPER2,
LOG1, LOG1,

View File

@@ -1,27 +1,10 @@
#pragma once #pragma once
//===================Libraries=================================================================================================================================================== //===================Libraries===================================================================================================================================================
#include <Arduino.h>
#include <ArduinoJson.h>
#include "ESP32.h"
#include "ESP8266.h"
#include "Consts.h"
#include "GyverFilters.h"
#include "Upgrade.h"
#include "Clock.h"
#include "MqttClient.h"
#include "Utils/FileUtils.h"
#include "Utils/JsonUtils.h"
#include "Utils/StringUtils.h"
#include "Utils/SysUtils.h"
#include "Utils/PrintMessage.h"
#include "Utils/WiFiUtils.h"
#include "Utils/SerialPrint.h"
#include <Adafruit_BME280.h> #include <Adafruit_BME280.h>
#include <Adafruit_BMP280.h> #include <Adafruit_BMP280.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ArduinoOTA.h>
#include <Bounce2.h> #include <Bounce2.h>
#include <DHTesp.h> #include <DHTesp.h>
#include <DallasTemperature.h> #include <DallasTemperature.h>
@@ -31,27 +14,37 @@
#include <TickerScheduler.h> #include <TickerScheduler.h>
#include <Wire.h> #include <Wire.h>
#include <time.h> #include <time.h>
#include <ArduinoOTA.h>
#include "Clock.h"
#include "Consts.h"
#include "ESP32.h"
#include "ESP8266.h"
#include "GyverFilters.h"
#include "MqttClient.h"
#include "Upgrade.h"
#include "Utils/FileUtils.h"
#include "Utils/JsonUtils.h"
#include "Utils/SerialPrint.h"
#include "Utils/StringUtils.h"
#include "Utils/SysUtils.h"
#include "Utils/WiFiUtils.h"
#ifdef WEBSOCKET_ENABLED #ifdef WEBSOCKET_ENABLED
extern AsyncWebSocket ws; extern AsyncWebSocket ws;
//extern AsyncEventSource events; //extern AsyncEventSource events;
#endif #endif
extern Clock* timeNow;
extern TickerScheduler ts; extern TickerScheduler ts;
extern WiFiClient espClient; extern WiFiClient espClient;
extern PubSubClient mqtt; extern PubSubClient mqtt;
extern StringCommand sCmd; extern StringCommand sCmd;
extern AsyncWebServer server; extern AsyncWebServer server;
extern DallasTemperature sensors; extern DallasTemperature sensors;
extern OneWire *oneWire; extern OneWire* oneWire;
extern boolean but[NUM_BUTTONS]; extern boolean but[NUM_BUTTONS];
extern Bounce* buttons; extern Bounce* buttons;
//Global vars //Global vars
extern boolean just_load; extern boolean just_load;
@@ -74,7 +67,7 @@ extern String itemsLine;
// Sensors // Sensors
extern String sensorReadingMap10sec; extern String sensorReadingMap10sec;
extern String sensorReadingMap60sec; extern String sensorReadingMap30sec;
extern int8_t dallasEnterCounter; extern int8_t dallasEnterCounter;
extern String logging_value_names_list; extern String logging_value_names_list;
@@ -98,7 +91,6 @@ extern String getURL(const String& urls);
extern void do_fscheck(); extern void do_fscheck();
extern void doBusScan(); extern void doBusScan();
extern void servo_(); extern void servo_();
extern void clock_init();
extern void setLedStatus(LedStatus_t); extern void setLedStatus(LedStatus_t);
@@ -106,8 +98,6 @@ extern void setLedStatus(LedStatus_t);
extern void eventGen(String event_name, String number); extern void eventGen(String event_name, String number);
extern String add_set(String param_name); extern String add_set(String param_name);
//Timers //Timers
extern void Timer_countdown_init(); extern void Timer_countdown_init();
extern void timerStart_(); extern void timerStart_();
@@ -132,8 +122,6 @@ extern void pushControl();
//extern void do_udp_data_parse(); //extern void do_udp_data_parse();
//extern void do_mqtt_send_settings_to_udp(); //extern void do_mqtt_send_settings_to_udp();
extern void do_update(); extern void do_update();
// Init // Init
@@ -141,4 +129,3 @@ extern void uptime_init();
// Web // Web
extern void web_init(); extern void web_init();

View File

@@ -1,46 +0,0 @@
#pragma once
#include "Arduino.h"
#include "Utils/StringUtils.h"
#include "Utils/TimeUtils.h"
#include "Global.h"
#define pm PrintMessage(MODULE)
class PrintMessage {
public:
PrintMessage(const char* module) {
_module = module;
}
void error(const String& str) {
print(EL_ERROR, str);
}
void info(const String& str) {
print(EL_INFO, str);
}
private:
void printErrorLevel(ErrorLevel_t level) {
Serial.printf("[%c] ", getErrorLevelStr(level));
}
void printUptime() {
Serial.printf("%lu ", ((unsigned long)millis() / 1000));
}
void printModule() {
Serial.printf("[%s] ", _module);
}
void print(const ErrorLevel_t level, const String& str) {
printUptime();
printErrorLevel(level);
printModule();
Serial.println(str.c_str());
}
private:
const char* _module;
};

View File

@@ -63,8 +63,8 @@ void sensorsInit() {
nullptr, true); nullptr, true);
ts.add( ts.add(
SENSORS60SEC, 60000, [&](void *) { SENSORS30SEC, 30000, [&](void *) {
String buf = sensorReadingMap60sec; String buf = sensorReadingMap30sec;
while (buf.length()) { while (buf.length()) {
String tmp = selectToMarker(buf, ","); String tmp = selectToMarker(buf, ",");
sCmd.readStr(tmp); sCmd.readStr(tmp);
@@ -74,8 +74,3 @@ void sensorsInit() {
nullptr, true); nullptr, true);
} }
//void loopSerial() {
// if (term) {
// term->loop();
// }
//}

16
src/Clock.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include "Clock.h"
#include "Global.h"
Clock* timeNow;
void clock_init() {
timeNow = new Clock;
timeNow->setNtpPool(jsonReadStr(configSetupJson, "ntp"));
timeNow->setTimezone(jsonReadStr(configSetupJson, "timezone").toInt());
ts.add(
TIME_SYNC, 30000, [&](void*) {
timeNow->hasSync();
},
nullptr, true);
}

View File

@@ -4,7 +4,7 @@
AsyncWebSocket ws; AsyncWebSocket ws;
//AsyncEventSource events; //AsyncEventSource events;
#endif #endif
Clock* timeNow;
TickerScheduler ts(TEST + 1); TickerScheduler ts(TEST + 1);
WiFiClient espClient; WiFiClient espClient;
PubSubClient mqtt(espClient); PubSubClient mqtt(espClient);
@@ -40,7 +40,7 @@ String itemsLine = "";
// Sensors // Sensors
int8_t dallasEnterCounter = -1; int8_t dallasEnterCounter = -1;
String sensorReadingMap10sec; String sensorReadingMap10sec;
String sensorReadingMap60sec; String sensorReadingMap30sec;
// Logging // Logging
String logging_value_names_list; String logging_value_names_list;

View File

@@ -14,8 +14,6 @@ String xmlNode(String tags, String data);
String decToHex(uint32_t decValue, byte desiredStringLength); String decToHex(uint32_t decValue, byte desiredStringLength);
//39164
//457684
void SsdpInit() { void SsdpInit() {
server.on("/description.xml", HTTP_GET, [](AsyncWebServerRequest* request) { server.on("/description.xml", HTTP_GET, [](AsyncWebServerRequest* request) {
String ssdpSend = F("<root xmlns=\"urn:schemas-upnp-org:device-1-0\">"); String ssdpSend = F("<root xmlns=\"urn:schemas-upnp-org:device-1-0\">");

View File

@@ -1,5 +1,6 @@
#include "Utils/FileUtils.h" #include "Utils/FileUtils.h"
#include "Utils/PrintMessage.h" #include "Utils\SerialPrint.h"
#include "Utils/StringUtils.h"

View File

@@ -1,7 +1,7 @@
#include "Utils/SysUtils.h" #include "Utils/SysUtils.h"
#include "Global.h" #include "Global.h"
#include "Utils/PrintMessage.h"
const String getUniqueId(const char* name) { const String getUniqueId(const char* name) {
return String(name) + getMacAddress(); return String(name) + getMacAddress();

View File

@@ -20,8 +20,6 @@ void inputTimeSet() {
void handle_time_init() { void handle_time_init() {
ts.add( ts.add(
TIME, 1000, [&](void *) { TIME, 1000, [&](void *) {
//jsonWriteStr(configLiveJson, "time", timeNow->getTime());
//jsonWriteStr(configLiveJson, "timenow", timeNow->getTimeJson());
jsonWriteStr(configLiveJson, "timenow", timeNow->getTime()); jsonWriteStr(configLiveJson, "timenow", timeNow->getTime());
eventGen("timenow", ""); eventGen("timenow", "");
}, },

View File

@@ -7,7 +7,7 @@ void sysUptime() {
myLineParsing.update(); myLineParsing.update();
String key = myLineParsing.gkey(); String key = myLineParsing.gkey();
sCmd.addCommand(key.c_str(), uptimeReading); sCmd.addCommand(key.c_str(), uptimeReading);
sensorReadingMap60sec += key + ","; sensorReadingMap30sec += key + ",";
myLineParsing.clear(); myLineParsing.clear();
} }

View File

@@ -116,16 +116,4 @@ void loop() {
myNotAsyncActions->loop(); myNotAsyncActions->loop();
ts.update(); ts.update();
} }
void clock_init() {
timeNow = new Clock();
timeNow->setNtpPool(jsonReadStr(configSetupJson, "ntp"));
timeNow->setTimezone(jsonReadStr(configSetupJson, "timezone").toInt());
ts.add(
TIME_SYNC, 30000, [&](void*) {
timeNow->hasSync();
},
nullptr, true);
}