sndlock/kirdy.cpp

48 lines
1.3 KiB
C++
Raw Permalink Normal View History

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-20 20:29:23 +08:00
if(!socket)
std::cerr << "failed to connect to Kirdy at " << host << ": " << socket.error().message() << "\n";
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-20 20:26:25 +08:00
std::cerr << "JSON parse error: " << err << "\n";
2025-01-19 18:56:04 +08:00
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
2025-01-21 10:58:43 +08:00
void Kirdy::set_tec_current(float milliamps)
2025-01-19 19:25:55 +08:00
{
Json::Value request;
2025-01-21 10:58:43 +08:00
request["tec_set_i"] = milliamps*0.001f;
2025-01-19 19:25:55 +08:00
Json::Value reply = command(request);
if(reply["msg_type"].asString() != "Acknowledge")
2025-01-20 20:26:25 +08:00
std::cerr << "TEC set current failed\n";
2025-01-19 19:25:55 +08:00
}