def update_driver_list(self, rid):
        service = DCModelService(get_db_conn())
        clients = self.server_socket.get_room_clients(rid)
        dids = []
        for item in clients:
            if item.driver and item.driver['did'] not in dids:
                dids.append(item.driver['did'])

        drivers = []

        for did in dids:
            driver = service.SearchDriverWithDid(did)
            if driver is not None:
                del driver['username']
                del driver['password']
                del driver['created_at']
                del driver['avatar']
                if driver['badge'] is None:
                    driver['badge'] = "1"
                drivers.append(driver)
        send_json = {
            'type': 'sys',
            'detail': 'driver list',
            'rid': rid,
            'drivers': drivers
        }
        self.server_socket.send_clients(
            min_json_dumps_to_bytes(send_json) + b'\n', clients)
 def handle_detail_room_list(self, msg):
     self.log_request('room list')
     service = DCModelService(get_db_conn())
     rooms = service.ListRooms()
     for i in rooms:
         i['created_at'] = i['created_at'].strftime("%Y-%m-%d %H:%M:%S %z")
     send_json = {'type': 'sys', 'detail': 'room list', 'rooms': rooms}
     self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
 def handle_detail_sign_up(self, msg):
     self.log_request('sign up')
     service = DCModelService(get_db_conn())
     driver = msg['driver']
     res = service.CreateDriver(driver['username'], driver['password'],
                                driver['name'])
     send_json = {
         "type": 'sys',
         'detail': 'sign up',
         'status': res['status'],
         'msg': 'Create Success' if res['status'] else 'Create Failed'
     }
     if res['status']:
         send_json['did'] = res['did']
     self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
 def handle_detail_badge(self, msg):
     send_json = msg
     service = DCModelService(get_db_conn())
     if msg['updown'] == 'down':
         send_json['status'] = True
         badges = service.GetBadge(msg['did'])
         badge = badges[0] if len(badges) > 0 else None
         if badge == None:
             send_json['status'] = False
             self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
         else:
             with open(config.FILE_DIR + badge, 'rb') as f:
                 data_bytes = f.read()
             send_json['length'] = len(data_bytes)
             self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
             self.sendall(data_bytes)
             self.sendall(b'\n')
 def handle_detail_driver_avatar(self, msg, data_bytes=None):
     send_json = msg
     service = DCModelService(get_db_conn())
     if msg['updown'] == 'up':
         # 验证身份
         if msg['driver']['did'] != self.driver['did']:
             send_json['status'] = False
             self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
             return
         name = gen_filename(data_bytes[0:20] +
                             str(time.clock()).encode('utf-8'))
         if name is None:
             name = 'driver_default.png'
         path = config.FILE_DIR + name
         # 写文件
         try:
             with open(path, 'wb') as f:
                 f.write(data_bytes)
         except Exception as e:
             logging.warn(str(e))
             send_json['status'] = False
             self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
             return
         service.SetAvatar(msg['driver']['did'], name)
     elif msg['updown'] == 'down':
         logging.info("dealing with down")
         send_json['status'] = True
         try:
             name = service.GetAvatar(msg['driver']['did'])
             if not name:
                 name = 'driver_default.png'
             path = config.FILE_DIR + name
             with open(path, 'rb') as f:
                 data_bytes = f.read()
             send_json['length'] = len(data_bytes)
             self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
             self.sendall(data_bytes)
             self.sendall(b'\n')
         except Exception as e:
             logging.warn(str(e))
             traceback.print_exc()
             send_json['status'] = False
             self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
             return
 def handle_detail_room_avatar(self, msg, data_bytes=None):
     send_json = msg
     service = DCModelService(get_db_conn())
     if msg['updown'] == 'down':
         send_json['status'] = True
         try:
             name = service.GetRoomAvatar(msg['room']['rid'])
             if name is None:
                 name = 'room_default.png'
             path = config.FILE_DIR + name
             with open(path, 'rb') as f:
                 data_bytes = f.read()
             send_json['length'] = len(data_bytes)
             self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
             self.sendall(data_bytes)
             self.sendall(b'\n')
         except Exception as e:
             send_json['status'] = False
             self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
             return
 def handle_detail_sign_in(self, msg):
     self.log_request('sign in')
     send_json = {
         'type': 'sys',
         'detail': 'sign in',
         'status': True,
         'msg': ''
     }
     service = DCModelService(get_db_conn())
     req_driver = msg['driver']
     logging.info(req_driver)
     driver = service.SearchDriverWithUsername(req_driver['username'])
     if driver != None and driver['password'] == hashlib.sha256(
             req_driver['password'].encode('utf-8') +
             config.DRIVER_SALT).hexdigest():
         self.driver = {
             'username': driver['username'],
             'did': driver['did']
         }
         # 登录成功
         send_json['driver'] = {
             'did': driver['did'],
             'name': driver['name'],
             'badge': driver['badge'],
             'created_at':
             driver['created_at'].strftime("%Y-%m-%d %H:%M:%S %z"),
             'avatar': driver['avatar']
         }
         send_json['msg'] = 'Sign in Success'
         send_json['status'] = True
         self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')
     else:
         # 登录失败
         send_json['status'] = False
         send_json['msg'] = 'Sign in Fail'
         self.sendall(min_json_dumps_to_bytes(send_json) + b'\n')