Add set_hostname to device class

This commit is contained in:
fvanroie 2021-02-18 22:17:29 +01:00
parent 06cba2ea8e
commit e8850d99fe
11 changed files with 225 additions and 168 deletions

View File

@ -26,6 +26,8 @@ class BaseDevice {
{ {
return ""; return "";
} }
virtual void set_hostname(const char*)
{}
virtual const char* get_core_version() virtual const char* get_core_version()
{ {
return ""; return "";

View File

@ -25,7 +25,11 @@ void Esp32Device::reboot()
const char* Esp32Device::get_hostname() const char* Esp32Device::get_hostname()
{ {
return hostname.c_str(); return _hostname.c_str();
}
void Esp32Device::set_hostname(const char* hostname)
{
_hostname = hostname;
} }
const char* Esp32Device::get_core_version() const char* Esp32Device::get_core_version()
{ {
@ -38,7 +42,7 @@ const char* Esp32Device::get_display_driver()
void Esp32Device::set_backlight_pin(uint8_t pin) void Esp32Device::set_backlight_pin(uint8_t pin)
{ {
Esp32Device::backlight_pin = pin; Esp32Device::_backlight_pin = pin;
/* Setup Backlight Control Pin */ /* Setup Backlight Control Pin */
if(pin != (uint8_t)-1) { if(pin != (uint8_t)-1) {
@ -53,33 +57,33 @@ void Esp32Device::set_backlight_pin(uint8_t pin)
void Esp32Device::set_backlight_level(uint8_t level) void Esp32Device::set_backlight_level(uint8_t level)
{ {
backlight_level = level >= 0 ? level : 0; _backlight_level = level >= 0 ? level : 0;
backlight_level = backlight_level <= 100 ? backlight_level : 100; _backlight_level = _backlight_level <= 100 ? _backlight_level : 100;
update_backlight(); update_backlight();
} }
uint8_t Esp32Device::get_backlight_level() uint8_t Esp32Device::get_backlight_level()
{ {
return backlight_level; return _backlight_level;
} }
void Esp32Device::set_backlight_power(bool power) void Esp32Device::set_backlight_power(bool power)
{ {
backlight_power = power; _backlight_power = power;
update_backlight(); update_backlight();
} }
bool Esp32Device::get_backlight_power() bool Esp32Device::get_backlight_power()
{ {
return backlight_power != 0; return _backlight_power != 0;
} }
void Esp32Device::update_backlight() void Esp32Device::update_backlight()
{ {
if(backlight_pin == (uint8_t)-1) return; if(_backlight_pin == (uint8_t)-1) return;
uint32_t duty = backlight_power ? map(backlight_level, 0, 100, 0, 4095) : 0; uint32_t duty = _backlight_power ? map(_backlight_level, 0, 100, 0, 4095) : 0;
ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value
} }

View File

@ -13,9 +13,17 @@ namespace dev {
class Esp32Device : public BaseDevice { class Esp32Device : public BaseDevice {
public: public:
Esp32Device()
{
_hostname = "plate";
_backlight_pin = TFT_BCKL;
_backlight_power = 1;
_backlight_level = 100;
}
void reboot() override; void reboot() override;
const char* get_hostname(); const char* get_hostname();
void set_hostname(const char*);
const char* get_core_version(); const char* get_core_version();
const char* get_display_driver(); const char* get_display_driver();
@ -31,11 +39,11 @@ class Esp32Device : public BaseDevice {
uint16_t get_cpu_frequency() override; uint16_t get_cpu_frequency() override;
private: private:
std::string hostname; std::string _hostname;
uint8_t backlight_pin; uint8_t _backlight_pin;
uint8_t backlight_level; uint8_t _backlight_level;
uint8_t backlight_power; uint8_t _backlight_power;
void update_backlight(); void update_backlight();
}; };

View File

@ -17,9 +17,18 @@ void Esp8266Device::reboot()
ESP.restart(); ESP.restart();
} }
const char* Esp8266Device::get_hostname()
{
return _hostname.c_str();
}
void Esp8266Device::set_hostname(const char* hostname)
{
_hostname = hostname;
}
void Esp8266Device::set_backlight_pin(uint8_t pin) void Esp8266Device::set_backlight_pin(uint8_t pin)
{ {
Esp8266Device::backlight_pin = pin; _backlight_pin = pin;
/* Setup Backlight Control Pin */ /* Setup Backlight Control Pin */
if(pin >= 0) { if(pin >= 0) {
LOG_VERBOSE(TAG_GUI, F("Backlight : Pin %d"), pin); LOG_VERBOSE(TAG_GUI, F("Backlight : Pin %d"), pin);
@ -30,33 +39,33 @@ void Esp8266Device::set_backlight_pin(uint8_t pin)
void Esp8266Device::set_backlight_level(uint8_t level) void Esp8266Device::set_backlight_level(uint8_t level)
{ {
backlight_level = level >= 0 ? level : 0; _backlight_level = level >= 0 ? level : 0;
backlight_level = backlight_level <= 100 ? backlight_level : 100; _backlight_level = _backlight_level <= 100 ? backlight_level : 100;
update_backlight(); update_backlight();
} }
uint8_t Esp8266Device::get_backlight_level() uint8_t Esp8266Device::get_backlight_level()
{ {
return backlight_level; return _backlight_level;
} }
void Esp8266Device::set_backlight_power(bool power) void Esp8266Device::set_backlight_power(bool power)
{ {
backlight_power = power; _backlight_power = power;
update_backlight(); update_backlight();
} }
bool Esp8266Device::get_backlight_power() bool Esp8266Device::get_backlight_power()
{ {
return backlight_power != 0; return _backlight_power != 0;
} }
void Esp8266Device::update_backlight() void Esp8266Device::update_backlight()
{ {
if(backlight_pin == -1) return; if(_backlight_pin == -1) return;
analogWrite(backlight_pin, backlight_power ? map(backlight_level, 0, 100, 0, 1023) : 0); analogWrite(backlight_pin, _backlight_power ? map(_backlight_level, 0, 100, 0, 1023) : 0);
} }
size_t Esp8266Device::get_free_max_block() size_t Esp8266Device::get_free_max_block()

View File

@ -14,30 +14,38 @@ namespace dev {
class Esp8266Device : public BaseDevice { class Esp8266Device : public BaseDevice {
public: public:
Esp8266Device()
{
hostname = "plate";
backlight_pin = TFT_BCKL;
backlight_power = 1;
backlight_level = 100;
}
void reboot() override; void reboot() override;
const char* get_hostname();
void set_hostname(const char*);
const char* get_core_version();
const char* get_display_driver();
void set_backlight_pin(uint8_t pin) override; void set_backlight_pin(uint8_t pin) override;
void set_backlight_level(uint8_t val) override; void set_backlight_level(uint8_t val) override;
uint8_t get_backlight_level() override; uint8_t get_backlight_level() override;
void set_backlight_power(bool power) override; void set_backlight_power(bool power) override;
bool get_backlight_power() override; bool get_backlight_power() override;
size_t get_free_max_block() override; size_t get_free_max_block() override;
size_t get_free_heap() override; size_t get_free_heap() override;
uint8_t get_heap_fragmentation() override; uint8_t get_heap_fragmentation() override;
uint16_t get_cpu_frequency() override; uint16_t get_cpu_frequency() override;
private: private:
uint8_t backlight_pin; std::string _hostname;
uint8_t backlight_level;
uint8_t backlight_power; uint8_t _backlight_pin;
uint8_t _backlight_level;
uint8_t _backlight_power;
void update_backlight(); void update_backlight();
}; };

View File

@ -15,7 +15,11 @@ void Win32Device::reboot()
const char* Win32Device::get_hostname() const char* Win32Device::get_hostname()
{ {
return "winhasp"; return _hostname.c_str();
}
void Win32Device::set_hostname(const char* hostname)
{
_hostname = hostname;
} }
const char* Win32Device::get_core_version() const char* Win32Device::get_core_version()
{ {
@ -23,40 +27,40 @@ const char* Win32Device::get_core_version()
} }
const char* Win32Device::get_display_driver() const char* Win32Device::get_display_driver()
{ {
return "test"; return "SDL2";
} }
void Win32Device::set_backlight_pin(uint8_t pin) void Win32Device::set_backlight_pin(uint8_t pin)
{ {
Win32Device::backlight_pin = pin; Win32Device::_backlight_pin = pin;
} }
void Win32Device::set_backlight_level(uint8_t level) void Win32Device::set_backlight_level(uint8_t level)
{ {
backlight_level = level >= 0 ? level : 0; _backlight_level = level >= 0 ? level : 0;
backlight_level = backlight_level <= 100 ? backlight_level : 100; _backlight_level = _backlight_level <= 100 ? _backlight_level : 100;
update_backlight(); update_backlight();
} }
uint8_t Win32Device::get_backlight_level() uint8_t Win32Device::get_backlight_level()
{ {
return backlight_level; return _backlight_level;
} }
void Win32Device::set_backlight_power(bool power) void Win32Device::set_backlight_power(bool power)
{ {
backlight_power = power; _backlight_power = power;
update_backlight(); update_backlight();
} }
bool Win32Device::get_backlight_power() bool Win32Device::get_backlight_power()
{ {
return backlight_power != 0; return _backlight_power != 0;
} }
void Win32Device::update_backlight() void Win32Device::update_backlight()
{ {
if(backlight_pin == -1) return; if(_backlight_pin == -1) return;
} }
size_t Win32Device::get_free_max_block() size_t Win32Device::get_free_max_block()

View File

@ -5,6 +5,7 @@
#define HASP_DEVICE_WINDOWS_H #define HASP_DEVICE_WINDOWS_H
#include <cstdint> #include <cstdint>
#include <string>
#include "Windows.h" #include "Windows.h"
#include "hasp_conf.h" #include "hasp_conf.h"
@ -17,11 +18,20 @@ namespace dev {
class Win32Device : public BaseDevice { class Win32Device : public BaseDevice {
public: public:
Win32Device()
{
_hostname = "winplate";
_backlight_pin = -1;
_backlight_power = 1;
_backlight_level = 100;
}
void reboot() override; void reboot() override;
const char* get_hostname() override; const char* get_hostname();
const char* get_core_version() override; void set_hostname(const char*);
const char* get_display_driver() override; const char* get_core_version();
const char* get_display_driver();
void set_backlight_pin(uint8_t pin); void set_backlight_pin(uint8_t pin);
void set_backlight_level(uint8_t val); void set_backlight_level(uint8_t val);
@ -35,9 +45,11 @@ class Win32Device : public BaseDevice {
uint16_t get_cpu_frequency(); uint16_t get_cpu_frequency();
private: private:
uint8_t backlight_pin; std::string _hostname;
uint8_t backlight_level;
uint8_t backlight_power; uint8_t _backlight_pin;
uint8_t _backlight_level;
uint8_t _backlight_power;
void update_backlight(); void update_backlight();
}; };

View File

@ -1026,7 +1026,7 @@ void dispatchLoop()
void dispatchEverySecond() void dispatchEverySecond()
{ {
if(dispatch_setings.teleperiod > 0 && (millis() - dispatchLastMillis) >= dispatch_setings.teleperiod * 1000) { if(dispatch_setings.teleperiod > 0 && (millis() - dispatchLastMillis) >= dispatch_setings.teleperiod * 1000) {
dispatchLastMillis = millis(); dispatchLastMillis += dispatch_setings.teleperiod * 1000;
dispatch_output_statusupdate(NULL, NULL); dispatch_output_statusupdate(NULL, NULL);
} }
} }

View File

@ -5,22 +5,24 @@
#include "hasp_conf.h" #include "hasp_conf.h"
#ifndef WINDOWS #ifndef WINDOWS
#if HASP_USE_MQTT > 0 #if HASP_USE_MQTT > 0
#include "PubSubClient.h" #include "PubSubClient.h"
#include "hasp/hasp.h" #include "hasp/hasp.h"
#include "hasp/hasp_dispatch.h" #include "hasp/hasp_dispatch.h"
#include "hal/hasp_hal.h" #include "hal/hasp_hal.h"
#include "hasp_mqtt.h" #include "dev/device.h"
#include "hasp_mqtt_ha.h"
#define RETAINED true #include "hasp_mqtt.h"
#define HASP_MAC_ADDRESS halGetMacAddress(0, "").c_str() #include "hasp_mqtt_ha.h"
#define HASP_MAC_ADDRESS_STR halGetMacAddress(0, "")
#define RETAINED true
#define HASP_MAC_ADDRESS halGetMacAddress(0, "").c_str()
#define HASP_MAC_ADDRESS_STR halGetMacAddress(0, "")
extern PubSubClient mqttClient; extern PubSubClient mqttClient;
extern char mqttNodeName[16]; // extern char mqttNodeName[16];
extern char mqttNodeTopic[24]; extern char mqttNodeTopic[24];
extern char mqttGroupTopic[24]; extern char mqttGroupTopic[24];
extern bool mqttEnabled; extern bool mqttEnabled;
@ -34,7 +36,7 @@ const char FP_MQTT_HA_NAME[] PROGMEM = "name";
const char FP_MQTT_HA_MODEL[] PROGMEM = "mdl"; const char FP_MQTT_HA_MODEL[] PROGMEM = "mdl";
const char FP_MQTT_HA_MANUFACTURER[] PROGMEM = "mf"; const char FP_MQTT_HA_MANUFACTURER[] PROGMEM = "mf";
void mqtt_ha_send_json(char * topic, JsonDocument & doc) void mqtt_ha_send_json(char* topic, JsonDocument& doc)
{ {
LOG_VERBOSE(TAG_MQTT_PUB, topic); LOG_VERBOSE(TAG_MQTT_PUB, topic);
mqttClient.beginPublish(topic, measureJson(doc), RETAINED); mqttClient.beginPublish(topic, measureJson(doc), RETAINED);
@ -43,18 +45,18 @@ void mqtt_ha_send_json(char * topic, JsonDocument & doc)
} }
// adds the device identifiers to the HA MQTT auto-discovery message // adds the device identifiers to the HA MQTT auto-discovery message
void mqtt_ha_add_device_ids(JsonDocument & doc) void mqtt_ha_add_device_ids(JsonDocument& doc)
{ {
JsonObject device = doc.createNestedObject(FPSTR(FP_MQTT_HA_DEVICE)); JsonObject device = doc.createNestedObject(FPSTR(FP_MQTT_HA_DEVICE));
JsonArray ids = device.createNestedArray(FPSTR(FP_MQTT_HA_IDENTIFIERS)); JsonArray ids = device.createNestedArray(FPSTR(FP_MQTT_HA_IDENTIFIERS));
ids.add(mqttNodeName); ids.add(haspDevice.get_hostname());
ids.add(HASP_MAC_ADDRESS_STR); ids.add(HASP_MAC_ADDRESS_STR);
char buffer[32]; char buffer[32];
haspGetVersion(buffer, sizeof(buffer)); haspGetVersion(buffer, sizeof(buffer));
device[F("sw")] = buffer; device[F("sw")] = buffer;
device[FPSTR(FP_MQTT_HA_NAME)] = mqttNodeName; device[FPSTR(FP_MQTT_HA_NAME)] = haspDevice.get_hostname();
device[FPSTR(FP_MQTT_HA_MODEL)] = F(PIOENV); device[FPSTR(FP_MQTT_HA_MODEL)] = F(PIOENV);
device[FPSTR(FP_MQTT_HA_MANUFACTURER)] = F(D_MANUFACTURER); device[FPSTR(FP_MQTT_HA_MANUFACTURER)] = F(D_MANUFACTURER);
@ -62,11 +64,11 @@ void mqtt_ha_add_device_ids(JsonDocument & doc)
} }
// adds the name and unique_id to the HA MQTT auto-discovery message // adds the name and unique_id to the HA MQTT auto-discovery message
void mqtt_ha_add_unique_id(JsonDocument & doc, char * item) void mqtt_ha_add_unique_id(JsonDocument& doc, char* item)
{ {
char buffer[64]; char buffer[64];
snprintf_P(buffer, sizeof(buffer), PSTR("HASP %s %s"), mqttNodeName, item); snprintf_P(buffer, sizeof(buffer), PSTR("HASP %s %s"), haspDevice.get_hostname(), item);
doc[FPSTR(FP_MQTT_HA_NAME)] = buffer; doc[FPSTR(FP_MQTT_HA_NAME)] = buffer;
snprintf_P(buffer, sizeof(buffer), PSTR("hasp_%s-%s"), HASP_MAC_ADDRESS, item); snprintf_P(buffer, sizeof(buffer), PSTR("hasp_%s-%s"), HASP_MAC_ADDRESS, item);
@ -90,28 +92,28 @@ void mqtt_ha_register_button(uint8_t page, uint8_t id)
doc[F("pl")] = buffer; doc[F("pl")] = buffer;
doc[F("type")] = "button_short_press"; doc[F("type")] = "button_short_press";
snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"), snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"),
discovery_prefix, mqttNodeName, page, id, "short_press"); discovery_prefix, haspDevice.get_hostname(), page, id, "short_press");
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
dispatch_get_event_name(HASP_EVENT_SHORT, buffer, sizeof(buffer)); dispatch_get_event_name(HASP_EVENT_SHORT, buffer, sizeof(buffer));
doc[F("pl")] = buffer; doc[F("pl")] = buffer;
doc[F("type")] = "button_short_release"; doc[F("type")] = "button_short_release";
snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"), snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"),
discovery_prefix, mqttNodeName, page, id, "short_release"); discovery_prefix, haspDevice.get_hostname(), page, id, "short_release");
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
dispatch_get_event_name(HASP_EVENT_LONG, buffer, sizeof(buffer)); dispatch_get_event_name(HASP_EVENT_LONG, buffer, sizeof(buffer));
doc[F("pl")] = buffer; doc[F("pl")] = buffer;
doc[F("type")] = "button_long_press"; doc[F("type")] = "button_long_press";
snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"), snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"),
discovery_prefix, mqttNodeName, page, id, "long_press"); discovery_prefix, haspDevice.get_hostname(), page, id, "long_press");
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
dispatch_get_event_name(HASP_EVENT_UP, buffer, sizeof(buffer)); dispatch_get_event_name(HASP_EVENT_UP, buffer, sizeof(buffer));
doc[F("pl")] = buffer; doc[F("pl")] = buffer;
doc[F("type")] = "button_long_release"; doc[F("type")] = "button_long_release";
snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"), snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"),
discovery_prefix, mqttNodeName, page, id, "long_release"); discovery_prefix, haspDevice.get_hostname(), page, id, "long_release");
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
} }
@ -131,7 +133,7 @@ void mqtt_ha_register_switch(uint8_t page, uint8_t id)
doc[F("type")] = F("button_short_release"); doc[F("type")] = F("button_short_release");
snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"), snprintf_P(buffer, sizeof(buffer), PSTR("%s/device_automation/%s/" HASP_OBJECT_NOTATION "_%s/config"),
discovery_prefix, mqttNodeName, page, id, "short"); discovery_prefix, haspDevice.get_hostname(), page, id, "short");
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
} }
@ -149,7 +151,8 @@ void mqtt_ha_register_connectivity()
mqtt_ha_add_unique_id(doc, item); mqtt_ha_add_unique_id(doc, item);
char buffer[128]; char buffer[128];
snprintf_P(buffer, sizeof(buffer), PSTR("%s/binary_sensor/%s/%s/config"), discovery_prefix, mqttNodeName, item); snprintf_P(buffer, sizeof(buffer), PSTR("%s/binary_sensor/%s/%s/config"), discovery_prefix,
haspDevice.get_hostname(), item);
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
} }
@ -174,7 +177,8 @@ void mqtt_ha_register_backlight()
// doc[F("pl_off")] = F("OFF"); // doc[F("pl_off")] = F("OFF");
char buffer[128]; char buffer[128];
snprintf_P(buffer, sizeof(buffer), PSTR("%s/light/%s/%s/config"), discovery_prefix, mqttNodeName, item); snprintf_P(buffer, sizeof(buffer), PSTR("%s/light/%s/%s/config"), discovery_prefix, haspDevice.get_hostname(),
item);
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
} }
@ -205,7 +209,8 @@ void mqtt_ha_register_moodlight()
// doc[F("rgb_command_template")] = F("{{ '%02x%02x%02x0000'| format(red, green, blue) }}"); // doc[F("rgb_command_template")] = F("{{ '%02x%02x%02x0000'| format(red, green, blue) }}");
char buffer[128]; char buffer[128];
snprintf_P(buffer, sizeof(buffer), PSTR("%s/light/%s/%s/config"), discovery_prefix, mqttNodeName, item); snprintf_P(buffer, sizeof(buffer), PSTR("%s/light/%s/%s/config"), discovery_prefix, haspDevice.get_hostname(),
item);
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
} }
@ -221,7 +226,8 @@ void mqtt_ha_register_idle()
mqtt_ha_add_unique_id(doc, item); mqtt_ha_add_unique_id(doc, item);
char buffer[128]; char buffer[128];
snprintf_P(buffer, sizeof(buffer), PSTR("%s/sensor/%s/%s/config"), discovery_prefix, mqttNodeName, item); snprintf_P(buffer, sizeof(buffer), PSTR("%s/sensor/%s/%s/config"), discovery_prefix, haspDevice.get_hostname(),
item);
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
} }
@ -237,7 +243,8 @@ void mqtt_ha_register_activepage()
mqtt_ha_add_unique_id(doc, item); mqtt_ha_add_unique_id(doc, item);
char buffer[128]; char buffer[128];
snprintf_P(buffer, sizeof(buffer), PSTR("%s/number/%s/%s/config"), discovery_prefix, mqttNodeName, item); snprintf_P(buffer, sizeof(buffer), PSTR("%s/number/%s/%s/config"), discovery_prefix, haspDevice.get_hostname(),
item);
mqtt_ha_send_json(buffer, doc); mqtt_ha_send_json(buffer, doc);
} }
@ -252,7 +259,7 @@ void mqtt_ha_register_auto_discovery()
mqtt_ha_register_idle(); mqtt_ha_register_idle();
mqtt_ha_register_connectivity(); mqtt_ha_register_connectivity();
} }
#endif #endif
#endif #endif
/* /*

View File

@ -81,10 +81,10 @@ bool mqttHAautodiscover = true;
#define LWT_TOPIC "LWT" #define LWT_TOPIC "LWT"
char mqttServer[16] = MQTT_HOST; char mqttServer[16] = MQTT_HOST;
char mqttUser[23] = MQTT_USER; char mqttUser[23] = MQTT_USER;
char mqttPassword[32] = MQTT_PASSW; char mqttPassword[32] = MQTT_PASSW;
char mqttNodeName[16] = MQTT_NODENAME; // char mqttNodeName[16] = MQTT_NODENAME;
char mqttGroupName[16] = MQTT_GROUPNAME; char mqttGroupName[16] = MQTT_GROUPNAME;
uint16_t mqttPort = MQTT_PORT; uint16_t mqttPort = MQTT_PORT;
PubSubClient mqttClient(mqttNetworkClient); PubSubClient mqttClient(mqttNetworkClient);
@ -368,8 +368,8 @@ bool mqttGetConfig(const JsonObject& settings)
{ {
bool changed = false; bool changed = false;
if(strcmp(mqttNodeName, settings[FPSTR(FP_CONFIG_NAME)].as<String>().c_str()) != 0) changed = true; if(strcmp(haspDevice.get_hostname(), settings[FPSTR(FP_CONFIG_NAME)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_NAME)] = mqttNodeName; settings[FPSTR(FP_CONFIG_NAME)] = haspDevice.get_hostname();
if(strcmp(mqttGroupName, settings[FPSTR(FP_CONFIG_GROUP)].as<String>().c_str()) != 0) changed = true; if(strcmp(mqttGroupName, settings[FPSTR(FP_CONFIG_GROUP)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_GROUP)] = mqttGroupName; settings[FPSTR(FP_CONFIG_GROUP)] = mqttGroupName;
@ -406,14 +406,17 @@ bool mqttSetConfig(const JsonObject& settings)
changed |= configSet(mqttPort, settings[FPSTR(FP_CONFIG_PORT)], F("mqttPort")); changed |= configSet(mqttPort, settings[FPSTR(FP_CONFIG_PORT)], F("mqttPort"));
if(!settings[FPSTR(FP_CONFIG_NAME)].isNull()) { if(!settings[FPSTR(FP_CONFIG_NAME)].isNull()) {
changed |= strcmp(mqttNodeName, settings[FPSTR(FP_CONFIG_NAME)]) != 0; changed |= strcmp(haspDevice.get_hostname(), settings[FPSTR(FP_CONFIG_NAME)]) != 0;
strncpy(mqttNodeName, settings[FPSTR(FP_CONFIG_NAME)], sizeof(mqttNodeName)); // strncpy(mqttNodeName, settings[FPSTR(FP_CONFIG_NAME)], sizeof(mqttNodeName));
haspDevice.set_hostname(settings[FPSTR(FP_CONFIG_NAME)].as<const char*>());
} }
// Prefill node name // Prefill node name
if(strlen(mqttNodeName) == 0) { if(strlen(haspDevice.get_hostname()) == 0) {
char mqttNodeName[64];
String mac = halGetMacAddress(3, ""); String mac = halGetMacAddress(3, "");
mac.toLowerCase(); mac.toLowerCase();
snprintf_P(mqttNodeName, sizeof(mqttNodeName), PSTR(D_MQTT_DEFAULT_NAME), mac.c_str()); snprintf_P(mqttNodeName, sizeof(mqttNodeName), PSTR(D_MQTT_DEFAULT_NAME), mac.c_str());
haspDevice.set_hostname(mqttNodeName);
changed = true; changed = true;
} }
@ -443,7 +446,7 @@ bool mqttSetConfig(const JsonObject& settings)
strncpy(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(mqttPassword)); strncpy(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(mqttPassword));
} }
snprintf_P(mqttNodeTopic, sizeof(mqttNodeTopic), PSTR(MQTT_PREFIX "/%s/"), mqttNodeName); snprintf_P(mqttNodeTopic, sizeof(mqttNodeTopic), PSTR(MQTT_PREFIX "/%s/"), haspDevice.get_hostname());
snprintf_P(mqttGroupTopic, sizeof(mqttGroupTopic), PSTR(MQTT_PREFIX "/%s/"), mqttGroupName); snprintf_P(mqttGroupTopic, sizeof(mqttGroupTopic), PSTR(MQTT_PREFIX "/%s/"), mqttGroupName);
return changed; return changed;

View File

@ -63,7 +63,7 @@ ESP8266WebServer webServer(80);
WebServer webServer(80); WebServer webServer(80);
#endif // ESP32 #endif // ESP32
HTTPUpload * upload; HTTPUpload* upload;
static const char HTTP_MENU_BUTTON[] PROGMEM = static const char HTTP_MENU_BUTTON[] PROGMEM =
"<p><form method='get' action='%s'><button type='submit'>%s</button></form></p>"; "<p><form method='get' action='%s'><button type='submit'>%s</button></form></p>";
@ -113,11 +113,11 @@ const char HTTP_FOOTER[] PROGMEM = " by Francis Van Roie</div></body></html>";
// // Default link to compiled Nextion firmware images // // Default link to compiled Nextion firmware images
// String lcdFirmwareUrl = "http://haswitchplate.com/update/HASwitchPlate.tft"; // String lcdFirmwareUrl = "http://haswitchplate.com/update/HASwitchPlate.tft";
#if HASP_USE_MQTT > 0 // #if HASP_USE_MQTT > 0
extern char mqttNodeName[16]; // extern char mqttNodeName[16];
#else // #else
char mqttNodeName[3] = "na"; // char mqttNodeName[3] = "na";
#endif // #endif
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
String getOption(int value, String label, bool selected) String getOption(int value, String label, bool selected)
@ -136,14 +136,14 @@ String getOption(String value, String label, bool selected)
return buffer; return buffer;
} }
static void add_gpio_select_option(String & str, uint8_t gpio, uint8_t bcklpin) static void add_gpio_select_option(String& str, uint8_t gpio, uint8_t bcklpin)
{ {
char buffer[10]; char buffer[10];
snprintf_P(buffer, sizeof(buffer), PSTR("GPIO %d"), gpio); snprintf_P(buffer, sizeof(buffer), PSTR("GPIO %d"), gpio);
str += getOption(gpio, buffer, bcklpin == gpio); str += getOption(gpio, buffer, bcklpin == gpio);
} }
static void add_button(String & str, const __FlashStringHelper * label, const __FlashStringHelper * extra) static void add_button(String& str, const __FlashStringHelper* label, const __FlashStringHelper* extra)
{ {
str += F("<button type='submit' "); str += F("<button type='submit' ");
str += extra; str += extra;
@ -152,13 +152,13 @@ static void add_button(String & str, const __FlashStringHelper * label, const __
str += F("</button>"); str += F("</button>");
} }
static void close_form(String & str) static void close_form(String& str)
{ {
str += F("</form></p>"); str += F("</form></p>");
} }
static void add_form_button(String & str, const __FlashStringHelper * label, const __FlashStringHelper * action, static void add_form_button(String& str, const __FlashStringHelper* label, const __FlashStringHelper* action,
const __FlashStringHelper * extra) const __FlashStringHelper* extra)
{ {
str += F("<p><form method='get' action='"); str += F("<p><form method='get' action='");
str += action; str += action;
@ -170,13 +170,13 @@ static void add_form_button(String & str, const __FlashStringHelper * label, con
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void webHandleHaspConfig(); void webHandleHaspConfig();
static inline char * httpGetNodename() // static inline char* haspDevice.get_hostname()
{ // {
return mqttNodeName; // return mqttNodeName;
} // }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
bool httpIsAuthenticated(const __FlashStringHelper * fstr_page) bool httpIsAuthenticated(const __FlashStringHelper* fstr_page)
{ {
if(http_config.password[0] != '\0') { // Request HTTP auth if httpPassword is set if(http_config.password[0] != '\0') { // Request HTTP auth if httpPassword is set
if(!webServer.authenticate(http_config.user, http_config.password)) { if(!webServer.authenticate(http_config.user, http_config.password)) {
@ -212,7 +212,7 @@ void webSendFooter()
#endif #endif
} }
void webSendPage(char * nodename, uint32_t httpdatalength, bool gohome = false) void webSendPage(const char* nodename, uint32_t httpdatalength, bool gohome = false)
{ {
{ {
char buffer[64]; char buffer[64];
@ -306,10 +306,10 @@ void webHandleRoot()
saveConfig(); saveConfig();
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<p><form method='get' action='/config/hasp'><button type='submit'>" D_HTTP_HASP_DESIGN httpMessage += F("<p><form method='get' action='/config/hasp'><button type='submit'>" D_HTTP_HASP_DESIGN
@ -337,7 +337,7 @@ void webHandleRoot()
httpMessage += F("<p><form method='get' action='reboot'><button class='red' type='submit'>" D_HTTP_REBOOT httpMessage += F("<p><form method='get' action='reboot'><button class='red' type='submit'>" D_HTTP_REBOOT
"</button></form></p>"); "</button></form></p>");
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -350,14 +350,14 @@ void httpHandleReboot()
if(!httpIsAuthenticated(F("reboot"))) return; if(!httpIsAuthenticated(F("reboot"))) return;
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage = F(D_DISPATCH_REBOOT); httpMessage = F(D_DISPATCH_REBOOT);
webSendPage(httpGetNodename(), httpMessage.length(), true); webSendPage(haspDevice.get_hostname(), httpMessage.length(), true);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -381,7 +381,7 @@ void webHandleScreenshot()
} }
if(webServer.hasArg(F("q"))) { if(webServer.hasArg(F("q"))) {
lv_disp_t * disp = lv_disp_get_default(); lv_disp_t* disp = lv_disp_get_default();
webServer.setContentLength(122 + disp->driver.hor_res * disp->driver.ver_res * sizeof(lv_color_t)); webServer.setContentLength(122 + disp->driver.hor_res * disp->driver.ver_res * sizeof(lv_color_t));
webServer.send_P(200, PSTR("image/bmp"), ""); webServer.send_P(200, PSTR("image/bmp"), "");
guiTakeScreenshot(); guiTakeScreenshot();
@ -389,10 +389,10 @@ void webHandleScreenshot()
} else { } else {
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += httpMessage +=
@ -413,7 +413,7 @@ void webHandleScreenshot()
"</button></form></p>"); "</button></form></p>");
httpMessage += FPSTR(MAIN_MENU_BUTTON); httpMessage += FPSTR(MAIN_MENU_BUTTON);
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -428,7 +428,7 @@ void webHandleAbout()
if(!httpIsAuthenticated(F("about"))) return; if(!httpIsAuthenticated(F("about"))) return;
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<p><h3>HASP OpenHardware edition</h3>Copyright&copy; 2020 Francis Van Roie "); httpMessage += F("<p><h3>HASP OpenHardware edition</h3>Copyright&copy; 2020 Francis Van Roie ");
@ -469,7 +469,7 @@ void webHandleAbout()
httpMessage += FPSTR(MAIN_MENU_BUTTON); httpMessage += FPSTR(MAIN_MENU_BUTTON);
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -483,10 +483,10 @@ void webHandleInfo()
{ {
char size_buf[32]; char size_buf[32];
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
/* HASP Stats */ /* HASP Stats */
@ -638,7 +638,7 @@ void webHandleInfo()
char mqttClientId[64]; char mqttClientId[64];
String mac = halGetMacAddress(3, ""); String mac = halGetMacAddress(3, "");
mac.toLowerCase(); mac.toLowerCase();
snprintf_P(mqttClientId, sizeof(mqttClientId), PSTR("%s-%s"), mqttNodeName, mac.c_str()); snprintf_P(mqttClientId, sizeof(mqttClientId), PSTR("%s-%s"), haspDevice.get_hostname(), mac.c_str());
httpMessage += mqttClientId; httpMessage += mqttClientId;
} }
@ -677,7 +677,7 @@ void webHandleInfo()
httpMessage += FPSTR(MAIN_MENU_BUTTON); httpMessage += FPSTR(MAIN_MENU_BUTTON);
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -714,7 +714,7 @@ void webHandleInfo()
// return F("text/plain"); // return F("text/plain");
// } // }
static String getContentType(const String & path) static String getContentType(const String& path)
{ {
char buff[sizeof(mime::mimeTable[0].mimeType)]; char buff[sizeof(mime::mimeTable[0].mimeType)];
// Check all entries but last one for match, return if found // Check all entries but last one for match, return if found
@ -772,9 +772,9 @@ void webUploadProgress()
static inline void webUpdatePrintError() static inline void webUpdatePrintError()
{ {
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
String output((char *)0); String output((char*)0);
output.reserve(128); output.reserve(128);
StringStream stream((String &)output); StringStream stream((String&)output);
Update.printError(stream); // ESP8266 only has printError() Update.printError(stream); // ESP8266 only has printError()
LOG_ERROR(TAG_HTTP, output.c_str()); LOG_ERROR(TAG_HTTP, output.c_str());
haspProgressMsg(output.c_str()); haspProgressMsg(output.c_str());
@ -789,14 +789,14 @@ void webUpdateReboot()
LOG_INFO(TAG_HTTP, F("Update Success: %u bytes received. Rebooting..."), upload->totalSize); LOG_INFO(TAG_HTTP, F("Update Success: %u bytes received. Rebooting..."), upload->totalSize);
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<b>Upload complete. Rebooting device, please wait...</b>"); httpMessage += F("<b>Upload complete. Rebooting device, please wait...</b>");
webSendPage(httpGetNodename(), httpMessage.length(), true); webSendPage(haspDevice.get_hostname(), httpMessage.length(), true);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -875,7 +875,7 @@ void handleFileUpload()
if(upload->status == UPLOAD_FILE_START) { if(upload->status == UPLOAD_FILE_START) {
if(!httpIsAuthenticated(F("fileupload"))) return; if(!httpIsAuthenticated(F("fileupload"))) return;
LOG_INFO(TAG_HTTP, F("Total size: %s"), webServer.headerName(0).c_str()); LOG_INFO(TAG_HTTP, F("Total size: %s"), webServer.headerName(0).c_str());
String filename((char *)0); String filename((char*)0);
filename.reserve(128); filename.reserve(128);
filename = upload->filename; filename = upload->filename;
if(!filename.startsWith("/")) { if(!filename.startsWith("/")) {
@ -1040,10 +1040,10 @@ void webHandleConfig()
#endif #endif
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
#if HASP_USE_WIFI > 0 #if HASP_USE_WIFI > 0
@ -1080,7 +1080,7 @@ void webHandleConfig()
httpMessage += FPSTR(MAIN_MENU_BUTTON); httpMessage += FPSTR(MAIN_MENU_BUTTON);
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -1098,10 +1098,10 @@ void webHandleMqttConfig()
{ {
// char buffer[128]; // char buffer[128];
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<form method='POST' action='/config'>"); httpMessage += F("<form method='POST' action='/config'>");
@ -1133,7 +1133,7 @@ void webHandleMqttConfig()
// D_HTTP_CONFIGURATION // D_HTTP_CONFIGURATION
// "</button></form></p>"); // "</button></form></p>");
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -1150,10 +1150,10 @@ void webHandleGuiConfig()
StaticJsonDocument<256> settings; StaticJsonDocument<256> settings;
guiGetConfig(settings.to<JsonObject>()); guiGetConfig(settings.to<JsonObject>());
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<form method='POST' action='/config'>"); httpMessage += F("<form method='POST' action='/config'>");
@ -1228,7 +1228,7 @@ void webHandleGuiConfig()
// D_HTTP_CONFIGURATION // D_HTTP_CONFIGURATION
// "</button></form></p>"); // "</button></form></p>");
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
webSendFooter(); webSendFooter();
@ -1245,10 +1245,10 @@ void webHandleWifiConfig()
StaticJsonDocument<256> settings; StaticJsonDocument<256> settings;
wifiGetConfig(settings.to<JsonObject>()); wifiGetConfig(settings.to<JsonObject>());
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<form method='POST' action='/config'>"); httpMessage += F("<form method='POST' action='/config'>");
@ -1272,7 +1272,7 @@ void webHandleWifiConfig()
} }
#endif #endif
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
#if defined(STM32F4xx) #if defined(STM32F4xx)
httpMessage = ""; httpMessage = "";
@ -1296,7 +1296,7 @@ void webHandleHttpConfig()
// String httpMessage((char *)0); // String httpMessage((char *)0);
// httpMessage.reserve(HTTP_PAGE_SIZE); // httpMessage.reserve(HTTP_PAGE_SIZE);
// httpMessage += F("<h1>"); // httpMessage += F("<h1>");
// httpMessage += httpGetNodename(); // httpMessage += haspDevice.get_hostname();
// httpMessage += F("</h1><hr>"); // httpMessage += F("</h1><hr>");
// httpMessage += F("<form method='POST' action='/config'>"); // httpMessage += F("<form method='POST' action='/config'>");
@ -1328,14 +1328,14 @@ void webHandleHttpConfig()
"<p><button type='submit' name='save' value='http'>" D_HTTP_SAVE_SETTINGS "</button></p></form>" "<p><button type='submit' name='save' value='http'>" D_HTTP_SAVE_SETTINGS "</button></p></form>"
"<p><form method='get' action='/config'><button type='submit'>&#8617; " D_HTTP_CONFIGURATION "<p><form method='get' action='/config'><button type='submit'>&#8617; " D_HTTP_CONFIGURATION
"</button></form></p>"), "</button></form></p>"),
httpGetNodename(), settings[FPSTR(FP_CONFIG_USER)].as<String>().c_str(), haspDevice.get_hostname(), settings[FPSTR(FP_CONFIG_USER)].as<String>().c_str(),
settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()); settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str());
// if(settings[FPSTR(FP_CONFIG_PASS)].as<String>() != "") { // if(settings[FPSTR(FP_CONFIG_PASS)].as<String>() != "") {
// httpMessage += F(D_PASSWORD_MASK); // httpMessage += F(D_PASSWORD_MASK);
// } // }
webSendPage(httpGetNodename(), len, false); webSendPage(haspDevice.get_hostname(), len, false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -1368,10 +1368,10 @@ void webHandleGpioConfig()
} }
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<form method='POST' action='/config'>"); httpMessage += F("<form method='POST' action='/config'>");
@ -1466,7 +1466,7 @@ void webHandleGpioConfig()
// D_HTTP_CONFIGURATION // D_HTTP_CONFIGURATION
// "</button></form></p>"); // "</button></form></p>");
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -1484,10 +1484,10 @@ void webHandleGpioOptions()
uint8_t config_id = webServer.arg(F("id")).toInt(); uint8_t config_id = webServer.arg(F("id")).toInt();
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<form method='GET' action='/config/gpio'>"); httpMessage += F("<form method='GET' action='/config/gpio'>");
@ -1542,7 +1542,7 @@ void webHandleGpioOptions()
httpMessage += F("<p><b>Group</b> <select id='group' name='group'>"); httpMessage += F("<p><b>Group</b> <select id='group' name='group'>");
httpMessage += getOption(0, F("None"), conf.group == 0); httpMessage += getOption(0, F("None"), conf.group == 0);
String group((char *)0); String group((char*)0);
group.reserve(10); group.reserve(10);
for(int i = 1; i < 15; i++) { for(int i = 1; i < 15; i++) {
group = F("Group "); group = F("Group ");
@ -1565,7 +1565,7 @@ void webHandleGpioOptions()
httpMessage += PSTR("<p><form method='get' action='/config/gpio'><button type='submit'>&#8617; " D_HTTP_BACK httpMessage += PSTR("<p><form method='get' action='/config/gpio'><button type='submit'>&#8617; " D_HTTP_BACK
"</button></form></p>"); "</button></form></p>");
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
webSendFooter(); webSendFooter();
@ -1583,10 +1583,10 @@ void webHandleDebugConfig()
debugGetConfig(settings.to<JsonObject>()); debugGetConfig(settings.to<JsonObject>());
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<form method='POST' action='/config'>"); httpMessage += F("<form method='POST' action='/config'>");
@ -1634,7 +1634,7 @@ void webHandleDebugConfig()
// D_HTTP_CONFIGURATION // D_HTTP_CONFIGURATION
// "</button></form></p>"); // "</button></form></p>");
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -1650,10 +1650,10 @@ void webHandleHaspConfig()
haspGetConfig(settings.to<JsonObject>()); haspGetConfig(settings.to<JsonObject>());
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<p><form action='/edit' method='post' enctype='multipart/form-data'><input type='file' " httpMessage += F("<p><form action='/edit' method='post' enctype='multipart/form-data'><input type='file' "
@ -1737,7 +1737,7 @@ void webHandleHaspConfig()
// type='submit'>"D_HTTP_CONFIGURATION"</button></form></p>"); // type='submit'>"D_HTTP_CONFIGURATION"</button></form></p>");
httpMessage += FPSTR(MAIN_MENU_BUTTON); httpMessage += FPSTR(MAIN_MENU_BUTTON);
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -1759,7 +1759,7 @@ void httpHandleNotFound()
// LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"), String(webServer.client().remoteIP()).c_str()); // LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"), String(webServer.client().remoteIP()).c_str());
#endif #endif
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("File Not Found\n\nURI: "); httpMessage += F("File Not Found\n\nURI: ");
@ -1781,10 +1781,10 @@ void webHandleFirmware()
if(!httpIsAuthenticated(F("firmware"))) return; if(!httpIsAuthenticated(F("firmware"))) return;
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<p><form action='/update' method='post' enctype='multipart/form-data'><input type='file' " httpMessage += F("<p><form action='/update' method='post' enctype='multipart/form-data'><input type='file' "
@ -1797,7 +1797,7 @@ void webHandleFirmware()
httpMessage += FPSTR(MAIN_MENU_BUTTON); httpMessage += FPSTR(MAIN_MENU_BUTTON);
webSendPage(httpGetNodename(), httpMessage.length(), false); webSendPage(haspDevice.get_hostname(), httpMessage.length(), false);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -1810,16 +1810,16 @@ void httpHandleEspFirmware()
if(!httpIsAuthenticated(F("espfirmware"))) return; if(!httpIsAuthenticated(F("espfirmware"))) return;
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
httpMessage += F("<p><b>ESP update</b></p>Updating ESP firmware from: "); httpMessage += F("<p><b>ESP update</b></p>Updating ESP firmware from: ");
httpMessage += webServer.arg("espFirmware"); httpMessage += webServer.arg("espFirmware");
webSendPage(httpGetNodename(), httpMessage.length(), true); webSendPage(haspDevice.get_hostname(), httpMessage.length(), true);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
// httpMessage.clear(); // httpMessage.clear();
} }
@ -1846,10 +1846,10 @@ void httpHandleResetConfig()
bool resetConfirmed = webServer.arg(F("confirm")) == F("yes"); bool resetConfirmed = webServer.arg(F("confirm")) == F("yes");
{ {
String httpMessage((char *)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
httpMessage += F("<h1>"); httpMessage += F("<h1>");
httpMessage += httpGetNodename(); httpMessage += haspDevice.get_hostname();
httpMessage += F("</h1><hr>"); httpMessage += F("</h1><hr>");
if(resetConfirmed) { // User has confirmed, so reset everything if(resetConfirmed) { // User has confirmed, so reset everything
@ -1877,7 +1877,7 @@ void httpHandleResetConfig()
// "</button></form></p>"); // "</button></form></p>");
} }
webSendPage(httpGetNodename(), httpMessage.length(), resetConfirmed); webSendPage(haspDevice.get_hostname(), httpMessage.length(), resetConfirmed);
webServer.sendContent(httpMessage); webServer.sendContent(httpMessage);
} }
// httpMessage.clear(); // httpMessage.clear();
@ -1928,8 +1928,8 @@ void httpSetup()
// httpSetConfig(settings); // httpSetConfig(settings);
// ask server to track these headers // ask server to track these headers
const char * headerkeys[] = {"Content-Length"}; // "Authentication" const char* headerkeys[] = {"Content-Length"}; // "Authentication"
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char *); size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
webServer.collectHeaders(headerkeys, headerkeyssize); webServer.collectHeaders(headerkeys, headerkeyssize);
// Shared pages // Shared pages
@ -2050,7 +2050,7 @@ void httpEvery5Seconds()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
bool httpGetConfig(const JsonObject & settings) bool httpGetConfig(const JsonObject& settings)
{ {
bool changed = false; bool changed = false;
@ -2077,7 +2077,7 @@ bool httpGetConfig(const JsonObject & settings)
* *
* @param[in] settings JsonObject with the config settings. * @param[in] settings JsonObject with the config settings.
**/ **/
bool httpSetConfig(const JsonObject & settings) bool httpSetConfig(const JsonObject& settings)
{ {
configOutput(settings, TAG_HTTP); configOutput(settings, TAG_HTTP);
bool changed = false; bool changed = false;
@ -2098,7 +2098,7 @@ bool httpSetConfig(const JsonObject & settings)
} }
#endif // HASP_USE_CONFIG #endif // HASP_USE_CONFIG
size_t httpClientWrite(const uint8_t * buf, size_t size) size_t httpClientWrite(const uint8_t* buf, size_t size)
{ {
/***** Sending 16Kb at once freezes on STM32 EthernetClient *****/ /***** Sending 16Kb at once freezes on STM32 EthernetClient *****/
size_t bytes_sent = 0; size_t bytes_sent = 0;