Exemple #1
0
    def user_login(self, email, password):
        """
        Check if the user exist and if the password is the correct one.

        Intput:   email -> User email
                  password -> User password
        Output:   success -> True if the email and password match
                  user_id -> The user identification
                  user_auth_token -> An identifier to check the user autenticity during
                    the sesion
        """
        success = False
        user_id = None
        user_auth_token = None

        db = DBBroker()
        sql = "SELECT user_id, password \
                FROM users \
                WHERE email = '%s'" % (email)
        user_data = db.select(sql)[0]
        raw_pass = str(user_data['user_id']) + email
        encrip_pass = AESCipher(password).encrypt(raw_pass)

        if encrip_pass == user_data['password']:
            success = True
            user_id = user_data['user_id']
            user_auth_token = str(uuid.uuid4())

            db = DBBroker()
            sql = "UPDATE users \
                    SET last_token = '%s' \
                    WHERE user_id = %d" % (user_auth_token, user_id)
            db.update(sql)

        return (success, user_id, user_auth_token)
    def getDevices(self):
        """
        Obtain the Raspberry Pi devices.

        Output:   devices -> List of dictionary with the obtained devices.
        """
        db = DBBroker()
        sql = "SELECT * \
        FROM devices t1 \
        LEFT OUTER JOIN( \
        	SELECT idDevice, max(DateAndTime) AS LastDate \
        	FROM data \
            GROUP BY idDevice \
        ) t2 ON t1.idDevice = t2.idDevice;"

        devices = db.select(sql)

        # Convert time to UTC - Europe/Madrid
        time_now = datetime.datetime.now().replace(
            tzinfo=self.server_tz).astimezone(self.local_tz)
        time_now = time_now.replace(tzinfo=None)

        for device in devices:
            if device['LastDate'] == None:
                device['status'] = False
            elif time_now - device['LastDate'] < datetime.timedelta(
                    minutes=device['timeInterval'] * 3):
                device['status'] = True
            else:
                device['status'] = False

        return devices
    def getData(self, devices, dates):
        """
        Obtain sensor data stored into de DB.

        Intput:   devices -> List of Raspberry Pi devices from which obtain the data
                  dates -> Initial and finish dates
        Output:   data -> List of dictionaries with the obtained data.
        """
        db = DBBroker()
        # Prepare SQL query to obtain the data
        sql = "SELECT idDevice, DateAndTime, Temperature, Humidity, Pressure, CO, LPG, VehiclesPerHour \
                FROM data \
                WHERE idDevice IN ("

        for i in range(0, len(devices)):
            sql += "'%d'" % (devices[i])
            if i != len(devices) - 1:
                sql += ", "

        sql += ") \
                AND DateAndTime BETWEEN '%s' AND '%s' \
                ORDER BY DateAndTime ASC" % (dates[0], dates[1])

        # Execute the SQL querry
        data = db.select(sql)
        if data == None:
            data = []

        return data
    def getNotifications(self):
        """
        Get all the email notifications.

        Output:   notifications -> List with all the notifications stored.
        """
        db = DBBroker()
        sql = "SELECT * FROM notifications "
        notifications = db.select(sql)
        return notifications
Exemple #5
0
    def checkUserSession(self, user_id, user_auth_token):
        """
        Verify the user session.

        Intput:   user_id -> The user identification
                  user_auth_token -> An identifier to check the user autenticity during
                    the sesion
        Output:   success -> True if the user session exists
        """
        success = False
        db = DBBroker()
        sql = "SELECT last_token, email \
                FROM users \
                WHERE user_id = %d" % (user_id)
        user_data = db.select(sql)[0]

        if user_auth_token == user_data['last_token']:
            success = True
            email = user_data['email']

        return (success, email)
    def getLastData(self, idDevice):
        """
        Obtain last data stored into de DB.

        Intput:   idDevice -> Raspberry Pi device from which obtain the data
        Output:   data -> Dictionary with the obtained data.
        """
        db = DBBroker()
        sql = "SELECT DateAndTime, Temperature, Humidity, Pressure, CO, LPG, VehiclesPerHour \
                FROM data t\
                inner join ( \
                    SELECT idDevice, max(DateAndTime) as LastDate \
                    FROM data \
                    GROUP BY idDevice \
                ) tmd on t.idDevice = tmd.idDevice and t.DateAndTime = tmd.LastDate \
                    WHERE t.idDevice = '%d'" % (idDevice)
        data = db.select(sql)[0]
        data['idDevice'] = idDevice
        if data == None:
            data = self.getDefaultData()

        return data