Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce class is not compatible with ConfigItem - SensESP v3.0.0 #796

Closed
kinyo666 opened this issue Dec 8, 2024 · 1 comment · Fixed by #798
Closed

Debounce class is not compatible with ConfigItem - SensESP v3.0.0 #796

kinyo666 opened this issue Dec 8, 2024 · 1 comment · Fixed by #798

Comments

@kinyo666
Copy link

kinyo666 commented Dec 8, 2024

The Debounce class needs to be rewritten for ConfigItem compatibility (SensESP >= 3.0.0).
https://signalk.org/SensESP/generated/docs/debounce_8h_source.html

1. Functions to_json() and from_json() MUST be public :

template <class T>
class Debounce : public SymmetricTransform<T> {
 public:
  Debounce(int ms_min_delay = 15, String config_path = "")
      : SymmetricTransform<T>(config_path), ms_min_delay_{ms_min_delay} {
    this->load();
  }

  virtual void set(const T& input) override {
    // Input has changed since the last emit, or this is the first
    // input since the program started to run.

    if (input != debounced_value_ || !value_received_) {
      debounced_value_ = input;

      if (event_) {
        event_->remove(event_loop());
        event_ = nullptr;
      }
      event_ = event_loop()->onDelay(ms_min_delay_, [this, input]() {
        this->event_ = nullptr;
        this->debounced_value_ = input;
        this->emit(input);
      });
      value_received_ = true;
    }
  }

  virtual bool to_json(JsonObject& doc) override {
    doc["min_delay"] = ms_min_delay_;
    return true;
  }

  virtual bool from_json(const JsonObject& config) override {
    const String expected[] = {"min_delay"};
    for (auto str : expected) {
      if (!config[str].is<JsonVariant>()) {
        return false;
      }
    }
    ms_min_delay_ = config["min_delay"];
    return true;
  }

 private:
  int ms_min_delay_;
  bool value_received_ = false;
  T debounced_value_;
  reactesp::DelayEvent* event_ = nullptr;
/*
  virtual bool to_json(JsonObject& doc) override {
    doc["min_delay"] = ms_min_delay_;
    return true;
  }
 
  virtual bool from_json(const JsonObject& config) override {
    const String expected[] = {"min_delay"};
    for (auto str : expected) {
      if (!config[str].is<JsonVariant>()) {
        return false;
      }
    }
    ms_min_delay_ = config["min_delay"];
    return true;
  }
*/
};

2. ConfigSchema MUST be a template and not only a Debounce<bool> :

//const String ConfigSchema(const Debounce<bool>& obj);
template <typename T>
const String ConfigSchema(const Debounce<T>& obj) {
  return R"({
    "type": "object",
    "properties": {
      "min_delay": {
        "title": "Minimum delay",
        "type": "number",
        "description": "The minimum time in ms between inputs for output to happen"
      }
    }
  })";
}

3. debounce.cpp SHOULD be removed as it remains nothing in this file :

/*
#include "debounce.h"

namespace sensesp {

const String ConfigSchema(const Debounce<bool>& obj) {
  return R"###({"type":"object","properties":{"min_delay":{"title":"Minimum delay","type":"number","description":"The minimum time in ms between inputs for output to happen"}}})###";
}

}  // namespace sensesp
*/
@mairas
Copy link
Collaborator

mairas commented Dec 12, 2024

Thanks! Seems like someone has done a sloppy job...

Could you have a look at PR #798?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants