コード例 #1
0
 def setUpClass(cls):
     search_path = sys.argv[0].split(':')
     import_modules(search_path, 'entities')
     EntityType.register(199, 'TestEntity', Entity)
     
     # empty history
     Database.instance().write('DELETE FROM history')
コード例 #2
0
 def test_23_power(self):
     tl = Entity('POWER-0', EntityType.find(100), 'Power plug')
     tl.assigned_id = 1
     tl.last_checkin = time.time() - 3600 # one hour before
     tl.save()
     self.printall()
     print tl
コード例 #3
0
 def test_20_light_create(self):
     tl = Entity('LIGHT-1', EntityType.find(101), name='Sample light')
     tl.assigned_id = 2
     tl.state = STATE_ON
     tl.last_checkin = time.time()
     tl.save()
     self.printall()
     print tl
コード例 #4
0
 def describe(self, address, unique_id, data):
     ''' Registers a device for the given physical address. '''
     
     DeviceHandler.describe(self, address, unique_id, data)
     
     etype_id = data[0]
     etype = EntityType.find(etype_id)
     if etype:
         entity = Entity.find(unique_id)
         if entity is None:
             entity = Entity(unique_id, etype, 'Unknown device: ' + unique_id, last_checkin=time.time())
             entity.save()
             print 'Device registered:', entity
             ClientModule.instance().send_state_change(entity)
         else:
             entity.last_checkin = time.time()
             entity.save()
             print 'Device found:', entity
             ClientModule.instance().send_state_change(entity)
     else:
         print 'Entity type not found:', etype_id
コード例 #5
0
 def __init__(self, unique_id, entity_type=EntityType.find(100), name='Unnamed entity', state=STATE_UNKNOWN, state_value=None, last_checkin=0):
     Entity.__init__(self, unique_id, entity_type, name=name, state=state, state_value=state_value, last_checkin=last_checkin)
コード例 #6
0
        
        state = state_message[0]
        if state == 0x00:
            if 0 != self.state_value:
                self.set_state(STATE_OFF, 0)
                return True
        elif state == 0x01:
            if 1 != self.state_value:
                self.set_state(STATE_ON, 1)
                return True
        
        return False

    def control(self, controller, command, value=None):
        if command.id == COMMAND_ON.id:
            controller.send_message(self.unique_id, [ chr(0x00), chr(0x01) ])
            self.log_command('Turning the power on')
            return
        elif command.id == COMMAND_OFF.id:
            controller.send_message(self.unique_id, [ chr(0x00), chr(0x00) ])
            self.log_command('Turning the power off')
            return  
        
        Entity.control(self, command, value=value)
        
    def describe_state(self):
        return str(self.state)

# register type
EntityType.register(100, 'Power', GenericPower, [COMMAND_ON, COMMAND_OFF], '#99CC00', 'power.png')
コード例 #7
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)
コード例 #8
0
 def test_10_Create(self):
     te = Entity( 'UID-1234', EntityType.find(199) )
     te.save()
     self.printall()
コード例 #9
0
        
        return False
    
    def control(self, controller, command, value=None):
        if command.id == COMMAND_LIGHT_LEVEL.id:
            if value is not None:
                msg = [ chr(0x00), chr(0x02), chr(int(round((int(value) * 255) / 100))) ]
                controller.send_message(self.unique_id, msg)
                
                self.log_command('Setting light level to ' + str(value))
                return
        elif command.id == COMMAND_ON.id:
            controller.send_message(self.unique_id, [ chr(0x00), chr(0x01) ])
            self.log_command('Turning the light on')
            return
        elif command.id == COMMAND_OFF.id:
            controller.send_message(self.unique_id, [ chr(0x00), chr(0x00) ])
            self.log_command('Turning the light off')
            return  
        
        Entity.control(self, command, value=value)
    
    def describe_state(self):
        if 0 < self.state_value < 100:
            return str(self.state) + ' (' + str(self.state_value) + '%)'
        else:
            return str(self.state)

# register type
EntityType.register(101, 'Light', GenericLight, [COMMAND_ON, COMMAND_OFF, COMMAND_LIGHT_LEVEL], '#CCCC00', 'light.png')