ESPHome 2025.5.0
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};
27 uint32_t cmd_duration_ms_{1000};
28 uint32_t timeout_ms_{1500};
29};
30
31class ReadStateCommand : public Command {
32 public:
33 uint8_t execute(DfrobotSen0395Component *parent) override;
34 uint8_t on_message(std::string &message) override;
35
36 protected:
37 uint32_t timeout_ms_{500};
38};
39
40class PowerCommand : public Command {
41 public:
42 PowerCommand(bool power_on) : power_on_(power_on) {
43 if (power_on) {
44 cmd_ = "sensorStart";
45 } else {
46 cmd_ = "sensorStop";
47 }
48 };
49 uint8_t on_message(std::string &message) override;
50
51 protected:
53};
54
56 public:
57 DetRangeCfgCommand(float min1, float max1, float min2, float max2, float min3, float max3, float min4, float max4);
58 uint8_t on_message(std::string &message) override;
59
60 protected:
62 // TODO: Set min max values in component, so they can be published as sensor.
63};
64
65class SetLatencyCommand : public Command {
66 public:
67 SetLatencyCommand(float delay_after_detection, float delay_after_disappear);
68 uint8_t on_message(std::string &message) override;
69
70 protected:
73};
74
76 public:
77 SensorCfgStartCommand(bool startup_mode) : startup_mode_(startup_mode) {
78 char tmp_cmd[20] = {0};
79 sprintf(tmp_cmd, "sensorCfgStart %d", startup_mode);
80 cmd_ = std::string(tmp_cmd);
81 }
82 uint8_t on_message(std::string &message) override;
83
84 protected:
86};
87
89 public:
90 FactoryResetCommand() { cmd_ = "factoryReset 0x45670123 0xCDEF89AB 0x956128C6 0xDF54AC89"; };
91 uint8_t on_message(std::string &message) override;
92};
93
95 public:
96 ResetSystemCommand() { cmd_ = "resetSystem"; }
97 uint8_t on_message(std::string &message) override;
98};
99
100class SaveCfgCommand : public Command {
101 public:
102 SaveCfgCommand() { cmd_ = "saveCfg 0x45670123 0xCDEF89AB 0x956128C6 0xDF54AC89"; }
103 uint8_t on_message(std::string &message) override;
104
105 protected:
106 uint32_t cmd_duration_ms_{3000};
107 uint32_t timeout_ms_{3500};
108};
109
110class LedModeCommand : public Command {
111 public:
112 LedModeCommand(bool active) : active_(active) {
113 if (active) {
114 cmd_ = "setLedMode 1 0";
115 } else {
116 cmd_ = "setLedMode 1 1";
117 }
118 };
119 uint8_t on_message(std::string &message) override;
120
121 protected:
123};
124
126 public:
127 UartOutputCommand(bool active) : active_(active) {
128 if (active) {
129 cmd_ = "setUartOutput 1 1";
130 } else {
131 cmd_ = "setUartOutput 1 0";
132 }
133 };
134 uint8_t on_message(std::string &message) override;
135
136 protected:
138};
139
141 public:
142 SensitivityCommand(uint8_t sensitivity) : sensitivity_(sensitivity) {
143 if (sensitivity > 9)
144 sensitivity_ = sensitivity = 9;
145 char tmp_cmd[20] = {0};
146 sprintf(tmp_cmd, "setSensitivity %d", sensitivity);
147 cmd_ = std::string(tmp_cmd);
148 };
149 uint8_t on_message(std::string &message) override;
150
151 protected:
153};
154
155} // namespace dfrobot_sen0395
156} // 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:177
uint8_t on_message(std::string &message) override
Definition commands.cpp:240
uint8_t on_message(std::string &message) override
Definition commands.cpp:270
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:251
uint8_t on_message(std::string &message) override
Definition commands.cpp:259
uint8_t on_message(std::string &message) override
Definition commands.cpp:308
uint8_t on_message(std::string &message) override
Definition commands.cpp:221
SetLatencyCommand(float delay_after_detection, float delay_after_disappear)
Definition commands.cpp:199
uint8_t on_message(std::string &message) override
Definition commands.cpp:207
uint8_t on_message(std::string &message) override
Definition commands.cpp:289
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7