Skip to content

Commit

Permalink
Remove https server because I don't want to have to manage ssl certif…
Browse files Browse the repository at this point in the history
…icates
  • Loading branch information
Dennis960 committed Sep 5, 2024
1 parent 4f36b13 commit cb59b7a
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 152 deletions.
64 changes: 56 additions & 8 deletions Firmware/frontend/src/pages/cloud-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { StateController } from "@lit-app/state";
import { html } from "lit";
import { customElement, query, state } from "lit/decorators.js";
import {
BlumyCloudConfiguration,
CloudConfiguration,
CloudConfigurationType,
cloudConfigurationTypes,
Expand Down Expand Up @@ -181,6 +182,48 @@ export class CloudPage extends BasePage {
return success;
}

async loadConfigFromQuery(queryParameters: URLSearchParams) {
const token = queryParameters.get("token")!;
const url = queryParameters.get("blumyUrl")!;
queryParameters.delete("token");
queryParameters.delete("blumyUrl");
window.history.replaceState(
null,
"",
`${location.pathname}?${queryParameters.toString()}`
);
const blumyConfiguration: BlumyCloudConfiguration = {
type: "blumy",
token: token,
url: url,
};
this.configurationStates.blumy.formElement!.setConfig(
blumyConfiguration as never
);

const connectionSuccess = await testCloudConnection(blumyConfiguration);

if (connectionSuccess) {
const res = await setCloudCredentials(blumyConfiguration);
if (!res.ok) {
this.errorText = "Fehler, Gerät antwortet nicht";
} else {
this.configurationStates.blumy.configuration =
blumyConfiguration;
this.configurationStates.blumy.open = true;
this.configurationStates.blumy.message = {
content: this.configurationStates.blumy.testMessage.success,
type: "success",
};
}
} else {
this.configurationStates.blumy.message = {
content: this.configurationStates.blumy.testMessage.error,
type: "error",
};
}
}

async firstUpdated() {
this.configurationStates.blumy.formElement = this.cloudFormElement;
this.configurationStates.http.formElement = this.httpFormElement;
Expand All @@ -194,26 +237,31 @@ export class CloudPage extends BasePage {
configurationState.open = true;
}
}
const queryParameters = new URLSearchParams(location.search);
if (queryParameters.has("token") && queryParameters.has("blumyUrl")) {
await this.loadConfigFromQuery(queryParameters);
}
this.configurationStates = { ...this.configurationStates };
this.updateConfigured();
}

async handleExternalSetup() {
const originHttps = location.origin.replace("http", "https");
location.href = `https://blumy.cloud/selector?redirect=${location.origin}/?page=5&apiUrl=${originHttps}/api/cloudSetup/blumy`;
location.href = `https://blumy.cloud/selector?redirect=${location.href}`;
}

render() {
return html`
<title-element
>Automatische Schnittstellen-Konfiguration</title-element
>
<button-element
name="Über die Blumy Cloud einrichten"
@click="${() => this.handleExternalSetup()}"
?disabled="${loadingState.state > 0}"
?secondary="${true}"
></button-element>
<div style="width: fit-content;">
<button-element
name="Über die Blumy Cloud einrichten"
@click="${() => this.handleExternalSetup()}"
?disabled="${loadingState.state > 0}"
?secondary="${true}"
></button-element>
</div>
<br />
<title-element>Manuelle Schnittstellen-Konfiguration</title-element>
<collapsible-element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class ButtonElement extends LitElement {
static styles = [
css`
button {
width: 100%;
border: 1px solid var(--grey);
border-radius: 0.25rem;
padding: 0.5rem 0.75rem;
Expand Down
25 changes: 13 additions & 12 deletions Firmware/frontend/src/pages/wifi-scanner-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ export class WifiScannerPage extends BasePage {
@state() errorText: string = "";
@state() networks: Network[] = [];

shouldUpdateNetworks = true;

async updateNetworks() {
const networks = await getNetworks();
if (networks === undefined || networks === null) {
this.errorText = "Fehler beim Scannen der Netzwerke";
return;
} else {
this.errorText = "";
this.networks = networks;
while (this.shouldUpdateNetworks) {
const networks = await getNetworks();
if (networks === undefined || networks === null) {
this.errorText = "Fehler beim Scannen der Netzwerke";
return;
} else {
this.errorText = "";
this.networks = networks;
}
await new Promise((res) => setTimeout(res, 5000));
}
}

interval = setInterval(() => {
this.updateNetworks();
}, 5000);

constructor() {
super();
this.updateNetworks();
Expand All @@ -33,7 +34,7 @@ export class WifiScannerPage extends BasePage {
disconnectedCallback() {
super.disconnectedCallback();

clearInterval(this.interval);
this.shouldUpdateNetworks = false;
}

onNetworkClick(e: CustomEvent) {
Expand Down
4 changes: 1 addition & 3 deletions Firmware/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
idf_component_register(SRCS "index_html.c" "plantstore.c" "plantfi.c" "configuration_mode_server.c" "peripherals/sensors.c" "peripherals/adc.c" "main.c"
INCLUDE_DIRS "."
EMBED_TXTFILES "certs/servercert.pem"
"certs/prvtkey.pem")
INCLUDE_DIRS ".")
1 change: 0 additions & 1 deletion Firmware/main/certs/generate-certs.sh

This file was deleted.

28 changes: 0 additions & 28 deletions Firmware/main/certs/prvtkey.pem

This file was deleted.

19 changes: 0 additions & 19 deletions Firmware/main/certs/servercert.pem

This file was deleted.

53 changes: 4 additions & 49 deletions Firmware/main/configuration_mode_server.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "configuration_mode_server.h"

#include "esp_log.h"
#include "esp_https_server.h"
#include "cJSON.h"

#include "plantfi.h"
Expand Down Expand Up @@ -213,7 +212,6 @@ esp_err_t post_api_cloudSetup_blumy_handler(httpd_req_t *req)
plantstore_setCloudConfigurationBlumy(token, url);

const char resp[] = "OK";
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
Expand Down Expand Up @@ -291,7 +289,6 @@ esp_err_t get_api_cloudSetup_blumy_handler(httpd_req_t *req)
char *resp = cJSON_Print(root);
cJSON_Delete(root);

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
free(resp);
return ESP_OK;
Expand Down Expand Up @@ -355,32 +352,30 @@ esp_err_t post_api_cloudTest_blumy_handler(httpd_req_t *req)
const bool connectionOk = plantfi_test_blumy_connection(token, url);
char resp[6];
sprintf(resp, "%s", connectionOk ? "true" : "false");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}

esp_err_t post_api_cloudDisable_mqtt_handler(httpd_req_t *req)
{
plantstore_setCloudConfigurationMqtt("", "", 0, "", "", "", "");
plantstore_resetCloudConfigurationMqtt();
const char resp[] = "OK";
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}

esp_err_t post_api_cloudDisable_http_handler(httpd_req_t *req)
{
plantstore_setCloudConfigurationHttp("", "", "");
plantstore_resetCloudConfigurationHttp();
const char resp[] = "OK";
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}

esp_err_t post_api_cloudDisable_blumy_handler(httpd_req_t *req)
{
plantstore_setCloudConfigurationBlumy("", "");
plantstore_resetCloudConfigurationBlumy();
const char resp[] = "OK";
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
Expand Down Expand Up @@ -532,11 +527,10 @@ esp_err_t get_api_sensor_data_handler(httpd_req_t *req)

esp_err_t post_api_factoryReset_handler(httpd_req_t *req)
{
plantstore_factoryReset();

const char resp[] = "OK";
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);

plantstore_factoryReset();
esp_restart();
return ESP_OK;
}
Expand Down Expand Up @@ -899,35 +893,6 @@ void register_uri_handlers(httpd_handle_t server)
httpd_register_uri_handler(server, &get);
}

/* Function for starting the webserver */
httpd_handle_t start_https_webserver(void)
{
ESP_LOGI("HTTP", "Starting HTTPS server");
/* Empty handle to esp_http_server */
httpd_handle_t server = NULL;

/* Generate default configuration */
httpd_ssl_config_t config = HTTPD_SSL_CONFIG_DEFAULT();
config.httpd.uri_match_fn = httpd_uri_match_wildcard;
config.httpd.max_uri_handlers = 50;

extern const unsigned char servercert_start[] asm("_binary_servercert_pem_start");
extern const unsigned char servercert_end[] asm("_binary_servercert_pem_end");
config.servercert = servercert_start;
config.servercert_len = servercert_end - servercert_start;

extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
config.prvtkey_pem = prvtkey_pem_start;
config.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;

/* Start the httpd server */
ESP_ERROR_CHECK(httpd_ssl_start(&server, &config));
register_uri_handlers(server);
/* If server failed to start, handle will be NULL */
return server;
}

httpd_handle_t start_webserver(void)
{
ESP_LOGI("HTTP", "Starting HTTP server");
Expand All @@ -946,16 +911,6 @@ httpd_handle_t start_webserver(void)
return server;
}

/* Function for stopping the webserver */
void stop_https_webserver(httpd_handle_t server)
{
if (server)
{
/* Stop the httpd server */
httpd_ssl_stop(server);
}
}

/* Function for stopping the webserver */
void stop_webserver(httpd_handle_t server)
{
Expand Down
4 changes: 1 addition & 3 deletions Firmware/main/configuration_mode_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@

/* Function for starting the webserver */
httpd_handle_t start_webserver(void);
httpd_handle_t start_https_webserver(void);
/* Function for stopping the webserver */
void stop_webserver(httpd_handle_t server);
void stop_https_webserver(httpd_handle_t server);
void stop_webserver(httpd_handle_t server);
2 changes: 0 additions & 2 deletions Firmware/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void configuration_mode(bool isConfigured)

ESP_LOGI("MODE", "Starting webserver");
httpd_handle_t webserver = start_webserver();
httpd_handle_t https_webserver = start_https_webserver();
plantfi_configureStaFromPlantstore();

bool wasBootButtonPressed = false;
Expand All @@ -64,7 +63,6 @@ void configuration_mode(bool isConfigured)
vTaskDelay(10 / portTICK_PERIOD_MS); // Reset watchdog
}
stop_webserver(webserver);
stop_https_webserver(https_webserver);
sensors_detach_boot_button_interrupt();
sensors_playShutdownSound();
start_deep_sleep();
Expand Down
Loading

0 comments on commit cb59b7a

Please sign in to comment.