sndlock/kirdy.cpp

46 lines
1.2 KiB
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 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" << std::endl;
}