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