whole lotta shit clion made me do
This commit is contained in:
46
src/hass.cpp
46
src/hass.cpp
@@ -1,10 +1,12 @@
|
||||
#include <libwebsockets.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <cstring>
|
||||
#include <hass.hpp>
|
||||
#include <log.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
HomeAssistant::HomeAssistant(std::string addr, std::string ent) {
|
||||
HomeAssistant::HomeAssistant(const std::string& addr, const std::string& ent) {
|
||||
address = addr;
|
||||
entity = ent;
|
||||
}
|
||||
@@ -13,7 +15,7 @@ HomeAssistant::~HomeAssistant() {
|
||||
disconnect();
|
||||
}
|
||||
|
||||
void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons reason, char* in) {
|
||||
void HomeAssistant::event_handler(struct lws* wsi, const enum lws_callback_reasons reason, char* in) {
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: {
|
||||
applog(LOG::ERR, "websocket conn error: {}", in ? in : "(null)");
|
||||
@@ -22,7 +24,7 @@ void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons rea
|
||||
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE: {
|
||||
nlohmann::json json = nlohmann::json::parse(in);
|
||||
std::string ha_type = json["type"].get<std::string>();
|
||||
const std::string ha_type = json["type"].get<std::string>();
|
||||
|
||||
if (ha_type == "auth_required") {
|
||||
json.clear();
|
||||
@@ -57,7 +59,7 @@ void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons rea
|
||||
break;
|
||||
}
|
||||
|
||||
applog(LOG::WRN, "unhandled event: {}", (char*)in);
|
||||
applog(LOG::WRN, "unhandled event: {}", static_cast<char*>(in));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -73,9 +75,9 @@ void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons rea
|
||||
|
||||
case LWS_CALLBACK_CLIENT_WRITEABLE: {
|
||||
/* skip whole callback if there's no tx to send */
|
||||
if (tx.size() > 0) {
|
||||
size_t msgLen = LWS_PRE + tx.size();
|
||||
unsigned char* buf = (unsigned char*)malloc(msgLen);
|
||||
if (! tx.empty()) {
|
||||
const size_t msgLen = LWS_PRE + tx.size();
|
||||
auto* buf = static_cast<unsigned char*>(malloc(msgLen));
|
||||
if (buf) {
|
||||
memcpy(&buf[LWS_PRE], tx.c_str(), tx.size());
|
||||
lws_write(wsi, &buf[LWS_PRE], tx.size(), LWS_WRITE_TEXT);
|
||||
@@ -91,32 +93,32 @@ void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons rea
|
||||
}
|
||||
}
|
||||
|
||||
int HomeAssistant::callback(struct lws* wsi, enum lws_callback_reasons reason, void* user, void* in, size_t) {
|
||||
HomeAssistant* self = static_cast<HomeAssistant*>(user);
|
||||
self->event_handler(wsi, reason, (char*)in);
|
||||
int HomeAssistant::callback(struct lws* wsi, const enum lws_callback_reasons reason, void* user, void* in, size_t) {
|
||||
auto* self = static_cast<HomeAssistant*>(user);
|
||||
self->event_handler(wsi, reason, static_cast<char*>(in));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HomeAssistant::connect() {
|
||||
struct lws_client_connect_info i;
|
||||
struct lws_context_creation_info info;
|
||||
struct lws_client_connect_info i{};
|
||||
struct lws_context_creation_info info{};
|
||||
|
||||
lws_set_log_level(LLL_ERR | LLL_WARN, lwsl_emit_syslog);
|
||||
|
||||
memset(&i, 0, sizeof(i));
|
||||
memset(&info, 0, sizeof info);
|
||||
std::memset(&i, 0, sizeof(i));
|
||||
std::memset(&info, 0, sizeof info);
|
||||
|
||||
static const struct lws_protocols protocols[] = {
|
||||
{"lws-minimal-client", callback, sizeof(void*), 0, 0, NULL, 0},
|
||||
static constexpr struct lws_protocols protocols[] = {
|
||||
{"lws-minimal-client", callback, sizeof(void*), 0, 0, nullptr, 0},
|
||||
LWS_PROTOCOL_LIST_TERM};
|
||||
|
||||
static const struct lws_extension extensions[] = {
|
||||
static constexpr struct lws_extension extensions[] = {
|
||||
{"permessage-deflate",
|
||||
lws_extension_callback_pm_deflate,
|
||||
"permessage-deflate"
|
||||
"; client_no_context_takeover"
|
||||
"; client_max_window_bits"},
|
||||
{NULL, NULL, NULL}};
|
||||
{nullptr, nullptr, nullptr}};
|
||||
|
||||
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
||||
info.port = CONTEXT_PORT_NO_LISTEN;
|
||||
@@ -137,7 +139,7 @@ int HomeAssistant::connect() {
|
||||
i.host = i.address;
|
||||
i.origin = i.address;
|
||||
i.ssl_connection = LCCSCF_USE_SSL | LCCSCF_PRIORITIZE_READS;
|
||||
i.protocol = NULL;
|
||||
i.protocol = nullptr;
|
||||
i.userdata = this;
|
||||
i.local_protocol_name = "lws-minimal-client";
|
||||
|
||||
@@ -149,11 +151,11 @@ int HomeAssistant::connect() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void HomeAssistant::service(int timeout_ms) {
|
||||
void HomeAssistant::service(const int timeout_ms) const {
|
||||
lws_callback_on_writable(websock);
|
||||
lws_service(context, timeout_ms);
|
||||
}
|
||||
|
||||
void HomeAssistant::disconnect() {
|
||||
void HomeAssistant::disconnect() const {
|
||||
lws_context_destroy(context);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user