Skip to content

Commit

Permalink
Improve NTP handling (#1676)
Browse files Browse the repository at this point in the history
* fix special case where number is named "default" (keep all topics in top level instead of in a sub-group)

* re-implemented SNTP usage, added way to disable NTP client, added timezone table

* minor fixes

Co-authored-by: CaCO3 <[email protected]>
  • Loading branch information
caco3 and CaCO3 authored Dec 23, 2022
1 parent 66eb1e8 commit 603e968
Show file tree
Hide file tree
Showing 15 changed files with 731 additions and 161 deletions.
1 change: 1 addition & 0 deletions code/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static char ota_write_data[SERVER_OTA_SCRATCH_BUFSIZE + 1] = { 0 };
static const char *TAG = "OTA";

esp_err_t handler_reboot(httpd_req_t *req);
static bool ota_update_task(std::string fn);

std::string _file_name_update;

Expand Down
1 change: 0 additions & 1 deletion code/components/jomjol_fileserver_ota/server_ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ void CheckOTAUpdate();
void doReboot();
void hard_restart();
void CheckUpdate();
static bool ota_update_task(std::string fn);

#endif //SERVEROTA_H
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ bool ClassFlowCNNGeneral::doNeuralNetwork(string time)
GENERAL[n]->ROI[roi]->isReject = true;
result = -1;
_result_save_file+= 100; // In case fit is not sufficient, the result should still be saved with "-10x.y".
string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + "Threshold: " + to_string(CNNGoodThreshold) + ")";
string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + ", Threshold: " + to_string(CNNGoodThreshold) + ")";
LogFile.WriteToFile(ESP_LOG_WARN, TAG, zw);
}
else
Expand Down
26 changes: 10 additions & 16 deletions code/components/jomjol_flowcontroll/ClassFlowControll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void ClassFlowControll::doFlowMakeImageOnly(string time){
for (int i = 0; i < FlowControll.size(); ++i)
{
if (FlowControll[i]->name() == "ClassFlowMakeImage") {
zw_time = gettimestring("%H:%M:%S");
zw_time = getCurrentTimeString("%H:%M:%S");
std::string flowStatus = TranslateAktstatus(FlowControll[i]->name());
aktstatus = flowStatus + " (" + zw_time + ")";
#ifdef ENABLE_MQTT
Expand All @@ -315,16 +315,19 @@ bool ClassFlowControll::doFlow(string time)
#endif

/* Check if we have a valid date/time and if not restart the NTP client */
if (! getTimeIsSet()) {
/* if (! getTimeIsSet()) {
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time not set, restarting NTP Client!");
restartNtpClient();
}
}*/

//checkNtpStatus(0);

for (int i = 0; i < FlowControll.size(); ++i)
{
zw_time = gettimestring("%H:%M:%S");
zw_time = getCurrentTimeString("%H:%M:%S");
std::string flowStatus = TranslateAktstatus(FlowControll[i]->name());
aktstatus = flowStatus + " (" + zw_time + ")";
//LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatus);
#ifdef ENABLE_MQTT
MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
#endif //ENABLE_MQTT
Expand Down Expand Up @@ -355,9 +358,10 @@ bool ClassFlowControll::doFlow(string time)
#endif

}
zw_time = gettimestring("%H:%M:%S");
zw_time = getCurrentTimeString("%H:%M:%S");
std::string flowStatus = "Flow finished";
aktstatus = flowStatus + " (" + zw_time + ")";
//LogFile.WriteToFile(ESP_LOG_INFO, TAG, aktstatus);
#ifdef ENABLE_MQTT
MQTTPublish(mqttServer_getMainTopic() + "/" + "status", flowStatus, false);
#endif //ENABLE_MQTT
Expand Down Expand Up @@ -541,17 +545,7 @@ bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph)
LogFile.SetLogFileRetention(std::stoi(splitted[1]));
}

if ((toUpper(splitted[0]) == "TIMEZONE") && (splitted.size() > 1))
{
string zw = "Set TimeZone: " + splitted[1];
setTimeZone(splitted[1]);
}

if ((toUpper(splitted[0]) == "TIMESERVER") && (splitted.size() > 1))
{
string zw = "Set TimeZone: " + splitted[1];
reset_servername(splitted[1]);
}
/* TimeServer and TimeZone got already read from the config, see setupTime () */

if ((toUpper(splitted[0]) == "RSSITHREASHOLD") && (splitted.size() > 1))
{
Expand Down
10 changes: 8 additions & 2 deletions code/components/jomjol_helper/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,11 +785,17 @@ bool isSetSystemStatusFlag(SystemStatusFlag_t flag) {
}
}


time_t getUpTime(void) {
return (uint32_t)(esp_timer_get_time()/1000/1000); // in seconds
}


string getResetReason(void) {
std::string reasonText;

switch(esp_reset_reason()) {
case ESP_RST_POWERON: reasonText = "Power-on event"; break; //!< Reset due to power-on event
case ESP_RST_POWERON: reasonText = "Power-on event (or reset button)"; break; //!< Reset due to power-on event
case ESP_RST_EXT: reasonText = "External pin"; break; //!< Reset by external pin (not applicable for ESP32)
case ESP_RST_SW: reasonText = "Via esp_restart"; break; //!< Software reset via esp_restart
case ESP_RST_PANIC: reasonText = "Exception/panic"; break; //!< Software reset due to exception/panic
Expand All @@ -814,7 +820,7 @@ std::string getFormatedUptime(bool compact) {
char buf[20];
#pragma GCC diagnostic ignored "-Wformat-truncation"

int uptime = (uint32_t)(esp_timer_get_time()/1000/1000); // in seconds
int uptime = getUpTime(); // in seconds

int days = int(floor(uptime / (3600*24)));
int hours = int(floor((uptime - days * 3600*24) / (3600)));
Expand Down
1 change: 1 addition & 0 deletions code/components/jomjol_helper/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void clearSystemStatusFlag(SystemStatusFlag_t flag);
int getSystemStatus(void);
bool isSetSystemStatusFlag(SystemStatusFlag_t flag);

time_t getUpTime(void);
string getResetReason(void);
std::string getFormatedUptime(bool compact);

Expand Down
12 changes: 11 additions & 1 deletion code/components/jomjol_mqtt/server_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ void sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
}
}
else {
if (field == "problem") { // Special binary sensor which is based on error topic
payload += "\"state_topic\": \"~/error\"," + nl;
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\"," + nl;
}
else {
payload += "\"state_topic\": \"~/" + field + "\"," + nl;
}
}

if (unit != "") {
Expand Down Expand Up @@ -149,7 +155,11 @@ void MQTThomeassistantDiscovery() {


for (int i = 0; i < (*NUMBERS).size(); ++i) {
std::string group = (*NUMBERS)[i]->name;
std::string group = (*NUMBERS)[i]->name;
if (group == "default") {
group = "";
}

// Group | Field | User Friendly Name | Icon | Unit | Device Class | State Class | Entity Category
sendHomeAssistantDiscoveryTopic(group, "value", "Value", "gauge", valueUnit, meterType, "total_increasing", "");
sendHomeAssistantDiscoveryTopic(group, "raw", "Raw Value", "raw", valueUnit, "", "total_increasing", "diagnostic");
Expand Down
4 changes: 2 additions & 2 deletions code/components/jomjol_tfliteclass/server_tflite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void doInit(void)

bool doflow(void)
{
std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
flowisrunning = true;
tfliteflow.doFlow(zw_time);
Expand Down Expand Up @@ -787,7 +787,7 @@ void task_autodoFlow(void *pvParameter)
auto_isrunning = tfliteflow.isAutoStart(auto_intervall);
if (isSetupModusActive()) {
auto_isrunning = false;
std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
tfliteflow.doFlowMakeImageOnly(zw_time);

}
Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_time_sntp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.*)

idf_component_register(SRCS ${app_sources}
INCLUDE_DIRS "."
REQUIRES tflite-lib jomjol_logfile)
REQUIRES tflite-lib jomjol_logfile jomjol_configfile)


Loading

0 comments on commit 603e968

Please sign in to comment.