Add independent robot connections, per-node device binding and isolated demo state. Improve canvas multi-selection, undo and parameter layouts. Remove remote configuration management and align protos with cmvr-es c8b71b2b. Include Release startup and loopback regression coverage.
49 lines
4.0 KiB
C++
49 lines
4.0 KiB
C++
// Loopback fake service only: no device manager, SDK or real device connection.
|
|
#include "../src/taskrpcrunner.h"
|
|
#include "../src/taskapi.h"
|
|
#include "cmvr/api/arm_service.grpc.pb.h"
|
|
#include <grpcpp/grpcpp.h>
|
|
#include <QCoreApplication>
|
|
#include <QElapsedTimer>
|
|
#include <QThread>
|
|
#include <QTimer>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <atomic>
|
|
#define CHECK(x) do{if(!(x))throw std::runtime_error(#x);}while(false)
|
|
struct TaskRpcTestAccess {static void configure(TaskRpcRunner& r,int port){r.m_endpoint=QString("127.0.0.1:%1").arg(port);r.m_connected=true;}};
|
|
class FakeArm final:public cmvr::api::ArmService::Service {
|
|
public:
|
|
std::atomic<bool> block{false},receivedHeader{false};std::atomic<int> capability{4},motions{0};
|
|
grpc::Status GetArmInfo(grpc::ServerContext* ctx,const cmvr::api::GetArmInfo::Request*,cmvr::api::GetArmInfo::Response* r)override{
|
|
while(block.load()&&!ctx->IsCancelled())std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
|
if(ctx->IsCancelled())return {grpc::StatusCode::CANCELLED,"fake canceled"};r->mutable_header()->set_success(true);return grpc::Status::OK;
|
|
}
|
|
grpc::Status torqueOn(grpc::ServerContext*,const cmvr::api::CommandHeader::Request* q,cmvr::api::CommandHeader::Feedback* r)override{
|
|
receivedHeader.store(q->device_id()=="fake-arm"&&q->timestamp().seconds()>0);r->set_success(true);return grpc::Status::OK;
|
|
}
|
|
grpc::Status ExecuteJsonCommand(grpc::ServerContext*,const cmvr::api::JsonDeviceCommand::Request*,cmvr::api::JsonDeviceCommand::Feedback* r)override{
|
|
r->mutable_header()->set_success(true);r->set_response_json("{\"coordinate_api_version\":"+std::to_string(capability.load())+",\"movel_target_mode\":\"absolute_selected_frame\"}");return grpc::Status::OK;
|
|
}
|
|
grpc::Status moveL(grpc::ServerContext*,const cmvr::api::MoveL::Request*,cmvr::api::MoveL::Response* r)override{motions++;r->mutable_header()->set_success(true);return grpc::Status::OK;}
|
|
};
|
|
int main(int argc,char** argv){QCoreApplication app(argc,argv);try{
|
|
FakeArm fake;grpc::ServerBuilder builder;int port=0;builder.AddListeningPort("127.0.0.1:0",grpc::InsecureServerCredentials(),&port);builder.RegisterService(&fake);auto server=builder.BuildAndStart();CHECK(server&&port);
|
|
TaskRpcRunner runner(nullptr);TaskRpcTestAccess::configure(runner,port);
|
|
auto run=[&](QString method,QJsonObject request={},bool cancel=false){
|
|
bool finished=false,ok=false;QString response;
|
|
const auto ex=TaskApi::execution(QString::fromUtf8("机械臂"),QString::fromUtf8("接口 · ")+method,"fake-arm",{{"request",request},{"timeoutMs",2000}});
|
|
runner.run(ex,[&](bool result,const QString& text){ok=result;response=text;finished=true;});
|
|
if(cancel)QTimer::singleShot(20,&runner,&TaskRpcRunner::cancel);
|
|
QElapsedTimer timer;timer.start();while(!finished&&timer.elapsed()<5000){app.processEvents();QThread::msleep(1);}CHECK(finished);return qMakePair(ok,response);
|
|
};
|
|
QString detail;auto info=run("GetArmInfo");CHECK(TaskApi::responseSucceeded(info.first,info.second,detail));
|
|
auto enable=run("torqueOn");CHECK(TaskApi::responseSucceeded(enable.first,enable.second,detail));CHECK(fake.receivedHeader.load());
|
|
QJsonObject custom{{"target",QJsonObject{{"x",.1}}},{"user_coordinate_name","fixture"},{"options",QJsonObject{{"velocity",.01},{"acceleration",.1}}}};
|
|
fake.capability=3;auto rejected=run("moveL",custom);CHECK(!rejected.first);CHECK(fake.motions==0);
|
|
fake.capability=4;auto moved=run("moveL",custom);CHECK(TaskApi::responseSucceeded(moved.first,moved.second,detail));CHECK(fake.motions==1);
|
|
fake.block=true;auto canceled=run("GetArmInfo",{},true);CHECK(!canceled.first);fake.block=false;
|
|
auto again=run("GetArmInfo");CHECK(TaskApi::responseSucceeded(again.first,again.second,detail));
|
|
server->Shutdown();std::cout<<"PASS: real async loopback Unary RPC, nested/direct headers, current protocol timestamps, valid response JSON, coordinate handshake rejection, fake motion dispatch, cancellation and subsequent calls; no hardware\n";
|
|
}catch(const std::exception& e){std::cerr<<"FAIL: "<<e.what()<<"\n";return 1;}return 0;}
|