Skip to content

Commit

Permalink
Merge pull request #755 from SignalK/status_page_item
Browse files Browse the repository at this point in the history
Web UI class cleanups
  • Loading branch information
mairas authored Oct 5, 2024
2 parents 0f73941 + 3ff2ff1 commit 24d1d9f
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 160 deletions.
12 changes: 7 additions & 5 deletions src/sensesp/net/web/base_command_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "base_command_handler.h"

#include "sensesp/net/web/autogen/frontend_files.h"
#include "sensesp/ui/status_page_item.h"

namespace sensesp {

void add_http_reset_handler(HTTPServer* server) {
Expand All @@ -9,8 +12,7 @@ void add_http_reset_handler(HTTPServer* server) {
"Resetting device back to factory defaults. "
"You may have to reconfigure the WiFi settings.",
0);
event_loop()->onDelay(
500, []() { SensESPBaseApp::get()->reset(); });
event_loop()->onDelay(500, []() { SensESPBaseApp::get()->reset(); });
return ESP_OK;
});
server->add_handler(reset_handler);
Expand All @@ -29,13 +31,13 @@ void add_http_restart_handler(HTTPServer* server) {
void add_http_info_handler(HTTPServer* server) {
HTTPRequestHandler* info_handler =
new HTTPRequestHandler(1 << HTTP_GET, "/api/info", [](httpd_req_t* req) {
auto ui_outputs = UIOutputBase::get_ui_outputs();
auto status_page_items = StatusPageItemBase::get_status_page_items();

JsonDocument json_doc;
JsonArray info_items = json_doc.to<JsonArray>();

for (auto info_item = ui_outputs->begin();
info_item != ui_outputs->end(); ++info_item) {
for (auto info_item = status_page_items->begin();
info_item != status_page_items->end(); ++info_item) {
info_items.add(info_item->second->as_json());
}

Expand Down
4 changes: 0 additions & 4 deletions src/sensesp/net/web/base_command_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

#include "ArduinoJson.h"
#include "sensesp/net/http_server.h"
#include "sensesp/net/web/autogen/frontend_files.h"
#include "sensesp/net/web/static_file_handler.h"
#include "sensesp/system/configurable.h"
#include "sensesp/ui/ui_output.h"
#include "sensesp_base_app.h"

namespace sensesp {
Expand Down
23 changes: 11 additions & 12 deletions src/sensesp/sensors/system_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace sensesp {
* @brief Connect a system information sensor to SKOutput
**/
template <typename T>
void connect_system_info_sensor(Sensor<T>* sensor, String prefix, String name) {
void connect_system_info_sensor(ValueProducer<T>* sensor, String prefix, String name) {
auto hostname_obs = SensESPBaseApp::get()->get_hostname_observable();
String hostname = hostname_obs->get();
String path = prefix + hostname + "." + name;
Expand Down Expand Up @@ -42,7 +42,7 @@ void connect_system_info_sensor(Sensor<T>* sensor, String prefix, String name) {
* appears in your project's output. That is configured with
* SensESPAppBuilder.
**/
class SystemHz : public FloatSensor {
class SystemHz : public ValueProducer<float> {
public:
SystemHz() {
elapsed_millis_ = 0;
Expand All @@ -53,10 +53,9 @@ class SystemHz : public FloatSensor {
}
String get_value_name() { return "systemhz"; }

private:
protected:
uint32_t tick_count_ = 0;
elapsedMillis elapsed_millis_;
float system_hz_;
void tick();
void update();
};
Expand All @@ -68,15 +67,15 @@ class SystemHz : public FloatSensor {
* appears in your project's output. That is configured with
* SensESPAppBuilder.
**/
class FreeMem : public IntSensor {
class FreeMem : public ValueProducer<uint32_t> {
public:
FreeMem() {
event_loop()->onRepeat(1000,
[this]() { this->update(); });
}
String get_value_name() { return "freemem"; }

private:
protected:
void update();
};

Expand All @@ -88,15 +87,15 @@ class FreeMem : public IntSensor {
* appears in your project's output. That is configured with
* SensESPAppBuilder.
**/
class Uptime : public FloatSensor {
class Uptime : public ValueProducer<float> {
public:
Uptime() {
event_loop()->onRepeat(1000,
[this]() { this->update(); });
}
String get_value_name() { return "uptime"; }

private:
protected:
void update();
};

Expand All @@ -108,15 +107,15 @@ class Uptime : public FloatSensor {
* appears in your project's output. That is configured with
* SensESPAppBuilder.
**/
class IPAddrDev : public StringSensor {
class IPAddrDev : public ValueProducer<String> {
public:
IPAddrDev() {
event_loop()->onRepeat(10000,
[this]() { this->update(); });
}
String get_value_name() { return "ipaddr"; }

private:
protected:
void update();
};

Expand All @@ -128,15 +127,15 @@ class IPAddrDev : public StringSensor {
* appears in your project's output. That is configured with
* SensESPAppBuilder.
**/
class WiFiSignal : public FloatSensor {
class WiFiSignal : public ValueProducer<int> {
public:
WiFiSignal() {
event_loop()->onRepeat(3000,
[this]() { this->update(); });
}
String get_value_name() { return "wifisignal"; }

private:
protected:
void update();
};

Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/system/ui_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#warning \
"This header file has been moved. For future code, use the new location."

#include "sensesp/ui/ui_output.h"
#include "sensesp/ui/status_page_item.h"

#endif
7 changes: 7 additions & 0 deletions src/sensesp/ui/status_page_item.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "status_page_item.h"

namespace sensesp {

std::map<String, StatusPageItemBase*> StatusPageItemBase::status_page_items_;

} // namespace sensesp
59 changes: 59 additions & 0 deletions src/sensesp/ui/status_page_item.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef SENSESP_UI_UI_OUTPUT_H
#define SENSESP_UI_UI_OUTPUT_H

#include <ArduinoJson.h>
#include <functional>
#include <map>

#include "Arduino.h"
#include "sensesp/system/observablevalue.h"
#include "sensesp/system/valueconsumer.h"
#include "sensesp/system/valueproducer.h"

namespace sensesp {

constexpr char kUIOutputDefaultGroup[] = "Default";
constexpr int kUIOutputDefaultOrder = 1000;

class StatusPageItemBase {
public:
StatusPageItemBase(String name, String group, int order)
: name_(name), group_(group), order_(order) {}

String& get_name() { return name_; }

virtual JsonDocument as_json() = 0;

static const std::map<String, StatusPageItemBase*>* get_status_page_items() {
return &status_page_items_;
}

protected:
String name_;
String group_ = kUIOutputDefaultGroup;
int order_ = kUIOutputDefaultOrder;
static std::map<String, StatusPageItemBase*> status_page_items_;
};

template <typename T>
class StatusPageItem : public StatusPageItemBase, public ObservableValue<T> {
public:
StatusPageItem(String name, const T& value, String group, int order)
: StatusPageItemBase(name, group, order), ObservableValue<T>(value) {
status_page_items_[name] = this;
}

protected:
virtual JsonDocument as_json() override{
JsonDocument obj;
obj["name"] = name_;
obj["value"] = this->get();
obj["group"] = group_;
obj["order"] = order_;
return obj;
}
};

} // namespace sensesp

#endif
7 changes: 0 additions & 7 deletions src/sensesp/ui/ui_output.cpp

This file was deleted.

93 changes: 0 additions & 93 deletions src/sensesp/ui/ui_output.h

This file was deleted.

26 changes: 26 additions & 0 deletions src/sensesp_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@ void SensESPApp::setup() {
if (button_gpio_pin_ != -1) {
button_handler_ = new ButtonHandler(button_gpio_pin_);
}

// connect status page items
connect_status_page_items();
}

void SensESPApp::connect_status_page_items() {
this->hostname_->connect_to(this->hostname_ui_output_);
this->event_loop_.onRepeat(
4999, [this]() { wifi_ssid_ui_output_->set(WiFi.SSID()); });
this->event_loop_.onRepeat(
4998, [this]() { free_memory_ui_output_->set(ESP.getFreeHeap()); });
this->event_loop_.onRepeat(
4997, [this]() { wifi_rssi_ui_output_->set(WiFi.RSSI()); });
this->event_loop_.onRepeat(4996, [this]() {
sk_server_address_ui_output_->set(ws_client_->get_server_address());
});
this->event_loop_.onRepeat(4995, [this]() {
sk_server_port_ui_output_->set(ws_client_->get_server_port());
});
this->event_loop_.onRepeat(4994, [this]() {
sk_server_connection_ui_output_->set(ws_client_->get_connection_status());
});
ws_client_->get_delta_tx_count_producer().connect_to(
delta_tx_count_ui_output_);
ws_client_->get_delta_rx_count_producer().connect_to(
delta_rx_count_ui_output_);
}

ObservableValue<String>* SensESPApp::get_hostname_observable() {
Expand Down
Loading

0 comments on commit 24d1d9f

Please sign in to comment.