-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
350 lines (303 loc) · 13.3 KB
/
main.py
File metadata and controls
350 lines (303 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import json
import os
from logging.config import dictConfig
from dotenv import load_dotenv
from flask import Flask, request
from flask_cors import CORS, cross_origin
from marshmallow import ValidationError
from waitress import serve
from logger import FlaskLogger, ColorFormatter
from schemas import PartialGripperRequestSchema, SetConfigRequestSchema, MoveJRequestSchema, \
MoveLRequestSchema, MoveLSRequestSchema, MoveRequestSchema
from urx_service import DefaultUrxEService, MockUrxEService
from utils import ApiResponse, validate_json_structure
load_dotenv()
dictConfig({
'version': 1,
'formatters': {'color': {
'()': ColorFormatter
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'color'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
app = Flask("Flask Server")
logger = FlaskLogger(app, "Flask Server")
BOT_NAME = os.getenv("BOT_NAME")
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
try:
if os.getenv("ENVIRONMENT") == "dev":
urx_service = MockUrxEService()
else:
urx_service = DefaultUrxEService(logger=logger)
except Exception as e:
logger.error(f"Failed to initialize UrxEService: {e}")
exit(1)
@app.route("/")
@cross_origin()
def root_path():
return json.loads('{"status": "ok"}'), 200
@app.route('/health', methods=['GET'])
@cross_origin()
def health():
return json.loads('{"status": "ok"}'), 200
@app.route(f'/{BOT_NAME}/health-connection', methods=['GET'])
@cross_origin()
def health_check():
status = urx_service.get_connection_status()
if status != 0:
return ApiResponse(500, {"status": f"Error: {status}"}).to_json()
return ApiResponse(200, {"status": "ok"}).to_json()
@app.route(f'/{BOT_NAME}/gripper/partial', methods=['POST'])
@cross_origin()
def partial_gripper():
try:
validate_json_structure(request)
data = PartialGripperRequestSchema().load(request.json)
logger.info(f'Entered POST /{BOT_NAME}/gripper/partial')
amount = data['amount']
urx_service.partial_gripper(amount=amount)
return ApiResponse(200, {"status": f"Gripper partially moved to {amount}"}).to_json()
except ValidationError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": f"Error: {e.messages}"}).to_json()
except AttributeError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": str(e)}).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/gripper/open', methods=['POST'])
@cross_origin()
def open_gripper():
try:
logger.info(f'Entered POST /{BOT_NAME}/gripper/open')
urx_service.open_gripper()
return ApiResponse(200, {"status": "Gripper fully open"}).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/gripper/close', methods=['POST'])
@cross_origin()
def close_gripper():
try:
logger.info(f'Entered POST /{BOT_NAME}/gripper/close')
urx_service.close_gripper()
return ApiResponse(200, {"status": "Gripper fully closed"}).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/movej', methods=['POST'])
@cross_origin()
def movej():
try:
validate_json_structure(request)
data = MoveJRequestSchema().load(request.json)
logger.info(f'Entered POST /{BOT_NAME}/movej')
joint_positions = data['joint_positions']
acceleration = data.get('acceleration', None)
velocity = data.get('velocity', None)
pose_object = data.get('pose_object', True)
relative = data.get('relative', False)
moved_to = urx_service.movej(joint_positions, acceleration, velocity, pose_object, relative)
return ApiResponse(200, {"status": moved_to}).to_json()
except ValidationError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": f"Error: {e.messages}"}).to_json()
except AttributeError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": str(e)}).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/movel', methods=['POST'])
@cross_origin()
def movel():
try:
validate_json_structure(request)
data = MoveLRequestSchema().load(request.json)
logger.info(f'Entered POST /{BOT_NAME}/movel')
coordinates_and_angles = data['coordinates_and_angles']
acceleration = data.get('acceleration', None)
velocity = data.get('velocity', None)
pose_object = data.get('pose_object', True)
relative = data.get('relative', False)
moved_to = urx_service.movel(coordinates_and_angles, acceleration, velocity, pose_object, relative)
return ApiResponse(200, {"status": moved_to}).to_json()
except ValidationError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": f"Error: {e.messages}"}).to_json()
except AttributeError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": str(e)}).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/movels', methods=['POST'])
@cross_origin()
def movels():
try:
validate_json_structure(request)
data = MoveLSRequestSchema().load(request.json)
logger.info(f'Entered POST /{BOT_NAME}/movels')
coordinates_list = data['coordinates_list']
acceleration = data.get('acceleration', None)
velocity = data.get('velocity', None)
moved_to = urx_service.movels(coordinates_list, acceleration, velocity)
return ApiResponse(200, {"status": moved_to}).to_json()
except ValidationError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": f"Error: {e.messages}"}).to_json()
except AttributeError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": str(e)}).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/move', methods=["POST"])
@cross_origin()
def move():
try:
validate_json_structure(request)
data = MoveRequestSchema().load(request.json)
logger.info(f'Entered POST /{BOT_NAME}/move')
direction = data["direction"]
distance = data.get("distance", None)
acceleration = data.get("acceleration", None)
velocity = data.get("velocity", None)
moved_to = getattr(urx_service, direction)(distance, acceleration, velocity)
return ApiResponse(200, {"status": moved_to}).to_json()
except ValidationError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": f"Error: {e.messages}"}).to_json()
except AttributeError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": str(e)}).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/config', methods=['GET'])
@cross_origin()
def get_config():
try:
logger.info(f'Entered GET /{BOT_NAME}/config')
velocity = urx_service.get_velocity()
acceleration = urx_service.get_acceleration()
wait_timeout_limit = urx_service.get_wait_timeout_limit()
program_running_timeout_limit = urx_service.get_program_running_timeout_limit()
amount_movement = urx_service.get_amount_movement()
amount_rotation = urx_service.get_amount_rotation()
return ApiResponse(200,
{
"velocity": velocity,
"acceleration": acceleration,
"wait_timeout_limit": wait_timeout_limit,
"program_running_timeout_limit": program_running_timeout_limit,
"amount_movement": amount_movement,
"amount_rotation": amount_rotation
}
).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/config', methods=['POST'])
@cross_origin()
def set_config():
try:
validate_json_structure(request)
data = SetConfigRequestSchema().load(request.json)
logger.info(f'Entered POST /{BOT_NAME}/config')
velocity = data.get('velocity', None)
acceleration = data.get('acceleration', None)
wait_timeout_limit = data.get('wait_timeout_limit', None)
program_running_timeout_limit = data.get('program_running_timeout_limit', None)
amount_movement = data.get('amount_movement', None)
amount_rotation = data.get('amount_rotation', None)
response = {}
if velocity is not None:
old_velocity = urx_service.set_velocity(velocity)
response["old_velocity"] = old_velocity
response["new_velocity"] = velocity
if acceleration is not None:
old_acceleration = urx_service.set_acceleration(acceleration)
response["old_acceleration"] = old_acceleration
response["new_acceleration"] = acceleration
if wait_timeout_limit is not None:
old_wait_timeout_limit = urx_service.set_wait_timeout_limit(wait_timeout_limit)
response["old_wait_timeout_limit"] = old_wait_timeout_limit
response["new_wait_timeout_limit"] = wait_timeout_limit
if program_running_timeout_limit is not None:
old_program_running_timeout_limit = urx_service.set_program_running_timeout_limit(
program_running_timeout_limit)
response["old_program_running_timeout_limit"] = old_program_running_timeout_limit
response["new_program_running_timeout_limit"] = program_running_timeout_limit
if amount_movement is not None:
old_amount_movement = urx_service.set_amount_movement(amount_movement)
response["old_amount_movement"] = old_amount_movement
response["new_amount_movement"] = amount_movement
if amount_rotation is not None:
old_amount_rotation = urx_service.set_amount_rotation(amount_rotation)
response["old_amount_rotation"] = old_amount_rotation
response["new_amount_rotation"] = amount_rotation
return ApiResponse(200, response).to_json()
except ValidationError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": f"Error: {e.messages}"}).to_json()
except AttributeError as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(400, {"status": str(e)}).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/current-pose', methods=['GET'])
@cross_origin()
def get_current_pose():
try:
logger.info(f'Entered GET /{BOT_NAME}/current-pose')
current_pose = urx_service.get_current_pose()
return ApiResponse(200,
{
"current_pose": current_pose
}
).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/current-joint-positions', methods=['GET'])
@cross_origin()
def get_current_joint_positions():
try:
logger.info(f'Entered GET /{BOT_NAME}/current-joint-positions')
current_joint_positions = urx_service.get_current_joint_positions()
return ApiResponse(200,
{
"current_joint_positions": current_joint_positions
}
).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
@app.route(f'/{BOT_NAME}/current-tool-position', methods=['GET'])
@cross_origin()
def get_current_tool_position():
try:
logger.info(f'Entered GET /{BOT_NAME}/current-tool-position')
current_tool_position = urx_service.get_current_tool_position()
return ApiResponse(200,
{
"current_tool_position": current_tool_position
}
).to_json()
except Exception as e:
logger.error(f'Error: {str(e)}')
return ApiResponse(500, {"status": f"Error: {e}"}).to_json()
if __name__ == "__main__":
logger.info(f"Flask server starting at {os.getenv('FLASK_HOST')}:{os.getenv('FLASK_PORT')}")
serve(app, host=os.getenv("FLASK_HOST"), port=os.getenv("FLASK_PORT"))