admin-dashboard-backend/groupTasks/libs/robots/rex.py

81 lines
2.1 KiB
Python

import requests
import uuid
class Rex:
"""
This class represents a rex robot.
"""
def __init__(self, robotName, jobName, address='http://localhost:50055'):
self.robotName = robotName
self.jobId = uuid.uuid4().__str__()
self.jobName = jobName
self.address = address
self.x = 0
self.y = 0
self.z = 0
self.connectedModule = 0
def _request_data(self):
return {'robotName': self.robotName, 'jobId': self.jobId, 'jobName': self.jobName}
def _post_request(self, json):
obj = {}
obj = self._request_data()
obj['task'] = json
res = requests.post(self.address + '/v1/control/1', json=obj)
print("status code", res.status_code)
def move_to_x(self, x):
"""
Move the robot to the new x position.
"""
self.x = x
self._post_request({'x': self.x})
def move_to_y(self, y):
"""
Move the robot to the new y position.
"""
self.y = y
def move_to_z(self, z):
"""
Move the robot to the new z position.
"""
self.z = z
def move_to_xyz(self, x, y, z):
"""
Move the robot to the new position.
"""
self.x = x
self.y = y
self.z = z
print('Robot: ' + self.robotName + ' moved to (' + str(self.x) + ', ' + str(self.y) + ')')
self._post_request({'x': self.x, 'y': self.y, 'z': self.z})
def change_connected_module(self, module):
"""
Change the connected module.
"""
print('Robot: ' + self.robotName + ' changed connected module to ' + str(module))
def finish(self):
"""
Finish the robot.
This will update the robot status to free.
"""
print('Robot finished')
# TODO: request to the server to finish the robot and update the robot status to free
res = requests.post(self.address + '/v1/control/1/finish', json={'robotName': self.robotName, 'jobId': self.jobId})
print("status code", res.status_code)