sndlock/kirdy.cpp

37 lines
974 B
C++

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