Пример #1
0
    def AddReading(self, data):
        """ Adds a module reading to the database

    :param data: Properly formatted (see the JSON data formats) reading dictionary
    :type data: JSON Formatted Dictionary

    :returns: int -- Status Code 701 SUCCESS
    :returns: int -- Status Code 705 FAILURE BAD M_ID
    :returns: int -- Status Code 706 FAIURE BAD M_AUTH_ID
    """

        m_id = data["module_id"]
        if not m_id in self.module_ids:
            return SC.BadMID()

        module_auth_id = data["module_auth_id"]
        if str(module_auth_id) != str(HashModuleID(m_id)):
            return SC.BadMAuth()

        temp = data["reading"]["temperature"]
        light = data["reading"]["light"]

        reading = ModuleReading(light, temp, m_id)

        self.db.session.add(reading)
        self.db.session.commit()
        return SC.Success()
Пример #2
0
    def ResetTable(self):
        """ Resets the datbase

    :returns: int -- Status Code 701 SUCCESS
    """
        self.db.drop_all()
        self.db.create_all()
        return SC.Success()
Пример #3
0
    def RemoveID(self, m_id):
        """ Removes a Module and all its data from the database

    :param m_id: The ID of the module to query
    :type m_id: int

    :returns: int -- Status Code 701 SUCCESS
    :returns: int -- Status Code 705 FAILURE BAD M_ID
    """
        if not m_id in self.module_ids:
            return SC.BadMID()

        for r in self.GetReadingsForModule(m_id):
            self.db.session.delete(r)

        self.db.session.commit()
        self.module_ids.remove(m_id)
        return SC.Success()
Пример #4
0
    def DropOldData(self, hours):
        """ Drops data older than hours from the database

    :param hours: The age in hours for which data should be dumped
    :type hours: float

    :returns: int -- Status Code 702 SUCCESS

    """
        time_stamp = MakeTimeStamp()
        age = (60 * 60 * hours)

        old_time_stamp = time_stamp - age
        old_readings = ModuleReading.query.filter(
            ModuleReading.time_stamp < old_time_stamp).all()

        for r in old_readings:
            self.db.session.delete(r)

        print('Deleted %i entries' % len(old_readings))

        self.db.session.commit()
        return SC.Success()