Compare commits
563 Commits
dev
...
applicatio
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ef86a9e5e | ||
|
|
fac20a1f97 | ||
|
|
c65586b5e1 | ||
|
|
b27b018b06 | ||
|
|
403da1e632 | ||
|
|
2371ec1f9e | ||
|
|
5e3ec2d34b | ||
|
|
78d84644c9 | ||
|
|
0cd0f8015a | ||
|
|
4b5424f695 | ||
|
|
a1d59040f7 | ||
|
|
0306398072 | ||
|
|
a7e0bf9013 | ||
|
|
ddb988cd83 | ||
|
|
6b5b0815d7 | ||
|
|
8388497038 | ||
|
|
9074ef792f | ||
|
|
23765cd4f5 | ||
|
|
e20c6468d0 | ||
|
|
b90516de1d | ||
|
|
ec5cc0f00f | ||
|
|
5dda5a976e | ||
|
|
915da9ae13 | ||
|
|
8652464f4e | ||
|
|
ce6ce1c1f8 | ||
|
|
39efe67e55 | ||
|
|
748ffa00f3 | ||
|
|
e8d9df2b0e | ||
|
|
17396d67de | ||
|
|
edd6a86714 | ||
|
|
85b4012c56 | ||
|
|
7d98433502 | ||
|
|
23774ae03b | ||
|
|
0dedbcdd71 | ||
|
|
4bdd08887e | ||
|
|
1fd8ebf386 | ||
|
|
d2fc3e749c | ||
|
|
71fbcbceaf | ||
|
|
27347b2088 | ||
|
|
599993d1a5 | ||
|
|
bf359cb8e3 | ||
|
|
509a704410 | ||
|
|
1f48e2b01f | ||
|
|
8b25b1eee6 | ||
|
|
3bbf30ff5f | ||
|
|
83613726d1 | ||
|
|
254b6a17f3 | ||
|
|
796e12bd70 | ||
|
|
ddbe17d3f6 | ||
|
|
591ec36f4a | ||
|
|
41eceb72ef | ||
|
|
0a5f094025 | ||
|
|
ca0f3ba262 | ||
|
|
30f4e782db | ||
|
|
192158ef1a | ||
|
|
602456db40 | ||
|
|
536e45668f | ||
|
|
10bf05ab0d | ||
|
|
5ad1af69e4 | ||
|
|
48f2911434 | ||
|
|
dbb0d6349a | ||
|
|
ac3598f12a | ||
|
|
66201be5ca | ||
|
|
ac0b0b652e | ||
|
|
d89ee2df42 | ||
|
|
418e248e5e | ||
|
|
8c2b141049 | ||
|
|
2f8e07302b | ||
|
|
c3776240b6 | ||
|
|
e370872ec1 | ||
|
|
d4e978369a | ||
|
|
8d5d7f5237 | ||
|
|
5cd498fbe9 | ||
|
|
250f515f08 | ||
|
|
0ec0a9e313 | ||
|
|
184f42ef03 | ||
|
|
499517418d | ||
|
|
606b9c1a6d | ||
|
|
971e954a54 | ||
|
|
e3aaf3219d | ||
|
|
0eea1c0e40 | ||
|
|
0773819778 | ||
|
|
170869b7db | ||
|
|
5dc54782e5 | ||
|
|
97b26fbefe | ||
|
|
686cc58d6c | ||
|
|
76a59759b2 | ||
|
|
93245a24b5 | ||
|
|
6a22ea1c7d | ||
|
|
56a02409c8 | ||
|
|
edeafd5a53 | ||
|
|
f67490b69b | ||
|
|
b76e34fb7b | ||
|
|
ddbda5032b | ||
|
|
5898d34b0a | ||
|
|
b0c02341ff | ||
|
|
19cbc8c33b | ||
|
|
02e61ef5d3 | ||
|
|
8d5d18064d | ||
|
|
c5ef7ebd27 | ||
|
|
047a3e0e8c | ||
|
|
13b23f840b | ||
|
|
147f6012b2 | ||
|
|
2c315595f0 | ||
|
|
20405c84ac | ||
|
|
0bc59b97de | ||
|
|
a3a3bdc7eb | ||
|
|
e767f30886 | ||
|
|
e8c250a03c | ||
|
|
d6725fc1ca | ||
|
|
8ec998ff30 | ||
|
|
23cc0c7f39 | ||
|
|
19b8bd6aa8 | ||
|
|
ed57e7c6b0 | ||
|
|
9f489c9f27 | ||
|
|
f036989361 | ||
|
|
6afa8141c0 | ||
|
|
587964c6f1 | ||
|
|
7aea82a273 | ||
|
|
20f946ccaf | ||
|
|
e5e972231c | ||
|
|
bfa80157f2 | ||
|
|
99b1b079d0 | ||
|
|
5697d549a8 | ||
|
|
754d2874e7 | ||
|
|
06de58ff8b | ||
|
|
a0b3527710 | ||
|
|
df24f48fa1 | ||
|
|
13d53590b2 | ||
|
|
5857f7b9a7 | ||
|
|
a5ea0cd41f | ||
|
|
d677934417 | ||
|
|
ba87a0b63c | ||
|
|
b725bb3dd1 | ||
|
|
c34ba3deb5 | ||
|
|
68b13340fb | ||
|
|
8831999ea6 | ||
|
|
c1853f8b84 | ||
|
|
2b9b7e2853 | ||
|
|
d3b18debf9 | ||
|
|
b01eb28d42 | ||
|
|
02019dd16c | ||
|
|
7be12f5ff6 | ||
|
|
a90d59b6ba | ||
|
|
e7fa156254 | ||
|
|
a8ab6b1c43 | ||
|
|
25ed7c890b | ||
|
|
85e3b63f05 | ||
|
|
a37bac1956 | ||
|
|
818a978dfc | ||
|
|
180aeb7d8e | ||
|
|
0764fa7292 | ||
|
|
17bf533ed7 | ||
|
|
d7eae1c1a0 | ||
|
|
7f2d979255 | ||
|
|
46b419ea8b | ||
|
|
b30b527ff9 | ||
|
|
41b1bfc504 | ||
|
|
f4f14a7507 | ||
|
|
61c29213a7 | ||
|
|
e6d7639209 | ||
|
|
3c07a186b2 | ||
|
|
8a725250a9 | ||
|
|
502b8a6073 | ||
|
|
6212c6f80f | ||
|
|
b03e3b8d4a | ||
|
|
a98e34d190 | ||
|
|
bf8d8b6e63 | ||
|
|
57599f7a98 | ||
|
|
ffccce7ffc | ||
|
|
bbd5d050a9 | ||
|
|
71a96fdcbf | ||
|
|
221e3c6c9c | ||
|
|
fb1679d572 | ||
|
|
c19065f112 | ||
|
|
f2b04a077e | ||
|
|
8e7841c880 | ||
|
|
1873490b24 | ||
|
|
4d231953f4 | ||
|
|
aa4c399657 | ||
|
|
1f99d18982 | ||
|
|
be37178ef8 | ||
|
|
fad86c655e | ||
|
|
4a7958586e | ||
|
|
f44ecd0891 | ||
|
|
3d0392d668 | ||
|
|
d300d2605b | ||
|
|
66cce6a2f2 | ||
|
|
65e3c6bfbb | ||
|
|
2a39060912 | ||
|
|
8714e80978 | ||
|
|
98de53f60b | ||
|
|
41e11e9a0e | ||
|
|
e7a4eac8bd | ||
|
|
1589a131db | ||
|
|
7d84f0e650 | ||
|
|
86fb0e317f | ||
|
|
32088d5ef7 | ||
|
|
63de88dd57 | ||
|
|
153a6440dc | ||
|
|
8937ed2269 | ||
|
|
02e922b56f | ||
|
|
bf9e901ab9 | ||
|
|
1234ef8de2 | ||
|
|
41697a7b1b | ||
|
|
912e265bc0 | ||
|
|
96ee6fb064 | ||
|
|
788dba8ef3 | ||
|
|
fdde9c4681 | ||
|
|
f195e73d38 | ||
|
|
b0d9ffc6a1 | ||
|
|
e17619841d | ||
|
|
eb6a7cf3b9 | ||
|
|
9901e2d72e | ||
|
|
bcf961c0b0 | ||
|
|
f84a4c9753 | ||
|
|
df56ca0236 | ||
|
|
de0cd0ec67 | ||
|
|
67c30245c4 | ||
|
|
1f72757591 | ||
|
|
35c2fdf6af | ||
|
|
d1ecd841be | ||
|
|
828a49697c | ||
|
|
0551495501 | ||
|
|
2bbffe4a68 | ||
|
|
281ad90e39 | ||
|
|
ed50976a07 | ||
|
|
a3400037d9 | ||
|
|
f0d82f75bc | ||
|
|
349cb80e90 | ||
|
|
c263ee39af | ||
|
|
e99bc52756 | ||
|
|
7944b2b8e9 | ||
|
|
ca6ae746c1 | ||
|
|
deabac18b2 | ||
|
|
5cf8681c61 | ||
|
|
ca7ede8f96 | ||
|
|
4969682d52 | ||
|
|
8002fe0dd5 | ||
|
|
7dfdf965b7 | ||
|
|
b408795dd6 | ||
|
|
a5a099336b | ||
|
|
4ae56fc004 | ||
|
|
3f71c09b7b | ||
|
|
bd50a7f1ab | ||
|
|
51e4c45e5c | ||
|
|
e3fae49add | ||
|
|
610215ab60 | ||
|
|
74acbda435 | ||
|
|
25c4af777c | ||
|
|
ec186e6324 | ||
|
|
150b7a98f3 | ||
|
|
8ae7c1cff0 | ||
|
|
7f1d0eef98 | ||
|
|
1179ab33f2 | ||
|
|
a09faa1c10 | ||
|
|
c0319d9b2f | ||
|
|
4870cd2921 | ||
|
|
d4280ec68b | ||
|
|
52cdc11927 | ||
|
|
8345b8c9ce | ||
|
|
c56f0677c3 | ||
|
|
00e9e1421e | ||
|
|
93c72c6e6c | ||
|
|
9cea930dbd | ||
|
|
7b9bd70729 | ||
|
|
5115c7a100 | ||
|
|
5634494e64 | ||
|
|
aa8bd4abf1 | ||
|
|
17fd69dd7f | ||
|
|
1d9dae374b | ||
|
|
cb2241ad91 | ||
|
|
d8a7e9abc8 | ||
|
|
969abc3f29 | ||
|
|
766fdc8a1f | ||
|
|
4c37c20d76 | ||
|
|
7d314398e1 | ||
|
|
b69191e3a8 | ||
|
|
b27c6b3596 | ||
|
|
5453835963 | ||
|
|
4d55ba057c | ||
|
|
325c01242c | ||
|
|
45b32bca89 | ||
|
|
7620049214 | ||
|
|
3553495a60 | ||
|
|
3ce6db61d5 | ||
|
|
798ff32c40 | ||
|
|
430cee8bda | ||
|
|
1fe3fb25a6 | ||
|
|
685ed87581 | ||
|
|
ea3ea1eee7 | ||
|
|
c9edcb909b | ||
|
|
35bfc9f069 | ||
|
|
c4aec194b9 | ||
|
|
e8547b16f6 | ||
|
|
2bbe08cee0 | ||
|
|
0a0c369b88 | ||
|
|
5d2f454a94 | ||
|
|
04bcc5c879 | ||
|
|
d4db16665f | ||
|
|
20b7a494f6 | ||
|
|
fbdce3ad89 | ||
|
|
4fc8807f02 | ||
|
|
83075bfb5c | ||
|
|
4074ec0425 | ||
|
|
8e1694dd0f | ||
|
|
911df18855 | ||
|
|
6b049e93f8 | ||
|
|
a335dcc379 | ||
|
|
c6478c8a79 | ||
|
|
cc9d40cb60 | ||
|
|
0a6b7f9a1b | ||
|
|
daa1fb9a7a | ||
|
|
b7d543290b | ||
|
|
ea852b60ac | ||
|
|
ed341988ea | ||
|
|
057b6c8e30 | ||
|
|
44444fe071 | ||
|
|
797330d6ab | ||
|
|
a630d5b5f5 | ||
|
|
eb3dc82b5d | ||
|
|
34ed18d562 | ||
|
|
1ce02ee313 | ||
|
|
2a26a0188c | ||
|
|
50cb05d1b1 | ||
|
|
6e739ac453 | ||
|
|
7aa2fd9f0e | ||
|
|
8e254e1b03 | ||
|
|
1ad9d717ff | ||
|
|
104658e43a | ||
|
|
e7e4b995bf | ||
|
|
b35b54f2c2 | ||
|
|
f80aeb1d1d | ||
|
|
6a756ab3b6 | ||
|
|
58a697bed1 | ||
|
|
280960ac18 | ||
|
|
0640ff13aa | ||
|
|
545505691f | ||
|
|
11fcf81321 | ||
|
|
c565b37dc8 | ||
|
|
3d18495270 | ||
|
|
419e4e63e9 | ||
|
|
724aa2bf65 | ||
|
|
573fa8aeb3 | ||
|
|
8a672e34c5 | ||
|
|
bc49211dab | ||
|
|
4ef9c3667e | ||
|
|
6babe516ac | ||
|
|
e0b258ef7e | ||
|
|
ff0c3a89b1 | ||
|
|
2511b81048 | ||
|
|
6ffcd94edc | ||
|
|
2fcf73c812 | ||
|
|
dee0608af9 | ||
|
|
d11860a383 | ||
|
|
1c05115bf5 | ||
|
|
d7e7382d0b | ||
|
|
872388f6e3 | ||
|
|
1215ef920b | ||
|
|
d19d5a23ea | ||
|
|
f49a779f1d | ||
|
|
d8bf5b80e1 | ||
|
|
69483b9353 | ||
|
|
14e8548989 | ||
|
|
4abd93b661 | ||
|
|
5d925af76f | ||
|
|
b999c6064a | ||
|
|
94e3576978 | ||
|
|
7a22406a2d | ||
|
|
e60684494f | ||
|
|
9db28ed779 | ||
|
|
6fd8c5cee7 | ||
|
|
787ec43266 | ||
|
|
a4efc63bf2 | ||
|
|
80a8f1437e | ||
|
|
fcca94169d | ||
|
|
d1924088e3 | ||
|
|
fd31afe09c | ||
|
|
7a763712c5 | ||
|
|
7216be5da7 | ||
|
|
711b0a291b | ||
|
|
dfc96496c8 | ||
|
|
2a1c5ef333 | ||
|
|
9755209499 | ||
|
|
0b26e537d4 | ||
|
|
98c6233ec3 | ||
|
|
f711706b1a | ||
|
|
cee7789ab6 | ||
|
|
8a06c4380d | ||
|
|
72ecf7a288 | ||
|
|
ef98c7502d | ||
|
|
03d0e74b65 | ||
|
|
5b8fdc0364 | ||
|
|
593b4bd137 | ||
|
|
267e12d058 | ||
|
|
4a5e39b651 | ||
|
|
ea24fa5b78 | ||
|
|
bb2bb128f7 | ||
|
|
94e8a856d7 | ||
|
|
4c19fbf98e | ||
|
|
60f8938bfa | ||
|
|
55679662b5 | ||
|
|
53df959e49 | ||
|
|
8e6ef9966f | ||
|
|
1d52fceafa | ||
|
|
99186ed864 | ||
|
|
383931d484 | ||
|
|
0b49a54cb3 | ||
|
|
705c0f1891 | ||
|
|
544c3ffc95 | ||
|
|
33f252a45d | ||
|
|
f55d82a015 | ||
|
|
8cf33fdef0 | ||
|
|
f858d98811 | ||
|
|
2a6165d440 | ||
|
|
4586528c40 | ||
|
|
23a07baa19 | ||
|
|
f9040ca932 | ||
|
|
4cea7f0237 | ||
|
|
b1847d5e98 | ||
|
|
9ce4d2e952 | ||
|
|
247078e06d | ||
|
|
a0cd72de28 | ||
|
|
e467f569f0 | ||
|
|
e31c7b7dfc | ||
|
|
dc2e0c832b | ||
|
|
7ddf51bb51 | ||
|
|
8fb3856665 | ||
|
|
183dd74f3e | ||
|
|
4f29039b41 | ||
|
|
102fcbec20 | ||
|
|
d00e5212c7 | ||
|
|
0e6bfb62cd | ||
|
|
aa930fb6b6 | ||
|
|
f327ed87e9 | ||
|
|
2de9be0589 | ||
|
|
345cde8645 | ||
|
|
cf152af9ae | ||
|
|
d6333dcfd9 | ||
|
|
0121f799f0 | ||
|
|
82c39580df | ||
|
|
53a578a46f | ||
|
|
62612ef80b | ||
|
|
61ac874c4c | ||
|
|
976b200ff6 | ||
|
|
852343b6d8 | ||
|
|
c56af9d52b | ||
|
|
05f18e2828 | ||
|
|
72804caab2 | ||
|
|
80cbe5c7c9 | ||
|
|
21892d1236 | ||
|
|
13824624f8 | ||
|
|
0fd72ecbab | ||
|
|
f848cb1546 | ||
|
|
633854081a | ||
|
|
4fed9a581b | ||
|
|
e9c1202aaa | ||
|
|
0a7ae279d0 | ||
|
|
0de2696543 | ||
|
|
a7dc239b71 | ||
|
|
fe0e6990f5 | ||
|
|
5ba65e92d9 | ||
|
|
a1452b52c9 | ||
|
|
dd2aa23a5f | ||
|
|
0e0359ba7d | ||
|
|
93b1b7aded | ||
|
|
9472dc6a53 | ||
|
|
67b681854e | ||
|
|
7b5990833e | ||
|
|
b6d5d04589 | ||
|
|
fdfbb3e944 | ||
|
|
faa7a3e37f | ||
|
|
23748b82bb | ||
|
|
bccb6f578a | ||
|
|
de8a5d6e9e | ||
|
|
a8eb3f7961 | ||
|
|
0877b3e2af | ||
|
|
99a54369bf | ||
|
|
f7533dfc5c | ||
|
|
ee7d95272d | ||
|
|
2b9b1d12e6 | ||
|
|
2cbb5c7d8e | ||
|
|
9686c7babe | ||
|
|
66bd4c96c4 | ||
|
|
dc47faa4b6 | ||
|
|
55ee0b116d | ||
|
|
c6957c08bc | ||
|
|
8fe6a323d8 | ||
|
|
8e51590c32 | ||
|
|
ae066d5627 | ||
|
|
6760279916 | ||
|
|
3c208050b0 | ||
|
|
bbc7c9fb37 | ||
|
|
e1c3862586 | ||
|
|
c24b7cb7bd | ||
|
|
c91e16549d | ||
|
|
6e70aca458 | ||
|
|
d9ffd0ac8e | ||
|
|
4641f73d19 | ||
|
|
9f0051c21f | ||
|
|
0331cb09e8 | ||
|
|
2f8946f86c | ||
|
|
88a3df4008 | ||
|
|
0adf514bd6 | ||
|
|
a1b5a2abcb | ||
|
|
068c62c6fe | ||
|
|
0e9f14f969 | ||
|
|
78315fd388 | ||
|
|
0ab69002df | ||
|
|
1eec1239ec | ||
|
|
57f4067fbf | ||
|
|
f4a9221232 | ||
|
|
3d4a75148d | ||
|
|
c2c5bd844d | ||
|
|
98a2f23024 | ||
|
|
c955897d1b | ||
|
|
9624efa21e | ||
|
|
831638210d | ||
|
|
cfdb0925ce | ||
|
|
83db3eddd9 | ||
|
|
cc2c5a544e | ||
|
|
8fba8c2800 | ||
|
|
51d1da8460 | ||
|
|
2f1257056d | ||
|
|
2f8f6967bf | ||
|
|
246527e618 | ||
|
|
3857cc9c83 | ||
|
|
a59a8c563e | ||
|
|
856829bcbb | ||
|
|
dd2b931f61 | ||
|
|
39beccbbb0 | ||
|
|
ff626b428f | ||
|
|
3915e1f012 | ||
|
|
7b460b6224 | ||
|
|
8fb8e79730 | ||
|
|
79bbc475f4 | ||
|
|
cef023283b | ||
|
|
d4fda79ada | ||
|
|
ff0bdcf4cd | ||
|
|
bfbc313144 | ||
|
|
31f2376f15 | ||
|
|
f76ecb6604 | ||
|
|
298cc58433 | ||
|
|
825c0593e1 | ||
|
|
87ed1dc3e3 | ||
|
|
67e9db021c | ||
|
|
3922950951 | ||
|
|
9c4aa0ba53 | ||
|
|
f5f1651b31 | ||
|
|
32f4e4ca13 | ||
|
|
962e0c4c33 | ||
|
|
2c01bc5795 | ||
|
|
0651f7cb3c | ||
|
|
01ac59ce2a | ||
|
|
c1fd597757 | ||
|
|
e79e244eee | ||
|
|
68ecc08111 | ||
|
|
3b5fbc359f | ||
|
|
583e5ea47f | ||
|
|
7b647c3fae | ||
|
|
a8b76c617c | ||
|
|
1bd8985dff | ||
|
|
25b5a6c4ae |
@@ -33,9 +33,14 @@ namespace api {
|
|||||||
// Since each message could contain multiple protobuf messages when using packet batching,
|
// Since each message could contain multiple protobuf messages when using packet batching,
|
||||||
// this limits the number of messages processed, not the number of TCP packets.
|
// this limits the number of messages processed, not the number of TCP packets.
|
||||||
static constexpr uint8_t MAX_MESSAGES_PER_LOOP = 5;
|
static constexpr uint8_t MAX_MESSAGES_PER_LOOP = 5;
|
||||||
|
static constexpr uint8_t MAX_PING_RETRIES = 60;
|
||||||
|
static constexpr uint16_t PING_RETRY_INTERVAL = 1000;
|
||||||
|
static constexpr uint32_t KEEPALIVE_DISCONNECT_TIMEOUT = (KEEPALIVE_TIMEOUT_MS * 5) / 2;
|
||||||
|
|
||||||
static const char *const TAG = "api.connection";
|
static const char *const TAG = "api.connection";
|
||||||
|
#ifdef USE_ESP32_CAMERA
|
||||||
static const int ESP32_CAMERA_STOP_STREAM = 5000;
|
static const int ESP32_CAMERA_STOP_STREAM = 5000;
|
||||||
|
#endif
|
||||||
|
|
||||||
APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
|
APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
|
||||||
: parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
|
: parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
|
||||||
@@ -60,10 +65,6 @@ uint32_t APIConnection::get_batch_delay_ms_() const { return this->parent_->get_
|
|||||||
void APIConnection::start() {
|
void APIConnection::start() {
|
||||||
this->last_traffic_ = App.get_loop_component_start_time();
|
this->last_traffic_ = App.get_loop_component_start_time();
|
||||||
|
|
||||||
// Set next_ping_retry_ to prevent immediate ping
|
|
||||||
// This ensures the first ping happens after the keepalive period
|
|
||||||
this->next_ping_retry_ = this->last_traffic_ + KEEPALIVE_TIMEOUT_MS;
|
|
||||||
|
|
||||||
APIError err = this->helper_->init();
|
APIError err = this->helper_->init();
|
||||||
if (err != APIError::OK) {
|
if (err != APIError::OK) {
|
||||||
on_fatal_error();
|
on_fatal_error();
|
||||||
@@ -90,16 +91,6 @@ APIConnection::~APIConnection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void APIConnection::loop() {
|
void APIConnection::loop() {
|
||||||
if (this->remove_)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!network::is_connected()) {
|
|
||||||
// when network is disconnected force disconnect immediately
|
|
||||||
// don't wait for timeout
|
|
||||||
this->on_fatal_error();
|
|
||||||
ESP_LOGW(TAG, "%s: Network unavailable; disconnecting", this->get_client_combined_info().c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (this->next_close_) {
|
if (this->next_close_) {
|
||||||
// requested a disconnect
|
// requested a disconnect
|
||||||
this->helper_->close();
|
this->helper_->close();
|
||||||
@@ -152,39 +143,31 @@ void APIConnection::loop() {
|
|||||||
|
|
||||||
// Process deferred batch if scheduled
|
// Process deferred batch if scheduled
|
||||||
if (this->deferred_batch_.batch_scheduled &&
|
if (this->deferred_batch_.batch_scheduled &&
|
||||||
App.get_loop_component_start_time() - this->deferred_batch_.batch_start_time >= this->get_batch_delay_ms_()) {
|
now - this->deferred_batch_.batch_start_time >= this->get_batch_delay_ms_()) {
|
||||||
this->process_batch_();
|
this->process_batch_();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->list_entities_iterator_.completed())
|
if (!this->list_entities_iterator_.completed()) {
|
||||||
this->list_entities_iterator_.advance();
|
this->list_entities_iterator_.advance();
|
||||||
if (!this->initial_state_iterator_.completed() && this->list_entities_iterator_.completed())
|
} else if (!this->initial_state_iterator_.completed()) {
|
||||||
this->initial_state_iterator_.advance();
|
this->initial_state_iterator_.advance();
|
||||||
|
}
|
||||||
|
|
||||||
static uint8_t max_ping_retries = 60;
|
|
||||||
static uint16_t ping_retry_interval = 1000;
|
|
||||||
if (this->sent_ping_) {
|
if (this->sent_ping_) {
|
||||||
// Disconnect if not responded within 2.5*keepalive
|
// Disconnect if not responded within 2.5*keepalive
|
||||||
if (now - this->last_traffic_ > (KEEPALIVE_TIMEOUT_MS * 5) / 2) {
|
if (now - this->last_traffic_ > KEEPALIVE_DISCONNECT_TIMEOUT) {
|
||||||
on_fatal_error();
|
on_fatal_error();
|
||||||
ESP_LOGW(TAG, "%s is unresponsive; disconnecting", this->get_client_combined_info().c_str());
|
ESP_LOGW(TAG, "%s is unresponsive; disconnecting", this->get_client_combined_info().c_str());
|
||||||
}
|
}
|
||||||
} else if (now - this->last_traffic_ > KEEPALIVE_TIMEOUT_MS && now > this->next_ping_retry_) {
|
} else if (now - this->last_traffic_ > KEEPALIVE_TIMEOUT_MS) {
|
||||||
ESP_LOGVV(TAG, "Sending keepalive PING");
|
ESP_LOGVV(TAG, "Sending keepalive PING");
|
||||||
this->sent_ping_ = this->send_message(PingRequest());
|
this->sent_ping_ = this->send_message(PingRequest());
|
||||||
if (!this->sent_ping_) {
|
if (!this->sent_ping_) {
|
||||||
this->next_ping_retry_ = now + ping_retry_interval;
|
// If we can't send the ping request directly (tx_buffer full),
|
||||||
this->ping_retries_++;
|
// schedule it at the front of the batch so it will be sent with priority
|
||||||
std::string warn_str = str_sprintf("%s: Sending keepalive failed %u time(s);",
|
ESP_LOGVV(TAG, "Failed to send ping directly, scheduling at front of batch");
|
||||||
this->get_client_combined_info().c_str(), this->ping_retries_);
|
this->schedule_message_front_(nullptr, &APIConnection::try_send_ping_request, PingRequest::MESSAGE_TYPE);
|
||||||
if (this->ping_retries_ >= max_ping_retries) {
|
this->sent_ping_ = true; // Mark as sent to avoid scheduling multiple pings
|
||||||
on_fatal_error();
|
|
||||||
ESP_LOGE(TAG, "%s disconnecting", warn_str.c_str());
|
|
||||||
} else if (this->ping_retries_ >= 10) {
|
|
||||||
ESP_LOGW(TAG, "%s retrying in %u ms", warn_str.c_str(), ping_retry_interval);
|
|
||||||
} else {
|
|
||||||
ESP_LOGD(TAG, "%s retrying in %u ms", warn_str.c_str(), ping_retry_interval);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,22 +190,20 @@ void APIConnection::loop() {
|
|||||||
// bool done = 3;
|
// bool done = 3;
|
||||||
buffer.encode_bool(3, done);
|
buffer.encode_bool(3, done);
|
||||||
|
|
||||||
bool success = this->send_buffer(buffer, 44);
|
bool success = this->send_buffer(buffer, CameraImageResponse::MESSAGE_TYPE);
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
this->image_reader_.consume_data(to_send);
|
this->image_reader_.consume_data(to_send);
|
||||||
}
|
if (done) {
|
||||||
if (success && done) {
|
this->image_reader_.return_image();
|
||||||
this->image_reader_.return_image();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (state_subs_at_ != -1) {
|
if (state_subs_at_ >= 0) {
|
||||||
const auto &subs = this->parent_->get_state_subs();
|
const auto &subs = this->parent_->get_state_subs();
|
||||||
if (state_subs_at_ >= (int) subs.size()) {
|
if (state_subs_at_ < static_cast<int>(subs.size())) {
|
||||||
state_subs_at_ = -1;
|
|
||||||
} else {
|
|
||||||
auto &it = subs[state_subs_at_];
|
auto &it = subs[state_subs_at_];
|
||||||
SubscribeHomeAssistantStateResponse resp;
|
SubscribeHomeAssistantStateResponse resp;
|
||||||
resp.entity_id = it.entity_id;
|
resp.entity_id = it.entity_id;
|
||||||
@@ -231,6 +212,8 @@ void APIConnection::loop() {
|
|||||||
if (this->send_message(resp)) {
|
if (this->send_message(resp)) {
|
||||||
state_subs_at_++;
|
state_subs_at_++;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
state_subs_at_ = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1440,7 +1423,7 @@ void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRe
|
|||||||
|
|
||||||
#ifdef USE_EVENT
|
#ifdef USE_EVENT
|
||||||
void APIConnection::send_event(event::Event *event, const std::string &event_type) {
|
void APIConnection::send_event(event::Event *event, const std::string &event_type) {
|
||||||
this->schedule_message_(event, MessageCreator(event_type, EventResponse::MESSAGE_TYPE), EventResponse::MESSAGE_TYPE);
|
this->schedule_message_(event, MessageCreator(event_type), EventResponse::MESSAGE_TYPE);
|
||||||
}
|
}
|
||||||
void APIConnection::send_event_info(event::Event *event) {
|
void APIConnection::send_event_info(event::Event *event) {
|
||||||
this->schedule_message_(event, &APIConnection::try_send_event_info, ListEntitiesEventResponse::MESSAGE_TYPE);
|
this->schedule_message_(event, &APIConnection::try_send_event_info, ListEntitiesEventResponse::MESSAGE_TYPE);
|
||||||
@@ -1760,6 +1743,11 @@ void APIConnection::DeferredBatch::add_item(EntityBase *entity, MessageCreator c
|
|||||||
items.emplace_back(entity, std::move(creator), message_type);
|
items.emplace_back(entity, std::move(creator), message_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void APIConnection::DeferredBatch::add_item_front(EntityBase *entity, MessageCreator creator, uint16_t message_type) {
|
||||||
|
// Insert at front for high priority messages (no deduplication check)
|
||||||
|
items.insert(items.begin(), BatchItem(entity, std::move(creator), message_type));
|
||||||
|
}
|
||||||
|
|
||||||
bool APIConnection::schedule_batch_() {
|
bool APIConnection::schedule_batch_() {
|
||||||
if (!this->deferred_batch_.batch_scheduled) {
|
if (!this->deferred_batch_.batch_scheduled) {
|
||||||
this->deferred_batch_.batch_scheduled = true;
|
this->deferred_batch_.batch_scheduled = true;
|
||||||
@@ -1795,7 +1783,8 @@ void APIConnection::process_batch_() {
|
|||||||
const auto &item = this->deferred_batch_.items[0];
|
const auto &item = this->deferred_batch_.items[0];
|
||||||
|
|
||||||
// Let the creator calculate size and encode if it fits
|
// Let the creator calculate size and encode if it fits
|
||||||
uint16_t payload_size = item.creator(item.entity, this, std::numeric_limits<uint16_t>::max(), true);
|
uint16_t payload_size =
|
||||||
|
item.creator(item.entity, this, std::numeric_limits<uint16_t>::max(), true, item.message_type);
|
||||||
|
|
||||||
if (payload_size > 0 &&
|
if (payload_size > 0 &&
|
||||||
this->send_buffer(ProtoWriteBuffer{&this->parent_->get_shared_buffer_ref()}, item.message_type)) {
|
this->send_buffer(ProtoWriteBuffer{&this->parent_->get_shared_buffer_ref()}, item.message_type)) {
|
||||||
@@ -1845,7 +1834,7 @@ void APIConnection::process_batch_() {
|
|||||||
for (const auto &item : this->deferred_batch_.items) {
|
for (const auto &item : this->deferred_batch_.items) {
|
||||||
// Try to encode message
|
// Try to encode message
|
||||||
// The creator will calculate overhead to determine if the message fits
|
// The creator will calculate overhead to determine if the message fits
|
||||||
uint16_t payload_size = item.creator(item.entity, this, remaining_size, false);
|
uint16_t payload_size = item.creator(item.entity, this, remaining_size, false, item.message_type);
|
||||||
|
|
||||||
if (payload_size == 0) {
|
if (payload_size == 0) {
|
||||||
// Message won't fit, stop processing
|
// Message won't fit, stop processing
|
||||||
@@ -1908,21 +1897,23 @@ void APIConnection::process_batch_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint16_t APIConnection::MessageCreator::operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
uint16_t APIConnection::MessageCreator::operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||||
bool is_single) const {
|
bool is_single, uint16_t message_type) const {
|
||||||
switch (message_type_) {
|
if (has_tagged_string_ptr_()) {
|
||||||
case 0: // Function pointer
|
// Handle string-based messages
|
||||||
return data_.ptr(entity, conn, remaining_size, is_single);
|
switch (message_type) {
|
||||||
|
|
||||||
#ifdef USE_EVENT
|
#ifdef USE_EVENT
|
||||||
case EventResponse::MESSAGE_TYPE: {
|
case EventResponse::MESSAGE_TYPE: {
|
||||||
auto *e = static_cast<event::Event *>(entity);
|
auto *e = static_cast<event::Event *>(entity);
|
||||||
return APIConnection::try_send_event_response(e, *data_.string_ptr, conn, remaining_size, is_single);
|
return APIConnection::try_send_event_response(e, *get_string_ptr_(), conn, remaining_size, is_single);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
default:
|
||||||
default:
|
// Should not happen, return 0 to indicate no message
|
||||||
// Should not happen, return 0 to indicate no message
|
return 0;
|
||||||
return 0;
|
}
|
||||||
|
} else {
|
||||||
|
// Function pointer case
|
||||||
|
return data_.ptr(entity, conn, remaining_size, is_single);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1938,6 +1929,12 @@ uint16_t APIConnection::try_send_disconnect_request(EntityBase *entity, APIConne
|
|||||||
return encode_message_to_buffer(req, DisconnectRequest::MESSAGE_TYPE, conn, remaining_size, is_single);
|
return encode_message_to_buffer(req, DisconnectRequest::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t APIConnection::try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||||
|
bool is_single) {
|
||||||
|
PingRequest req;
|
||||||
|
return encode_message_to_buffer(req, PingRequest::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t APIConnection::get_estimated_message_size(uint16_t message_type) {
|
uint16_t APIConnection::get_estimated_message_size(uint16_t message_type) {
|
||||||
// Use generated ESTIMATED_SIZE constants from each message type
|
// Use generated ESTIMATED_SIZE constants from each message type
|
||||||
switch (message_type) {
|
switch (message_type) {
|
||||||
|
|||||||
@@ -185,7 +185,6 @@ class APIConnection : public APIServerConnection {
|
|||||||
void on_disconnect_response(const DisconnectResponse &value) override;
|
void on_disconnect_response(const DisconnectResponse &value) override;
|
||||||
void on_ping_response(const PingResponse &value) override {
|
void on_ping_response(const PingResponse &value) override {
|
||||||
// we initiated ping
|
// we initiated ping
|
||||||
this->ping_retries_ = 0;
|
|
||||||
this->sent_ping_ = false;
|
this->sent_ping_ = false;
|
||||||
}
|
}
|
||||||
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override;
|
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override;
|
||||||
@@ -441,13 +440,16 @@ class APIConnection : public APIServerConnection {
|
|||||||
// Helper function to get estimated message size for buffer pre-allocation
|
// Helper function to get estimated message size for buffer pre-allocation
|
||||||
static uint16_t get_estimated_message_size(uint16_t message_type);
|
static uint16_t get_estimated_message_size(uint16_t message_type);
|
||||||
|
|
||||||
|
// Batch message method for ping requests
|
||||||
|
static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||||
|
bool is_single);
|
||||||
|
|
||||||
// Pointers first (4 bytes each, naturally aligned)
|
// Pointers first (4 bytes each, naturally aligned)
|
||||||
std::unique_ptr<APIFrameHelper> helper_;
|
std::unique_ptr<APIFrameHelper> helper_;
|
||||||
APIServer *parent_;
|
APIServer *parent_;
|
||||||
|
|
||||||
// 4-byte aligned types
|
// 4-byte aligned types
|
||||||
uint32_t last_traffic_;
|
uint32_t last_traffic_;
|
||||||
uint32_t next_ping_retry_{0};
|
|
||||||
int state_subs_at_ = -1;
|
int state_subs_at_ = -1;
|
||||||
|
|
||||||
// Strings (12 bytes each on 32-bit)
|
// Strings (12 bytes each on 32-bit)
|
||||||
@@ -470,8 +472,7 @@ class APIConnection : public APIServerConnection {
|
|||||||
bool sent_ping_{false};
|
bool sent_ping_{false};
|
||||||
bool service_call_subscription_{false};
|
bool service_call_subscription_{false};
|
||||||
bool next_close_ = false;
|
bool next_close_ = false;
|
||||||
uint8_t ping_retries_{0};
|
// 7 bytes used, 1 byte padding
|
||||||
// 8 bytes used, no padding needed
|
|
||||||
|
|
||||||
// Larger objects at the end
|
// Larger objects at the end
|
||||||
InitialStateIterator initial_state_iterator_;
|
InitialStateIterator initial_state_iterator_;
|
||||||
@@ -483,55 +484,57 @@ class APIConnection : public APIServerConnection {
|
|||||||
// Function pointer type for message encoding
|
// Function pointer type for message encoding
|
||||||
using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size, bool is_single);
|
using MessageCreatorPtr = uint16_t (*)(EntityBase *, APIConnection *, uint32_t remaining_size, bool is_single);
|
||||||
|
|
||||||
// Optimized MessageCreator class using union dispatch
|
// Optimized MessageCreator class using tagged pointer
|
||||||
class MessageCreator {
|
class MessageCreator {
|
||||||
|
// Ensure pointer alignment allows LSB tagging
|
||||||
|
static_assert(alignof(std::string *) > 1, "String pointer alignment must be > 1 for LSB tagging");
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor for function pointer (message_type = 0)
|
// Constructor for function pointer
|
||||||
MessageCreator(MessageCreatorPtr ptr) : message_type_(0) { data_.ptr = ptr; }
|
MessageCreator(MessageCreatorPtr ptr) {
|
||||||
|
// Function pointers are always aligned, so LSB is 0
|
||||||
|
data_.ptr = ptr;
|
||||||
|
}
|
||||||
|
|
||||||
// Constructor for string state capture
|
// Constructor for string state capture
|
||||||
MessageCreator(const std::string &value, uint16_t msg_type) : message_type_(msg_type) {
|
explicit MessageCreator(const std::string &str_value) {
|
||||||
data_.string_ptr = new std::string(value);
|
// Allocate string and tag the pointer
|
||||||
|
auto *str = new std::string(str_value);
|
||||||
|
// Set LSB to 1 to indicate string pointer
|
||||||
|
data_.tagged = reinterpret_cast<uintptr_t>(str) | 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~MessageCreator() {
|
~MessageCreator() {
|
||||||
// Clean up string data for string-based message types
|
if (has_tagged_string_ptr_()) {
|
||||||
if (uses_string_data_()) {
|
delete get_string_ptr_();
|
||||||
delete data_.string_ptr;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy constructor
|
// Copy constructor
|
||||||
MessageCreator(const MessageCreator &other) : message_type_(other.message_type_) {
|
MessageCreator(const MessageCreator &other) {
|
||||||
if (message_type_ == 0) {
|
if (other.has_tagged_string_ptr_()) {
|
||||||
data_.ptr = other.data_.ptr;
|
auto *str = new std::string(*other.get_string_ptr_());
|
||||||
} else if (uses_string_data_()) {
|
data_.tagged = reinterpret_cast<uintptr_t>(str) | 1;
|
||||||
data_.string_ptr = new std::string(*other.data_.string_ptr);
|
|
||||||
} else {
|
} else {
|
||||||
data_ = other.data_; // For POD types
|
data_ = other.data_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move constructor
|
// Move constructor
|
||||||
MessageCreator(MessageCreator &&other) noexcept : data_(other.data_), message_type_(other.message_type_) {
|
MessageCreator(MessageCreator &&other) noexcept : data_(other.data_) { other.data_.ptr = nullptr; }
|
||||||
other.message_type_ = 0; // Reset other to function pointer type
|
|
||||||
other.data_.ptr = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assignment operators (needed for batch deduplication)
|
// Assignment operators (needed for batch deduplication)
|
||||||
MessageCreator &operator=(const MessageCreator &other) {
|
MessageCreator &operator=(const MessageCreator &other) {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
// Clean up current string data if needed
|
// Clean up current string data if needed
|
||||||
if (uses_string_data_()) {
|
if (has_tagged_string_ptr_()) {
|
||||||
delete data_.string_ptr;
|
delete get_string_ptr_();
|
||||||
}
|
}
|
||||||
// Copy new data
|
// Copy new data
|
||||||
message_type_ = other.message_type_;
|
if (other.has_tagged_string_ptr_()) {
|
||||||
if (other.message_type_ == 0) {
|
auto *str = new std::string(*other.get_string_ptr_());
|
||||||
data_.ptr = other.data_.ptr;
|
data_.tagged = reinterpret_cast<uintptr_t>(str) | 1;
|
||||||
} else if (other.uses_string_data_()) {
|
|
||||||
data_.string_ptr = new std::string(*other.data_.string_ptr);
|
|
||||||
} else {
|
} else {
|
||||||
data_ = other.data_;
|
data_ = other.data_;
|
||||||
}
|
}
|
||||||
@@ -542,30 +545,35 @@ class APIConnection : public APIServerConnection {
|
|||||||
MessageCreator &operator=(MessageCreator &&other) noexcept {
|
MessageCreator &operator=(MessageCreator &&other) noexcept {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
// Clean up current string data if needed
|
// Clean up current string data if needed
|
||||||
if (uses_string_data_()) {
|
if (has_tagged_string_ptr_()) {
|
||||||
delete data_.string_ptr;
|
delete get_string_ptr_();
|
||||||
}
|
}
|
||||||
// Move data
|
// Move data
|
||||||
message_type_ = other.message_type_;
|
|
||||||
data_ = other.data_;
|
data_ = other.data_;
|
||||||
// Reset other to safe state
|
// Reset other to safe state
|
||||||
other.message_type_ = 0;
|
|
||||||
other.data_.ptr = nullptr;
|
other.data_.ptr = nullptr;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call operator
|
// Call operator - now accepts message_type as parameter
|
||||||
uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single) const;
|
uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single,
|
||||||
|
uint16_t message_type) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Helper to check if this message type uses heap-allocated strings
|
// Check if this contains a string pointer
|
||||||
bool uses_string_data_() const { return message_type_ == EventResponse::MESSAGE_TYPE; }
|
bool has_tagged_string_ptr_() const { return (data_.tagged & 1) != 0; }
|
||||||
union CreatorData {
|
|
||||||
MessageCreatorPtr ptr; // 8 bytes
|
// Get the actual string pointer (clears the tag bit)
|
||||||
std::string *string_ptr; // 8 bytes
|
std::string *get_string_ptr_() const {
|
||||||
} data_; // 8 bytes
|
// NOLINTNEXTLINE(performance-no-int-to-ptr)
|
||||||
uint16_t message_type_; // 2 bytes (0 = function ptr, >0 = state capture)
|
return reinterpret_cast<std::string *>(data_.tagged & ~uintptr_t(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
union {
|
||||||
|
MessageCreatorPtr ptr;
|
||||||
|
uintptr_t tagged;
|
||||||
|
} data_; // 4 bytes on 32-bit
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generic batching mechanism for both state updates and entity info
|
// Generic batching mechanism for both state updates and entity info
|
||||||
@@ -591,6 +599,8 @@ class APIConnection : public APIServerConnection {
|
|||||||
|
|
||||||
// Add item to the batch
|
// Add item to the batch
|
||||||
void add_item(EntityBase *entity, MessageCreator creator, uint16_t message_type);
|
void add_item(EntityBase *entity, MessageCreator creator, uint16_t message_type);
|
||||||
|
// Add item to the front of the batch (for high priority messages like ping)
|
||||||
|
void add_item_front(EntityBase *entity, MessageCreator creator, uint16_t message_type);
|
||||||
void clear() {
|
void clear() {
|
||||||
items.clear();
|
items.clear();
|
||||||
batch_scheduled = false;
|
batch_scheduled = false;
|
||||||
@@ -630,6 +640,12 @@ class APIConnection : public APIServerConnection {
|
|||||||
bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint16_t message_type) {
|
bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint16_t message_type) {
|
||||||
return schedule_message_(entity, MessageCreator(function_ptr), message_type);
|
return schedule_message_(entity, MessageCreator(function_ptr), message_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to schedule a high priority message at the front of the batch
|
||||||
|
bool schedule_message_front_(EntityBase *entity, MessageCreatorPtr function_ptr, uint16_t message_type) {
|
||||||
|
this->deferred_batch_.add_item_front(entity, MessageCreator(function_ptr), message_type);
|
||||||
|
return this->schedule_batch_();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|||||||
@@ -66,6 +66,17 @@ const char *api_error_to_str(APIError err) {
|
|||||||
return "UNKNOWN";
|
return "UNKNOWN";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Default implementation for loop - handles sending buffered data
|
||||||
|
APIError APIFrameHelper::loop() {
|
||||||
|
if (!this->tx_buf_.empty()) {
|
||||||
|
APIError err = try_send_tx_buf_();
|
||||||
|
if (err != APIError::OK && err != APIError::WOULD_BLOCK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return APIError::OK; // Convert WOULD_BLOCK to OK to avoid connection termination
|
||||||
|
}
|
||||||
|
|
||||||
// Helper method to buffer data from IOVs
|
// Helper method to buffer data from IOVs
|
||||||
void APIFrameHelper::buffer_data_from_iov_(const struct iovec *iov, int iovcnt, uint16_t total_write_len) {
|
void APIFrameHelper::buffer_data_from_iov_(const struct iovec *iov, int iovcnt, uint16_t total_write_len) {
|
||||||
SendBuffer buffer;
|
SendBuffer buffer;
|
||||||
@@ -287,13 +298,8 @@ APIError APINoiseFrameHelper::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->tx_buf_.empty()) {
|
// Use base class implementation for buffer sending
|
||||||
APIError err = try_send_tx_buf_();
|
return APIFrameHelper::loop();
|
||||||
if (err != APIError::OK && err != APIError::WOULD_BLOCK) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return APIError::OK; // Convert WOULD_BLOCK to OK to avoid connection termination
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read a packet into the rx_buf_. If successful, stores frame data in the frame parameter
|
/** Read a packet into the rx_buf_. If successful, stores frame data in the frame parameter
|
||||||
@@ -339,17 +345,15 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
|||||||
return APIError::WOULD_BLOCK;
|
return APIError::WOULD_BLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rx_header_buf_[0] != 0x01) {
|
||||||
|
state_ = State::FAILED;
|
||||||
|
HELPER_LOG("Bad indicator byte %u", rx_header_buf_[0]);
|
||||||
|
return APIError::BAD_INDICATOR;
|
||||||
|
}
|
||||||
// header reading done
|
// header reading done
|
||||||
}
|
}
|
||||||
|
|
||||||
// read body
|
// read body
|
||||||
uint8_t indicator = rx_header_buf_[0];
|
|
||||||
if (indicator != 0x01) {
|
|
||||||
state_ = State::FAILED;
|
|
||||||
HELPER_LOG("Bad indicator byte %u", indicator);
|
|
||||||
return APIError::BAD_INDICATOR;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t msg_size = (((uint16_t) rx_header_buf_[1]) << 8) | rx_header_buf_[2];
|
uint16_t msg_size = (((uint16_t) rx_header_buf_[1]) << 8) | rx_header_buf_[2];
|
||||||
|
|
||||||
if (state_ != State::DATA && msg_size > 128) {
|
if (state_ != State::DATA && msg_size > 128) {
|
||||||
@@ -595,10 +599,6 @@ APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
|||||||
return APIError::BAD_DATA_PACKET;
|
return APIError::BAD_DATA_PACKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
// uint16_t type;
|
|
||||||
// uint16_t data_len;
|
|
||||||
// uint8_t *data;
|
|
||||||
// uint8_t *padding; zero or more bytes to fill up the rest of the packet
|
|
||||||
uint16_t type = (((uint16_t) msg_data[0]) << 8) | msg_data[1];
|
uint16_t type = (((uint16_t) msg_data[0]) << 8) | msg_data[1];
|
||||||
uint16_t data_len = (((uint16_t) msg_data[2]) << 8) | msg_data[3];
|
uint16_t data_len = (((uint16_t) msg_data[2]) << 8) | msg_data[3];
|
||||||
if (data_len > msg_size - 4) {
|
if (data_len > msg_size - 4) {
|
||||||
@@ -831,18 +831,12 @@ APIError APIPlaintextFrameHelper::init() {
|
|||||||
state_ = State::DATA;
|
state_ = State::DATA;
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
}
|
}
|
||||||
/// Not used for plaintext
|
|
||||||
APIError APIPlaintextFrameHelper::loop() {
|
APIError APIPlaintextFrameHelper::loop() {
|
||||||
if (state_ != State::DATA) {
|
if (state_ != State::DATA) {
|
||||||
return APIError::BAD_STATE;
|
return APIError::BAD_STATE;
|
||||||
}
|
}
|
||||||
if (!this->tx_buf_.empty()) {
|
// Use base class implementation for buffer sending
|
||||||
APIError err = try_send_tx_buf_();
|
return APIFrameHelper::loop();
|
||||||
if (err != APIError::OK && err != APIError::WOULD_BLOCK) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return APIError::OK; // Convert WOULD_BLOCK to OK to avoid connection termination
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read a packet into the rx_buf_. If successful, stores frame data in the frame parameter
|
/** Read a packet into the rx_buf_. If successful, stores frame data in the frame parameter
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ struct PacketInfo {
|
|||||||
: message_type(type), offset(off), payload_size(size), padding(0) {}
|
: message_type(type), offset(off), payload_size(size), padding(0) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class APIError : int {
|
enum class APIError : uint16_t {
|
||||||
OK = 0,
|
OK = 0,
|
||||||
WOULD_BLOCK = 1001,
|
WOULD_BLOCK = 1001,
|
||||||
BAD_HANDSHAKE_PACKET_LEN = 1002,
|
BAD_HANDSHAKE_PACKET_LEN = 1002,
|
||||||
@@ -74,7 +74,7 @@ class APIFrameHelper {
|
|||||||
}
|
}
|
||||||
virtual ~APIFrameHelper() = default;
|
virtual ~APIFrameHelper() = default;
|
||||||
virtual APIError init() = 0;
|
virtual APIError init() = 0;
|
||||||
virtual APIError loop() = 0;
|
virtual APIError loop();
|
||||||
virtual APIError read_packet(ReadPacketBuffer *buffer) = 0;
|
virtual APIError read_packet(ReadPacketBuffer *buffer) = 0;
|
||||||
bool can_write_without_blocking() { return state_ == State::DATA && tx_buf_.empty(); }
|
bool can_write_without_blocking() { return state_ == State::DATA && tx_buf_.empty(); }
|
||||||
std::string getpeername() { return socket_->getpeername(); }
|
std::string getpeername() { return socket_->getpeername(); }
|
||||||
|
|||||||
@@ -47,6 +47,11 @@ void APIServer::setup() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Schedule reboot if no clients connect within timeout
|
||||||
|
if (this->reboot_timeout_ != 0) {
|
||||||
|
this->schedule_reboot_timeout_();
|
||||||
|
}
|
||||||
|
|
||||||
this->socket_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections
|
this->socket_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections
|
||||||
if (this->socket_ == nullptr) {
|
if (this->socket_ == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not create socket");
|
ESP_LOGW(TAG, "Could not create socket");
|
||||||
@@ -106,8 +111,6 @@ void APIServer::setup() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
this->last_connected_ = App.get_loop_component_start_time();
|
|
||||||
|
|
||||||
#ifdef USE_ESP32_CAMERA
|
#ifdef USE_ESP32_CAMERA
|
||||||
if (esp32_camera::global_esp32_camera != nullptr && !esp32_camera::global_esp32_camera->is_internal()) {
|
if (esp32_camera::global_esp32_camera != nullptr && !esp32_camera::global_esp32_camera->is_internal()) {
|
||||||
esp32_camera::global_esp32_camera->add_image_callback(
|
esp32_camera::global_esp32_camera->add_image_callback(
|
||||||
@@ -121,6 +124,16 @@ void APIServer::setup() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void APIServer::schedule_reboot_timeout_() {
|
||||||
|
this->status_set_warning();
|
||||||
|
this->set_timeout("api_reboot", this->reboot_timeout_, []() {
|
||||||
|
if (!global_api_server->is_connected()) {
|
||||||
|
ESP_LOGE(TAG, "No clients; rebooting");
|
||||||
|
App.reboot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void APIServer::loop() {
|
void APIServer::loop() {
|
||||||
// Accept new clients only if the socket exists and has incoming connections
|
// Accept new clients only if the socket exists and has incoming connections
|
||||||
if (this->socket_ && this->socket_->ready()) {
|
if (this->socket_ && this->socket_->ready()) {
|
||||||
@@ -130,51 +143,61 @@ void APIServer::loop() {
|
|||||||
auto sock = this->socket_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len);
|
auto sock = this->socket_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len);
|
||||||
if (!sock)
|
if (!sock)
|
||||||
break;
|
break;
|
||||||
ESP_LOGD(TAG, "Accepted %s", sock->getpeername().c_str());
|
ESP_LOGD(TAG, "Accept %s", sock->getpeername().c_str());
|
||||||
|
|
||||||
auto *conn = new APIConnection(std::move(sock), this);
|
auto *conn = new APIConnection(std::move(sock), this);
|
||||||
this->clients_.emplace_back(conn);
|
this->clients_.emplace_back(conn);
|
||||||
conn->start();
|
conn->start();
|
||||||
|
|
||||||
|
// Clear warning status and cancel reboot when first client connects
|
||||||
|
if (this->clients_.size() == 1 && this->reboot_timeout_ != 0) {
|
||||||
|
this->status_clear_warning();
|
||||||
|
this->cancel_timeout("api_reboot");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this->clients_.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Process clients and remove disconnected ones in a single pass
|
// Process clients and remove disconnected ones in a single pass
|
||||||
if (!this->clients_.empty()) {
|
// Check network connectivity once for all clients
|
||||||
size_t client_index = 0;
|
if (!network::is_connected()) {
|
||||||
while (client_index < this->clients_.size()) {
|
// Network is down - disconnect all clients
|
||||||
auto &client = this->clients_[client_index];
|
for (auto &client : this->clients_) {
|
||||||
|
client->on_fatal_error();
|
||||||
if (client->remove_) {
|
ESP_LOGW(TAG, "%s: Network down; disconnect", client->get_client_combined_info().c_str());
|
||||||
// Handle disconnection
|
|
||||||
this->client_disconnected_trigger_->trigger(client->client_info_, client->client_peername_);
|
|
||||||
ESP_LOGV(TAG, "Removing connection to %s", client->client_info_.c_str());
|
|
||||||
|
|
||||||
// Swap with the last element and pop (avoids expensive vector shifts)
|
|
||||||
if (client_index < this->clients_.size() - 1) {
|
|
||||||
std::swap(this->clients_[client_index], this->clients_.back());
|
|
||||||
}
|
|
||||||
this->clients_.pop_back();
|
|
||||||
// Don't increment client_index since we need to process the swapped element
|
|
||||||
} else {
|
|
||||||
// Process active client
|
|
||||||
client->loop();
|
|
||||||
client_index++; // Move to next client
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// Continue to process and clean up the clients below
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->reboot_timeout_ != 0) {
|
size_t client_index = 0;
|
||||||
const uint32_t now = App.get_loop_component_start_time();
|
while (client_index < this->clients_.size()) {
|
||||||
if (!this->is_connected()) {
|
auto &client = this->clients_[client_index];
|
||||||
if (now - this->last_connected_ > this->reboot_timeout_) {
|
|
||||||
ESP_LOGE(TAG, "No client connected; rebooting");
|
if (!client->remove_) {
|
||||||
App.reboot();
|
// Common case: process active client
|
||||||
}
|
client->loop();
|
||||||
this->status_set_warning();
|
client_index++;
|
||||||
} else {
|
continue;
|
||||||
this->last_connected_ = now;
|
|
||||||
this->status_clear_warning();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rare case: handle disconnection
|
||||||
|
this->client_disconnected_trigger_->trigger(client->client_info_, client->client_peername_);
|
||||||
|
ESP_LOGV(TAG, "Remove connection %s", client->client_info_.c_str());
|
||||||
|
|
||||||
|
// Swap with the last element and pop (avoids expensive vector shifts)
|
||||||
|
if (client_index < this->clients_.size() - 1) {
|
||||||
|
std::swap(this->clients_[client_index], this->clients_.back());
|
||||||
|
}
|
||||||
|
this->clients_.pop_back();
|
||||||
|
|
||||||
|
// Schedule reboot when last client disconnects
|
||||||
|
if (this->clients_.empty() && this->reboot_timeout_ != 0) {
|
||||||
|
this->schedule_reboot_timeout_();
|
||||||
|
}
|
||||||
|
// Don't increment client_index since we need to process the swapped element
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -503,8 +526,8 @@ void APIServer::on_shutdown() {
|
|||||||
for (auto &c : this->clients_) {
|
for (auto &c : this->clients_) {
|
||||||
if (!c->send_message(DisconnectRequest())) {
|
if (!c->send_message(DisconnectRequest())) {
|
||||||
// If we can't send the disconnect request directly (tx_buffer full),
|
// If we can't send the disconnect request directly (tx_buffer full),
|
||||||
// schedule it in the batch so it will be sent with the 5ms timer
|
// schedule it at the front of the batch so it will be sent with priority
|
||||||
c->schedule_message_(nullptr, &APIConnection::try_send_disconnect_request, DisconnectRequest::MESSAGE_TYPE);
|
c->schedule_message_front_(nullptr, &APIConnection::try_send_disconnect_request, DisconnectRequest::MESSAGE_TYPE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,6 +142,7 @@ class APIServer : public Component, public Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void schedule_reboot_timeout_();
|
||||||
// Pointers and pointer-like types first (4 bytes each)
|
// Pointers and pointer-like types first (4 bytes each)
|
||||||
std::unique_ptr<socket::Socket> socket_ = nullptr;
|
std::unique_ptr<socket::Socket> socket_ = nullptr;
|
||||||
Trigger<std::string, std::string> *client_connected_trigger_ = new Trigger<std::string, std::string>();
|
Trigger<std::string, std::string> *client_connected_trigger_ = new Trigger<std::string, std::string>();
|
||||||
@@ -150,7 +151,6 @@ class APIServer : public Component, public Controller {
|
|||||||
// 4-byte aligned types
|
// 4-byte aligned types
|
||||||
uint32_t reboot_timeout_{300000};
|
uint32_t reboot_timeout_{300000};
|
||||||
uint32_t batch_delay_{100};
|
uint32_t batch_delay_{100};
|
||||||
uint32_t last_connected_{0};
|
|
||||||
|
|
||||||
// Vectors and strings (12 bytes each on 32-bit)
|
// Vectors and strings (12 bytes each on 32-bit)
|
||||||
std::vector<std::unique_ptr<APIConnection>> clients_;
|
std::vector<std::unique_ptr<APIConnection>> clients_;
|
||||||
|
|||||||
@@ -10,11 +10,24 @@ GPIOBinarySensor = gpio_ns.class_(
|
|||||||
"GPIOBinarySensor", binary_sensor.BinarySensor, cg.Component
|
"GPIOBinarySensor", binary_sensor.BinarySensor, cg.Component
|
||||||
)
|
)
|
||||||
|
|
||||||
|
CONF_USE_INTERRUPT = "use_interrupt"
|
||||||
|
CONF_INTERRUPT_TYPE = "interrupt_type"
|
||||||
|
|
||||||
|
INTERRUPT_TYPES = {
|
||||||
|
"RISING": gpio_ns.INTERRUPT_RISING_EDGE,
|
||||||
|
"FALLING": gpio_ns.INTERRUPT_FALLING_EDGE,
|
||||||
|
"ANY": gpio_ns.INTERRUPT_ANY_EDGE,
|
||||||
|
}
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
binary_sensor.binary_sensor_schema(GPIOBinarySensor)
|
binary_sensor.binary_sensor_schema(GPIOBinarySensor)
|
||||||
.extend(
|
.extend(
|
||||||
{
|
{
|
||||||
cv.Required(CONF_PIN): pins.gpio_input_pin_schema,
|
cv.Required(CONF_PIN): pins.gpio_input_pin_schema,
|
||||||
|
cv.Optional(CONF_USE_INTERRUPT, default=True): cv.boolean,
|
||||||
|
cv.Optional(CONF_INTERRUPT_TYPE, default="ANY"): cv.enum(
|
||||||
|
INTERRUPT_TYPES, upper=True
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.extend(cv.COMPONENT_SCHEMA)
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
@@ -27,3 +40,7 @@ async def to_code(config):
|
|||||||
|
|
||||||
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
||||||
cg.add(var.set_pin(pin))
|
cg.add(var.set_pin(pin))
|
||||||
|
|
||||||
|
cg.add(var.set_use_interrupt(config[CONF_USE_INTERRUPT]))
|
||||||
|
if config[CONF_USE_INTERRUPT]:
|
||||||
|
cg.add(var.set_interrupt_type(INTERRUPT_TYPES[config[CONF_INTERRUPT_TYPE]]))
|
||||||
|
|||||||
@@ -6,17 +6,91 @@ namespace gpio {
|
|||||||
|
|
||||||
static const char *const TAG = "gpio.binary_sensor";
|
static const char *const TAG = "gpio.binary_sensor";
|
||||||
|
|
||||||
|
void IRAM_ATTR GPIOBinarySensorStore::gpio_intr(GPIOBinarySensorStore *arg) {
|
||||||
|
bool new_state = arg->isr_pin_.digital_read();
|
||||||
|
if (new_state != arg->last_state_) {
|
||||||
|
arg->state_ = new_state;
|
||||||
|
arg->last_state_ = new_state;
|
||||||
|
arg->changed_ = true;
|
||||||
|
// Wake up the component from its disabled loop state
|
||||||
|
if (arg->component_ != nullptr) {
|
||||||
|
arg->component_->enable_loop_soon_any_context();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GPIOBinarySensorStore::setup(InternalGPIOPin *pin, gpio::InterruptType type, Component *component) {
|
||||||
|
pin->setup();
|
||||||
|
this->isr_pin_ = pin->to_isr();
|
||||||
|
this->component_ = component;
|
||||||
|
|
||||||
|
// Read initial state
|
||||||
|
this->last_state_ = pin->digital_read();
|
||||||
|
this->state_ = this->last_state_;
|
||||||
|
|
||||||
|
// Attach interrupt - from this point on, any changes will be caught by the interrupt
|
||||||
|
pin->attach_interrupt(&GPIOBinarySensorStore::gpio_intr, this, type);
|
||||||
|
}
|
||||||
|
|
||||||
void GPIOBinarySensor::setup() {
|
void GPIOBinarySensor::setup() {
|
||||||
this->pin_->setup();
|
if (this->use_interrupt_ && !this->pin_->is_internal()) {
|
||||||
this->publish_initial_state(this->pin_->digital_read());
|
ESP_LOGD(TAG, "GPIO is not internal, falling back to polling mode");
|
||||||
|
this->use_interrupt_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this->use_interrupt_) {
|
||||||
|
auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_);
|
||||||
|
this->store_.setup(internal_pin, this->interrupt_type_, this);
|
||||||
|
this->publish_initial_state(this->store_.get_state());
|
||||||
|
} else {
|
||||||
|
this->pin_->setup();
|
||||||
|
this->publish_initial_state(this->pin_->digital_read());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPIOBinarySensor::dump_config() {
|
void GPIOBinarySensor::dump_config() {
|
||||||
LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
|
LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
|
||||||
LOG_PIN(" Pin: ", this->pin_);
|
LOG_PIN(" Pin: ", this->pin_);
|
||||||
|
const char *mode = this->use_interrupt_ ? "interrupt" : "polling";
|
||||||
|
ESP_LOGCONFIG(TAG, " Mode: %s", mode);
|
||||||
|
if (this->use_interrupt_) {
|
||||||
|
const char *interrupt_type;
|
||||||
|
switch (this->interrupt_type_) {
|
||||||
|
case gpio::INTERRUPT_RISING_EDGE:
|
||||||
|
interrupt_type = "RISING_EDGE";
|
||||||
|
break;
|
||||||
|
case gpio::INTERRUPT_FALLING_EDGE:
|
||||||
|
interrupt_type = "FALLING_EDGE";
|
||||||
|
break;
|
||||||
|
case gpio::INTERRUPT_ANY_EDGE:
|
||||||
|
interrupt_type = "ANY_EDGE";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
interrupt_type = "UNKNOWN";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ESP_LOGCONFIG(TAG, " Interrupt Type: %s", interrupt_type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPIOBinarySensor::loop() { this->publish_state(this->pin_->digital_read()); }
|
void GPIOBinarySensor::loop() {
|
||||||
|
if (this->use_interrupt_) {
|
||||||
|
if (this->store_.is_changed()) {
|
||||||
|
// Clear the flag immediately to minimize the window where we might miss changes
|
||||||
|
this->store_.clear_changed();
|
||||||
|
// Read the state and publish it
|
||||||
|
// Note: If the ISR fires between clear_changed() and get_state(), that's fine -
|
||||||
|
// we'll process the new change on the next loop iteration
|
||||||
|
bool state = this->store_.get_state();
|
||||||
|
this->publish_state(state);
|
||||||
|
} else {
|
||||||
|
// No changes, disable the loop until the next interrupt
|
||||||
|
this->disable_loop();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this->publish_state(this->pin_->digital_read());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
float GPIOBinarySensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
float GPIOBinarySensor::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,51 @@
|
|||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace gpio {
|
namespace gpio {
|
||||||
|
|
||||||
|
// Store class for ISR data (no vtables, ISR-safe)
|
||||||
|
class GPIOBinarySensorStore {
|
||||||
|
public:
|
||||||
|
void setup(InternalGPIOPin *pin, gpio::InterruptType type, Component *component);
|
||||||
|
|
||||||
|
static void gpio_intr(GPIOBinarySensorStore *arg);
|
||||||
|
|
||||||
|
bool get_state() const {
|
||||||
|
// No lock needed: state_ is atomically updated by ISR
|
||||||
|
// Volatile ensures we read the latest value
|
||||||
|
return this->state_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_changed() const {
|
||||||
|
// Simple read of volatile bool - no clearing here
|
||||||
|
return this->changed_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_changed() {
|
||||||
|
// Separate method to clear the flag
|
||||||
|
this->changed_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ISRInternalGPIOPin isr_pin_;
|
||||||
|
volatile bool state_{false};
|
||||||
|
volatile bool last_state_{false};
|
||||||
|
volatile bool changed_{false};
|
||||||
|
Component *component_{nullptr}; // Pointer to the component for enable_loop_soon_any_context()
|
||||||
|
};
|
||||||
|
|
||||||
class GPIOBinarySensor : public binary_sensor::BinarySensor, public Component {
|
class GPIOBinarySensor : public binary_sensor::BinarySensor, public Component {
|
||||||
public:
|
public:
|
||||||
|
// No destructor needed: ESPHome components are created at boot and live forever.
|
||||||
|
// Interrupts are only detached on reboot when memory is cleared anyway.
|
||||||
|
|
||||||
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
||||||
|
void set_use_interrupt(bool use_interrupt) { use_interrupt_ = use_interrupt; }
|
||||||
|
void set_interrupt_type(gpio::InterruptType type) { interrupt_type_ = type; }
|
||||||
// ========== INTERNAL METHODS ==========
|
// ========== INTERNAL METHODS ==========
|
||||||
// (In most use cases you won't need these)
|
// (In most use cases you won't need these)
|
||||||
/// Setup pin
|
/// Setup pin
|
||||||
@@ -22,6 +59,9 @@ class GPIOBinarySensor : public binary_sensor::BinarySensor, public Component {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
GPIOPin *pin_;
|
GPIOPin *pin_;
|
||||||
|
bool use_interrupt_{true};
|
||||||
|
gpio::InterruptType interrupt_type_{gpio::INTERRUPT_ANY_EDGE};
|
||||||
|
GPIOBinarySensorStore store_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace gpio
|
} // namespace gpio
|
||||||
|
|||||||
@@ -48,6 +48,11 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
|
|||||||
// For non-main tasks, queue the message for callbacks - but only if we have any callbacks registered
|
// For non-main tasks, queue the message for callbacks - but only if we have any callbacks registered
|
||||||
message_sent =
|
message_sent =
|
||||||
this->log_buffer_->send_message_thread_safe(level, tag, static_cast<uint16_t>(line), current_task, format, args);
|
this->log_buffer_->send_message_thread_safe(level, tag, static_cast<uint16_t>(line), current_task, format, args);
|
||||||
|
if (message_sent) {
|
||||||
|
// Enable logger loop to process the buffered message
|
||||||
|
// This is safe to call from any context including ISRs
|
||||||
|
this->enable_loop_soon_any_context();
|
||||||
|
}
|
||||||
#endif // USE_ESPHOME_TASK_LOG_BUFFER
|
#endif // USE_ESPHOME_TASK_LOG_BUFFER
|
||||||
|
|
||||||
// Emergency console logging for non-main tasks when ring buffer is full or disabled
|
// Emergency console logging for non-main tasks when ring buffer is full or disabled
|
||||||
@@ -139,6 +144,10 @@ Logger::Logger(uint32_t baud_rate, size_t tx_buffer_size) : baud_rate_(baud_rate
|
|||||||
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
|
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
|
||||||
void Logger::init_log_buffer(size_t total_buffer_size) {
|
void Logger::init_log_buffer(size_t total_buffer_size) {
|
||||||
this->log_buffer_ = esphome::make_unique<logger::TaskLogBuffer>(total_buffer_size);
|
this->log_buffer_ = esphome::make_unique<logger::TaskLogBuffer>(total_buffer_size);
|
||||||
|
|
||||||
|
// Start with loop disabled when using task buffer (unless using USB CDC)
|
||||||
|
// The loop will be enabled automatically when messages arrive
|
||||||
|
this->disable_loop_when_buffer_empty_();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -189,6 +198,10 @@ void Logger::loop() {
|
|||||||
this->write_msg_(this->tx_buffer_);
|
this->write_msg_(this->tx_buffer_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// No messages to process, disable loop if appropriate
|
||||||
|
// This reduces overhead when there's no async logging activity
|
||||||
|
this->disable_loop_when_buffer_empty_();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -358,6 +358,26 @@ class Logger : public Component {
|
|||||||
static const uint16_t RESET_COLOR_LEN = strlen(ESPHOME_LOG_RESET_COLOR);
|
static const uint16_t RESET_COLOR_LEN = strlen(ESPHOME_LOG_RESET_COLOR);
|
||||||
this->write_body_to_buffer_(ESPHOME_LOG_RESET_COLOR, RESET_COLOR_LEN, buffer, buffer_at, buffer_size);
|
this->write_body_to_buffer_(ESPHOME_LOG_RESET_COLOR, RESET_COLOR_LEN, buffer, buffer_at, buffer_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_ESP32
|
||||||
|
// Disable loop when task buffer is empty (with USB CDC check)
|
||||||
|
inline void disable_loop_when_buffer_empty_() {
|
||||||
|
// Thread safety note: This is safe even if another task calls enable_loop_soon_any_context()
|
||||||
|
// concurrently. If that happens between our check and disable_loop(), the enable request
|
||||||
|
// will be processed on the next main loop iteration since:
|
||||||
|
// - disable_loop() takes effect immediately
|
||||||
|
// - enable_loop_soon_any_context() sets a pending flag that's checked at loop start
|
||||||
|
#if defined(USE_LOGGER_USB_CDC) && defined(USE_ARDUINO)
|
||||||
|
// Only disable if not using USB CDC (which needs loop for connection detection)
|
||||||
|
if (this->uart_ != UART_SELECTION_USB_CDC) {
|
||||||
|
this->disable_loop();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// No USB CDC support, always safe to disable
|
||||||
|
this->disable_loop();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
extern Logger *global_logger; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
extern Logger *global_logger; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
|
|||||||
26
esphome/components/runtime_stats/__init__.py
Normal file
26
esphome/components/runtime_stats/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
"""
|
||||||
|
Runtime statistics component for ESPHome.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import esphome.codegen as cg
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
|
||||||
|
DEPENDENCIES = []
|
||||||
|
|
||||||
|
CONF_ENABLED = "enabled"
|
||||||
|
CONF_LOG_INTERVAL = "log_interval"
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = cv.Schema(
|
||||||
|
{
|
||||||
|
cv.Optional(CONF_ENABLED, default=True): cv.boolean,
|
||||||
|
cv.Optional(
|
||||||
|
CONF_LOG_INTERVAL, default=60000
|
||||||
|
): cv.positive_time_period_milliseconds,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def to_code(config):
|
||||||
|
"""Generate code for the runtime statistics component."""
|
||||||
|
cg.add(cg.App.set_runtime_stats_enabled(config[CONF_ENABLED]))
|
||||||
|
cg.add(cg.App.set_runtime_stats_log_interval(config[CONF_LOG_INTERVAL]))
|
||||||
@@ -136,6 +136,10 @@ void Application::loop() {
|
|||||||
this->in_loop_ = false;
|
this->in_loop_ = false;
|
||||||
this->app_state_ = new_app_state;
|
this->app_state_ = new_app_state;
|
||||||
|
|
||||||
|
// Process any pending runtime stats printing after all components have run
|
||||||
|
// This ensures stats printing doesn't affect component timing measurements
|
||||||
|
runtime_stats.process_pending_stats(last_op_end_time);
|
||||||
|
|
||||||
// Use the last component's end time instead of calling millis() again
|
// Use the last component's end time instead of calling millis() again
|
||||||
auto elapsed = last_op_end_time - this->last_loop_;
|
auto elapsed = last_op_end_time - this->last_loop_;
|
||||||
if (elapsed >= this->loop_interval_ || HighFrequencyLoopRequester::is_high_frequency()) {
|
if (elapsed >= this->loop_interval_ || HighFrequencyLoopRequester::is_high_frequency()) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
@@ -7,6 +9,7 @@
|
|||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/preferences.h"
|
#include "esphome/core/preferences.h"
|
||||||
|
#include "esphome/core/runtime_stats.h"
|
||||||
#include "esphome/core/scheduler.h"
|
#include "esphome/core/scheduler.h"
|
||||||
|
|
||||||
#ifdef USE_DEVICES
|
#ifdef USE_DEVICES
|
||||||
@@ -337,9 +340,23 @@ class Application {
|
|||||||
*
|
*
|
||||||
* @param loop_interval The interval in milliseconds to run the core loop at. Defaults to 16 milliseconds.
|
* @param loop_interval The interval in milliseconds to run the core loop at. Defaults to 16 milliseconds.
|
||||||
*/
|
*/
|
||||||
void set_loop_interval(uint32_t loop_interval) { this->loop_interval_ = loop_interval; }
|
void set_loop_interval(uint32_t loop_interval) {
|
||||||
|
this->loop_interval_ = std::min(loop_interval, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t get_loop_interval() const { return this->loop_interval_; }
|
uint32_t get_loop_interval() const { return static_cast<uint32_t>(this->loop_interval_); }
|
||||||
|
|
||||||
|
/** Enable or disable runtime statistics collection.
|
||||||
|
*
|
||||||
|
* @param enable Whether to enable runtime statistics collection.
|
||||||
|
*/
|
||||||
|
void set_runtime_stats_enabled(bool enable) { runtime_stats.set_enabled(enable); }
|
||||||
|
|
||||||
|
/** Set the interval at which runtime statistics are logged.
|
||||||
|
*
|
||||||
|
* @param interval The interval in milliseconds between logging of runtime statistics.
|
||||||
|
*/
|
||||||
|
void set_runtime_stats_log_interval(uint32_t interval) { runtime_stats.set_log_interval(interval); }
|
||||||
|
|
||||||
void schedule_dump_config() { this->dump_config_at_ = 0; }
|
void schedule_dump_config() { this->dump_config_at_ = 0; }
|
||||||
|
|
||||||
@@ -618,6 +635,17 @@ class Application {
|
|||||||
/// Perform a delay while also monitoring socket file descriptors for readiness
|
/// Perform a delay while also monitoring socket file descriptors for readiness
|
||||||
void yield_with_select_(uint32_t delay_ms);
|
void yield_with_select_(uint32_t delay_ms);
|
||||||
|
|
||||||
|
// === Member variables ordered by size to minimize padding ===
|
||||||
|
|
||||||
|
// Pointer-sized members first
|
||||||
|
Component *current_component_{nullptr};
|
||||||
|
const char *comment_{nullptr};
|
||||||
|
const char *compilation_time_{nullptr};
|
||||||
|
|
||||||
|
// size_t members
|
||||||
|
size_t dump_config_at_{SIZE_MAX};
|
||||||
|
|
||||||
|
// Vectors (largest members)
|
||||||
std::vector<Component *> components_{};
|
std::vector<Component *> components_{};
|
||||||
|
|
||||||
// Partitioned vector design for looping components
|
// Partitioned vector design for looping components
|
||||||
@@ -637,11 +665,6 @@ class Application {
|
|||||||
// and active_end_ is incremented
|
// and active_end_ is incremented
|
||||||
// - This eliminates branch mispredictions from flag checking in the hot loop
|
// - This eliminates branch mispredictions from flag checking in the hot loop
|
||||||
std::vector<Component *> looping_components_{};
|
std::vector<Component *> looping_components_{};
|
||||||
uint16_t looping_components_active_end_{0};
|
|
||||||
|
|
||||||
// For safe reentrant modifications during iteration
|
|
||||||
uint16_t current_loop_index_{0};
|
|
||||||
bool in_loop_{false};
|
|
||||||
|
|
||||||
#ifdef USE_DEVICES
|
#ifdef USE_DEVICES
|
||||||
std::vector<Device *> devices_{};
|
std::vector<Device *> devices_{};
|
||||||
@@ -713,26 +736,39 @@ class Application {
|
|||||||
std::vector<update::UpdateEntity *> updates_{};
|
std::vector<update::UpdateEntity *> updates_{};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||||
|
std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// String members
|
||||||
std::string name_;
|
std::string name_;
|
||||||
std::string friendly_name_;
|
std::string friendly_name_;
|
||||||
const char *comment_{nullptr};
|
|
||||||
const char *compilation_time_{nullptr};
|
// 4-byte members
|
||||||
bool name_add_mac_suffix_;
|
|
||||||
uint32_t last_loop_{0};
|
uint32_t last_loop_{0};
|
||||||
uint32_t loop_interval_{16};
|
|
||||||
size_t dump_config_at_{SIZE_MAX};
|
|
||||||
uint8_t app_state_{0};
|
|
||||||
volatile bool has_pending_enable_loop_requests_{false};
|
|
||||||
Component *current_component_{nullptr};
|
|
||||||
uint32_t loop_component_start_time_{0};
|
uint32_t loop_component_start_time_{0};
|
||||||
|
|
||||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||||
// Socket select management
|
int max_fd_{-1}; // Highest file descriptor number for select()
|
||||||
std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
|
#endif
|
||||||
|
|
||||||
|
// 2-byte members (grouped together for alignment)
|
||||||
|
uint16_t loop_interval_{16}; // Loop interval in ms (max 65535ms = 65.5 seconds)
|
||||||
|
uint16_t looping_components_active_end_{0};
|
||||||
|
uint16_t current_loop_index_{0}; // For safe reentrant modifications during iteration
|
||||||
|
|
||||||
|
// 1-byte members (grouped together to minimize padding)
|
||||||
|
uint8_t app_state_{0};
|
||||||
|
bool name_add_mac_suffix_;
|
||||||
|
bool in_loop_{false};
|
||||||
|
volatile bool has_pending_enable_loop_requests_{false};
|
||||||
|
|
||||||
|
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||||
bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
|
bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
|
||||||
int max_fd_{-1}; // Highest file descriptor number for select()
|
|
||||||
fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes
|
// Variable-sized members at end
|
||||||
fd_set read_fds_{}; // Working fd_set for select(), copied from base_read_fds_
|
fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes
|
||||||
|
fd_set read_fds_{}; // Working fd_set for select(), copied from base_read_fds_
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -27,20 +27,67 @@ template<typename T, typename... X> class TemplatableValue {
|
|||||||
public:
|
public:
|
||||||
TemplatableValue() : type_(NONE) {}
|
TemplatableValue() : type_(NONE) {}
|
||||||
|
|
||||||
template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
|
template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0> TemplatableValue(F value) : type_(VALUE) {
|
||||||
TemplatableValue(F value) : type_(VALUE), value_(std::move(value)) {}
|
new (&this->value_) T(std::move(value));
|
||||||
|
}
|
||||||
|
|
||||||
template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0>
|
template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0> TemplatableValue(F f) : type_(LAMBDA) {
|
||||||
TemplatableValue(F f) : type_(LAMBDA), f_(f) {}
|
this->f_ = new std::function<T(X...)>(std::move(f));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy constructor
|
||||||
|
TemplatableValue(const TemplatableValue &other) : type_(other.type_) {
|
||||||
|
if (type_ == VALUE) {
|
||||||
|
new (&this->value_) T(other.value_);
|
||||||
|
} else if (type_ == LAMBDA) {
|
||||||
|
this->f_ = new std::function<T(X...)>(*other.f_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move constructor
|
||||||
|
TemplatableValue(TemplatableValue &&other) noexcept : type_(other.type_) {
|
||||||
|
if (type_ == VALUE) {
|
||||||
|
new (&this->value_) T(std::move(other.value_));
|
||||||
|
} else if (type_ == LAMBDA) {
|
||||||
|
this->f_ = other.f_;
|
||||||
|
other.f_ = nullptr;
|
||||||
|
}
|
||||||
|
other.type_ = NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assignment operators
|
||||||
|
TemplatableValue &operator=(const TemplatableValue &other) {
|
||||||
|
if (this != &other) {
|
||||||
|
this->~TemplatableValue();
|
||||||
|
new (this) TemplatableValue(other);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
TemplatableValue &operator=(TemplatableValue &&other) noexcept {
|
||||||
|
if (this != &other) {
|
||||||
|
this->~TemplatableValue();
|
||||||
|
new (this) TemplatableValue(std::move(other));
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
~TemplatableValue() {
|
||||||
|
if (type_ == VALUE) {
|
||||||
|
this->value_.~T();
|
||||||
|
} else if (type_ == LAMBDA) {
|
||||||
|
delete this->f_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool has_value() { return this->type_ != NONE; }
|
bool has_value() { return this->type_ != NONE; }
|
||||||
|
|
||||||
T value(X... x) {
|
T value(X... x) {
|
||||||
if (this->type_ == LAMBDA) {
|
if (this->type_ == LAMBDA) {
|
||||||
return this->f_(x...);
|
return (*this->f_)(x...);
|
||||||
}
|
}
|
||||||
// return value also when none
|
// return value also when none
|
||||||
return this->value_;
|
return this->type_ == VALUE ? this->value_ : T{};
|
||||||
}
|
}
|
||||||
|
|
||||||
optional<T> optional_value(X... x) {
|
optional<T> optional_value(X... x) {
|
||||||
@@ -58,14 +105,16 @@ template<typename T, typename... X> class TemplatableValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
enum {
|
enum : uint8_t {
|
||||||
NONE,
|
NONE,
|
||||||
VALUE,
|
VALUE,
|
||||||
LAMBDA,
|
LAMBDA,
|
||||||
} type_;
|
} type_;
|
||||||
|
|
||||||
T value_{};
|
union {
|
||||||
std::function<T(X...)> f_{};
|
T value_;
|
||||||
|
std::function<T(X...)> *f_;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Base class for all automation conditions.
|
/** Base class for all automation conditions.
|
||||||
|
|||||||
@@ -303,6 +303,9 @@ uint32_t WarnIfComponentBlockingGuard::finish() {
|
|||||||
uint32_t curr_time = millis();
|
uint32_t curr_time = millis();
|
||||||
|
|
||||||
uint32_t blocking_time = curr_time - this->started_;
|
uint32_t blocking_time = curr_time - this->started_;
|
||||||
|
|
||||||
|
// Record component runtime stats
|
||||||
|
runtime_stats.record_component_time(this->component_, blocking_time, curr_time);
|
||||||
bool should_warn;
|
bool should_warn;
|
||||||
if (this->component_ != nullptr) {
|
if (this->component_ != nullptr) {
|
||||||
should_warn = this->component_->should_warn_of_blocking(blocking_time);
|
should_warn = this->component_->should_warn_of_blocking(blocking_time);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "esphome/core/optional.h"
|
#include "esphome/core/optional.h"
|
||||||
|
#include "esphome/core/runtime_stats.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
|
|||||||
@@ -375,7 +375,7 @@ void ComponentIterator::advance() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (advance_platform) {
|
if (advance_platform) {
|
||||||
this->state_ = static_cast<IteratorState>(static_cast<uint32_t>(this->state_) + 1);
|
this->state_ = static_cast<IteratorState>(static_cast<uint16_t>(this->state_) + 1);
|
||||||
this->at_ = 0;
|
this->at_ = 0;
|
||||||
} else if (success) {
|
} else if (success) {
|
||||||
this->at_++;
|
this->at_++;
|
||||||
|
|||||||
@@ -93,7 +93,9 @@ class ComponentIterator {
|
|||||||
virtual bool on_end();
|
virtual bool on_end();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
enum class IteratorState {
|
// Iterates over all ESPHome entities (sensors, switches, lights, etc.)
|
||||||
|
// Supports up to 256 entity types and up to 65,535 entities of each type
|
||||||
|
enum class IteratorState : uint8_t {
|
||||||
NONE = 0,
|
NONE = 0,
|
||||||
BEGIN,
|
BEGIN,
|
||||||
#ifdef USE_BINARY_SENSOR
|
#ifdef USE_BINARY_SENSOR
|
||||||
@@ -167,7 +169,7 @@ class ComponentIterator {
|
|||||||
#endif
|
#endif
|
||||||
MAX,
|
MAX,
|
||||||
} state_{IteratorState::NONE};
|
} state_{IteratorState::NONE};
|
||||||
size_t at_{0};
|
uint16_t at_{0}; // Supports up to 65,535 entities per type
|
||||||
bool include_internal_{false};
|
bool include_internal_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -132,6 +132,8 @@
|
|||||||
|
|
||||||
// ESP32-specific feature flags
|
// ESP32-specific feature flags
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
#define USE_ESPHOME_TASK_LOG_BUFFER
|
||||||
|
|
||||||
#define USE_BLUETOOTH_PROXY
|
#define USE_BLUETOOTH_PROXY
|
||||||
#define USE_CAPTIVE_PORTAL
|
#define USE_CAPTIVE_PORTAL
|
||||||
#define USE_ESP32_BLE
|
#define USE_ESP32_BLE
|
||||||
|
|||||||
92
esphome/core/runtime_stats.cpp
Normal file
92
esphome/core/runtime_stats.cpp
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
#include "esphome/core/runtime_stats.h"
|
||||||
|
#include "esphome/core/component.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
|
||||||
|
RuntimeStatsCollector runtime_stats;
|
||||||
|
|
||||||
|
void RuntimeStatsCollector::record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time) {
|
||||||
|
if (!this->enabled_ || component == nullptr)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Check if we have cached the name for this component
|
||||||
|
auto name_it = this->component_names_cache_.find(component);
|
||||||
|
if (name_it == this->component_names_cache_.end()) {
|
||||||
|
// First time seeing this component, cache its name
|
||||||
|
const char *source = component->get_component_source();
|
||||||
|
this->component_names_cache_[component] = source;
|
||||||
|
this->component_stats_[source].record_time(duration_ms);
|
||||||
|
} else {
|
||||||
|
// Use cached name - no string operations, just map lookup
|
||||||
|
this->component_stats_[name_it->second].record_time(duration_ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If next_log_time_ is 0, initialize it
|
||||||
|
if (this->next_log_time_ == 0) {
|
||||||
|
this->next_log_time_ = current_time + this->log_interval_;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't print stats here anymore - let process_pending_stats handle it
|
||||||
|
}
|
||||||
|
|
||||||
|
void RuntimeStatsCollector::log_stats_() {
|
||||||
|
ESP_LOGI(RUNTIME_TAG, "Component Runtime Statistics");
|
||||||
|
ESP_LOGI(RUNTIME_TAG, "Period stats (last %" PRIu32 "ms):", this->log_interval_);
|
||||||
|
|
||||||
|
// First collect stats we want to display
|
||||||
|
std::vector<ComponentStatPair> stats_to_display;
|
||||||
|
|
||||||
|
for (const auto &it : this->component_stats_) {
|
||||||
|
const ComponentRuntimeStats &stats = it.second;
|
||||||
|
if (stats.get_period_count() > 0) {
|
||||||
|
ComponentStatPair pair = {it.first, &stats};
|
||||||
|
stats_to_display.push_back(pair);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by period runtime (descending)
|
||||||
|
std::sort(stats_to_display.begin(), stats_to_display.end(), std::greater<ComponentStatPair>());
|
||||||
|
|
||||||
|
// Log top components by period runtime
|
||||||
|
for (const auto &it : stats_to_display) {
|
||||||
|
const std::string &source = it.name;
|
||||||
|
const ComponentRuntimeStats *stats = it.stats;
|
||||||
|
|
||||||
|
ESP_LOGI(RUNTIME_TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", source.c_str(),
|
||||||
|
stats->get_period_count(), stats->get_period_avg_time_ms(), stats->get_period_max_time_ms(),
|
||||||
|
stats->get_period_time_ms());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log total stats since boot
|
||||||
|
ESP_LOGI(RUNTIME_TAG, "Total stats (since boot):");
|
||||||
|
|
||||||
|
// Re-sort by total runtime for all-time stats
|
||||||
|
std::sort(stats_to_display.begin(), stats_to_display.end(),
|
||||||
|
[](const ComponentStatPair &a, const ComponentStatPair &b) {
|
||||||
|
return a.stats->get_total_time_ms() > b.stats->get_total_time_ms();
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const auto &it : stats_to_display) {
|
||||||
|
const std::string &source = it.name;
|
||||||
|
const ComponentRuntimeStats *stats = it.stats;
|
||||||
|
|
||||||
|
ESP_LOGI(RUNTIME_TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", source.c_str(),
|
||||||
|
stats->get_total_count(), stats->get_total_avg_time_ms(), stats->get_total_max_time_ms(),
|
||||||
|
stats->get_total_time_ms());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RuntimeStatsCollector::process_pending_stats(uint32_t current_time) {
|
||||||
|
if (!this->enabled_ || this->next_log_time_ == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (current_time >= this->next_log_time_) {
|
||||||
|
this->log_stats_();
|
||||||
|
this->reset_stats_();
|
||||||
|
this->next_log_time_ = current_time + this->log_interval_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace esphome
|
||||||
121
esphome/core/runtime_stats.h
Normal file
121
esphome/core/runtime_stats.h
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <algorithm>
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
|
||||||
|
static const char *const RUNTIME_TAG = "runtime";
|
||||||
|
|
||||||
|
class Component; // Forward declaration
|
||||||
|
|
||||||
|
class ComponentRuntimeStats {
|
||||||
|
public:
|
||||||
|
ComponentRuntimeStats()
|
||||||
|
: period_count_(0),
|
||||||
|
total_count_(0),
|
||||||
|
period_time_ms_(0),
|
||||||
|
total_time_ms_(0),
|
||||||
|
period_max_time_ms_(0),
|
||||||
|
total_max_time_ms_(0) {}
|
||||||
|
|
||||||
|
void record_time(uint32_t duration_ms) {
|
||||||
|
// Update period counters
|
||||||
|
this->period_count_++;
|
||||||
|
this->period_time_ms_ += duration_ms;
|
||||||
|
if (duration_ms > this->period_max_time_ms_)
|
||||||
|
this->period_max_time_ms_ = duration_ms;
|
||||||
|
|
||||||
|
// Update total counters
|
||||||
|
this->total_count_++;
|
||||||
|
this->total_time_ms_ += duration_ms;
|
||||||
|
if (duration_ms > this->total_max_time_ms_)
|
||||||
|
this->total_max_time_ms_ = duration_ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset_period_stats() {
|
||||||
|
this->period_count_ = 0;
|
||||||
|
this->period_time_ms_ = 0;
|
||||||
|
this->period_max_time_ms_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Period stats (reset each logging interval)
|
||||||
|
uint32_t get_period_count() const { return this->period_count_; }
|
||||||
|
uint32_t get_period_time_ms() const { return this->period_time_ms_; }
|
||||||
|
uint32_t get_period_max_time_ms() const { return this->period_max_time_ms_; }
|
||||||
|
float get_period_avg_time_ms() const {
|
||||||
|
return this->period_count_ > 0 ? this->period_time_ms_ / static_cast<float>(this->period_count_) : 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Total stats (persistent until reboot)
|
||||||
|
uint32_t get_total_count() const { return this->total_count_; }
|
||||||
|
uint32_t get_total_time_ms() const { return this->total_time_ms_; }
|
||||||
|
uint32_t get_total_max_time_ms() const { return this->total_max_time_ms_; }
|
||||||
|
float get_total_avg_time_ms() const {
|
||||||
|
return this->total_count_ > 0 ? this->total_time_ms_ / static_cast<float>(this->total_count_) : 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// Period stats (reset each logging interval)
|
||||||
|
uint32_t period_count_;
|
||||||
|
uint32_t period_time_ms_;
|
||||||
|
uint32_t period_max_time_ms_;
|
||||||
|
|
||||||
|
// Total stats (persistent until reboot)
|
||||||
|
uint32_t total_count_;
|
||||||
|
uint32_t total_time_ms_;
|
||||||
|
uint32_t total_max_time_ms_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// For sorting components by run time
|
||||||
|
struct ComponentStatPair {
|
||||||
|
std::string name;
|
||||||
|
const ComponentRuntimeStats *stats;
|
||||||
|
|
||||||
|
bool operator>(const ComponentStatPair &other) const {
|
||||||
|
// Sort by period time as that's what we're displaying in the logs
|
||||||
|
return stats->get_period_time_ms() > other.stats->get_period_time_ms();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class RuntimeStatsCollector {
|
||||||
|
public:
|
||||||
|
RuntimeStatsCollector() : log_interval_(60000), next_log_time_(0), enabled_(true) {}
|
||||||
|
|
||||||
|
void set_log_interval(uint32_t log_interval) { this->log_interval_ = log_interval; }
|
||||||
|
uint32_t get_log_interval() const { return this->log_interval_; }
|
||||||
|
|
||||||
|
void set_enabled(bool enabled) { this->enabled_ = enabled; }
|
||||||
|
bool is_enabled() const { return this->enabled_; }
|
||||||
|
|
||||||
|
void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time);
|
||||||
|
|
||||||
|
// Process any pending stats printing (should be called after component loop)
|
||||||
|
void process_pending_stats(uint32_t current_time);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void log_stats_();
|
||||||
|
|
||||||
|
void reset_stats_() {
|
||||||
|
for (auto &it : this->component_stats_) {
|
||||||
|
it.second.reset_period_stats();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Back to string keys, but we'll cache the source name per component
|
||||||
|
std::map<std::string, ComponentRuntimeStats> component_stats_;
|
||||||
|
std::map<Component *, std::string> component_names_cache_;
|
||||||
|
uint32_t log_interval_;
|
||||||
|
uint32_t next_log_time_;
|
||||||
|
bool enabled_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Global instance for runtime stats collection
|
||||||
|
extern RuntimeStatsCollector runtime_stats;
|
||||||
|
|
||||||
|
} // namespace esphome
|
||||||
7
tests/integration/fixtures/api_reboot_timeout.yaml
Normal file
7
tests/integration/fixtures/api_reboot_timeout.yaml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
esphome:
|
||||||
|
name: api-reboot-test
|
||||||
|
host:
|
||||||
|
api:
|
||||||
|
reboot_timeout: 0.5s # Very short timeout for fast testing
|
||||||
|
logger:
|
||||||
|
level: DEBUG
|
||||||
35
tests/integration/test_api_reboot_timeout.py
Normal file
35
tests/integration/test_api_reboot_timeout.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
"""Test API server reboot timeout functionality."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import re
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from .types import RunCompiledFunction
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_api_reboot_timeout(
|
||||||
|
yaml_config: str,
|
||||||
|
run_compiled: RunCompiledFunction,
|
||||||
|
) -> None:
|
||||||
|
"""Test that the device reboots when no API clients connect within the timeout."""
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
reboot_future = loop.create_future()
|
||||||
|
reboot_pattern = re.compile(r"No clients; rebooting")
|
||||||
|
|
||||||
|
def check_output(line: str) -> None:
|
||||||
|
"""Check output for reboot message."""
|
||||||
|
if not reboot_future.done() and reboot_pattern.search(line):
|
||||||
|
reboot_future.set_result(True)
|
||||||
|
|
||||||
|
# Run the device without connecting any API client
|
||||||
|
async with run_compiled(yaml_config, line_callback=check_output):
|
||||||
|
# Wait for reboot with timeout
|
||||||
|
# (0.5s reboot timeout + some margin for processing)
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(reboot_future, timeout=2.0)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
pytest.fail("Device did not reboot within expected timeout")
|
||||||
|
|
||||||
|
# Test passes if we get here - reboot was detected
|
||||||
Reference in New Issue
Block a user