Exemplo n.º 1
0
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()
Exemplo n.º 2
0
def camera_connection(ssid, passphrase):
    # 2摄像头TCP部分:建立TCP连接,监听端口获取摄像头IP地址
    while gl.get_value('camera_connecting'):
        t2 = threading.Thread(target=camera_receiver, name='TCP')
        t2.start()
        # 1摄像头UDP部分:UDP广播SSID和密码给摄像头配网
        from DRcode.app.libs.camera import Camera_UDP
        SENDERPORT = 45725
        if Camera_UDP(SENDERPORT).send_message(ssid, passphrase):
            return True
        else:
            return False
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
    def send_message(self, ssid, passphrase):
        print('UDP send message------------------------------')
        guide_code, datum_data, data = creat_content(ssid, passphrase, self.ip,
                                                     self.bssid)
        messages = get_content(guide_code, datum_data, data)
        k = 0
        while gl.get_value('camera_connecting'):
            for i, msg in enumerate(add_contents(messages)):
                if i % 5 == 0:
                    k += 1
                MYGROUP = '224' + '.' + str(k % 100) + '.' + str(
                    k % 100) + '.' + str(k % 100)
                self.s.sendto(bytes(msg, 'utf-8'), (MYGROUP, MYPORT))

        self.s.close()
        return True
Exemplo n.º 6
0
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()
Exemplo n.º 7
0
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))
Exemplo n.º 8
0
 def get_camera_ip(self):
     print('get camera ip')
     return str(gl.get_value('camera_ip'), 'utf-8')