def queryPossible(self):
        '''Check from the database if we even have the requested number of devices to service the query.'''
        #if found return True
        #To Write a SELECT query when DB Schema finalized.
        #assuming no future queries for now
        #Untested code
        if self.queryDBObject.frequency==0:
            u=Sensor.select().where((str(self.queryObject['dataReqd'])== Sensor.SensorType)).distinct()
        else:
            u=Sensor.select().where((str(self.queryObject['dataReqd'])== Sensor.SensorType)&((Sensor.minDelay==0)|(eval(str(self.queryObject['frequency']))<1000000.0/Sensor.minDelay)) ).distinct()
        z = User.select().join(SensorUserRel).where(SensorUserRel.sensor << u).distinct()

        ''' Possible query to get all users of such sensors in u:
         z = User.select().join(SensorUserRel).where(SensorUserRel.sensor << u).distinct()
         '''
        lat1 = self.queryDBObject.Latitude
        lon1 = self.queryDBObject.Longitude
        if self.queryDBObject.Radius<0 or lat1<0 or lon1<0:
            ''' Radius to not be considered! '''
            for i in z:
                self.providerList += [i]
        else:
            R = self.queryDBObject.Radius
            for i in z:
                if self.getDistanceInKM(lon1, lat1, i.Longitude, i.Latitude) <= R:
                    self.providerList += [i]
            
        print 'Found count=' + str(len(self.providerList)) + ' for query Number: ' + self.queryNo
        
        if(len(self.providerList)>=eval(str(self.queryObject['countMin']))):
            #Query is possible; initiate messages to valid subscribers to respond with an acknowledgement.            
            return True
        else:
            return False
 def registerUser(self, msgObject, userName): #(long userID,long password, string[] sensorsPresent): #msg contains userid+capabilities 
     ''' To do:
         Parse the incoming message and store the capabilities of a particular user in the DB
     '''
     print "Inside RegisterUser"
     foundFlag = False
     try:
         u = User.get(User.username==userName)
         foundFlag = True
     except User.DoesNotExist:
         if str(msgObject['ActivityRecognition'])=='present':
             activity = True
         else:
             activity = False
         if str(msgObject['DownloadAllowed'])=='yes':
             download = True
         else:
             download = False
         if 'Location' in msgObject:
             lAr = msgObject['Location']
             location = str(lAr[0])
             latitude = str(lAr[1])
             longitude = str(lAr[2])
         else:
             location = 'Hardcoded'
             latitude = 0.0
             longitude = 0.0
         u = User(username=str(userName), RegistrationDate=datetime.datetime.now(), ActivityRecognition=activity, DownloadAllowed=download, Location=location, Latitude=latitude, Longitude=longitude)
         u.save()
     #print "User Saved"
     
     '''Update location anyways, if the key is present '''
     if 'Location' in msgObject:
         lAr = msgObject['Location']
         location = str(lAr[0])
         latitude = str(lAr[1])
         longitude = str(lAr[2])
         u.Longitude = longitude
         u.Latitude = latitude
         u.Location = location
         u.save()
             
     
     if foundFlag:
         #User already exists. Should handle it differently.
         #Means it could be a updation message. Delete all his previous Sensor Relation entries and fill new ones for now.
         print "User already existed! Deleting previous relation entries!"
         surtoD = SensorUserRel.select().where(SensorUserRel.user==u)
         for s in surtoD:
             s.delete_instance()
                 
     
     sensorQueue = []
     numSensor = eval(str(msgObject['noSensors']))
     for i in range(1, numSensor+1):
         ''' TODO: Check for fields to be empty before parsing them with eval! '''
         sName = "sensor" + str(i)
         try:
             s = Sensor.get(Sensor.SensorType==str(msgObject[sName][0]), Sensor.maxRange==eval(str(msgObject[sName][1])), Sensor.minDelay==eval(str(msgObject[sName][2])), Sensor.power==eval(str(msgObject[sName][3])), Sensor.resolution==eval(str(msgObject[sName][4])))
         except Sensor.DoesNotExist:
             s = Sensor(name="temp", SensorType=str(msgObject[sName][0]), maxRange=eval(str(msgObject[sName][1])), minDelay = eval(str(msgObject[sName][2])), power = eval(str(msgObject[sName][3])), resolution=eval(str(msgObject[sName][4])))
             s.save()
         sensorQueue += [s]
         
         
     for s in sensorQueue:
         sur = SensorUserRel(user=u, sensor=s)
         sur.save()     
             
     print "Registered" + str(userName) + " Successfully!"
     
     self.msgHandler.send_message(mto=str(self.rMessage['from']).split("/")[0], msubject="Registration Successful!", mbody="Thank you! Registration/Updation Successful!")
     
     return