antenna-switcher-client 0.5.0
Async C++17 client for the antenna-switcher device over the ESPHome native API
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <cstdint>
12#include <functional>
13#include <memory>
14#include <string>
15#include <vector>
16
18
21enum class Channel { One = 1, Two = 2 };
22
24enum class TimeUnit { Ms, Us };
25
27enum class Mode { Manual, Auto, Plan, Unknown };
28
30struct PlanStep {
31 enum class Kind { Input, Delay } kind = Kind::Input;
32 int input = 0;
33 int delay = 0;
35
37 static PlanStep input_step(const int n) {
38 return PlanStep{Kind::Input, n, 0, TimeUnit::Ms};
39 }
41 static PlanStep delay_step(const int value, const TimeUnit unit = TimeUnit::Ms) {
42 return PlanStep{Kind::Delay, 0, value, unit};
43 }
44};
45
48 int bearing = 0;
49 int activeInput = 0;
50 int angleOffset = 0;
51 long intervalUs = 0;
53 std::vector<int> activeInputs;
54};
55
57struct Options {
58 std::string host;
59 std::uint16_t port = 6053;
60 std::string noise_psk;
61 std::string client_info = "antenna-switcher-client";
62};
63
70public:
72 using StateCallback = std::function<void(Channel, const ChannelState&)>;
73
74 explicit AntennaSwitcher(Options opts);
76
79
83 void connect() const;
85 void disconnect() const;
86 [[nodiscard]] bool isConnected() const;
87
89 void setInput(Channel channel, int input) const;
92 void startAuto(Channel channel,
93 int interval,
94 TimeUnit unit,
95 const std::vector<int>& inputs = {}) const;
97 void runPlan(Channel channel, const std::vector<PlanStep>& steps, bool repeat = false) const;
99 void stop(Channel channel) const;
101 void setAngleOffset(Channel channel, int degrees) const;
102
104 [[nodiscard]] ChannelState state(Channel channel) const;
107
108private:
109 void shutdown_worker() const;
110
111 struct Impl;
112 std::unique_ptr<Impl> impl_;
113};
114
117namespace detail {
118
119std::string build_set_input(int input);
120std::string build_start_auto(int interval, TimeUnit unit, const std::vector<int>& inputs);
121std::string build_run_plan(const std::vector<PlanStep>& steps, bool repeat);
122std::string build_stop();
123
124} // namespace detail
125
126} // namespace antenna_switcher
Async, event-driven controller for the antenna-switcher device.
Definition client.hpp:69
void runPlan(Channel channel, const std::vector< PlanStep > &steps, bool repeat=false) const
Run an ordered plan on channel, optionally repeating.
void onStateChanged(StateCallback cb) const
Register a state-change listener (invoked on the loop thread).
void connect() const
Connect, enumerate entities, and resolve the per-channel handles.
void setInput(Channel channel, int input) const
Select input input (1..10) on channel (sends set:N).
void stop(Channel channel) const
Stop any auto/plan activity on channel (sends stop).
void setAngleOffset(Channel channel, int degrees) const
Set the compass angle offset for input 1 (degrees, written to the number entity).
ChannelState state(Channel channel) const
Thread-safe snapshot of channel's latest state.
AntennaSwitcher(const AntennaSwitcher &)=delete
AntennaSwitcher & operator=(const AntennaSwitcher &)=delete
void startAuto(Channel channel, int interval, TimeUnit unit, const std::vector< int > &inputs={}) const
Start auto-cycling channel with the given interval.
void disconnect() const
Gracefully disconnect and stop the worker thread.
std::function< void(Channel, const ChannelState &)> StateCallback
Called with the channel and its new state after each state update.
Definition client.hpp:72
Definition client.hpp:17
Channel
One of the two switchers on the device.
Definition client.hpp:21
TimeUnit
Time unit for auto-cycle intervals and plan delays.
Definition client.hpp:24
Mode
Operating mode reported by the device.
Definition client.hpp:27
A snapshot of one channel's live state.
Definition client.hpp:47
int angleOffset
Compass offset for input 1, in degrees (0..359).
Definition client.hpp:50
int activeInput
Currently selected input (1..10), 0 if unknown.
Definition client.hpp:49
std::vector< int > activeInputs
Inputs in the current auto cycle (CSV-parsed).
Definition client.hpp:53
Mode mode
Manual / auto / plan.
Definition client.hpp:52
long intervalUs
Auto-cycle interval in microseconds.
Definition client.hpp:51
int bearing
Current compass bearing in degrees.
Definition client.hpp:48
Everything needed to reach and authenticate with the device.
Definition client.hpp:57
std::string client_info
Identification sent in HelloRequest.
Definition client.hpp:61
std::string host
Device hostname or IP.
Definition client.hpp:58
std::uint16_t port
ESPHome native API port.
Definition client.hpp:59
std::string noise_psk
base64 Noise key (api.encryption.key); empty ⇒ plaintext.
Definition client.hpp:60
One step of a plan: either switch to an input, or wait for a delay.
Definition client.hpp:30
enum antenna_switcher::PlanStep::Kind kind
TimeUnit unit
Definition client.hpp:34
int input
Definition client.hpp:32
Kind
Definition client.hpp:31
int delay
Definition client.hpp:33
static PlanStep input_step(const int n)
A step that selects input n (1..10).
Definition client.hpp:37
static PlanStep delay_step(const int value, const TimeUnit unit=TimeUnit::Ms)
A step that waits value in unit.
Definition client.hpp:41