#include ClimateIoT::ClimateIoT() { } ClimateIoT::~ClimateIoT() { // Destructor implementation } bool ClimateIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) { this->card = (ClimateCard *)card; // Reister Callbacks auto bindedSensorCallback = std::bind(&ClimateIoT::handleSensorUpdate, this, std::placeholders::_1, std::placeholders::_2); this->card->registerSensorCallback(bindedSensorCallback); auto bindedAirConditionerCallback = std::bind(&ClimateIoT::handleAirConditionerUpdate, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); this->card->registerChangeCallback(bindedAirConditionerCallback); return true; } void ClimateIoT::handleMqttMessage(char *topic, char *payload) { uint8_t topic_length = strlen(topic); if (this->processSetTemperatureMessage(topic, payload, topic_length)) return; if (this->processSetModeMessage(topic, payload, topic_length)) return; if (this->processSetFanSpeedMessage(topic, payload, topic_length)) return; if (this->processRequestStateMessage(topic, payload, topic_length)) return; } void ClimateIoT::publishClimateTemperature() { char payload[5]; itoa(this->card->getTemperature(), payload, 10); this->publishRelative(AC_TEMPERATURE_REPORT_TOPIC, payload); } void ClimateIoT::publishClimateMode() { char payload[2]; itoa(this->card->getMode(), payload, 10); this->publishRelative(AC_MODE_REPORT_TOPIC, payload); } void ClimateIoT::publishClimateFanSpeed() { char payload[2]; itoa(this->card->getFanSpeed(), payload, 10); this->publishRelative(AC_FAN_SPEED_REPORT_TOPIC, payload); } void ClimateIoT::publishSensor() { this->publishRoomTemperature(); this->publishHumidity(); } void ClimateIoT::publishClimate() { this->publishClimateTemperature(); this->publishClimateMode(); this->publishClimateFanSpeed(); } void ClimateIoT::publishRoomTemperature() { char payload[5]; itoa(this->card->getRoomTemperature(), payload, 10); this->publishRelative(AC_ROOM_TEMPERATURE_REPORT_TOPIC, payload); } void ClimateIoT::publishHumidity() { if (this->card->getSensorType() == AC_SENSOR_TYPE_DHT22) { char payload[5]; itoa(this->card->getHumidity(), payload, 10); this->publishRelative(AC_HUMIDITY_REPORT_TOPIC, payload); } } void ClimateIoT::handleStateChange(uint8_t temperature, uint8_t mode, uint8_t fan_speed) { this->publishClimate(); } void ClimateIoT::publishReport() { this->publishClimate(); this->publishSensor(); } void ClimateIoT::subscribe() { this->subscribeRelative(AC_TEMPERATURE_SET_TOPIC); this->subscribeRelative(AC_MODE_SET_TOPIC); this->subscribeRelative(AC_FAN_SPEED_SET_TOPIC); } void ClimateIoT::loop() { } uint8_t ClimateIoT::getType() { return CARD_TYPE_CLIMATE; } bool ClimateIoT::processSetTemperatureMessage(char *topic, char *payload, uint8_t topic_length) { if (!strcmp(topic, AC_TEMPERATURE_SET_TOPIC)) { uint8_t temperature = atoi(payload); this->card->setTemperature(temperature); return true; } return false; } bool ClimateIoT::processSetModeMessage(char *topic, char *payload, uint8_t topic_length) { if (!strcmp(topic, AC_MODE_SET_TOPIC)) { uint8_t mode = atoi(payload); this->card->setMode(mode); return true; } return false; } bool ClimateIoT::processSetFanSpeedMessage(char *topic, char *payload, uint8_t topic_length) { if (!strcmp(topic, AC_FAN_SPEED_SET_TOPIC)) { uint8_t fan_speed = atoi(payload); this->card->setFanSpeed(fan_speed); return true; } return false; } bool ClimateIoT::processRequestStateMessage(char *topic, char *payload, uint8_t topic_length) { if (!strcmp(topic, AC_REQUEST_STATE_TOPIC)) { this->publishReport(); return true; } return false; }