mirror of
https://github.com/esphome/esphome.git
synced 2026-06-04 01:18:26 +08:00
Reduce code duplication in auto-generated API protocol code (#9097)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -379,6 +379,26 @@ class ProtoService {
|
|||||||
// Send the buffer
|
// Send the buffer
|
||||||
return this->send_buffer(buffer, message_type);
|
return this->send_buffer(buffer, message_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Authentication helper methods
|
||||||
|
bool check_connection_setup_() {
|
||||||
|
if (!this->is_connection_setup()) {
|
||||||
|
this->on_no_setup_connection();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool check_authenticated_() {
|
||||||
|
if (!this->check_connection_setup_()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!this->is_authenticated()) {
|
||||||
|
this->on_unauthenticated_access();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|||||||
@@ -1424,18 +1424,32 @@ def main() -> None:
|
|||||||
hpp_protected += f" void {on_func}(const {inp} &msg) override;\n"
|
hpp_protected += f" void {on_func}(const {inp} &msg) override;\n"
|
||||||
hpp += f" virtual {ret} {func}(const {inp} &msg) = 0;\n"
|
hpp += f" virtual {ret} {func}(const {inp} &msg) = 0;\n"
|
||||||
cpp += f"void {class_name}::{on_func}(const {inp} &msg) {{\n"
|
cpp += f"void {class_name}::{on_func}(const {inp} &msg) {{\n"
|
||||||
body = ""
|
|
||||||
if needs_conn:
|
|
||||||
body += "if (!this->is_connection_setup()) {\n"
|
|
||||||
body += " this->on_no_setup_connection();\n"
|
|
||||||
body += " return;\n"
|
|
||||||
body += "}\n"
|
|
||||||
if needs_auth:
|
|
||||||
body += "if (!this->is_authenticated()) {\n"
|
|
||||||
body += " this->on_unauthenticated_access();\n"
|
|
||||||
body += " return;\n"
|
|
||||||
body += "}\n"
|
|
||||||
|
|
||||||
|
# Start with authentication/connection check if needed
|
||||||
|
if needs_auth or needs_conn:
|
||||||
|
# Determine which check to use
|
||||||
|
if needs_auth:
|
||||||
|
check_func = "this->check_authenticated_()"
|
||||||
|
else:
|
||||||
|
check_func = "this->check_connection_setup_()"
|
||||||
|
|
||||||
|
body = f"if ({check_func}) {{\n"
|
||||||
|
|
||||||
|
# Add the actual handler code, indented
|
||||||
|
handler_body = ""
|
||||||
|
if is_void:
|
||||||
|
handler_body = f"this->{func}(msg);\n"
|
||||||
|
else:
|
||||||
|
handler_body = f"{ret} ret = this->{func}(msg);\n"
|
||||||
|
handler_body += "if (!this->send_message(ret)) {\n"
|
||||||
|
handler_body += " this->on_fatal_error();\n"
|
||||||
|
handler_body += "}\n"
|
||||||
|
|
||||||
|
body += indent(handler_body) + "\n"
|
||||||
|
body += "}\n"
|
||||||
|
else:
|
||||||
|
# No auth check needed, just call the handler
|
||||||
|
body = ""
|
||||||
if is_void:
|
if is_void:
|
||||||
body += f"this->{func}(msg);\n"
|
body += f"this->{func}(msg);\n"
|
||||||
else:
|
else:
|
||||||
@@ -1443,6 +1457,7 @@ def main() -> None:
|
|||||||
body += "if (!this->send_message(ret)) {\n"
|
body += "if (!this->send_message(ret)) {\n"
|
||||||
body += " this->on_fatal_error();\n"
|
body += " this->on_fatal_error();\n"
|
||||||
body += "}\n"
|
body += "}\n"
|
||||||
|
|
||||||
cpp += indent(body) + "\n" + "}\n"
|
cpp += indent(body) + "\n" + "}\n"
|
||||||
|
|
||||||
if ifdef is not None:
|
if ifdef is not None:
|
||||||
|
|||||||
Reference in New Issue
Block a user