Example #1
0
def handle_module_request(m_id):
    """ Module Admin Route

  """
    r_method = request.method
    m_id = int(m_id)

    if r_method == 'GET':
        readings = db_actor.GetReadingsForModule(m_id)
        if readings == None:
            return SC.BadMID()

        j_data = ReadingsToHistoryJSON(m_id, readings)
        return json.dumps(j_data)

    else:
        data = request.json
        VerifyAuthData(data)

        if r_method == 'POST':
            return json.dumps(db_actor.RegisterID(m_id))
        elif r_method == 'DELETE':
            return json.dumps(db_actor.RemoveID(m_id))
        else:
            return SC.Fauilure()
Example #2
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()
Example #3
0
def drop_old_data():
    data = request.json
    ## Do Authrorization ##
    if not VerifyAuthData(data):
        return SC.BadForm()
    if not AuthorizeAuthData(data):
        return SC.BadAuth()

    return json.dumps(db_actor.DropOldData(24))
Example #4
0
def reset_data():
    """ Table Reset Route

  """
    data = request.json
    if not VerifyAuthData(data):
        return SC.BadForm()
    if not AuthorizeAuthData(data):
        return SC.BadAuth()
    return json.dumps(db_actor.ResetTable())
Example #5
0
    def ResetTable(self):
        """ Resets the datbase

    :returns: int -- Status Code 701 SUCCESS
    """
        self.db.drop_all()
        self.db.create_all()
        return SC.Success()
Example #6
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()
Example #7
0
    def RegisterID(self, m_id):
        """ Registers a module with the database

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

    :returns: int -- A Unique (to m_id) hash id meant for pcDuino consumption
    :returns: int -- Status Code 705 FAILURE BAD M_ID

    The Unique ID is required for module with m_id to add readings after registration.
    """
        if m_id in self.module_ids:
            return SC.BadMID()

        self.module_ids.append(m_id)
        return HashModuleID(m_id)
Example #8
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()