Compare commits

...

2 Commits

Author SHA1 Message Date
f24c710af2 Kirdy::set_tec_current 2025-01-19 19:25:55 +08:00
22492da21b use asio iostream 2025-01-19 19:18:13 +08:00
3 changed files with 18 additions and 20 deletions

View File

@ -7,41 +7,39 @@
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;
socket.connect(host, service);
}
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;
socket << request_str;
std::string reply_str;
std::size_t len = asio::read_until(*socket, asio::dynamic_buffer(reply_str, 1024), '\n');
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.c_str(), reply_str.c_str() + len, &reply, &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 json;
json["device_cmd"] = "GetStatusReport";
auto reply = command(json);
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;
}

View File

@ -5,11 +5,10 @@
class Kirdy {
private:
asio::io_context io_context;
asio::ip::tcp::socket *socket;
asio::ip::tcp::iostream socket;
Json::Value command(Json::Value &request);
public:
Kirdy(asio::string_view host, asio::string_view service);
~Kirdy();
float get_laser_temp();
void set_tec_current(float amps);
};

View File

@ -260,6 +260,7 @@ static void servo_thread(int channel)
while(!shutdown_threads) {
clocker.tick();
laser_temp[channel] = kirdy.get_laser_temp();
kirdy.set_tec_current(0.01);
}
}