mirror of
https://github.com/esphome/esphome.git
synced 2026-08-31 10:06:03 +00:00
[core] Inline api_is_connected() for hot-path callers (#15888)
This commit is contained in:
@@ -1,28 +1,14 @@
|
||||
#include "esphome/core/util.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/version.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_API
|
||||
#include "esphome/components/api/api_server.h"
|
||||
#endif
|
||||
|
||||
#ifdef USE_MQTT
|
||||
#include "esphome/components/mqtt/mqtt_client.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
|
||||
bool api_is_connected() {
|
||||
#ifdef USE_API
|
||||
if (api::global_api_server != nullptr) {
|
||||
return api::global_api_server->is_connected();
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
bool mqtt_is_connected() {
|
||||
#ifdef USE_MQTT
|
||||
if (mqtt::global_mqtt_client != nullptr) {
|
||||
|
||||
+20
-2
@@ -1,10 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#ifdef USE_API
|
||||
#include "esphome/components/api/api_server.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
|
||||
/// Return whether the node has at least one client connected to the native API
|
||||
bool api_is_connected();
|
||||
/// Return whether the node has at least one client connected to the native API.
|
||||
///
|
||||
/// Inline so that hot-path callers (e.g. component loop() ticks that check connectivity every
|
||||
/// iteration) can skip the call8/return pair. With USE_API disabled this trivially returns false
|
||||
/// and collapses at compile time.
|
||||
#ifdef USE_API
|
||||
ESPHOME_ALWAYS_INLINE inline bool api_is_connected() {
|
||||
return api::global_api_server != nullptr && api::global_api_server->is_connected();
|
||||
}
|
||||
#else
|
||||
ESPHOME_ALWAYS_INLINE inline bool api_is_connected() { return false; }
|
||||
#endif
|
||||
|
||||
/// Return whether the node has an active connection to an MQTT broker
|
||||
bool mqtt_is_connected();
|
||||
|
||||
Reference in New Issue
Block a user