Ejemplo 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()
Ejemplo n.º 2
0
def add_network():
    from DRcode.app.libs.robot import Robot
    form = NetworkForm().validate_for_api()
    new_ssid = form.ssid.data
    new_password = form.secret.data
    try:
        # 判断需要添加的网络是不是新的网络
        bk_flag, netid = Robot().robot_if_backup(new_ssid)
        # 是新的网络则直接添加
        if bk_flag:
            Robot.robot_network_backup()
            if netid is not None:
                print('exist network----------------------')
                Robot.robot_delete_network(netid)
            else:
                print('new network----------------------')
            if Robot.robot_add_network(new_ssid, new_password):
                print('add network successfully')
                Robot.robot_delete_backup()
                return Success(
                    msg=
                    'add network successfully, please restart to take effect')
            else:
                Robot.robot_network_restore()
                print('add network failed')
                Robot.robot_delete_backup()
                return InstructFailed(msg='add network failed')
        else:
            print('This wifi cannot be modified-----------')
            return InstructFailed(msg='This wifi cannot be modified')
    except Exception as result:
        raise InstructFailed(msg='add network failed:{}'.format(result))
Ejemplo n.º 3
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))
Ejemplo n.º 4
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))
Ejemplo n.º 5
0
 def __init__(self, port):
     from DRcode.app.libs.robot import Robot
     robot = Robot()
     self.ip = robot.ip
     self.addr = (robot.ip, port)
     self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.s.bind(self.addr)
     self.s.listen(2)
Ejemplo n.º 6
0
    def __init__(self, port):
        from DRcode.app.libs.robot import Robot
        import struct
        robot = Robot()
        self.ip = robot.ip
        self.bssid = robot.bssid
        self.addr = (robot.ip, port)

        self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                               socket.IPPROTO_UDP)
        self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.s.bind(self.addr)

        ttl_bin = struct.pack('@i', 255)
        self.s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
        self.s.setsockopt(
            socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
            socket.inet_aton(MYGROUP) + socket.inet_aton(self.ip))
Ejemplo n.º 7
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()
Ejemplo n.º 8
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))
Ejemplo n.º 9
0
def udp():
    # from DRcode.app.libs.robot import Robot
    Robot().udp_connect(UDP_PORT)