mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
start syslog when network connected
This commit is contained in:
@@ -125,8 +125,7 @@ void MqttSettingsService::configureMqtt() {
|
||||
_mqttClient.disconnect();
|
||||
|
||||
// only connect if WiFi is connected and MQTT is enabled
|
||||
if (_state.enabled && (WiFi.isConnected() || ETH.linkUp())) {
|
||||
// Serial.println(F("Connecting to MQTT..."));
|
||||
if (_state.enabled && emsesp::EMSESP::system_.network_connected()) {
|
||||
_mqttClient.setServer(retainCstr(_state.host.c_str(), &_retainedHost), _state.port);
|
||||
if (_state.username.length() > 0) {
|
||||
_mqttClient.setCredentials(retainCstr(_state.username.c_str(), &_retainedUsername), retainCstr(_state.password.length() > 0 ? _state.password.c_str() : nullptr, &_retainedPassword));
|
||||
|
||||
@@ -41,6 +41,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
#endif
|
||||
EMSESP::system_.wifi_tweak();
|
||||
EMSESP::system_.send_heartbeat();
|
||||
EMSESP::system_.syslog_start();
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_ETH_START:
|
||||
@@ -55,6 +56,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
EMSESP::logger().info(F("Ethernet Connected with IP=%s, speed %d Mbps"), ETH.localIP().toString().c_str(), ETH.linkSpeed());
|
||||
#endif
|
||||
EMSESP::system_.send_heartbeat();
|
||||
EMSESP::system_.syslog_start();
|
||||
EMSESP::system_.ethernet_connected(true);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -611,7 +611,7 @@ void Mqtt::on_connect() {
|
||||
|
||||
doc["version"] = EMSESP_APP_VERSION;
|
||||
#ifndef EMSESP_STANDALONE
|
||||
if (ETH.linkUp()) {
|
||||
if (EMSESP::system_.ethernet_connected()) {
|
||||
doc["ip"] = ETH.localIP().toString();
|
||||
} else {
|
||||
doc["ip"] = WiFi.localIP().toString();
|
||||
|
||||
@@ -119,19 +119,29 @@ void System::format(uuid::console::Shell & shell) {
|
||||
System::restart();
|
||||
}
|
||||
|
||||
void System::syslog_start() {
|
||||
if (syslog_enabled_) {
|
||||
#ifndef EMSESP_STANDALONE
|
||||
syslog_.start();
|
||||
#endif
|
||||
EMSESP::logger().info(F("Starting Syslog"));
|
||||
}
|
||||
}
|
||||
|
||||
void System::syslog_init(bool refresh) {
|
||||
if (refresh) {
|
||||
get_settings();
|
||||
}
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
// check for empty hostname
|
||||
// check for empty or invalid hostname
|
||||
IPAddress addr;
|
||||
if (!addr.fromString(syslog_host_.c_str())) {
|
||||
syslog_enabled_ = false;
|
||||
}
|
||||
|
||||
// in case service is still running, this flushes the queue - https://github.com/emsesp/EMS-ESP/issues/496
|
||||
// in case service is still running, this flushes the queue
|
||||
// https://github.com/emsesp/EMS-ESP/issues/496
|
||||
if (!syslog_enabled_) {
|
||||
syslog_.log_level((uuid::log::Level)-1);
|
||||
syslog_.mark_interval(0);
|
||||
@@ -140,13 +150,10 @@ void System::syslog_init(bool refresh) {
|
||||
}
|
||||
|
||||
// start & configure syslog
|
||||
syslog_.start();
|
||||
syslog_.log_level((uuid::log::Level)syslog_level_);
|
||||
syslog_.mark_interval(syslog_mark_interval_);
|
||||
syslog_.destination(addr, syslog_port_);
|
||||
syslog_.hostname(hostname_.c_str());
|
||||
|
||||
EMSESP::logger().info(F("Syslog started"));
|
||||
syslog_.hostname(hostname().c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -229,7 +236,7 @@ void System::start(uint32_t heap_start) {
|
||||
get_settings();
|
||||
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
|
||||
hostname(networkSettings.hostname.c_str());
|
||||
hostname(networkSettings.hostname.c_str()); // sets the hostname
|
||||
LOG_INFO(F("System %s booted (EMS-ESP version %s) "), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); // print boot message
|
||||
});
|
||||
|
||||
@@ -238,7 +245,7 @@ void System::start(uint32_t heap_start) {
|
||||
adc_init(false); // analog ADC
|
||||
button_init(false); // the special button
|
||||
network_init(false); // network
|
||||
syslog_init(false); // init SysLog, must start after network
|
||||
syslog_init(false); // init SysLog
|
||||
|
||||
EMSESP::init_uart(); // start UART
|
||||
}
|
||||
@@ -484,7 +491,7 @@ void System::network_init(bool refresh) {
|
||||
eth_clock_mode_t clock_mode; // ETH_CLOCK_GPIO0_IN or ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT for 50Hz inverted clock
|
||||
|
||||
if (board_profile_.equals("E32") || board_profile_.equals("LAN8720")) {
|
||||
// Gateway E32 (LAN8720)
|
||||
// BBQKees Gateway E32 (LAN8720)
|
||||
phy_addr = 1;
|
||||
power = 16;
|
||||
mdc = 23;
|
||||
|
||||
19
src/system.h
19
src/system.h
@@ -66,6 +66,9 @@ class System {
|
||||
void show_mem(const char * note);
|
||||
void get_settings();
|
||||
void wifi_tweak();
|
||||
void syslog_start();
|
||||
bool check_upgrade();
|
||||
void send_heartbeat();
|
||||
|
||||
void led_init(bool refresh);
|
||||
void syslog_init(bool refresh);
|
||||
@@ -75,12 +78,8 @@ class System {
|
||||
void commands_init();
|
||||
|
||||
static bool is_valid_gpio(uint8_t pin);
|
||||
|
||||
static bool load_board_profile(std::vector<uint8_t> & data, const std::string & board_profile);
|
||||
|
||||
bool check_upgrade();
|
||||
void send_heartbeat();
|
||||
|
||||
std::string hostname() {
|
||||
return hostname_;
|
||||
}
|
||||
@@ -97,6 +96,14 @@ class System {
|
||||
ethernet_connected_ = b;
|
||||
}
|
||||
|
||||
bool network_connected() {
|
||||
#ifndef EMSESP_STANDALONE
|
||||
return (ethernet_connected_ || WiFi.isConnected());
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
static uuid::log::Logger logger_;
|
||||
static uint32_t heap_start_;
|
||||
@@ -138,11 +145,11 @@ class System {
|
||||
uint32_t last_heartbeat_ = 0;
|
||||
uint32_t last_system_check_ = 0;
|
||||
bool upload_status_ = false; // true if we're in the middle of a OTA firmware upload
|
||||
bool ethernet_connected_;
|
||||
bool ethernet_connected_ = false;
|
||||
uint16_t analog_;
|
||||
|
||||
// settings
|
||||
std::string hostname_;
|
||||
std::string hostname_ = "ems-esp";
|
||||
bool hide_led_;
|
||||
uint8_t led_gpio_;
|
||||
bool syslog_enabled_;
|
||||
|
||||
Reference in New Issue
Block a user