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 19:25:55 +08:00
|
|
|
Json::Value request;
|
|
|
|
request["device_cmd"] = "GetStatusReport";
|
|
|
|
Json::Value reply = command(request);
|
2025-01-19 18:56:04 +08:00
|
|
|
return reply["thermostat"]["temperature"].asFloat();
|
2025-01-19 18:22:51 +08:00
|
|
|
}
|
2025-01-19 19:25:55 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|