示例#1
0
 def handle_received_message(self, handler, sender, header, message):
     ''' Handles received messages from client connections. '''
     
     if header == Header.MSG_A_LOGIN:
         if ClientModule.DEBUG:
             print 'Login Message received from', sender, ':', header, message
         
         try:
             username, password = message.split(':')
             session_id, admin = Authentication.instance().authenticate(username, password)
             if session_id is not None:
                 handler.authentication_succeeded(session_id, sender)
                 self.respond(handler, header, session_id + ('*' if admin else ''), sender)
             else:
                 handler.authentication_failed(sender)
         except:
             handler.authentication_failed(sender)
     
     # needs session checking
     elif handler.is_valid_session(message, sender):
         
         original_message = message
         message = handler.strip_session_prefix(message)
         
         if ClientModule.DEBUG:
             print 'Message received from', sender, ':', header, 
             print '\'' + message + '\'',
             print '| original was', '\'' + original_message + '\''
         
         if header == Header.MSG_A_KEEPALIVE:
             self.respond(handler, header, None, sender)
         
         elif header == Header.MSG_A_LIST_DEVICE_TYPES:
             rsp = ''
             for t in EntityType.all():
                 rsp = rsp + t.serialize() + ','
             if len(rsp) > 0:
                 rsp = rsp[0:-1]
             rsp = '[' + rsp + ']'
             
             self.respond(handler, header, rsp, sender)
             
         elif header == Header.MSG_A_LIST_DEVICES:
             typeid, name_pattern = None, None
             if re.match('^[0-9]+;.*$', message):
                 typeid, name_pattern = message.split(';')
                 typeid = int(typeid)
             elif re.match('^[0-9]+', message):
                 typeid = int(message)
             elif len(message) > 0:
                 name_pattern = message
             
             rsp = ''
             for e in Entity.list(typeid, name_pattern):
                 rsp = rsp + e.serialize() + ','
             if len(rsp) > 0:
                 rsp = rsp[0:-1]
             rsp = '[' + rsp + ']'
             
             self.respond(handler, header, rsp, sender)
             
         elif header == Header.MSG_A_SEND_COMMAND:
             entity_id, cmd = message.split('#')
             cmd_param = None
             if ';' in cmd:
                 cmd, cmd_param = cmd.split(';')
             
             entity = Entity.find(entity_id)
             if entity:
                 command = EntityCommand.find( int(cmd) )
                 if command:
                     entity.control(self, command, cmd_param)
                     self.respond(handler, header, None, sender)
                 else:
                     self.respond(handler, Header.MSG_A_ERROR, _('error.not.found.command') + ': ' + str(cmd), sender)
             else:
                 self.respond(handler, Header.MSG_A_ERROR, _('error.not.found.device') + ': ' + str(entity_id), sender)
         
         elif header == Header.MSG_A_LOAD_TYPE_IMAGE:
             imgname = message
             
             content = None
             
             image_path = self.__find_image_path(imgname)
             if image_path: 
                 imgfile = file(image_path)
                 try:
                     content = base64.b64encode(imgfile.read())
                 finally:
                     imgfile.close()
             
             if content:
                 self.respond(handler, header, content, sender)
             else:
                 self.respond(handler, Header.MSG_A_ERROR, _('error.load.image') + ': ' + imgname, sender)
                 
         elif header == Header.MSG_A_RENAME_DEVICE:
             eid, name = message.split(';', 1)
             
             entity = Entity.find(eid)
             if entity:
                 entity.name = name
                 entity.save()
                 self.send_state_change(entity)
             else:
                 self.respond(handler, Header.MSG_A_ERROR, _('error.not.found.device') + ': ' + imgname, sender)
         
         elif header == Header.MSG_A_COUNT_HISTORY:
             ts_from, ts_to, entity_id = message.split(';')
             
             time_from = None
             if ts_from:
                 time_from = int(ts_from) / 1000.0
             time_to = None
             if ts_to:
                 time_to = int(ts_to) / 1000.0
             eid = None if len(entity_id) == 0 else entity_id
             
             count = EntityHistory.count(time_from, time_to, eid)
             self.respond(handler, header, str(count), sender)
         
         elif header == Header.MSG_A_LIST_HISTORY:
             ts_from, ts_to, entity_id, limit, offset = message.split(';')
             
             time_from = None
             if ts_from:
                 time_from = int(ts_from) / 1000.0
             time_to = None
             if ts_to:
                 time_to = int(ts_to) / 1000.0
             eid = None if len(entity_id) == 0 else entity_id
             
             rsp = ''
             for h in EntityHistory.query(time_from, time_to, eid, int(limit), int(offset)):
                 rsp = rsp + '#' + str(h.timestamp) + ';' + str(h.entity_id) + ';' + str(h.entity_name) + ';' + str(h.action) + ';' + str(h.action_type)
             
             self.respond(handler, header, rsp, sender)
             
         elif header == Header.MSG_A_LIST_USERS:
             rsp_items = []
             for uid, username, administrator in Authentication.instance().list_users():
                 rsp_items.append(str(uid) + ('*' if administrator else '#') + str(username))
             
             self.respond(handler, header, ';'.join(rsp_items), sender)
             
         elif header == Header.MSG_A_USER_CREATE:
             username, password = message.split(';')
             if Authentication.instance().create_user(username, password):
                 self.respond(handler, Header.MSG_A_USERS_CHANGED, None, sender)
             else:
                 self.respond(handler, Header.MSG_A_ERROR, _('error.create.user'), sender)
             
         elif header == Header.MSG_A_USER_EDIT:
             uid, username, password = message.split(';')
             if Authentication.instance().edit_user(int(uid), username, password):
                 self.respond(handler, Header.MSG_A_USERS_CHANGED, None, sender)
             else:
                 self.respond(handler, Header.MSG_A_ERROR, _('error.edit.user'), sender)
         
         elif header == Header.MSG_A_USER_DELETE:
             uid = int(message)
             Authentication.instance().delete_user(uid)
             
             self.respond(handler, Header.MSG_A_USERS_CHANGED, None, sender)
     
     else:
         print 'Auth failed for (raw) message: \'' + str(message) + '\''
         handler.authentication_failed(sender)
示例#2
0
 def test_21_light_command(self):
     tl = Entity.find( 'LIGHT-1' )
     tl.control(COMMAND_ON)
     tl.control(EntityCommand.find(100), 30)
     tl.control(COMMAND_OFF)