switch to nlohmann json
This commit is contained in:
@@ -11,7 +11,6 @@ Same as plugins. Just write in more features scrub, learn some C++, damn
|
||||
## TODOs
|
||||
- LOOK AT WHAT CLANG SAYS, tweak .clang-format, we really dont need all deez libs in headers, move them out to cpp files, remove unused
|
||||
- also use clang-tidy
|
||||
- move away from ArduinoJson. This ain't an arduino, be fr. Switch to nlohmann/json libs.
|
||||
- Use `std::string_view` or `const std::string &` when possible, its faster, no copy
|
||||
- Extra screens support perhaps? just halt sensors monitoring thread and print another struct on display if needed, have a semaphore or sth
|
||||
- Maybe not even screens, keep the 1st line and change lines 2-4
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
21
src/hass.cpp
21
src/hass.cpp
@@ -1,7 +1,5 @@
|
||||
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <hass.hpp>
|
||||
#include <log.hpp>
|
||||
#include <string>
|
||||
@@ -16,7 +14,6 @@ HomeAssistant::~HomeAssistant() {
|
||||
}
|
||||
|
||||
void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons reason, char* in) {
|
||||
ArduinoJson::JsonDocument json;
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: {
|
||||
log(LOG::ERR, "websocket conn error: {}", in ? in : "(null)");
|
||||
@@ -24,14 +21,14 @@ void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons rea
|
||||
}
|
||||
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE: {
|
||||
ArduinoJson::deserializeJson(json, in);
|
||||
std::string ha_type = json["type"].as<std::string>();
|
||||
nlohmann::json json = nlohmann::json::parse(in);
|
||||
std::string ha_type = json["type"].get<std::string>();
|
||||
|
||||
if (ha_type == "auth_required") {
|
||||
json.clear();
|
||||
json["type"] = "auth";
|
||||
json["access_token"] = HASS_TOKEN;
|
||||
ArduinoJson::serializeJson(json, tx);
|
||||
tx = json.dump();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -41,18 +38,22 @@ void HomeAssistant::event_handler(struct lws* wsi, enum lws_callback_reasons rea
|
||||
json["id"] = id++;
|
||||
json["type"] = "subscribe_entities";
|
||||
json["entity_ids"][0] = entity.c_str();
|
||||
ArduinoJson::serializeJson(json, tx);
|
||||
tx = json.dump();
|
||||
break;
|
||||
}
|
||||
|
||||
if (ha_type == "result") {
|
||||
if (json["success"].as<std::string>() == "false")
|
||||
if (json["success"].get<bool>() != true)
|
||||
log(LOG::ERR, "api said success was \"false\" on message id {}", id - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ha_type == "event") {
|
||||
state = json["event"]["c"][entity]["+"]["s"].as<float>();
|
||||
json = json["event"];
|
||||
if(json.contains("a"))
|
||||
state = std::stof(json["a"][entity]["s"].get<std::string>());
|
||||
if(json.contains("c"))
|
||||
state = std::stof(json["c"][entity]["+"]["s"].get<std::string>());
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user