sndlock/kirdy.cpp

37 lines
974 B
C++
Raw Normal View History

2025-01-19 18:56:04 +08:00
#include <iostream>
2025-01-19 18:22:51 +08:00
#include <asio.hpp>
2025-01-19 18:56:04 +08:00
#include <json/json.h>
2025-01-19 18:22:51 +08:00
#include "kirdy.hpp"
Kirdy::Kirdy(asio::string_view host, asio::string_view service)
{
2025-01-19 19:18:13 +08:00
socket.connect(host, service);
2025-01-19 18:22:51 +08:00
}
2025-01-19 18:56:04 +08:00
Json::Value Kirdy::command(Json::Value &request)
2025-01-19 18:22:51 +08:00
{
2025-01-19 18:56:04 +08:00
Json::StreamWriterBuilder builder_w;
std::string request_str = Json::writeString(builder_w, request);
2025-01-19 19:18:13 +08:00
socket << request_str;
2025-01-19 18:56:04 +08:00
std::string reply_str;
2025-01-19 19:18:13 +08:00
std::getline(socket, reply_str);
2025-01-19 18:56:04 +08:00
Json::CharReaderBuilder builder_r;
const std::unique_ptr<Json::CharReader> reader(builder_r.newCharReader());
Json::Value reply;
JSONCPP_STRING err;
2025-01-19 19:18:13 +08:00
if(!reader->parse(reply_str.data(), reply_str.data() + reply_str.size(), &reply, &err))
2025-01-19 18:56:04 +08:00
std::cerr << "JSON parse error: " << err << std::endl;
return reply;
2025-01-19 18:22:51 +08:00
}
float Kirdy::get_laser_temp()
{
2025-01-19 18:56:04 +08:00
Json::Value json;
2025-01-19 18:22:51 +08:00
json["device_cmd"] = "GetStatusReport";
auto reply = command(json);
2025-01-19 18:56:04 +08:00
return reply["thermostat"]["temperature"].asFloat();
2025-01-19 18:22:51 +08:00
}