ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
commands.cpp
Go to the documentation of this file.
1#include "commands.h"
2
3#include <cmath>
4
5#include "esphome/core/log.h"
6
7#include "dfrobot_sen0395.h"
8
10
11static const char *const TAG = "dfrobot_sen0395.commands";
12
14 this->parent_ = parent;
15 if (this->cmd_sent_) {
16 if (this->parent_->read_message_()) {
17 std::string message(this->parent_->read_buffer_);
18 if (message.rfind("is not recognized as a CLI command") != std::string::npos) {
19 ESP_LOGD(TAG, "Command not recognized properly by sensor");
20 if (this->retries_left_ > 0) {
21 this->retries_left_ -= 1;
22 this->cmd_sent_ = false;
23 ESP_LOGD(TAG, "Retrying");
24 return 0;
25 } else {
26 this->parent_->find_prompt_();
27 return 1; // Command done
28 }
29 }
30 uint8_t rc = on_message(message);
31 if (rc == 2) {
32 if (this->retries_left_ > 0) {
33 this->retries_left_ -= 1;
34 this->cmd_sent_ = false;
35 ESP_LOGD(TAG, "Retrying");
36 return 0;
37 } else {
38 this->parent_->find_prompt_();
39 return 1; // Command done
40 }
41 } else if (rc == 0) {
42 return 0;
43 } else {
44 this->parent_->find_prompt_();
45 return 1;
46 }
47 }
48 if (millis() - this->parent_->ts_last_cmd_sent_ > this->timeout_ms_) {
49 ESP_LOGD(TAG, "Command timeout");
50 if (this->retries_left_ > 0) {
51 this->retries_left_ -= 1;
52 this->cmd_sent_ = false;
53 ESP_LOGD(TAG, "Retrying");
54 } else {
55 return 1; // Command done
56 }
57 }
58 } else if (this->parent_->send_cmd_(this->cmd_.c_str(), this->cmd_duration_ms_)) {
59 this->cmd_sent_ = true;
60 }
61 return 0; // Command not done yet
62}
63
65 this->parent_ = parent;
66 if (this->parent_->read_message_()) {
67 std::string message(this->parent_->read_buffer_);
68 if (message.rfind("$JYBSS,0, , , *") != std::string::npos) {
69 this->parent_->set_detected_(false);
70 this->parent_->set_active(true);
71 return 1; // Command done
72 } else if (message.rfind("$JYBSS,1, , , *") != std::string::npos) {
73 this->parent_->set_detected_(true);
74 this->parent_->set_active(true);
75 return 1; // Command done
76 }
77 }
78 if (millis() - this->parent_->ts_last_cmd_sent_ > this->timeout_ms_) {
79 return 1; // Command done, timeout
80 }
81 return 0; // Command not done yet.
82}
83
84uint8_t ReadStateCommand::on_message(std::string &message) { return 1; }
85
86uint8_t PowerCommand::on_message(std::string &message) {
87 if (message == "sensor stopped already") {
88 this->parent_->set_active(false);
89 ESP_LOGI(TAG, "Stopped sensor (already stopped)");
90 return 1; // Command done
91 } else if (message == "sensor started already") {
92 this->parent_->set_active(true);
93 ESP_LOGI(TAG, "Started sensor (already started)");
94 return 1; // Command done
95 } else if (message == "new parameter isn't save, can't startSensor") {
96 this->parent_->set_active(false);
97 ESP_LOGE(TAG, "Can't start sensor! (Use SaveCfgCommand to save config first)");
98 return 1; // Command done
99 } else if (message == "Done") {
100 this->parent_->set_active(this->power_on_);
101 if (this->power_on_) {
102 ESP_LOGI(TAG, "Started sensor");
103 } else {
104 ESP_LOGI(TAG, "Stopped sensor");
105 }
106 return 1; // Command done
107 }
108 return 0; // Command not done yet.
109}
110
111DetRangeCfgCommand::DetRangeCfgCommand(float min1, float max1, float min2, float max2, float min3, float max3,
112 float min4, float max4) {
113 // TODO: Print warning when values are rounded
114 if (min1 < 0 || max1 < 0) {
115 this->min1_ = min1 = 0;
116 this->max1_ = max1 = 0;
117 this->min2_ = min2 = this->max2_ = max2 = this->min3_ = min3 = this->max3_ = max3 = this->min4_ = min4 =
118 this->max4_ = max4 = -1;
119
120 ESP_LOGW(TAG, "DetRangeCfgCommand invalid input parameters. Using range config 0 0.");
121
122 this->cmd_ = "detRangeCfg -1 0 0";
123 } else if (min2 < 0 || max2 < 0) {
124 this->min1_ = min1 = round(min1 / 0.15) * 0.15;
125 this->max1_ = max1 = round(max1 / 0.15) * 0.15;
126 this->min2_ = min2 = this->max2_ = max2 = this->min3_ = min3 = this->max3_ = max3 = this->min4_ = min4 =
127 this->max4_ = max4 = -1;
128
129 char buf[72]; // max 72: "detRangeCfg -1 "(15) + 8 * (float(5) + space(1)) + null
130 snprintf(buf, sizeof(buf), "detRangeCfg -1 %.0f %.0f", min1 / 0.15, max1 / 0.15);
131 this->cmd_ = buf;
132 } else if (min3 < 0 || max3 < 0) {
133 this->min1_ = min1 = round(min1 / 0.15) * 0.15;
134 this->max1_ = max1 = round(max1 / 0.15) * 0.15;
135 this->min2_ = min2 = round(min2 / 0.15) * 0.15;
136 this->max2_ = max2 = round(max2 / 0.15) * 0.15;
137 this->min3_ = min3 = this->max3_ = max3 = this->min4_ = min4 = this->max4_ = max4 = -1;
138
139 char buf[72]; // max 72: "detRangeCfg -1 "(15) + 8 * (float(5) + space(1)) + null
140 snprintf(buf, sizeof(buf), "detRangeCfg -1 %.0f %.0f %.0f %.0f", min1 / 0.15, max1 / 0.15, min2 / 0.15,
141 max2 / 0.15);
142 this->cmd_ = buf;
143 } else if (min4 < 0 || max4 < 0) {
144 this->min1_ = min1 = round(min1 / 0.15) * 0.15;
145 this->max1_ = max1 = round(max1 / 0.15) * 0.15;
146 this->min2_ = min2 = round(min2 / 0.15) * 0.15;
147 this->max2_ = max2 = round(max2 / 0.15) * 0.15;
148 this->min3_ = min3 = round(min3 / 0.15) * 0.15;
149 this->max3_ = max3 = round(max3 / 0.15) * 0.15;
150 this->min4_ = min4 = this->max4_ = max4 = -1;
151
152 char buf[72]; // max 72: "detRangeCfg -1 "(15) + 8 * (float(5) + space(1)) + null
153 snprintf(buf, sizeof(buf), "detRangeCfg -1 %.0f %.0f %.0f %.0f %.0f %.0f", min1 / 0.15, max1 / 0.15, min2 / 0.15,
154 max2 / 0.15, min3 / 0.15, max3 / 0.15);
155 this->cmd_ = buf;
156 } else {
157 this->min1_ = min1 = round(min1 / 0.15) * 0.15;
158 this->max1_ = max1 = round(max1 / 0.15) * 0.15;
159 this->min2_ = min2 = round(min2 / 0.15) * 0.15;
160 this->max2_ = max2 = round(max2 / 0.15) * 0.15;
161 this->min3_ = min3 = round(min3 / 0.15) * 0.15;
162 this->max3_ = max3 = round(max3 / 0.15) * 0.15;
163 this->min4_ = min4 = round(min4 / 0.15) * 0.15;
164 this->max4_ = max4 = round(max4 / 0.15) * 0.15;
165
166 char buf[72]; // max 72: "detRangeCfg -1 "(15) + 8 * (float(5) + space(1)) + null
167 snprintf(buf, sizeof(buf), "detRangeCfg -1 %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f", min1 / 0.15, max1 / 0.15,
168 min2 / 0.15, max2 / 0.15, min3 / 0.15, max3 / 0.15, min4 / 0.15, max4 / 0.15);
169 this->cmd_ = buf;
170 }
171
172 this->min1_ = min1;
173 this->max1_ = max1;
174 this->min2_ = min2;
175 this->max2_ = max2;
176 this->min3_ = min3;
177 this->max3_ = max3;
178 this->min4_ = min4;
179 this->max4_ = max4;
180};
181
183 if (message == "sensor is not stopped") {
184 ESP_LOGE(TAG, "Cannot configure range config. Sensor is not stopped!");
185 return 1; // Command done
186 } else if (message == "Done") {
187 ESP_LOGI(TAG,
188 "Updated detection area config:\n"
189 " Detection area 1 from %.02fm to %.02fm.",
190 this->min1_, this->max1_);
191 if (this->min2_ >= 0 && this->max2_ >= 0) {
192 ESP_LOGI(TAG, " Detection area 2 from %.02fm to %.02fm.", this->min2_, this->max2_);
193 }
194 if (this->min3_ >= 0 && this->max3_ >= 0) {
195 ESP_LOGI(TAG, " Detection area 3 from %.02fm to %.02fm.", this->min3_, this->max3_);
196 }
197 if (this->min4_ >= 0 && this->max4_ >= 0) {
198 ESP_LOGI(TAG, " Detection area 4 from %.02fm to %.02fm.", this->min4_, this->max4_);
199 }
200 ESP_LOGD(TAG, " Used command: %s", this->cmd_.c_str());
201 return 1; // Command done
202 }
203 return 0; // Command not done yet.
204}
205
206SetLatencyCommand::SetLatencyCommand(float delay_after_detection, float delay_after_disappear) {
207 delay_after_detection = std::round(delay_after_detection / 0.025f) * 0.025f;
208 delay_after_disappear = std::round(delay_after_disappear / 0.025f) * 0.025f;
209 this->delay_after_detection_ = clamp(delay_after_detection, 0.0f, 1638.375f);
210 this->delay_after_disappear_ = clamp(delay_after_disappear, 0.0f, 1638.375f);
211 // max 32: "setLatency "(11) + float(8) + " "(1) + float(8) + null, rounded to 32
212 char buf[32];
213 snprintf(buf, sizeof(buf), "setLatency %.03f %.03f", this->delay_after_detection_, this->delay_after_disappear_);
214 this->cmd_ = buf;
215};
216
218 if (message == "sensor is not stopped") {
219 ESP_LOGE(TAG, "Cannot configure output latency. Sensor is not stopped!");
220 return 1; // Command done
221 } else if (message == "Done") {
222 ESP_LOGI(TAG,
223 "Updated output latency config:\n"
224 " Signal that someone was detected is delayed by %.03f s.\n"
225 " Signal that nobody is detected anymore is delayed by %.03f s.",
227 ESP_LOGD(TAG, " Used command: %s", this->cmd_.c_str());
228 return 1; // Command done
229 }
230 return 0; // Command not done yet
231}
232
234 if (message == "sensor is not stopped") {
235 ESP_LOGE(TAG, "Cannot configure sensor startup behavior. Sensor is not stopped!");
236 return 1; // Command done
237 } else if (message == "Done") {
238 ESP_LOGI(TAG, "Updated sensor startup behavior:");
239 if (startup_mode_) {
240 this->parent_->set_start_after_boot(true);
241 ESP_LOGI(TAG, "Sensor will start automatically after power-on.");
242 } else {
243 this->parent_->set_start_after_boot(false);
244 ESP_LOGI(TAG, "Sensor needs to be started manually after power-on.");
245 }
246 ESP_LOGD(TAG, "Used command: %s", this->cmd_.c_str());
247 return 1; // Command done
248 }
249 return 0; // Command not done yet
250}
251
253 if (message == "sensor is not stopped") {
254 ESP_LOGE(TAG, "Cannot factory reset. Sensor is not stopped!");
255 return 1; // Command done
256 } else if (message == "Done") {
257 ESP_LOGI(TAG, "Sensor factory reset done.");
258 return 1; // Command done
259 }
260 return 0; // Command not done yet
261}
262
264 if (message == "leapMMW:/>") {
265 ESP_LOGI(TAG, "Restarted sensor.");
266 return 1; // Command done
267 }
268 return 0; // Command not done yet
269}
270
271uint8_t SaveCfgCommand::on_message(std::string &message) {
272 if (message == "no parameter has changed") {
273 ESP_LOGI(TAG, "Not saving config (no parameter changed).");
274 return 1; // Command done
275 } else if (message == "Done") {
276 ESP_LOGI(TAG, "Saved config. Saving a lot may damage the sensor.");
277 return 1; // Command done
278 }
279 return 0; // Command not done yet
280}
281
282uint8_t LedModeCommand::on_message(std::string &message) {
283 if (message == "sensor is not stopped") {
284 ESP_LOGE(TAG, "Cannot set led mode. Sensor is not stopped!");
285 return 1; // Command done
286 } else if (message == "Done") {
287 ESP_LOGI(TAG, "Set led mode done.");
288 if (this->active_) {
289 this->parent_->set_led_active(true);
290 ESP_LOGI(TAG, "Sensor LED will blink.");
291 } else {
292 this->parent_->set_led_active(false);
293 ESP_LOGI(TAG, "Turned off LED.");
294 }
295 ESP_LOGD(TAG, "Used command: %s", this->cmd_.c_str());
296 return 1; // Command done
297 }
298 return 0; // Command not done yet
299}
300
302 if (message == "sensor is not stopped") {
303 ESP_LOGE(TAG, "Cannot set uart output mode. Sensor is not stopped!");
304 return 1; // Command done
305 } else if (message == "Done") {
306 ESP_LOGI(TAG, "Set uart mode done.");
307 if (this->active_) {
308 this->parent_->set_uart_presence_active(true);
309 ESP_LOGI(TAG, "Presence information is sent via UART and GPIO.");
310 } else {
311 this->parent_->set_uart_presence_active(false);
312 ESP_LOGI(TAG, "Presence information is only sent via GPIO.");
313 }
314 ESP_LOGD(TAG, "Used command: %s", this->cmd_.c_str());
315 return 1; // Command done
316 }
317 return 0; // Command not done yet
318}
319
321 if (message == "sensor is not stopped") {
322 ESP_LOGE(TAG, "Cannot set sensitivity. Sensor is not stopped!");
323 return 1; // Command done
324 } else if (message == "Done") {
325 ESP_LOGI(TAG, "Set sensitivity done. Set to value %d.", this->sensitivity_);
326 ESP_LOGD(TAG, "Used command: %s", this->cmd_.c_str());
327 return 1; // Command done
328 }
329 return 0; // Command not done yet
330}
331
332} // 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 send_cmd_(const char *cmd, uint32_t duration)
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
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28