Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set a custom hostname #72

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"savedStatus":"Stopped","savedTPD":"220","savedHour":"00","savedMinutes":"00","savedTimerState":"0","savedDirection":"BOTH", "customWindDuration":"180", "customWindPauseDuration":"15"}
{"savedStatus":"Stopped","savedTPD":"220","savedHour":"00","savedMinutes":"00","savedTimerState":"0","savedDirection":"BOTH", "customWindDuration":"180", "customWindPauseDuration":"15", "customHostname":""}
32 changes: 29 additions & 3 deletions src/platformio/osww-server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct RUNTIME_VARS
String timerEnabled = "0";
String customWindDuration = "";
String customWindPauseDuration = "";
String customHostname = "";
};

/*
Expand All @@ -95,7 +96,7 @@ AsyncWebServer server(80);
HTTPClient http;
WiFiClient client;
ESP32Time rtc;
String winderooVersion = "3.0.0";
String winderooVersion = "4.0.1";

#if PWM_MOTOR_CONTROL
MotorControl motor(directionalPinA, directionalPinB, true);
Expand Down Expand Up @@ -533,6 +534,7 @@ void loadConfigVarsFromFile(String file_name)
userDefinedSettings.direction = json["savedDirection"].as<String>(); // CW || CCW || BOTH
userDefinedSettings.customWindDuration = json["customWindDuration"].as<String>(); // 180 (in seconds)
userDefinedSettings.customWindPauseDuration = json["customWindPauseDuration"].as<String>(); // 15 (in seconds)
userDefinedSettings.customHostname = json["customHostname"].as<String>(); // <String>

this_file.close();
}
Expand Down Expand Up @@ -564,6 +566,7 @@ bool writeConfigVarsToFile(String file_name, const RUNTIME_VARS& userDefinedSett
json["savedDirection"] = userDefinedSettings.direction;
json["customWindDuration"] = userDefinedSettings.customWindDuration;
json["customWindPauseDuration"] = userDefinedSettings.customWindPauseDuration;
json["customHostname"] = userDefinedSettings.customHostname;

if (serializeJson(json, this_file) == 0)
{
Expand Down Expand Up @@ -618,6 +621,7 @@ void startWebserver()
json["screenEquipped"] = screenEquipped;
json["customWindDuration"] = userDefinedSettings.customWindDuration;
json["customWindPauseDuration"] = userDefinedSettings.customWindPauseDuration;
json["customHostname"] = userDefinedSettings.customHostname;
serializeJson(json, *response);

request->send(response);
Expand Down Expand Up @@ -734,6 +738,7 @@ void startWebserver()
String requestTPD = json["tpd"].as<String>();
String requestAction = json["action"].as<String>();
screenSleep = json["screenSleep"].as<bool>();
String customHostname = json["customHostname"].as<String>();

// Update Home Assistant state
if (HOME_ASSISTANT_ENABLED)
Expand Down Expand Up @@ -776,7 +781,7 @@ void startWebserver()
}

// Update (turns) rotations per day
if (strcmp(requestTPD.c_str(), userDefinedSettings.rotationsPerDay .c_str()) != 0)
if (strcmp(requestTPD.c_str(), userDefinedSettings.rotationsPerDay.c_str()) != 0)
{
userDefinedSettings.rotationsPerDay = requestTPD;

Expand Down Expand Up @@ -834,6 +839,11 @@ void startWebserver()
updateRtcEpoch(rtc, rtcUpdateHours, rtcUpdateMinutes);
}

if (strcmp(customHostname.c_str(), userDefinedSettings.customHostname.c_str()) != 0)
{
userDefinedSettings.customHostname = customHostname;
}

// Write new parameters to file
bool writeSuccess = writeConfigVarsToFile(settingsFile, userDefinedSettings);
if ( !writeSuccess )
Expand Down Expand Up @@ -1225,6 +1235,18 @@ void onPowerSwitchCommand(bool state, HASwitch* sender)
sender->setState(state);
}

String determineHostname()
{
String defaultHostname = "winderoo";

if (userDefinedSettings.customHostname != nullptr && strcmp(userDefinedSettings.customHostname.c_str(), "") != 0)
{
return userDefinedSettings.customHostname;
}

return defaultHostname;
}

void setup()
{
WiFi.mode(WIFI_STA);
Expand Down Expand Up @@ -1278,7 +1300,11 @@ void setup()
// retrieve & read saved settings
loadConfigVarsFromFile(settingsFile);

if (!MDNS.begin("winderoo"))
Serial.print("[STATUS] - using hostname: ");
Serial.print(determineHostname().c_str());
Serial.println(".local");

if (!MDNS.begin(determineHostname().c_str()))
{
Serial.println("[STATUS] - Failed to start mDNS");
drawNotification("Failed to start mDNS");
Expand Down
Loading