38 lines
2.6 KiB
Python
38 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""HTTP -> RoboBrain domain service fixture -> sequential C++ routes -> replay."""
|
||
|
|
import json,sys,tempfile,time,threading,urllib.request
|
||
|
|
from pathlib import Path
|
||
|
|
ROOT=Path(__file__).resolve().parents[1];sys.path[:0]=[str(ROOT/'coordinator'),str(ROOT/'robobrain')]
|
||
|
|
from robot_robobrain.demo_backend import BrainDemoBackend
|
||
|
|
from robot_bt_coordinator.service import Coordinator
|
||
|
|
from robot_bt_coordinator.http_api import make_server
|
||
|
|
from robot_bt_coordinator.replay import replay_events
|
||
|
|
|
||
|
|
def run(route):
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
site=json.loads((ROOT/'config'/('sim_site_'+route.lower()+'.json')).read_text())
|
||
|
|
backend=BrainDemoBackend(ROOT/'build/bt_demo',tmp+'/sim',site)
|
||
|
|
c=Coordinator(tmp+'/tasks.db',backend,['robot_01'],site)
|
||
|
|
server=make_server(c,'127.0.0.1',0,'simulation-token');thread=threading.Thread(target=server.serve_forever,daemon=True);thread.start()
|
||
|
|
base='http://127.0.0.1:'+str(server.server_port)
|
||
|
|
def api(path,body=None):
|
||
|
|
req=urllib.request.Request(base+path,data=None if body is None else json.dumps(body).encode(),headers={'Authorization':'Bearer simulation-token','Content-Type':'application/json'})
|
||
|
|
with urllib.request.urlopen(req,timeout=3) as r:return json.load(r)
|
||
|
|
try:
|
||
|
|
request=json.loads((ROOT/'config/demo_multi_request.json').read_text());t=api('/v1/tasks',request);tid=t['task_id'];deadline=time.monotonic()+20
|
||
|
|
while time.monotonic()<deadline:
|
||
|
|
c.tick();t=api('/v1/tasks/'+tid)
|
||
|
|
if t['status'] in ('SUCCEEDED','FAILED','INTERVENTION_REQUIRED'):break
|
||
|
|
time.sleep(.01)
|
||
|
|
assert t['status']=='SUCCEEDED',t
|
||
|
|
assert t['completed_quantity']==3 and len(backend.executions)==3
|
||
|
|
assert len({x['run_id'] for x in backend.executions})==3
|
||
|
|
assert [x['context']['target_id'] for x in backend.executions]==['water','water','doll']
|
||
|
|
assert api('/v1/tasks',request)['deduplicated']
|
||
|
|
audit=replay_events(c.events(tid,limit=500));assert audit['completed_quantity']==3
|
||
|
|
assert len(list((Path(tmp)/'sim/planning_records').glob('*.json')))==1
|
||
|
|
assert api('/v1/capabilities')['quantity_max']==20
|
||
|
|
return {'route':route,'status':t['status'],'delivered':3,'independent_runs':3,'replay':audit['status'],'model':'fixture','motion':'simulated'}
|
||
|
|
finally:server.shutdown();thread.join();server.server_close();c.close()
|
||
|
|
if __name__=='__main__':print(json.dumps([run(r) for r in ('OBJECT_TABLE','SHELF_CELL')],indent=2))
|