ihormelnyk/opentherm_library

Add method getLastResponse

Laxilef opened this issue · 3 comments

Hello.
Please add the getLastResponse method to the OpenTherm class:

unsigned long OpenTherm::getLastResponse()
{
	return response;
}

This is necessary to change the class to work in "async" mode (ESP8266Scheduler).
Now response is private and it's impossible to get its value by inheritance. Without these changes, the sendRequest method will block other tasks while waiting for a response.

My example:

#include <OpenTherm.h>

class CustomOpenTherm : public OpenTherm {
private:
  unsigned long send_ts = millis();
  void(*handleSendRequestCallback)(unsigned long, unsigned long, OpenThermResponseStatus status, byte attempt);
  
public:
  CustomOpenTherm(int inPin = 4, int outPin = 5, bool isSlave = false) : OpenTherm(inPin, outPin, isSlave) {}
  void setHandleSendRequestCallback(void(*handleSendRequestCallback)(unsigned long, unsigned long, OpenThermResponseStatus status, byte attempt)){
    this->handleSendRequestCallback = handleSendRequestCallback;
  }
  
  unsigned long sendRequest(unsigned long request, byte attempts = 5, byte _attempt = 0) {
    _attempt++;
    while (send_ts > 0 && millis() - send_ts < 200) {
      Scheduler.yield();
    }
    
    //unsigned long response = OpenTherm::sendRequest(request);
    unsigned long _response;
    if (!sendRequestAync(request)) {
      _response = 0;
    } else {
      while (!isReady()) {
        Scheduler.yield();
        process();
      }

      _response = getLastResponse();
    }

    if (handleSendRequestCallback != NULL) {
      handleSendRequestCallback(request, _response, getLastResponseStatus(), _attempt);
    }
    
    send_ts = millis();
    if ( getLastResponseStatus() == OpenThermResponseStatus::SUCCESS || _attempt >= attempts ) {
      return _response;
    } else {
      return sendRequest(request, attempts, _attempt);
    }
  }
};

Don't expect an answer any time soon. Ihor is Ukrainian, so he's probably got other things on his mind right now.

Done, thanks for your contribution.

I want to publish a project that uses your library. In order not to explain to users that they need to download the master branch, can you publish a new version with the changes? Thank you.