ESPHome 2026.5.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
8namespace esphome {
9namespace dfrobot_sen0395 {
10
11class DfrobotSen0395Component;
12
13// Use command queue and time stamps to avoid blocking.
14// When component has run time, check if minimum time (1s) between
15// commands has passed. After that run a command from the queue.
16class Command {
17 public:
18 virtual ~Command() = default;
19 virtual uint8_t execute(DfrobotSen0395Component *parent);
20 virtual uint8_t on_message(std::string &message) = 0;
21
22 protected:
24 std::string cmd_;
25 bool cmd_sent_{false};
26 int8_t retries_left_{2};
29};
30
31class ReadStateCommand : public Command {
32 public:
34 uint8_t execute(DfrobotSen0395Component *parent) override;
35 uint8_t on_message(std::string &message) override;
36};
37
38class PowerCommand : public Command {
39 public:
40 PowerCommand(bool power_on) : power_on_(power_on) {
41 if (power_on) {
42 cmd_ = "sensorStart";
43 } else {
44 cmd_ = "sensorStop";
45 }
46 };
47 uint8_t on_message(std::string &message) override;
48
49 protected:
51};
52
54 public:
55 DetRangeCfgCommand(float min1, float max1, float min2, float max2, float min3, float max3, float min4, float max4);
56 uint8_t on_message(std::string &message) override;
57
58 protected:
60 // TODO: Set min max values in component, so they can be published as sensor.
61};
62
63class SetLatencyCommand : public Command {
64 public:
65 SetLatencyCommand(float delay_after_detection, float delay_after_disappear);
66 uint8_t on_message(std::string &message) override;
67
68 protected:
71};
72
74 public:
75 SensorCfgStartCommand(bool startup_mode) : startup_mode_(startup_mode) {
76 char tmp_cmd[20]; // "sensorCfgStart " (15) + "0/1" (1) + null = 17
77 buf_append_printf(tmp_cmd, sizeof(tmp_cmd), 0, "sensorCfgStart %d", startup_mode);
78 cmd_ = std::string(tmp_cmd);
79 }
80 uint8_t on_message(std::string &message) override;
81
82 protected:
84};
85
87 public:
88 FactoryResetCommand() { cmd_ = "factoryReset 0x45670123 0xCDEF89AB 0x956128C6 0xDF54AC89"; };
89 uint8_t on_message(std::string &message) override;
90};
91
93 public:
94 ResetSystemCommand() { cmd_ = "resetSystem"; }
95 uint8_t on_message(std::string &message) override;
96};
97
98class SaveCfgCommand : public Command {
99 public:
101 cmd_ = "saveCfg 0x45670123 0xCDEF89AB 0x956128C6 0xDF54AC89";
102 cmd_duration_ms_ = 3000;
103 timeout_ms_ = 3500;
104 }
105 uint8_t on_message(std::string &message) override;
106};
107
108class LedModeCommand : public Command {
109 public:
110 LedModeCommand(bool active) : active_(active) {
111 if (active) {
112 cmd_ = "setLedMode 1 0";
113 } else {
114 cmd_ = "setLedMode 1 1";
115 }
116 };
117 uint8_t on_message(std::string &message) override;
118
119 protected:
121};
122
124 public:
125 UartOutputCommand(bool active) : active_(active) {
126 if (active) {
127 cmd_ = "setUartOutput 1 1";
128 } else {
129 cmd_ = "setUartOutput 1 0";
130 }
131 };
132 uint8_t on_message(std::string &message) override;
133
134 protected:
136};
137
139 public:
140 SensitivityCommand(uint8_t sensitivity) : sensitivity_(sensitivity) {
141 if (sensitivity > 9)
142 sensitivity_ = sensitivity = 9;
143 char tmp_cmd[20]; // "setSensitivity " (15) + "0-9" (1) + null = 17
144 buf_append_printf(tmp_cmd, sizeof(tmp_cmd), 0, "setSensitivity %d", sensitivity);
145 cmd_ = std::string(tmp_cmd);
146 };
147 uint8_t on_message(std::string &message) override;
148
149 protected:
151};
152
153} // namespace dfrobot_sen0395
154} // namespace esphome
DfrobotSen0395Component * parent_
Definition commands.h:23
virtual uint8_t on_message(std::string &message)=0
virtual uint8_t execute(DfrobotSen0395Component *parent)
Definition commands.cpp:14
DetRangeCfgCommand(float min1, float max1, float min2, float max2, float min3, float max3, float min4, float max4)
Definition commands.cpp:112
uint8_t on_message(std::string &message) override
Definition commands.cpp:183
uint8_t on_message(std::string &message) override
Definition commands.cpp:253
uint8_t on_message(std::string &message) override
Definition commands.cpp:283
uint8_t on_message(std::string &message) override
Definition commands.cpp:87
uint8_t on_message(std::string &message) override
Definition commands.cpp:85
uint8_t execute(DfrobotSen0395Component *parent) override
Definition commands.cpp:65
uint8_t on_message(std::string &message) override
Definition commands.cpp:264
uint8_t on_message(std::string &message) override
Definition commands.cpp:272
uint8_t on_message(std::string &message) override
Definition commands.cpp:321
uint8_t on_message(std::string &message) override
Definition commands.cpp:234
SetLatencyCommand(float delay_after_detection, float delay_after_disappear)
Definition commands.cpp:207
uint8_t on_message(std::string &message) override
Definition commands.cpp:218
uint8_t on_message(std::string &message) override
Definition commands.cpp:302
const char * message
Definition component.cpp:35
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t