use asio iostream
This commit is contained in:
parent
9415402a1e
commit
22492da21b
19
kirdy.cpp
19
kirdy.cpp
@ -7,32 +7,22 @@
|
|||||||
|
|
||||||
Kirdy::Kirdy(asio::string_view host, asio::string_view service)
|
Kirdy::Kirdy(asio::string_view host, asio::string_view service)
|
||||||
{
|
{
|
||||||
asio::ip::tcp::resolver resolver(io_context);
|
socket.connect(host, service);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value Kirdy::command(Json::Value &request)
|
Json::Value Kirdy::command(Json::Value &request)
|
||||||
{
|
{
|
||||||
Json::StreamWriterBuilder builder_w;
|
Json::StreamWriterBuilder builder_w;
|
||||||
std::string request_str = Json::writeString(builder_w, request);
|
std::string request_str = Json::writeString(builder_w, request);
|
||||||
asio::write(*socket, asio::buffer(request_str + "\n"));
|
socket << request_str;
|
||||||
|
|
||||||
std::cout << request_str << std::endl;
|
|
||||||
|
|
||||||
std::string reply_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;
|
Json::CharReaderBuilder builder_r;
|
||||||
const std::unique_ptr<Json::CharReader> reader(builder_r.newCharReader());
|
const std::unique_ptr<Json::CharReader> reader(builder_r.newCharReader());
|
||||||
Json::Value reply;
|
Json::Value reply;
|
||||||
JSONCPP_STRING err;
|
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;
|
std::cerr << "JSON parse error: " << err << std::endl;
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
@ -44,4 +34,3 @@ float Kirdy::get_laser_temp()
|
|||||||
auto reply = command(json);
|
auto reply = command(json);
|
||||||
return reply["thermostat"]["temperature"].asFloat();
|
return reply["thermostat"]["temperature"].asFloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,9 @@
|
|||||||
|
|
||||||
class Kirdy {
|
class Kirdy {
|
||||||
private:
|
private:
|
||||||
asio::io_context io_context;
|
asio::ip::tcp::iostream socket;
|
||||||
asio::ip::tcp::socket *socket;
|
|
||||||
Json::Value command(Json::Value &request);
|
Json::Value command(Json::Value &request);
|
||||||
public:
|
public:
|
||||||
Kirdy(asio::string_view host, asio::string_view service);
|
Kirdy(asio::string_view host, asio::string_view service);
|
||||||
~Kirdy();
|
|
||||||
float get_laser_temp();
|
float get_laser_temp();
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user