SensESP 3.4.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
async_response_handler.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_SENSESP_SIGNALK_SYSTEM_ASYNC_RESPONSE_HANDLER_H_
2#define SENSESP_SRC_SENSESP_SIGNALK_SYSTEM_ASYNC_RESPONSE_HANDLER_H_
3
4#include "sensesp.h"
5
6#include <Arduino.h>
7
8#include "sensesp_base_app.h"
9#include "valueproducer.h"
10
11namespace sensesp {
12
14 kReady,
19};
20
31 public ValueProducer<AsyncResponseStatus> {
32 public:
36 : ValueConsumer<bool>(),
38 timeout_{timeout} {}
39
45 void activate() {
46 ESP_LOGV("AsyncResponseHandler", "Activating response handler");
48 this->emit(status_);
49
50 if (timeout_event_ != nullptr) {
51 event_loop()->remove(timeout_event_);
52 timeout_event_ = nullptr;
53 }
55 event_loop()->onDelay(timeout_, [this]() {
57 ESP_LOGV("AsyncResponseHandler", "Timeout");
59 this->emit(status_);
60 }
61 this->timeout_event_ = nullptr;
62 });
63 }
64
65 void set(const bool& success) override {
66 ESP_LOGV("AsyncResponseHandler", "status: %d", status_);
68 ESP_LOGV("AsyncResponseHandler",
69 "Received response when not in pending state");
70 return;
71 }
72
73 // Clear the timeout event
74 if (timeout_event_ != nullptr) {
75 event_loop()->remove(timeout_event_);
76 timeout_event_ = nullptr;
77 }
78
79 ESP_LOGV("AsyncResponseHandler", "Received response: %d", success);
80
81 if (success) {
83 } else {
85 }
86 this->emit(status_);
87 }
88
89 const AsyncResponseStatus get_status() const { return status_; }
90
91 protected:
92 reactesp::DelayEvent* timeout_event_ = nullptr;
94 int timeout_ = 3000; // Default timeout in ms
95};
96
97} // namespace sensesp
98
99#endif // SENSESP_SRC_SENSESP_SIGNALK_SYSTEM_ASYNC_RESPONSE_HANDLER_H_
Handle async command responses from remote systems.
void set(const bool &success) override
void activate()
Activate must be called when a command is sent to the remote system.
const AsyncResponseStatus get_status() const
reactesp::DelayEvent * timeout_event_
A base class for piece of code (like a transform) that accepts data for input. ValueConsumers can acc...
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(const AsyncResponseStatus &new_value)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9