lift restriction when disconnected & aqi working

This commit is contained in:
Siwat Sirichai 2024-03-23 19:56:55 +07:00
parent 1fd1ab1119
commit 4d8562a64a
5 changed files with 113 additions and 13 deletions

Binary file not shown.

View File

@ -14,6 +14,6 @@ board = wt32-eth01
framework = arduino
lib_deps = siwats/ESPMegaPROR3@^2.4.3
monitor_speed = 115200
build_flags = -DCORE_DEBUG_LEVEL=5
build_flags = -DCORE_DEBUG_LEVEL=0
monitor_port = COM36
upload_port = COM36

View File

@ -73,12 +73,15 @@ void CUDDisplay::begin(cud_display_cards_t cards)
auto binded_touch_callback = std::bind(&CUDDisplay::handle_touch, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
auto binded_ac_callback = std::bind(&CUDDisplay::handle_ac_change, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
auto binded_ac_sensor_callback = std::bind(&CUDDisplay::handle_ac_sensor, this, std::placeholders::_1, std::placeholders::_2);
auto binded_handle_mqtt_connected = std::bind(&CUDDisplay::handle_mqtt_connected, this);
ESP_LOGV("CUD Display", "Registering Input Card Callbacks");
this->cards.inputCard->registerCallback(binded_input_callback);
ESP_LOGV("CUD Display", "Registering Output Card Callbacks");
this->cards.outputCard->registerChangeCallback(binded_output_callback);
ESP_LOGV("CUD Display", "Registering AQI Callbacks");
this->conf->aqi->registerCallback(binded_aqi_callback);
ESP_LOGV("CUD Display", "Registering MQTT Connected Callbacks");
this->cards.iot->registerSubscribeCallback(binded_handle_mqtt_connected);
ESP_LOGV("CUD Display", "Registering Display Payload Callbacks");
this->registerPayloadCallback(binded_payload_callback);
ESP_LOGV("CUD Display", "Registering Display Touch Callbacks");
@ -150,13 +153,13 @@ void CUDDisplay::begin(cud_display_cards_t cards)
this->ac_temp_upper_bound.setIntValue(DEFAULT_TEMP_UPPER_BOUND);
}
// Initialize the display
ESP_LOGV("CUD Display", "Initializing display");
this->display_init();
}
void CUDDisplay::loop() {
void CUDDisplay::loop()
{
ESPMegaDisplay::loop();
// Check if the AC button press is pending
if (this->ac_button_press_pending)
@ -171,6 +174,54 @@ void CUDDisplay::loop() {
this->ac_button_press_pending = false;
}
}
// Check if MQTT is connected
static uint32_t last_mqtt_connected_check = 0;
if (last_mqtt_connected_check == 0)
{
last_mqtt_connected_check = millis() + 15000; // Wait 15 seconds before checking
}
static bool first_disconnect = true;
if (millis() - last_mqtt_connected_check > 3000)
{
ESP_LOGD("CUD Display", "Checking MQTT Connection, Connection is %s", this->cards.iot->mqttConnected() ? "true" : "false");
if (!this->cards.iot->mqttConnected())
{
if (first_disconnect)
{
// When MQTT is disconnected, enter standalone mode
// In standalone mode, socket contactor is turned on
// A/C lock is lifted
// A/C temperature range limits are lifted
this->refresh_display_ac();
this->cards.outputCard->setState(this->conf->socket_contactor_pin, true);
first_disconnect = false;
}
this->standalone = true;
}
else
{
standalone = false;
if (first_disconnect == false)
{
// When MQTT is connected, exit standalone mode
// In standalone mode, socket contactor is turned off
// A/C lock is set
// A/C temperature range limits are set
first_disconnect = true;
// Rebound the temperature
if(this->cards.ac->getTemperature() < this->get_ac_temp_lower_bound())
{
this->cards.ac->setTemperature(this->get_ac_temp_lower_bound());
}
else if(this->cards.ac->getTemperature() > this->get_ac_temp_upper_bound())
{
this->cards.ac->setTemperature(this->get_ac_temp_upper_bound());
}
this->refresh_display_ac();
}
}
last_mqtt_connected_check = millis();
}
}
void CUDDisplay::display_init()
@ -421,10 +472,9 @@ void CUDDisplay::handle_touch(uint8_t page_id, uint8_t element_id, uint8_t touch
}
newTemp++;
// Does the temperature exceed the upper bound?, if it does, bound it
ESP_LOGD("CUD Display", "Requested Temp: %d, Lower Bound: %d", newTemp, this->ac_temp_lower_bound.getIntValue());
if (newTemp > this->ac_temp_upper_bound.getIntValue())
if (newTemp > this->get_ac_temp_upper_bound())
{
newTemp = this->ac_temp_upper_bound.getIntValue();
newTemp = this->get_ac_temp_upper_bound();
}
ESP_LOGD("CUD Display", "New Temp: %d", newTemp);
// Increase the AC temperature
@ -452,10 +502,10 @@ void CUDDisplay::handle_touch(uint8_t page_id, uint8_t element_id, uint8_t touch
}
newTemp--;
// Does the temperature exceed the lower bound?, if it does, bound it
ESP_LOGD("CUD Display", "Requested Temp: %d, Lower Bound: %d", newTemp, this->ac_temp_lower_bound.getIntValue());
if (newTemp < this->ac_temp_lower_bound.getIntValue())
ESP_LOGD("CUD Display", "Requested Temp: %d, Lower Bound: %d", newTemp, this->get_ac_temp_lower_bound());
if (newTemp < this->get_ac_temp_lower_bound())
{
newTemp = this->ac_temp_lower_bound.getIntValue();
newTemp = this->get_ac_temp_lower_bound();
}
ESP_LOGD("CUD Display", "New Temp: %d", newTemp);
// Decrease the AC temperature
@ -510,7 +560,7 @@ void CUDDisplay::handle_aqi_change(char *value)
{
ESP_LOGV("CUD Display", "AQI Changed: %s", value);
// Update the AQI value on the display
uint8_t aqi = atoi(value);
uint16_t aqi = atoi(value);
this->takeSerialMutex();
this->displayAdapter->printf("%s.txt=\"%03d\"", LCD_DASHBOARD_ELEMENT_NAME_AQI_TEXT, (int)aqi);
this->sendStopBytes();
@ -519,31 +569,43 @@ void CUDDisplay::handle_aqi_change(char *value)
{
this->displayAdapter->printf("%s.pic=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_PICTURE, LCD_DASHBOARD_PIC_AQI_GOOD);
this->sendStopBytes();
this->displayAdapter->printf("%s.pco=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_TEXT, LCD_DASHBOARD_COLOR_AQI_TEXT_GOOD);
this->sendStopBytes();
}
else if (aqi <= 100)
{
this->displayAdapter->printf("%s.pic=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_PICTURE, LCD_DASHBOARD_PIC_AQI_MODERATE);
this->sendStopBytes();
this->displayAdapter->printf("%s.pco=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_TEXT, LCD_DASHBOARD_COLOR_AQI_TEXT_MODERATE);
this->sendStopBytes();
}
else if (aqi <= 150)
{
this->displayAdapter->printf("%s.pic=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_PICTURE, LCD_DASHBOARD_PIC_AQI_UNHEALTHY_FOR_SENSITIVE_GROUPS);
this->sendStopBytes();
this->displayAdapter->printf("%s.pco=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_TEXT, LCD_DASHBOARD_COLOR_AQI_TEXT_UNHEALTHY_FOR_SENSITIVE_GROUPS);
this->sendStopBytes();
}
else if (aqi <= 200)
{
this->displayAdapter->printf("%s.pic=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_PICTURE, LCD_DASHBOARD_PIC_AQI_UNHEALTHY);
this->sendStopBytes();
this->displayAdapter->printf("%s.pco=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_TEXT, LCD_DASHBOARD_COLOR_AQI_TEXT_UNHEALTHY);
this->sendStopBytes();
}
else if (aqi <= 300)
{
this->displayAdapter->printf("%s.pic=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_PICTURE, LCD_DASHBOARD_PIC_AQI_VERY_UNHEALTHY);
this->sendStopBytes();
this->displayAdapter->printf("%s.pco=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_TEXT, LCD_DASHBOARD_COLOR_AQI_TEXT_VERY_UNHEALTHY);
this->sendStopBytes();
}
else
{
this->displayAdapter->printf("%s.pic=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_PICTURE, LCD_DASHBOARD_PIC_AQI_HAZARDOUS);
this->sendStopBytes();
this->displayAdapter->printf("%s.pco=%d", LCD_DASHBOARD_ELEMENT_NAME_AQI_TEXT, LCD_DASHBOARD_COLOR_AQI_TEXT_HAZARDOUS);
this->sendStopBytes();
}
this->giveSerialMutex();
}
@ -581,6 +643,12 @@ void CUDDisplay::handle_ac_sensor(float temperature, float humidity)
// Not needed for now
}
void CUDDisplay::handle_mqtt_connected()
{
// Refresh the display
this->refresh_display_ac();
}
void CUDDisplay::set_display_light_state(uint8_t row, bool state)
{
this->takeSerialMutex();
@ -759,9 +827,10 @@ void CUDDisplay::refresh_display_allsystem()
this->giveSerialMutex();
}
void CUDDisplay::refresh_display_socket() {
void CUDDisplay::refresh_display_socket()
{
this->takeSerialMutex();
this->displayAdapter->printf("%s.pic=%d", LCD_DASHBOARD_ELEMENT_NAME_SOCKET_POWER, this->cards.outputCard->getState(this->conf->socket_contactor_pin) ? LCD_DASHBOARD_PIC_SOCKET_POWER_ON: LCD_DASHBOARD_PIC_SOCKET_POWER_OFF);
this->displayAdapter->printf("%s.pic=%d", LCD_DASHBOARD_ELEMENT_NAME_SOCKET_POWER, this->cards.outputCard->getState(this->conf->socket_contactor_pin) ? LCD_DASHBOARD_PIC_SOCKET_POWER_ON : LCD_DASHBOARD_PIC_SOCKET_POWER_OFF);
this->sendStopBytes();
this->giveSerialMutex();
}
@ -819,5 +888,27 @@ void CUDDisplay::set_ac_lock(bool state)
bool CUDDisplay::get_ac_lock()
{
if (standalone)
{
return false;
}
return (bool)ac_lock.getIntValue();
}
uint8_t CUDDisplay::get_ac_temp_lower_bound()
{
if (standalone)
{
return AC_MIN_TEMP;
}
return ac_temp_lower_bound.getIntValue();
}
uint8_t CUDDisplay::get_ac_temp_upper_bound()
{
if (standalone)
{
return AC_MAX_TEMP;
}
return ac_temp_upper_bound.getIntValue();
}

View File

@ -74,7 +74,6 @@ public:
void set_ac_lock(bool state);
bool get_ac_lock();
void loop();
private:
// States Calculation
bool get_lights_state();
@ -89,6 +88,7 @@ private:
void handle_touch(uint8_t page_id, uint8_t element_id, uint8_t touch_type);
void handle_lock_change(char* value);
void handle_bound_change(char* value);
void handle_mqtt_connected();
// Change Display Elements
void set_display_light_state(uint8_t row, bool state);
void set_display_light_all_state();
@ -108,6 +108,9 @@ private:
uint32_t last_ac_button_press;
uint8_t pending_temperature;
bool ac_button_press_pending;
bool standalone;
uint8_t get_ac_temp_lower_bound();
uint8_t get_ac_temp_upper_bound();
// Local Variables
cud_display_conf_t *conf;
cud_display_cards_t cards;

View File

@ -103,6 +103,12 @@
// AQI
#define LCD_DASHBOARD_ELEMENT_AQI_TEXT 3
#define LCD_DASHBOARD_COLOR_AQI_TEXT_GOOD 29767
#define LCD_DASHBOARD_COLOR_AQI_TEXT_MODERATE 41956
#define LCD_DASHBOARD_COLOR_AQI_TEXT_UNHEALTHY_FOR_SENSITIVE_GROUPS 45764
#define LCD_DASHBOARD_COLOR_AQI_TEXT_UNHEALTHY 43367
#define LCD_DASHBOARD_COLOR_AQI_TEXT_VERY_UNHEALTHY 25134
#define LCD_DASHBOARD_COLOR_AQI_TEXT_HAZARDOUS 27114
#define LCD_DASHBOARD_ELEMENT_NAME_AQI_TEXT "txt_aqi"
#define LCD_DASHBOARD_ELEMENT_AQI_PICTURE 2
#define LCD_DASHBOARD_ELEMENT_NAME_AQI_PICTURE "ico_aqi"