def test_12_modify2(self):
     te = Entity.find( 'UID-1234' )
     te.state = STATE_OFF
     te.state_value = None
     te.last_checkin = time.time()
     te.save()
     self.printall()
 def test_11_modify1(self):
     te = Entity.find( 'UID-1234' )
     te.name = 'Test entity 1'
     te.state = STATE_ON
     te.state_value = '30%'
     te.assigned_id = 0x01
     te.last_checkin = time.time()
     te.save()
     self.printall()
 def test_24_power_state_plus_control(self):
     tl = Entity.find( 'POWER-0' )
     tl.control(COMMAND_ON)
     self.printall()
     print tl
     
     tl.set_state(STATE_ON)
     self.printall()
     print tl
 def receive(self, address, unique_id, flags, data):
     ''' Processes received data from a physical device. '''
     
     DeviceHandler.receive(self, address, unique_id, flags, data)
     
     entity = Entity.find(unique_id)
     if entity is None:
         print 'No device found with id:', unique_id
     else:
         if entity.state_changed(data):
             ClientModule.instance().send_state_change(entity)
 def send_message(self, unique_id, message):
     ''' Sends a message to the entity identified by "unique_id". '''
     
     entity = Entity.find(unique_id)
     if entity is None:
         print 'No device found with id:', unique_id
     else:
         if entity.entity_type.comm_type == EntityType.COMM_TYPE_RADIO:
             self.__radio_handler.send(unique_id, message)
         else:
             print 'No communication type found for:', entity.entity_type, '| entity:', entity
 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
 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)
 def test_22_light_state_changed(self):
     tl = Entity.find( 'LIGHT-1' )
     tl.set_state(STATE_OFF)
     self.printall()
     print tl
 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)
 def test_14_find(self):
     te3 = Entity.find( 'UID-1234' )
     print te3
 def test_13_non_existent(self):
     te2 = Entity.find( 'UID-xxx' )
     print te2