ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
commands.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5
7
9
10class DfrobotSen0395Component;
11
12// Use command queue and time stamps to avoid blocking.
13// When component has run time, check if minimum time (1s) between
14// commands has passed. After that run a command from the queue.
15class Command {
16 public:
17 virtual ~Command() = default;
18 virtual uint8_t execute(DfrobotSen0395Component *parent);
19 virtual uint8_t on_message(std::string &message) = 0;
20
21 protected:
23 std::string cmd_;
24 bool cmd_sent_{false};
25 int8_t retries_left_{2};
28};
29
30class ReadStateCommand : public Command {
31 public:
33 uint8_t execute(DfrobotSen0395Component *parent) override;
34 uint8_t on_message(std::string &message) override;
35};
36
37class PowerCommand : public Command {
38 public:
39 PowerCommand(bool power_on) : power_on_(power_on) {
40 if (power_on) {
41 cmd_ = "sensorStart";
42 } else {
43 cmd_ = "sensorStop";
44 }
45 };
46 uint8_t on_message(std::string &message) override;
47
48 protected:
50};
51
53 public:
54 DetRangeCfgCommand(float min1, float max1, float min2, float max2, float min3, float max3, float min4, float max4);
55 uint8_t on_message(std::string &message) override;
56
57 protected:
59 // TODO: Set min max values in component, so they can be published as sensor.
60};
61
62class SetLatencyCommand : public Command {
63 public:
64 SetLatencyCommand(float delay_after_detection, float delay_after_disappear);
65 uint8_t on_message(std::string &message) override;
66
67 protected:
70};
71
73 public:
74 SensorCfgStartCommand(bool startup_mode) : startup_mode_(startup_mode) {
75 char tmp_cmd[20]; // "sensorCfgStart " (15) + "0/1" (1) + null = 17
76 buf_append_printf(tmp_cmd, sizeof(tmp_cmd), 0, "sensorCfgStart %d", startup_mode);
77 cmd_ = std::string(tmp_cmd);
78 }
79 uint8_t on_message(std::string &message) override;
80
81 protected:
83};
84
86 public:
87 FactoryResetCommand() { cmd_ = "factoryReset 0x45670123 0xCDEF89AB 0x956128C6 0xDF54AC89"; };
88 uint8_t on_message(std::string &message) override;
89};
90
92 public:
93 ResetSystemCommand() { cmd_ = "resetSystem"; }
94 uint8_t on_message(std::string &message) override;
95};
96
97class SaveCfgCommand : public Command {
98 public:
100 cmd_ = "saveCfg 0x45670123 0xCDEF89AB 0x956128C6 0xDF54AC89";
101 cmd_duration_ms_ = 3000;
102 timeout_ms_ = 3500;
103 }
104 uint8_t on_message(std::string &message) override;
105};
106
107class LedModeCommand : public Command {
108 public:
109 LedModeCommand(bool active) : active_(active) {
110 if (active) {
111 cmd_ = "setLedMode 1 0";
112 } else {
113 cmd_ = "setLedMode 1 1";
114 }
115 };
116 uint8_t on_message(std::string &message) override;
117
118 protected:
120};
121
123 public:
124 UartOutputCommand(bool active) : active_(active) {
125 if (active) {
126 cmd_ = "setUartOutput 1 1";
127 } else {
128 cmd_ = "setUartOutput 1 0";
129 }
130 };
131 uint8_t on_message(std::string &message) override;
132
133 protected:
135};
136
138 public:
139 SensitivityCommand(uint8_t sensitivity) : sensitivity_(sensitivity) {
140 if (sensitivity > 9)
141 sensitivity_ = sensitivity = 9;
142 char tmp_cmd[20]; // "setSensitivity " (15) + "0-9" (1) + null = 17
143 buf_append_printf(tmp_cmd, sizeof(tmp_cmd), 0, "setSensitivity %d", sensitivity);
144 cmd_ = std::string(tmp_cmd);
145 };
146 uint8_t on_message(std::string &message) override;
147
148 protected:
150};
151
152} // namespace esphome::dfrobot_sen0395
DfrobotSen0395Component * parent_
Definition commands.h:22
virtual uint8_t on_message(std::string &message)=0
virtual uint8_t execute(DfrobotSen0395Component *parent)
Definition commands.cpp:13
DetRangeCfgCommand(float min1, float max1, float min2, float max2, float min3, float max3, float min4, float max4)
Definition commands.cpp:111
uint8_t on_message(std::string &message) override
Definition commands.cpp:182
uint8_t on_message(std::string &message) override
Definition commands.cpp:252
uint8_t on_message(std::string &message) override
Definition commands.cpp:282
uint8_t on_message(std::string &message) override
Definition commands.cpp:86
uint8_t on_message(std::string &message) override
Definition commands.cpp:84
uint8_t execute(DfrobotSen0395Component *parent) override
Definition commands.cpp:64
uint8_t on_message(std::string &message) override
Definition commands.cpp:263
uint8_t on_message(std::string &message) override
Definition commands.cpp:271
uint8_t on_message(std::string &message) override
Definition commands.cpp:320
uint8_t on_message(std::string &message) override
Definition commands.cpp:233
SetLatencyCommand(float delay_after_detection, float delay_after_disappear)
Definition commands.cpp:206
uint8_t on_message(std::string &message) override
Definition commands.cpp:217
uint8_t on_message(std::string &message) override
Definition commands.cpp:301
const LogString * message
Definition component.cpp:35
static void uint32_t