async def update_object_parameters_cb( req: srpc.s.UpdateObjectParameters.Request, ui: WsClient) -> None: assert glob.SCENE can_modify_scene() obj = glob.SCENE.object(req.args.id) if obj.type not in glob.OBJECT_TYPES: raise Arcor2Exception("Unknown object type.") obj_type = glob.OBJECT_TYPES[obj.type] check_object_parameters(obj_type, req.args.parameters) if req.dry_run: return None obj.parameters = req.args.parameters glob.SCENE.update_modified() evt = sevts.s.SceneObjectChanged(obj) evt.change_type = Event.Type.UPDATE asyncio.ensure_future(notif.broadcast_event(evt)) return None
async def remove_from_scene_cb(req: srpc.s.RemoveFromScene.Request, ui: WsClient) -> None: assert glob.SCENE can_modify_scene() if not req.args.force and { proj.name async for proj in projects_using_object(glob.SCENE.id, req.args.id) }: raise Arcor2Exception( "Can't remove object that is used in project(s).") if req.dry_run: return None if req.args.id not in glob.SCENE.object_ids: raise Arcor2Exception("Unknown id.") obj = glob.SCENE.object(req.args.id) glob.SCENE.delete_object(req.args.id) if req.args.id in glob.OBJECTS_WITH_UPDATED_POSE: glob.OBJECTS_WITH_UPDATED_POSE.remove(req.args.id) evt = sevts.s.SceneObjectChanged(obj) evt.change_type = Event.Type.REMOVE asyncio.ensure_future(notif.broadcast_event(evt)) # TODO this should be done after scene is saved asyncio.ensure_future(remove_object_references_from_projects(req.args.id)) return None
async def update_object_parameters_cb( req: srpc.s.UpdateObjectParameters.Request, ui: WsClient) -> None: scene = glob.LOCK.scene_or_exception(ensure_project_closed=True) can_modify_scene() obj = scene.object(req.args.id) if obj.type not in glob.OBJECT_TYPES: raise Arcor2Exception("Unknown object type.") obj_type = glob.OBJECT_TYPES[obj.type] check_object_parameters(obj_type, req.args.parameters) await ensure_locked(req.args.id, ui) if req.dry_run: return None obj.parameters = req.args.parameters scene.update_modified() evt = sevts.s.SceneObjectChanged(obj) evt.change_type = Event.Type.UPDATE asyncio.ensure_future(notif.broadcast_event(evt)) return None
async def add_object_to_scene_cb(req: srpc.s.AddObjectToScene.Request, ui: WsClient) -> None: async with ctx_write_lock(glob.LOCK.SpecialValues.SCENE_NAME, glob.USERS.user_name(ui)): scene = glob.LOCK.scene_or_exception() if glob.LOCK.project: raise Arcor2Exception("Project has to be closed first.") can_modify_scene() obj = common.SceneObject(req.args.name, req.args.type, req.args.pose, req.args.parameters) await add_object_to_scene(scene, obj, dry_run=req.dry_run) if req.dry_run: return None scene.update_modified() evt = sevts.s.SceneObjectChanged(obj) evt.change_type = Event.Type.ADD asyncio.ensure_future(notif.broadcast_event(evt)) return None
async def close_scene_cb(req: srpc.s.CloseScene.Request, ui: WsClient) -> None: """Closes scene on the server. :param req: :return: """ async with ctx_write_lock(glob.LOCK.SpecialValues.SCENE_NAME, glob.USERS.user_name(ui), dry_run=req.dry_run): scene = glob.LOCK.scene_or_exception() if glob.LOCK.project: raise Arcor2Exception("Project has to be closed first.") if not req.args.force and scene.has_changes: raise Arcor2Exception("Scene has unsaved changes.") can_modify_scene() # can't close scene while started if await glob.LOCK.get_locked_roots_count() > 1: raise LockingException( glob.LOCK.ErrMessages.SOMETHING_LOCKED.value) if req.dry_run: return None scene_id = scene.id glob.LOCK.scene = None glob.OBJECTS_WITH_UPDATED_POSE.clear() asyncio.ensure_future(notify_scene_closed(scene_id))
async def close_project_cb(req: srpc.p.CloseProject.Request, ui: WsClient) -> None: can_modify_scene() assert glob.PROJECT if not req.args.force and glob.PROJECT.has_changes: raise Arcor2Exception("Project has unsaved changes.") if req.dry_run: return None await close_project() return None
async def update_object_pose_cb(req: srpc.s.UpdateObjectPose.Request, ui: WsClient) -> None: can_modify_scene() assert glob.SCENE obj = glob.SCENE.object(req.args.object_id) if not obj.pose: raise Arcor2Exception("Object without pose.") if req.dry_run: return asyncio.ensure_future(update_scene_object_pose(obj, req.args.pose)) return None
async def update_object_pose_cb(req: srpc.s.UpdateObjectPose.Request, ui: WsClient) -> None: scene = glob.LOCK.scene_or_exception(ensure_project_closed=True) can_modify_scene() obj = scene.object(req.args.object_id) if not obj.pose: raise Arcor2Exception("Object without pose.") await ensure_locked(req.args.object_id, ui) if req.dry_run: return asyncio.ensure_future(update_scene_object_pose(scene, obj, req.args.pose)) return None
async def add_object_to_scene_cb(req: srpc.s.AddObjectToScene.Request, ui: WsClient) -> None: assert glob.SCENE can_modify_scene() obj = common.SceneObject(req.args.name, req.args.type, req.args.pose, req.args.parameters) await add_object_to_scene(obj, dry_run=req.dry_run) if req.dry_run: return None glob.SCENE.update_modified() evt = sevts.s.SceneObjectChanged(obj) evt.change_type = Event.Type.ADD asyncio.ensure_future(notif.broadcast_event(evt)) return None
async def close_scene_cb(req: srpc.s.CloseScene.Request, ui: WsClient) -> None: """Closes scene on the server. :param req: :return: """ assert glob.SCENE if not req.args.force and glob.SCENE.has_changes(): raise Arcor2Exception("Scene has unsaved changes.") can_modify_scene() # can't close scene while started if req.dry_run: return None scene_id = glob.SCENE.id glob.SCENE = None glob.OBJECTS_WITH_UPDATED_POSE.clear() asyncio.ensure_future(notify_scene_closed(scene_id))
async def remove_from_scene_cb(req: srpc.s.RemoveFromScene.Request, ui: WsClient) -> None: scene = glob.LOCK.scene_or_exception(ensure_project_closed=True) user_name = glob.USERS.user_name(ui) to_lock = await get_unlocked_objects(req.args.id, user_name) async with ctx_write_lock(to_lock, user_name, auto_unlock=req.dry_run): can_modify_scene() if not req.args.force and { proj.name async for proj in projects_using_object(scene.id, req.args.id) }: raise Arcor2Exception( "Can't remove object that is used in project(s).") if req.dry_run: return None if req.args.id not in scene.object_ids: raise Arcor2Exception("Unknown id.") await glob.LOCK.write_unlock(req.args.id, user_name) obj = scene.object(req.args.id) scene.delete_object(req.args.id) if req.args.id in glob.OBJECTS_WITH_UPDATED_POSE: glob.OBJECTS_WITH_UPDATED_POSE.remove(req.args.id) evt = sevts.s.SceneObjectChanged(obj) evt.change_type = Event.Type.REMOVE asyncio.ensure_future(notif.broadcast_event(evt)) # TODO this should be done after scene is saved asyncio.ensure_future( remove_object_references_from_projects(req.args.id)) return None
async def update_object_model_cb(req: srpc.o.UpdateObjectModel.Request, ui: WsClient) -> None: can_modify_scene() glob.LOCK.scene_or_exception(True) # only allow while editing scene obj_data = glob.OBJECT_TYPES[req.args.object_type_id] if not obj_data.type_def: raise Arcor2Exception("ObjectType disabled.") if not issubclass(obj_data.type_def, CollisionObject): raise Arcor2Exception("Not a CollisionObject.") assert obj_data.meta.object_model assert obj_data.ast if req.args.object_model == obj_data.meta.object_model: raise Arcor2Exception("No change requested.") await ensure_write_locked(req.args.object_type_id, glob.USERS.user_name(ui)) if req.dry_run: return await update_object_model(obj_data.meta, req.args.object_model) obj_data.meta.object_model = req.args.object_model ot = obj_data.meta.to_object_type() ot.source = tree_to_str(obj_data.ast) obj_data.meta.modified = await storage.update_object_type(ot) evt = sevts.o.ChangedObjectTypes([obj_data.meta]) evt.change_type = events.Event.Type.UPDATE asyncio.ensure_future(notif.broadcast_event(evt))