Skip to content

Commit

Permalink
Merge pull request #482 from mairas/get_hostname
Browse files Browse the repository at this point in the history
Implement a get_hostname() convenience method
  • Loading branch information
mairas authored Dec 8, 2021
2 parents 186380b + 1351779 commit 8ddb33d
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/minimal_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void setup() {
// the HTTP configuration interface

auto* networking = new Networking(
"/system/net", "", "", sensesp_app->get_hostname_observable()->get(),
"/system/net", "", "", sensesp_app->get_hostname(),
"thisisfine");
auto* http_server = new HTTPServer();

Expand Down
2 changes: 1 addition & 1 deletion src/net/debug_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ DEBUG_CLASS Debug;

void DebugOutput::start() {
#ifndef DEBUG_DISABLED
Debug.begin(SensESPBaseApp::get()->get_hostname_observable()->get());
Debug.begin(SensESPBaseApp::get_hostname());
Debug.setResetCmdEnabled(true);
// serial port debugging happens synchronously
#ifdef REMOTE_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/net/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace sensesp {

void MDNSDiscovery::start() {
const char* hostname =
SensESPBaseApp::get()->get_hostname_observable()->get().c_str();
SensESPBaseApp::get_hostname().c_str();

// MDNS.begin(hostname) will crash if hostname is blank
if ((hostname == NULL) || (hostname[0] == '\0')) {
Expand Down
2 changes: 1 addition & 1 deletion src/net/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void HTTPServer::handle_info(AsyncWebServerRequest* request) {
response->setCode(200);
response->printf(
"Name: %s, build at %s %s\n",
SensESPBaseApp::get()->get_hostname_observable()->get().c_str(), __DATE__,
SensESPBaseApp::get_hostname().c_str(), __DATE__,
__TIME__);

response->printf("MAC: %s\n", WiFi.macAddress().c_str());
Expand Down
10 changes: 5 additions & 5 deletions src/net/networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void Networking::setup_saved_ssid() {
setup_wifi_callbacks();

const char* hostname =
SensESPBaseApp::get()->get_hostname_observable()->get().c_str();
SensESPBaseApp::get_hostname().c_str();

WiFi.setHostname(hostname);

Expand All @@ -89,7 +89,7 @@ void Networking::wifi_station_disconnected() {
void Networking::setup_wifi_manager() {
should_save_config = false;

String hostname = SensESPBaseApp::get()->get_hostname_observable()->get();
String hostname = SensESPBaseApp::get_hostname();

setup_wifi_callbacks();

Expand All @@ -106,14 +106,14 @@ void Networking::setup_wifi_manager() {
wifi_manager->addParameter(&custom_hostname);

// Create a unique SSID for configuring each SensESP Device
String config_ssid = SensESPBaseApp::get()->get_hostname_observable()->get();
String config_ssid = SensESPBaseApp::get_hostname();
config_ssid = "Configure " + config_ssid;
const char* pconfig_ssid = config_ssid.c_str();

this->emit(WifiState::kWifiManagerActivated);

WiFi.setHostname(
SensESPBaseApp::get()->get_hostname_observable()->get().c_str());
SensESPBaseApp::get_hostname().c_str());

if (!wifi_manager->autoConnect(pconfig_ssid, wifi_manager_password_)) {
debugE("Failed to connect to wifi and config timed out. Restarting...");
Expand Down Expand Up @@ -173,7 +173,7 @@ String Networking::get_config_schema() {
// FIXME: hostname should be saved in SensESPApp

void Networking::get_configuration(JsonObject& root) {
// root["hostname"] = SensESPBaseApp::get()->get_hostname_observable()->get();
// root["hostname"] = SensESPBaseApp::get_hostname();
}

bool Networking::set_configuration(const JsonObject& config) {
Expand Down
2 changes: 1 addition & 1 deletion src/net/ws_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void WSClient::send_access_request(const String server_address,
DynamicJsonDocument doc(1024);
doc["clientId"] = client_id_;
doc["description"] = String("SensESP device: ") +
SensESPBaseApp::get()->get_hostname_observable()->get();
SensESPBaseApp::get_hostname();
doc["permissions"] = kRequestPermission;
String json_req = "";
serializeJson(doc, json_req);
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void SensESPApp::setup() {
// create the networking object
networking_ =
new Networking("/system/networking", ssid_, wifi_password_,
SensESPBaseApp::get()->get_hostname_observable()->get(),
SensESPBaseApp::get_hostname(),
wifi_manager_password_);

if (ota_password_ != nullptr) {
Expand Down
9 changes: 9 additions & 0 deletions src/sensesp_base_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,13 @@ ObservableValue<String>* SensESPBaseApp::get_hostname_observable() {
return hostname_;
}

/**
* @brief Get the current hostname.
*
* @return String
*/
String SensESPBaseApp::get_hostname() {
return SensESPBaseApp::get()->get_hostname_observable()->get();
}

} // namespace sensesp
2 changes: 2 additions & 0 deletions src/sensesp_base_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class SensESPBaseApp {
*/
ObservableValue<String>* get_hostname_observable();

static String get_hostname();

protected:
/**
* @brief Construct a new SensESP Base App object
Expand Down
2 changes: 1 addition & 1 deletion src/signalk/signalk_delta_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void SKDeltaQueue::get_delta(String& output) {

JsonObject current = updates.createNestedObject();
JsonObject source = current.createNestedObject("source");
source["label"] = SensESPBaseApp::get()->get_hostname_observable()->get();
source["label"] = SensESPBaseApp::get_hostname();
JsonArray values = current.createNestedArray("values");

while (!buffer.empty()) {
Expand Down

0 comments on commit 8ddb33d

Please sign in to comment.