sndlock/kirdy.cpp

34 lines
850 B
C++
Raw Normal View History

2025-01-19 18:22:51 +08:00
#include <asio.hpp>
#include <nlohmann/json.hpp>
#include "kirdy.hpp"
Kirdy::Kirdy(asio::string_view host, asio::string_view service)
{
asio::ip::tcp::resolver resolver(io_context);
asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(host, service);
socket = new asio::ip::tcp::socket(io_context);
asio::connect(*socket, endpoints);
}
Kirdy::~Kirdy()
{
delete socket;
}
nlohmann::json Kirdy::command(nlohmann::json &request)
{
std::string reply;
asio::write(*socket, asio::buffer(request.dump() + "\n"));
asio::read_until(*socket, asio::dynamic_buffer(reply, 1024), '\n');
return nlohmann::json::parse(reply);
}
float Kirdy::get_laser_temp()
{
nlohmann::json json;
json["device_cmd"] = "GetStatusReport";
auto reply = command(json);
return reply["thermostat"]["temperature"];
}