Prevent redundant idle messages

This commit is contained in:
fvanroie 2022-04-11 03:26:14 +02:00
parent e3a6391496
commit b073983275
5 changed files with 46 additions and 32 deletions

View File

@ -114,25 +114,23 @@ HASP_ATTRIBUTE_FAST_MEM void hasp_update_sleep_state()
if(sleepTimeLong > 0 && idle >= (sleepTimeShort + sleepTimeLong)) {
if(hasp_sleep_state != HASP_SLEEP_LONG) {
hasp_sleep_state = HASP_SLEEP_LONG;
gui_hide_pointer(true);
dispatch_idle(NULL, NULL, TAG_HASP);
hasp_sleep_state = HASP_SLEEP_LONG;
dispatch_idle_state(HASP_SLEEP_LONG);
}
} else if(sleepTimeShort > 0 && idle >= sleepTimeShort) {
if(hasp_sleep_state != HASP_SLEEP_SHORT) {
hasp_sleep_state = HASP_SLEEP_SHORT;
gui_hide_pointer(true);
dispatch_idle(NULL, NULL, TAG_HASP);
hasp_sleep_state = HASP_SLEEP_SHORT;
dispatch_idle_state(HASP_SLEEP_SHORT);
}
} else {
if(hasp_sleep_state != HASP_SLEEP_OFF) {
hasp_sleep_state = HASP_SLEEP_OFF;
gui_hide_pointer(false);
dispatch_idle(NULL, NULL, TAG_HASP);
hasp_sleep_state = HASP_SLEEP_OFF;
dispatch_idle_state(HASP_SLEEP_OFF);
}
}
// return (hasp_sleep_state != HASP_SLEEP_OFF);
}
void hasp_set_sleep_offset(uint32_t offset)
@ -140,6 +138,11 @@ void hasp_set_sleep_offset(uint32_t offset)
sleepTimeOffset = offset;
}
uint8_t hasp_get_sleep_state()
{
return hasp_sleep_state;
}
void hasp_set_sleep_state(uint8_t state)
{
switch(state) {
@ -151,6 +154,7 @@ void hasp_set_sleep_state(uint8_t state)
break;
case HASP_SLEEP_OFF:
hasp_set_sleep_offset(0);
hasp_set_wakeup_touch(false);
break;
default:
return;
@ -159,9 +163,9 @@ void hasp_set_sleep_state(uint8_t state)
hasp_sleep_state = state;
}
void hasp_get_sleep_state(char* payload)
void hasp_get_sleep_payload(uint8_t state, char* payload)
{
switch(hasp_sleep_state) {
switch(state) {
case HASP_SLEEP_LONG:
memcpy_P(payload, PSTR("long"), 5);
break;
@ -712,7 +716,7 @@ void hasp_get_info(JsonDocument& doc)
buffer += "s";
info[F(D_INFO_ENVIRONMENT)] = PIOENV;
info[F(D_INFO_UPTIME)] = buffer;
hasp_get_sleep_state(size_buf);
hasp_get_sleep_payload(hasp_get_sleep_state(), size_buf);
info[F("Idle")] = size_buf;
info[F("Active Page")] = haspPages.get();

View File

@ -65,7 +65,8 @@ bool haspSetConfig(const JsonObject& settings);
lv_font_t* hasp_get_font(uint8_t fontid);
HASP_ATTRIBUTE_FAST_MEM void hasp_update_sleep_state();
void hasp_get_sleep_state(char* payload);
void hasp_get_sleep_payload(uint8_t state, char* payload);
uint8_t hasp_get_sleep_state();
void hasp_set_sleep_state(uint8_t state);
void hasp_get_sleep_time(uint16_t& short_time, uint16_t& long_time);
void hasp_set_sleep_time(uint16_t short_time, uint16_t long_time);

View File

@ -1169,7 +1169,7 @@ void dispatch_statusupdate(const char*, const char*, uint8_t source)
{
char buffer[128];
hasp_get_sleep_state(topic);
hasp_get_sleep_payload(hasp_get_sleep_state(), topic);
snprintf_P(data, sizeof(data), PSTR("{\"node\":\"%s\",\"idle\":\"%s\",\"version\":\"%s\",\"uptime\":%lu,"),
haspDevice.get_hostname(), topic, haspDevice.get_version(),
long(millis() / 1000)); // \"status\":\"available\",
@ -1216,8 +1216,8 @@ void dispatch_statusupdate(const char*, const char*, uint8_t source)
void dispatch_current_state(uint8_t source)
{
dispatch_idle(NULL, NULL, source);
dispatch_current_page();
dispatch_idle_state(hasp_get_sleep_state());
dispatch_state_antiburn(hasp_get_antiburn());
// delayed published topic
@ -1249,11 +1249,18 @@ void dispatch_calibrate(const char*, const char*, uint8_t source)
guiCalibrate();
}
void dispatch_wakeup()
{
if(hasp_get_sleep_state() == HASP_SLEEP_OFF) return;
hasp_set_sleep_state(HASP_SLEEP_OFF);
dispatch_idle_state(HASP_SLEEP_OFF);
}
void dispatch_wakeup_obsolete(const char* topic, const char*, uint8_t source)
{
LOG_WARNING(TAG_MSGR, F(D_ATTRIBUTE_OBSOLETE D_ATTRIBUTE_INSTEAD), topic,
"idle=off"); // TODO: obsolete dim, light and brightness
lv_disp_trig_activity(NULL);
dispatch_wakeup();
hasp_set_wakeup_touch(false);
}
@ -1262,31 +1269,32 @@ void dispatch_sleep(const char*, const char*, uint8_t source)
hasp_set_wakeup_touch(false);
}
void dispatch_idle(const char*, const char* payload, uint8_t source)
void dispatch_idle_state(uint8_t state)
{
char topic[8];
char buffer[8];
memcpy_P(topic, PSTR("idle"), 8);
hasp_get_sleep_payload(state, buffer);
dispatch_state_subtopic(topic, buffer);
}
// idle off command
void dispatch_idle(const char*, const char* payload, uint8_t source)
{
if(payload && strlen(payload)) {
uint8_t state = HASP_SLEEP_LAST;
if(!strcmp_P(payload, "off")) {
hasp_set_wakeup_touch(false);
state = HASP_SLEEP_OFF;
hasp_set_sleep_state(HASP_SLEEP_OFF);
} else if(!strcmp_P(payload, "short")) {
state = HASP_SLEEP_SHORT;
hasp_set_sleep_state(HASP_SLEEP_SHORT);
} else if(!strcmp_P(payload, "long")) {
state = HASP_SLEEP_LONG;
hasp_set_sleep_state(HASP_SLEEP_LONG);
} else {
state = HASP_SLEEP_LAST;
LOG_WARNING(TAG_MSGR, F("Invalid idle value %s"), payload);
return;
}
hasp_set_sleep_state(state);
}
// idle state
memcpy_P(topic, PSTR("idle"), 8);
hasp_get_sleep_state(buffer);
dispatch_state_subtopic(topic, buffer);
dispatch_idle_state(hasp_get_sleep_state()); // always send the current state
}
void dispatch_reboot(const char*, const char*, uint8_t source)

View File

@ -72,10 +72,11 @@ void dispatch_web_update(const char*, const char* espOtaUrl, uint8_t source);
void dispatch_statusupdate(const char*, const char*, uint8_t source);
void dispatch_send_discovery(const char*, const char*, uint8_t source);
void dispatch_send_sensordata(const char*, const char*, uint8_t source);
void dispatch_idle(const char*, const char*, uint8_t source);
// void dispatch_idle(const char*, const char*, uint8_t source);
void dispatch_idle_state(uint8_t state);
void dispatch_calibrate(const char*, const char*, uint8_t source);
void dispatch_antiburn(const char*, const char* payload, uint8_t source);
void dispatch_wakeup(const char*, const char*, uint8_t source);
void dispatch_wakeup();
void dispatch_exec(const char*, const char* payload, uint8_t source);
void dispatch_config(const char* topic, const char* payload, uint8_t source);

View File

@ -961,16 +961,16 @@ static void handleFileCreate()
}
}
if(webServer.hasArg(F("init"))) {
dispatch_idle(NULL, "0", TAG_HTTP);
dispatch_wakeup();
hasp_init();
}
if(webServer.hasArg(F("load"))) {
dispatch_idle(NULL, "0", TAG_HTTP);
dispatch_wakeup();
hasp_load_json();
}
if(webServer.hasArg(F("page"))) {
dispatch_wakeup();
uint8_t pageid = atoi(webServer.arg(F("page")).c_str());
dispatch_idle(NULL, "0", TAG_HTTP);
dispatch_set_page(pageid, LV_SCR_LOAD_ANIM_NONE);
}
webServer.send(200, PSTR("text/plain"), "");