83 lines
2.5 KiB
C++
83 lines
2.5 KiB
C++
#ifndef CONVEYOR_BELT_GRPC_CLIENT_H_
|
|
#define CONVEYOR_BELT_GRPC_CLIENT_H_
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
#include "conveyorBelt.grpc.pb.h"
|
|
#include "conveyorBelt.pb.h"
|
|
#include "conveyorBeltControlUnit.h"
|
|
|
|
#include <glog/logging.h>
|
|
#include <grpc/grpc.h>
|
|
#include <grpcpp/channel.h>
|
|
#include <grpcpp/client_context.h>
|
|
#include <grpcpp/create_channel.h>
|
|
#include <grpcpp/grpcpp.h>
|
|
#include <grpcpp/security/credentials.h>
|
|
|
|
using grpc::Channel;
|
|
using grpc::ClientContext;
|
|
using grpc::ClientReader;
|
|
using grpc::ClientWriter;
|
|
using grpc::Status;
|
|
|
|
class ConveyorBeltGrpcClient {
|
|
private:
|
|
std::unique_ptr<ConveyorBelt::ConveyorBeltService::Stub> stub_;
|
|
|
|
public:
|
|
int startWithSpeedAndDirection(float speed, int direction) {
|
|
DLOG(INFO) << GRAY << "start connect to belt" << EMPTY;
|
|
ConveyorBelt::SpeedInfo speedInfo;
|
|
speedInfo.set_speed(speed);
|
|
ConveyorBelt::DirectionInfo directionInfo;
|
|
directionInfo.set_direction(direction);
|
|
ConveyorBelt::Response response;
|
|
ClientContext context_direction;
|
|
grpc::Status ret = stub_->setDirection(&context_direction, directionInfo, &response);
|
|
if (!ret.ok()) {
|
|
DLOG(INFO) << GRAY << "grpc server cannot connect" << EMPTY;
|
|
return -2;
|
|
}
|
|
if (response.code() < 0) { return -1; }
|
|
ClientContext context_start;
|
|
stub_->open(&context_start, speedInfo, &response);
|
|
if (response.code() < 0) { return -1; }
|
|
DLOG(INFO) << GRAY << "finish connect to belt" << EMPTY;
|
|
return 0;
|
|
}
|
|
|
|
int close() {
|
|
DLOG(INFO) << GRAY << "start connect to belt" << EMPTY;
|
|
ConveyorBelt::DeviceID id;
|
|
id.set_conveyorbeltid(0);
|
|
ClientContext context_close;
|
|
ConveyorBelt::Response response;
|
|
stub_->close(&context_close, id, &response);
|
|
if (response.code() < 0) { return -1; }
|
|
DLOG(INFO) << GRAY << "start connect to belt" << EMPTY;
|
|
return 0;
|
|
}
|
|
|
|
int getWorkState() {
|
|
DLOG(INFO) << GRAY << "start connect to belt" << EMPTY;
|
|
ConveyorBelt::DeviceID id;
|
|
id.set_conveyorbeltid(0);
|
|
ClientContext context_get;
|
|
ConveyorBelt::Response response;
|
|
stub_->getWorkState(&context_get, id, &response);
|
|
DLOG(INFO) << GRAY << "start connect to belt" << EMPTY;
|
|
return response.code();
|
|
}
|
|
|
|
ConveyorBeltGrpcClient(std::shared_ptr<Channel> channel)
|
|
: stub_(ConveyorBelt::ConveyorBeltService::NewStub(channel)) {}
|
|
|
|
~ConveyorBeltGrpcClient() {}
|
|
};
|
|
|
|
#endif
|