#include #include #include #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; } Json::Value Kirdy::command(Json::Value &request) { Json::StreamWriterBuilder builder_w; std::string request_str = Json::writeString(builder_w, request); asio::write(*socket, asio::buffer(request_str + "\n")); std::cout << request_str << std::endl; std::string reply_str; std::size_t len = asio::read_until(*socket, asio::dynamic_buffer(reply_str, 1024), '\n'); Json::CharReaderBuilder builder_r; const std::unique_ptr reader(builder_r.newCharReader()); Json::Value reply; JSONCPP_STRING err; if(!reader->parse(reply_str.c_str(), reply_str.c_str() + len, &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(); }