#include #include "freescaninterop.h" #include "workflowgraph.h" #include "jointpointdata.h" #include static void check(bool value,const QString &message){if(!value)throw std::runtime_error(message.toUtf8().constData());} static QJsonObject task(const QString &id,const QString &method) { QJsonObject request{{"device_id","armA"}}; if(method=="moveJ")request={{"header",QJsonObject{{"device_id","armA"}}},{"target",QJsonObject{{"position",QJsonArray{0.1,0.2}}}}, {"options",QJsonObject{{"velocity",.2},{"acceleration",.3},{"asynchronous",false}}}}; return {{"id",id},{"kind","task"},{"name",id},{"deviceType",QStringLiteral("机械臂")},{"deviceId","armA"}, {"action",QStringLiteral("接口 · ")+method},{"waitMs",10},{"waitUnit","ms"}, {"parameters",QJsonObject{{"service","cmvr.api.ArmService"},{"method",method},{"request",request},{"timeoutMs",120000}}}}; } int main(int argc,char **argv) { QApplication app(argc,argv);try { const auto move=task("move","moveJ"); QJsonObject doc{{"groups",QJsonArray{QJsonObject{{"id","flow"},{"name","互导"}, {"nodes",QJsonArray{task("enable","torqueOn"),QJsonObject{{"id","loop"},{"name","循环"},{"kind","loop"},{"count",2},{"waitMs",5},{"children",QJsonArray{move}}}}}}}}}; QString error;QJsonObject graph,back; check(FreeScanInterop::exportGraph(doc,0,graph,error),error); check(FreeScanInterop::importGraph(graph,back,error),error); const auto imported=back["groups"].toArray().first().toObject()["nodes"].toArray(); check(imported.size()==2,"two top-level nodes"); check(imported[1].toObject()["count"]==2&&imported[1].toObject()["waitMs"]==5,"loop and final wait preserved"); check(imported[1].toObject()["children"].toArray().first().toObject()["waitMs"]==10,"per-iteration wait preserved"); check(WorkflowGraph::order(graph,error).value("")==QStringList({"enable","loop"}),"edge order"); QDir().mkpath("build/tests/interop-fixture");QFile fixture("build/tests/interop-fixture/workflow.json"); check(fixture.open(QIODevice::WriteOnly),"write fixture");fixture.write(QJsonDocument(graph).toJson());fixture.close(); auto disconnected=graph;auto edges=disconnected["edges"].toArray();edges.removeFirst();disconnected["edges"]=edges; check(!FreeScanInterop::importGraph(disconnected,back,error),"disconnected run rejected");error.clear(); check(FreeScanInterop::importGraph(disconnected,back,error,false,true),"disconnected draft retained"); auto merged=WorkflowGraph::merge(graph,disconnected); check(merged["edges"].toArray().size()==edges.size(),"saving does not recreate deleted edges"); WorkflowGraph::position(graph,"move",QPointF(2000,700)); check(WorkflowGraph::node(WorkflowGraph::merge(merged,graph),"move")["x"]==2000,"free position preserved"); RecordedJointPoint point;point.name="双臂点位"; JointPointTarget a;a.deviceId="armA";a.jointNames=QStringList{"J1","J2"};a.positions={.1,.2}; auto b=a;b.deviceId="armB";b.positions={.3,.4};point.arms={a,b}; QJsonArray native{QJsonObject{{"id","point"},{"type","single"},{"nodeName","双臂"},{"pointName",point.name},{"intervalSeconds",.1},{"order",0},{"x",40},{"y",40}}}; QJsonObject group{{"name","点位组"},{"points",QJsonArray{point.toJson()}},{"flows",QJsonArray{QJsonObject{{"name","流程"},{"flow",native},{"moveJVelocity",.1},{"moveJAcceleration",.2}}}}}; QJsonObject pointGraph,pointBack; check(FreeScanInterop::exportPoints(group,0,pointGraph,error),error); check(FreeScanInterop::importPoints(pointGraph,pointBack,error),error); check(pointBack["points"].toArray().first().toObject()["arms"].toArray().size()==2,"parallel targets retained"); check(!FreeScanInterop::importGraph(pointGraph,back,error),"combined point cannot degrade to single task"); QFile batch("build/tests/interop-fixture/point.json");check(batch.open(QIODevice::WriteOnly),"write batch fixture");batch.write(QJsonDocument(pointGraph).toJson()); auto leading=graph;QString start; for(const auto &v:leading.value("nodes").toArray())if(v.toObject().value("type")=="start")start=v.toObject().value("id").toString(); WorkflowGraph::setWait(leading,start,25);check(FreeScanInterop::importGraph(leading,back,error),error); check(WorkflowGraph::leadingWait(leading,QString())==25,"initial delay retained"); RecordedJointPoint single;single.name="单臂";single.deviceId="armA";single.jointNames=a.jointNames;single.positions=a.positions; auto sg=group;sg["points"]=QJsonArray{single.toJson()};auto sf=native;auto sn=sf[0].toObject();sn["pointName"]=single.name;sf[0]=sn; sg["flows"]=QJsonArray{QJsonObject{{"name","流程"},{"flow",sf},{"moveJVelocity",.1},{"moveJAcceleration",.2}}}; check(FreeScanInterop::exportPoints(sg,0,pointGraph,error),error); auto rebound=WorkflowGraph::node(pointGraph,"point"),properties=rebound["properties"].toObject();auto params=properties["nodeParams"].toArray(); for(int i=0;i