#include #include #include #include "kirdy.hpp" Kirdy::Kirdy(asio::string_view host, asio::string_view service) { socket.connect(host, service); if(!socket) std::cerr << "failed to connect to Kirdy at " << host << ": " << socket.error().message() << "\n"; } 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 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 << "\n"; return reply; } float Kirdy::get_laser_temp() { Json::Value request; request["device_cmd"] = "GetStatusReport"; Json::Value reply = command(request); return reply["thermostat"]["temperature"].asFloat(); } void Kirdy::set_tec_current(float amps) { Json::Value request; request["tec_set_i"] = amps; Json::Value reply = command(request); if(reply["msg_type"].asString() != "Acknowledge") std::cerr << "TEC set current failed\n"; }