SensESP 3.0.0-beta.6
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#include <elapsedMillis.h>
8
9#include "sensesp_base_app.h"
10#include "valueproducer.h"
11
12namespace sensesp {
13
15 kReady,
20};
21
32 public ValueProducer<AsyncResponseStatus> {
33 public:
37 : ValueConsumer<bool>(),
39 timeout_{timeout} {}
40
46 void activate() {
47 ESP_LOGV("AsyncResponseHandler", "Activating response handler");
50 this->emit(status_);
51
52 if (timeout_event_ != nullptr) {
53 event_loop()->remove(timeout_event_);
54 timeout_event_ = nullptr;
55 }
57 event_loop()->onDelay(timeout_, [this]() {
59 ESP_LOGV("AsyncResponseHandler", "Timeout");
61 this->emit(status_);
62 }
63 this->timeout_event_ = nullptr;
64 });
65 }
66
67 void set(const bool& success) override {
68 ESP_LOGV("AsyncResponseHandler", "status: %d", status_);
70 ESP_LOGV("AsyncResponseHandler",
71 "Received response when not in pending state");
72 return;
73 }
74
75 // Clear the timeout event
76 if (timeout_event_ != nullptr) {
77 event_loop()->remove(timeout_event_);
78 timeout_event_ = nullptr;
79 }
80
81 ESP_LOGV("AsyncResponseHandler", "Received response: %d", success);
82
83 if (success) {
85 } else {
87 }
88 this->emit(status_);
89 }
90
91 const AsyncResponseStatus get_status() const { return status_; }
92
93 protected:
94 reactesp::DelayEvent* timeout_event_ = nullptr;
97 int timeout_ = 3000; // Default timeout in ms
98 elapsedMillis elapsed_millis_;
99};
100
101} // namespace sensesp
102
103#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)
reactesp::EventLoop * event_loop()
Definition sensesp.cpp:7