156 lines
5.7 KiB
C++
156 lines
5.7 KiB
C++
#pragma once
|
|
#include <ctime>
|
|
#include <grpcpp/channel.h>
|
|
#include <grpcpp/client_context.h>
|
|
#include <grpcpp/support/status.h>
|
|
#include <grpcpp/grpcpp.h>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
#include "Local.Status.pb.h"
|
|
#include "Local.Status.grpc.pb.h"
|
|
#include <cstdint>
|
|
#include "easylogging++.h"
|
|
|
|
using grpc::Channel;
|
|
using grpc::ClientContext;
|
|
using grpc::Server;
|
|
using grpc::ServerBuilder;
|
|
using grpc::ServerContext;
|
|
using grpc::Status;
|
|
using Local::Status::LocalStatus;
|
|
using Local::Status::LocalDeviceReq;
|
|
using Local::Status::LocalDeviceRes;
|
|
using Local::Status::LocalMqttReq;
|
|
using Local::Status::LocalMqttRes;
|
|
using Local::Status::LocalFeedReq;
|
|
using Local::Status::LocalFeedRes;
|
|
using Local::Status::LocalHardReq;
|
|
using Local::Status::LocalHardRes;
|
|
|
|
constexpr char kDefaultServerAddress[] = "0.0.0.0:50051";
|
|
constexpr char kDefaultClientTarget[] = "127.0.0.1:50051";
|
|
|
|
class LocalStatusClient {
|
|
public:
|
|
LocalStatusClient(std::shared_ptr<Channel> channel)
|
|
: stub_(LocalStatus::NewStub(channel)){
|
|
|
|
}
|
|
|
|
bool Device(const bool status, const std::string& device_id, const std::string& soft_version,
|
|
const uint64_t timestamp, LocalDeviceRes* out) {
|
|
LocalDeviceReq request;
|
|
request.set_status(status);
|
|
request.set_device_id(device_id);
|
|
request.set_soft_version(soft_version);
|
|
request.set_heart_time(timestamp);
|
|
|
|
LocalDeviceRes response;
|
|
ClientContext context;
|
|
LOG(INFO) << "-> Device req";
|
|
Status st = stub_->Device(&context, request, &response);
|
|
if (st.ok()) {
|
|
LOG(INFO) << "status:" << status << ", device_id:" << device_id
|
|
<< ", soft_version:" << soft_version << ", heart_time:" << timestamp
|
|
<< ", result_code:" << response.result_code();
|
|
if (out != nullptr) {
|
|
*out = response;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
LOG(WARNING) << "Device rpc failed: status:" << status << ", device_id:" << device_id
|
|
<< ", soft_version:" << soft_version << ", heart_time:" << timestamp;
|
|
return false;
|
|
}
|
|
|
|
bool Mqtt(const uint32_t status, uint32_t delay, uint64_t last_up, uint64_t last_down, LocalMqttRes* out) {
|
|
LocalMqttReq request;
|
|
request.set_status(status);
|
|
request.set_delay(delay);
|
|
request.set_last_up(last_up);
|
|
request.set_last_down(last_down);
|
|
|
|
LocalMqttRes response;
|
|
ClientContext context;
|
|
LOG(INFO) << "-> Mqtt req";
|
|
Status st = stub_->Mqtt(&context, request, &response);
|
|
if (st.ok()) {
|
|
LOG(INFO) << "status:" << status << ", delay:" << delay << ", last_up:" << last_up
|
|
<< ", last_down:" << last_down << ", result_code:" << response.result_code();
|
|
if (out != nullptr) {
|
|
*out = response;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
LOG(WARNING) << "Mqtt rpc failed: status:" << status << ", delay:" << delay
|
|
<< ", last_up:" << last_up << ", last_down:" << last_down;
|
|
return false;
|
|
}
|
|
|
|
bool Feed(const uint32_t feed_count, const uint32_t feed_weight, const std::string& last_feed_time,
|
|
const std::string& last_feed_weight, LocalFeedRes* out) {
|
|
LocalFeedReq request;
|
|
request.set_feed_count(feed_count);
|
|
request.set_feed_weight(feed_weight);
|
|
request.set_last_feed_time(last_feed_time);
|
|
request.set_last_feed_weight(last_feed_weight);
|
|
|
|
LocalFeedRes response;
|
|
ClientContext context;
|
|
LOG(INFO) << "-> Feed req";
|
|
Status st = stub_->Feed(&context, request, &response);
|
|
if (st.ok()) {
|
|
LOG(INFO) << "feed_count:" << feed_count << ", feed_weight:" << feed_weight
|
|
<< ", last_feed_time:" << last_feed_time
|
|
<< ", last_feed_weight:" << last_feed_weight
|
|
<< ", result_code:" << response.result_code();
|
|
if (out != nullptr) {
|
|
*out = response;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
LOG(WARNING) << "Feed rpc failed: feed_count:" << feed_count
|
|
<< ", feed_weight:" << feed_weight << ", last_feed_time:" << last_feed_time
|
|
<< ", last_feed_weight:" << last_feed_weight;
|
|
return false;
|
|
}
|
|
|
|
bool Hard(const uint32_t food_remain, const uint32_t temperature, const bool motor, const bool weight,
|
|
const bool door, const bool stuck, LocalHardRes* out) {
|
|
LocalHardReq request;
|
|
request.set_food_remain(food_remain);
|
|
request.set_temperature(temperature);
|
|
request.set_motor(motor);
|
|
request.set_weight(weight);
|
|
request.set_door(door);
|
|
request.set_stuck(stuck);
|
|
|
|
LocalHardRes response;
|
|
ClientContext context;
|
|
LOG(INFO) << "-> Hard req";
|
|
Status st = stub_->Hard(&context, request, &response);
|
|
if (st.ok()) {
|
|
LOG(INFO) << "food_remain:" << food_remain << ", temperature:" << temperature
|
|
<< ", motor:" << motor << ", weight:" << weight << ", door:" << door
|
|
<< ", stuck:" << stuck << ", action:" << response.action()
|
|
<< ", result_code:" << response.result_code();
|
|
if (out != nullptr) {
|
|
*out = response;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
LOG(WARNING) << "Hard rpc failed: food_remain:" << food_remain << ", temperature:" << temperature
|
|
<< ", motor:" << motor << ", weight:" << weight << ", door:" << door
|
|
<< ", stuck:" << stuck;
|
|
return false;
|
|
}
|
|
|
|
private:
|
|
std::unique_ptr<LocalStatus::Stub> stub_;
|
|
}; |