Exemple #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()
Exemple #2
0
def get_action_list():
    try:
        from DRcode.app.libs.robot import Robot
        robot = Robot()
        return jsonify(robot.action_user_list), 200
    except Exception as result:
        print('检测出异常{}'.format(result))
        return ServerError(msg='Error{}'.format(result))
Exemple #3
0
def get_states():
    from DRcode.app.libs.robot import Robot
    try:
        states = Robot().robot_get_states()
        return jsonify(states), 200
    except Exception as result:
        print('检测出异常{}'.format(result))
        return ServerError(msg='Error{}'.format(result))
Exemple #4
0
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))
Exemple #5
0
def create_password():
    from DRcode.app.libs.password import Password
    password = Password()
    form = PasswordForm().validate_for_api()
    try:
        password.write_password(form.nickname.data, form.secret.data)
        return Success()
    except Exception as result:
        print('检测出异常{}'.format(result))
        return ServerError(msg='Error{}'.format(result))
Exemple #6
0
def get_action(action_name):
    action = {'name': action_name, 'body': ''}
    path = ACTION_PATH_USER + action_name + '.txt'
    if os.path.isfile(path):
        instruct = 'cat ' + path
        try:
            action['body'] = os.popen(instruct).read()
            return jsonify(action), 200
        except Exception as result:
            print('检测出异常{}'.format(result))
            return ServerError()
    else:
        raise NotFound()
Exemple #7
0
def get_code(code_name):
    code = {'name': code_name, 'body': ''}
    path = CODE_PATH_USER + code_name + '.py'
    if os.path.isfile(path):
        instruct = 'cat ' + path
        try:
            code['body'] = os.popen(instruct).read()
            return jsonify(code), 200
        except Exception as result:
            print('检测出异常{}'.format(result))
            return ServerError()
    else:
        raise NotFound()
Exemple #8
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()
Exemple #9
0
def get_output():
    code = {'output': ''}
    path = '/home/pi/DRrobot/output.py'
    if os.path.isfile(path):
        instruct = 'sed \'s/\\x0//g\' ' + path
        #instruct = 'cat -A ' + path + ' |sed \'s/[\^@]//g\''
        try:
            code['output'] = os.popen(instruct).read()
            cmd_ = 'cat /dev/null > ' + path
            os.popen(cmd_)
            return jsonify(code), 200
        except Exception as result:
            print('检测出异常{}'.format(result))
            return ServerError()
    else:
        raise NotFound()
Exemple #10
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()
Exemple #11
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))
Exemple #12
0
 def default(self, o):
     if hasattr(o, 'keys') and hasattr(o, '__getitem__'):
         return dict(o)
     if isinstance(o, date):
         return o.strftime('%Y-%m-%d')
     raise ServerError()