|
| 1 | +"""Contains the points_crud skill.""" |
| 2 | + |
| 3 | +from absl import logging |
| 4 | +import grpc |
| 5 | +from intrinsic.skills.python import proto_utils |
| 6 | +from intrinsic.skills.python import skill_interface |
| 7 | +from intrinsic.util.decorators import overrides |
| 8 | +from intrinsic.util.grpc import connection |
| 9 | +from intrinsic.util.grpc import interceptor |
| 10 | +from services.points_storage import points_storage_service_pb2 as points_storage_proto |
| 11 | +from services.points_storage import points_storage_service_pb2_grpc as points_storage_grpc |
| 12 | +from skills.points_crud import points_crud_pb2 |
| 13 | + |
| 14 | + |
| 15 | +def make_grpc_stub(resource_handle): |
| 16 | + logging.info(f"Address: {resource_handle.connection_info.grpc.address}") |
| 17 | + logging.info( |
| 18 | + f"Server Instance: {resource_handle.connection_info.grpc.server_instance}" |
| 19 | + ) |
| 20 | + logging.info(f"Header: {resource_handle.connection_info.grpc.header}") |
| 21 | + |
| 22 | + # Create a gRPC channel without using TLS |
| 23 | + grpc_info = resource_handle.connection_info.grpc |
| 24 | + grpc_channel = grpc.insecure_channel(grpc_info.address) |
| 25 | + connection_params = connection.ConnectionParams( |
| 26 | + grpc_info.address, grpc_info.server_instance, grpc_info.header |
| 27 | + ) |
| 28 | + |
| 29 | + intercepted_channel = grpc.intercept_channel( |
| 30 | + grpc_channel, |
| 31 | + interceptor.HeaderAdderInterceptor(connection_params.headers), |
| 32 | + ) |
| 33 | + return points_storage_grpc.PointsStorageServiceStub(intercepted_channel) |
| 34 | + |
| 35 | + |
| 36 | +class PointsCrud(skill_interface.Skill): |
| 37 | + """Implementation of the points_crud skill.""" |
| 38 | + |
| 39 | + def store_point(self, stub, name: str, x: float, y: float, z: float): |
| 40 | + logging.info(f"Storing point ({x}, {y}, {z}) under the name {name}") |
| 41 | + request = points_storage_proto.StorePointRequest() |
| 42 | + request.name = name |
| 43 | + request.point.x = x |
| 44 | + request.point.y = y |
| 45 | + request.point.z = z |
| 46 | + stub.Store(request) |
| 47 | + logging.info(f"Point {name} has been stored") |
| 48 | + |
| 49 | + def get_point(self, stub, name: str): |
| 50 | + logging.info(f"Fetching point {name}") |
| 51 | + request = points_storage_proto.GetPointRequest() |
| 52 | + request.name = name |
| 53 | + response = stub.Get(request) |
| 54 | + pt = response.point |
| 55 | + logging.info(f"Got ({pt.x}, {pt.y}, {pt.z})") |
| 56 | + return pt |
| 57 | + |
| 58 | + def get_all_points(self, stub): |
| 59 | + logging.info("Fetching all points") |
| 60 | + request = points_storage_proto.GetAllPointsRequest() |
| 61 | + response = stub.GetAll(request) |
| 62 | + logging.info(f"Got {len(response.items)} points:") |
| 63 | + for item in response.items: |
| 64 | + pt = item.point |
| 65 | + logging.info(f"- Point {item.name}: ({pt.x}, {pt.y}, {pt.z})") |
| 66 | + |
| 67 | + def delete_point(self, stub, name: str): |
| 68 | + logging.info(f"Deleting point {name}") |
| 69 | + request = points_storage_proto.DeleteRequest() |
| 70 | + request.name = name |
| 71 | + stub.Delete(request) |
| 72 | + logging.info(f"Point {name} has been deleted") |
| 73 | + |
| 74 | + |
| 75 | + @overrides(skill_interface.Skill) |
| 76 | + def execute( |
| 77 | + self, |
| 78 | + request: skill_interface.ExecuteRequest[ |
| 79 | + points_crud_pb2.PointsCrudParams |
| 80 | + ], |
| 81 | + context: skill_interface.ExecuteContext, |
| 82 | + ) -> None: |
| 83 | + params = request.params |
| 84 | + logging.info(f"Invoking the skill with create={params.create}, read={params.read}, update={params.update}, delete={params.delete}") |
| 85 | + stub = make_grpc_stub(context.resource_handles["points_storage_service"]) |
| 86 | + |
| 87 | + if params.create: |
| 88 | + logging.info("Creating two points") |
| 89 | + self.store_point(stub, "A", 1.0, 2.0, 3.0) |
| 90 | + self.store_point(stub, "B", 10.0, 20.0, 30.0) |
| 91 | + |
| 92 | + if params.read: |
| 93 | + self.get_point(stub, "A") |
| 94 | + self.get_all_points(stub) |
| 95 | + |
| 96 | + if params.update: |
| 97 | + logging.info("Moving point A by 10 units along X axis") |
| 98 | + pt = self.get_point(stub, "A") |
| 99 | + self.store_point(stub, "A", pt.x + 10.0, pt.y, pt.z) |
| 100 | + |
| 101 | + logging.info("Fetching point A to confirm that it has been updated") |
| 102 | + self.get_point(stub, "A") |
| 103 | + |
| 104 | + if params.delete: |
| 105 | + self.delete_point(stub, "A") |
| 106 | + self.delete_point(stub, "B") |
| 107 | + |
| 108 | + logging.info("Fetching all points, expecting an empty list") |
| 109 | + self.get_all_points(stub) |
| 110 | + logging.info("Done fetching all points") |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
0 commit comments