Skip to content

Commit

Permalink
Issue #2 correction
Browse files Browse the repository at this point in the history
  • Loading branch information
echavet committed Jan 15, 2024
1 parent aadc792 commit 950e36c
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 89 deletions.
75 changes: 75 additions & 0 deletions components/cn105/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,56 @@ struct heatpumpSettings {
const char* wideVane; //horizontal vane, left/right
bool iSee; //iSee sensor, at the moment can only detect it, not set it
bool connected;

heatpumpSettings& operator=(const heatpumpSettings& other) {
if (this != &other) { // protection contre l'auto-affectation
power = other.power;
mode = other.mode;
temperature = other.temperature;
fan = other.fan;
vane = other.vane;
wideVane = other.wideVane;
iSee = other.iSee;
connected = other.connected;
}
return *this;
}


bool operator==(const heatpumpSettings& other) const {
return power == other.power &&
mode == other.mode &&
temperature == other.temperature &&
fan == other.fan &&
vane == other.vane &&
wideVane == other.wideVane &&
iSee == other.iSee;
}

bool operator!=(const heatpumpSettings& other) {
return !(this->operator==(other));
}

};

struct wantedHeatpumpSettings : heatpumpSettings {
bool hasChanged;
wantedHeatpumpSettings& operator=(const wantedHeatpumpSettings& other) {
if (this != &other) { // protection contre l'auto-affectation
heatpumpSettings::operator=(other); // Appel à l'opérateur d'affectation de la classe de base
hasChanged = other.hasChanged;
}
return *this;
}
wantedHeatpumpSettings& operator=(const heatpumpSettings& other) {
if (this != &other) { // protection contre l'auto-affectation
heatpumpSettings::operator=(other); // Copie des membres de base
// `hasBeenUpdated` peut être réglé selon la logique de votre application
// Par exemple, le mettre à true pour indiquer que l'objet a été mis à jour
hasChanged = true;
}
return *this;
}
};

struct heatpumpTimers {
Expand All @@ -117,8 +167,33 @@ struct heatpumpTimers {
int onMinutesRemaining;
int offMinutesSet;
int offMinutesRemaining;

heatpumpTimers& operator=(const heatpumpTimers& other) {
if (this != &other) { // protection contre l'auto-affectation
mode = other.mode;
onMinutesSet = other.onMinutesSet;
onMinutesRemaining = other.onMinutesRemaining;
offMinutesSet = other.offMinutesSet;
offMinutesRemaining = other.offMinutesRemaining;
}
return *this;
}
bool operator==(const heatpumpTimers& other) {
return
mode == other.mode &&
onMinutesSet == other.onMinutesSet &&
onMinutesRemaining == other.onMinutesRemaining &&
offMinutesSet == other.offMinutesSet &&
offMinutesRemaining == other.offMinutesRemaining;
}


bool operator!=(const heatpumpTimers& other) {
return !(this->operator==(other));
}
};


struct heatpumpStatus {
float roomTemperature;
bool operating; // if true, the heatpump is operating to reach the desired temperature
Expand Down
19 changes: 15 additions & 4 deletions components/cn105/climateControls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ void CN105Climate::checkPendingWantedSettings() {
(this->wantedSettings.power != this->currentSettings.power) ||
(this->wantedSettings.temperature != this->currentSettings.temperature) ||
(this->wantedSettings.vane != this->currentSettings.vane)) {
this->sendWantedSettings();
if (this->wantedSettings.hasChanged) {
ESP_LOGD(TAG, "checkPendingWantedSettings - wanted settings have changed, sending them to the heatpump...");
this->sendWantedSettings();
} else {
ESP_LOGI(TAG, "checkPendingWantedSettings - detected a change from IR Remote Control, update the wanted settings...");
// if not wantedSettings.hasChanged this is because we've had a change from IR Remote Control
this->wantedSettings = this->currentSettings;
}

}

}

//#region climate
Expand Down Expand Up @@ -59,14 +68,16 @@ void CN105Climate::control(const esphome::climate::ClimateCall& call) {


if (updated) {
this->wantedSettings.hasChanged = true;
// we don't call sendWantedSettings() anymore because it will be called by the loop() method
// just because we changed something doesn't mean we want to send it to the heatpump right away
//ESP_LOGD(TAG, "User changed something, sending change to heatpump...");
//this->sendWantedSettings();
//this->sendWantedSettings();
}

// send the update back to esphome:
this->publish_state();
// send the update back to esphome: this will update the UI
// this is not necessary because state will be published after the response and the call off settingsChanged() in the updateSuccess() method
// this->publish_state();
}
void CN105Climate::controlSwing() {
switch (this->swing_mode) {
Expand Down
4 changes: 2 additions & 2 deletions components/cn105/cn105.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class CN105Climate : public climate::Climate, public Component {
void processDataPacket();
void getDataFromResponsePacket();
void programUpdateInterval();

void updateSuccess();
void processCommand();
bool checkSum();
byte checkSum(byte bytes[], int len);
Expand Down Expand Up @@ -168,7 +168,7 @@ class CN105Climate : public climate::Climate, public Component {
void createPacket(byte* packet, heatpumpSettings settings);
void createInfoPacket(byte* packet, byte packetType);
heatpumpSettings currentSettings{};
heatpumpSettings wantedSettings{};
wantedHeatpumpSettings wantedSettings{};


unsigned long lastResponseMs;
Expand Down
76 changes: 41 additions & 35 deletions components/cn105/hp_readings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ void CN105Climate::getDataFromResponsePacket() {
heatpumpStatus receivedStatus{};
heatpumpSettings receivedSettings{};

//receivedSettings.temperature = -1; // -1 means not set
receivedStatus.roomTemperature = -1; // -1 means not set
//receivedStatus.compressorFrequency = -1; // -1 means not set


bool statusDidChange = false;

Expand All @@ -149,7 +147,7 @@ void CN105Climate::getDataFromResponsePacket() {
int temp = data[11];
temp -= 128;
receivedSettings.temperature = (float)temp / 2;
tempMode = true;
this->tempMode = true;
ESP_LOGD("Decoder", "tempMode is true");
} else {
receivedSettings.temperature = lookupByteMapValue(TEMP_MAP, TEMP, 16, data[5]);
Expand Down Expand Up @@ -203,6 +201,10 @@ void CN105Climate::getDataFromResponsePacket() {
}
ESP_LOGD("Decoder", "[Room °C: %f]", receivedStatus.roomTemperature);

// no change with this packet to currentStatus for operating and compressorFrequency
receivedStatus.operating = currentStatus.operating;
receivedStatus.compressorFrequency = currentStatus.compressorFrequency;

statusDidChange = true;

}
Expand All @@ -227,19 +229,14 @@ void CN105Climate::getDataFromResponsePacket() {

// reset counter (because a reply indicates it is connected)
this->nonResponseCounter = 0;


receivedStatus.operating = data[4];
receivedStatus.compressorFrequency = data[3];
currentStatus.operating = receivedStatus.operating;
currentStatus.compressorFrequency = receivedStatus.compressorFrequency;

this->compressor_frequency_sensor->publish_state(currentStatus.compressorFrequency);
statusDidChange = true;
// no change with this packet to roomTemperature
receivedStatus.roomTemperature = currentStatus.roomTemperature;

ESP_LOGD("Decoder", "[Operating: %d]", currentStatus.operating);
ESP_LOGD("Decoder", "[Compressor Freq: %d]", currentStatus.compressorFrequency);

statusDidChange = true;
// RCVD_PKT_STATUS;
}
break;
Expand Down Expand Up @@ -273,40 +270,43 @@ void CN105Climate::getDataFromResponsePacket() {
}

if (statusDidChange) {
this->debugStatus("received", receivedStatus);
this->debugStatus("current", currentStatus);
this->statusChanged(receivedStatus);
}
}

void CN105Climate::updateSuccess() {
ESP_LOGI(TAG, "Last heatpump data update successful!");
//this->last_received_packet_sensor->publish_state("0x61: update success");
// as the update was successful, we can set currentSettings to wantedSettings
// even if the next settings request will do the same.
this->wantedSettings.hasChanged = false;
this->settingsChanged(this->wantedSettings);

/*this->currentSettings.power = this->wantedSettings.power;
this->currentSettings.mode = this->wantedSettings.mode;
this->currentSettings.fan = this->wantedSettings.fan;
this->currentSettings.vane = this->wantedSettings.vane;
//this->currentSettings.wideVane = this->wantedSettings.wideVane;
this->currentSettings.temperature = this->wantedSettings.temperature;*/

if (!this->autoUpdate) {
this->buildAndSendRequestsInfoPackets();
}
}

void CN105Climate::processCommand() {
switch (this->command) {
case 0x61: /* last update was successful */
ESP_LOGI(TAG, "Last heatpump data update successfull!");
//this->last_received_packet_sensor->publish_state("0x61: update success");
// as the updat was successfull, we can set currentSettings to wantedSettings
// even if the next settings request will do the same.
this->currentSettings.power = this->wantedSettings.power;
this->currentSettings.mode = this->wantedSettings.mode;
this->currentSettings.fan = this->wantedSettings.fan;
this->currentSettings.vane = this->wantedSettings.vane;
//this->currentSettings.wideVane = this->wantedSettings.wideVane;
this->currentSettings.temperature = this->wantedSettings.temperature;

if (!this->autoUpdate) {
this->buildAndSendRequestsInfoPackets();
}

this->updateSuccess();
break;

case 0x62: /* packet contains data (room °C, settings, timer, status, or functions...)*/
this->getDataFromResponsePacket();

break;
case 0x7a:
ESP_LOGI(TAG, "--> Heatpump did reply: connection success! <--");
this->isHeatpumpConnected_ = true;
//this->last_received_packet_sensor->publish_state("0x7A: Connection success");

programUpdateInterval(); // we know a check in this method is done on autoupdate value
break;
default:
Expand All @@ -316,11 +316,17 @@ void CN105Climate::processCommand() {


void CN105Climate::statusChanged(heatpumpStatus status) {
if (status.roomTemperature != -1) {
currentStatus.roomTemperature = status.roomTemperature;
this->current_temperature = currentStatus.roomTemperature;
}

this->debugStatus("received", status);
this->debugStatus("current", currentStatus);

currentStatus.operating = status.operating;
currentStatus.compressorFrequency = status.compressorFrequency;
currentStatus.roomTemperature = status.roomTemperature;
this->current_temperature = currentStatus.roomTemperature;

this->publish_state();
this->compressor_frequency_sensor->publish_state(currentStatus.compressorFrequency);
}


Expand Down
48 changes: 0 additions & 48 deletions components/cn105/operators.cpp

This file was deleted.

0 comments on commit 950e36c

Please sign in to comment.