def camera_receiver(): # 监听10000端口,若连续接收到3次来自同一ip的TCP连接,则返回"OK" if TCP.receive_message(): gl.set_value('camera_connecting', False) return True else: return False
def turn_off(): from DRcode.app.libs.camera import Camera try: Camera().close_camera() gl.set_value('camera_open', False) return Success(msg='close camera successfully') except Exception as result: print('检测出异常{}'.format(result)) return ServerError(msg='Error{}'.format(result))
def receive_message(self): connetion, client_address = self.s.accept() print("Connection from: ", client_address) data = connetion.recv(1024) [connetion.send(bytes('OK', 'utf-8')) for i in range(10)] gl.set_value('camera_ip', data) print('Connected! IP: ', data) gl.set_value('camera_connecting', False) connetion.close() self.s.close() return True
def edit_action(action_name): if gl.get_value('state') == 'WAITING': gl.set_value('state', 'BUSY') form = ActionForm().validate_for_api() from DRcode.app.libs.robot import Robot robot = Robot() # 动作不存在 if action_name not in robot.action_user_list: gl.set_value('state', 'WAITING') return NotFound() # 重命名并修改动作 else: try: # 仅修改名称 if form.body.data == '': new_name = form.name.data Robot.robot_rename_action(action_name, new_name) # 仅修改内容 elif form.name.data == '': Robot.robot_delete_action(action_name) Robot.robot_add_action(action_name, form.body.data) # 都修改 else: Robot.robot_delete_action(action_name) Robot.robot_add_action(form.name.data, form.body.data) gl.set_value('state', 'WAITING') return Success(msg='rename and edit successfully') except Exception as result: print('检测出异常{}'.format(result)) gl.set_value('state', 'WAITING') return ServerError(msg='Error{}'.format(result)) else: return InstructBusy()
def create_action(): if gl.get_value('state') == 'WAITING': gl.set_value('state', 'BUSY') form = ActionForm().validate_for_api() try: from DRcode.app.libs.robot import Robot Robot.robot_add_action(form.name.data, form.body.data) gl.set_value('state', 'WAITING') return Success() except Exception as result: print('检测出异常{}'.format(result)) return ServerError(msg='Error{}'.format(result)) else: return InstructBusy()
def write_data(data=[], cn=0): # print("当前服务器状态:" + gl.get_value('state')) if gl.get_value('state') == 'PAUSE': print("正在执行暂停指令-pause,等待开始-continue命令继续执行!") while gl.get_value('state') == 'PAUSE': pass if gl.get_value('state') == 'STOP': gl.set_value('state', 'WAITING') set_mode(121, 3) # print("正在执行急停指令-stop!!!- WRITE_DATA") raise KeyboardInterrupt return False if cn == 0: if len(data) == 10: data[6] = 0 sum = 0 for i in range(8): sum += data[i + 1] data[6] = sum % 100 uart.write(data) else: if len(data) > 0: uart.write(data)
def delete_action(action_name): if gl.get_value('state') == 'WAITING': gl.set_value('state', 'BUSY') from DRcode.app.libs.robot import Robot robot = Robot() if action_name not in robot.action_user_list: gl.set_value('state', 'WAITING') return NotFound() else: try: Robot.robot_delete_action(action_name) gl.set_value('state', 'WAITING') return DeleteSuccess() except Exception as result: print('检测出异常{}'.format(result)) gl.set_value('state', 'WAITING') return ServerError(msg='Error{}'.format(result)) else: return InstructBusy()
def add_camera(): try: from DRcode.app.libs.camera import Camera camera = Camera() # 如果摄像头已经处于打开状态,即摄像头处于网络配置成功阶段,直接返回摄像头IP地址 if camera.get_camera_state(): gl.set_value('camera_open', True) print('camera already turned on') ip_str = TCP.get_camera_ip() print('camera connectiong ip', ip_str) camera_ip = {"camera_ip": ip_str} return jsonify(camera_ip), 200 # 否则打开摄像头、配网并返回IP地址 else: print('turn on the camera') camera.start_camera() if not camera.get_camera_state(): print('can not turn on the camera') return InstructFailed(msg='failed to turn on the camera') else: gl.set_value('camera_open', True) gl.set_value('camera_connecting', True) # 给摄像头配网 print('add network for camera') from DRcode.app.libs.robot import Robot robot = Robot() ssid = robot.ssid wifi_password = robot.wifi_password # 向上位机返回摄像头IP地址 if camera_connection(ssid, wifi_password): ip_str = str(gl.get_value('camera_ip'), 'utf-8') print('camera connectiong ip', ip_str) camera_ip = {"camera_ip": ip_str} return jsonify(camera_ip), 200 else: return InstructFailed(msg='failed to get camera id') except Exception as result: print('检测出异常{}'.format(result)) return ServerError(msg='Error{}'.format(result))
def create_global_var(): import DRcode.app.libs.global_var as gl gl.set_value('state', 'WAITING') gl.set_value('stop', 0) gl.set_value('start', 0) gl.set_value('camera_open', False) gl.set_value('camera_connecting', True) gl.set_value('camera_ip', '')