grpc_client/clients/press_client.py

54 lines
1.5 KiB
Python
Raw Normal View History

2025-11-13 03:01:00 +08:00
import sys
from google.protobuf import timestamp_pb2
sys.path.append("../generated")
from generated.cmvr.api import hlc_command_pb2 as pb
from clients.base_client import RobotClientBase
class TouchClient(RobotClientBase):
"""Client to send Touch commands"""
def send(self, u: int, v: int, max_force: int, device_id="hc01"):
"""
u, v: target coordinates
max_force: maximum touch force
device_id: device ID
"""
# Construct request
req = pb.Touch.Request()
req.header.device_id = device_id
ts = timestamp_pb2.Timestamp()
ts.GetCurrentTime()
req.header.timestamp.CopyFrom(ts)
req.u = u
req.v = v
req.max_force = max_force
# Call RPC
try:
resp = self.stub.touch(req, timeout=self.timeout)
success = getattr(resp.header, "success", None)
error_msg = getattr(resp.header, "error_message", "")
timestamp_sec = getattr(resp.header.timestamp, "seconds", 0)
print("Touch RPC call succeeded")
print(f"Success: {success}")
print(f"Error message: {error_msg}")
print(f"Timestamp: {timestamp_sec}")
except Exception as e:
print("Touch RPC call failed:", e)
if __name__ == "__main__":
# Initialize client
client = TouchClient()
# Send Touch command
client.send(u=12, v=13, max_force=1300, device_id="hc01")
# Close client
client.close()