Change formatting to be more in line with ESPHome

This commit is contained in:
Oxan van Leeuwen
2023-02-03 00:23:36 +01:00
parent 0f49c6c589
commit 2848bad8d4
2 changed files with 28 additions and 31 deletions

View File

@@ -29,20 +29,21 @@ MULTI_CONF = True
StreamServerComponent = cg.global_ns.class_("StreamServerComponent", cg.Component)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(StreamServerComponent),
cv.Optional(CONF_PORT): cv.port,
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(uart.UART_DEVICE_SCHEMA)
cv.Schema(
{
cv.GenerateID(): cv.declare_id(StreamServerComponent),
cv.Optional(CONF_PORT): cv.port,
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(uart.UART_DEVICE_SCHEMA)
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
if CONF_PORT in config:
cg.add(var.set_port(config[CONF_PORT]))
yield cg.register_component(var, config)
yield uart.register_uart_device(var, config)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
if CONF_PORT in config:
cg.add(var.set_port(config[CONF_PORT]))
await cg.register_component(var, config)
await uart.register_uart_device(var, config)

View File

@@ -23,7 +23,7 @@
#include "esphome/components/network/util.h"
#include "esphome/components/socket/socket.h"
static const char *TAG = "streamserver";
static const char *TAG = "stream_server";
using namespace esphome;
@@ -51,6 +51,16 @@ void StreamServerComponent::loop() {
this->cleanup();
}
void StreamServerComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Stream Server:");
ESP_LOGCONFIG(TAG, " Address: %s:%u", esphome::network::get_ip_address().str().c_str(), this->port_);
}
void StreamServerComponent::on_shutdown() {
for (const Client &client : this->clients_)
client.socket->shutdown(SHUT_RDWR);
}
void StreamServerComponent::accept() {
struct sockaddr_in client_addr;
socklen_t client_addrlen = sizeof(struct sockaddr_in);
@@ -75,7 +85,7 @@ void StreamServerComponent::read() {
while ((len = this->stream_->available()) > 0) {
char buf[128];
len = std::min(len, 128);
this->stream_->read_array(reinterpret_cast<uint8_t*>(buf), len);
this->stream_->read_array(reinterpret_cast<uint8_t *>(buf), len);
for (const Client &client : this->clients_)
client.socket->write(buf, len);
}
@@ -96,19 +106,5 @@ void StreamServerComponent::write() {
}
}
void StreamServerComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Stream Server:");
ESP_LOGCONFIG(TAG, " Address: %s:%u",
esphome::network::get_ip_address().str().c_str(),
this->port_);
}
void StreamServerComponent::on_shutdown() {
for (const Client &client : this->clients_)
client.socket->shutdown(SHUT_RDWR);
}
StreamServerComponent::Client::Client(std::unique_ptr<esphome::socket::Socket> socket, std::string identifier)
: socket(std::move(socket)), identifier{identifier}
{
}
: socket(std::move(socket)), identifier{identifier} {}