예제 #1
0
def readTemperature():
    """This is the option 1 in Application Menu.
    Reads the temperatura and inserts it into the database
    This function deppends of `readUnity` and of `insertDataInto`
    There also are prints showing the status of the request.

    Example
    -------
        >>> readTemperature()
        Reading and inserting TEMPERATURE data into DB...
        The read temperature is 25.0ºC.
        Success! Data inserted into database.

    """
    print("Reading and inserting TEMPERATURE data into DB...")
    read_temperature = readUnity(TEMP_CHARACTER)

    if read_temperature != -1:
        print("The read temperature is " + str(read_temperature) + "ºC.")
        # columns: id_user, id_envrmt, read_value
        database.insertDataInto(table='measures',
                                id_user=USER_ID,
                                id_environment=3,
                                id_pquantity=1,
                                read_value=read_temperature)
        print("Success! Data inserted into database.\n")
    else:
        print("Failed to read temperature. Try again in 5 seconds.")
예제 #2
0
def readSoilHumidity():
    """This is the option 2 in Application Menu.
    Reads the humidity and inserts it into the database
    This function deppends of `readUnity` and of `insertDataInto`
    There also are prints showing the status of the request.

    Example
    -------
        >>> readAirHumidity()
        Reading and inserting HUMIDITY data into DB...
        The read humidity is 19% UR.
        Success! Data inserted into database.

    """
    print("Reading and inserting HUMIDITY data into DB...")
    read_humidity = readUnity(HUMIDITY_CHARACTER)
    if read_humidity != -1:
        print("The read humidity of the soil is " + str(read_humidity) + "%")
        # columns: id_user, id_envrmt, read_value
        database.insertDataInto(table='measures',
                                id_user=USER_ID,
                                id_environment=1,
                                id_pquantity=2,
                                read_value=read_humidity)
        print("Success! Data inserted into database.\n")
    else:
        print("Failed to read temperature. Try again in 5 seconds.")
예제 #3
0
def main():
    """Main Application
    """
    menu()

    while True:
        item = str(raw_input(">>> SELECT AN OPTION: "))
        if item == '0' or item == 'q':
            comport.close()
            break
        elif item == '1':
            readTemperature()
        elif item == '2':
            readAirHumidity()
        elif item == '3':
            readSoilHumidity()
        elif item == '4':
            table = str(raw_input("> Enter table name: "))
            selectLastRecord(table)
        elif item == '5':
            table = str(raw_input("> Enter table name: "))
            selectAllRecord(table)

        elif item == '6':
            ans = str(
                raw_input("You are about to delete the LAST " +
                          "record of a table.\nARE YOU SURE? (y/n) "))
            if ans.lower() == 'y' or ans.lower() == 'yes':
                table = str(raw_input("> Enter the table's name: "))
                deleteLastRecord(table)
            else:
                print("Operation aborted. Returning to menu...")
                print("--------- \n")

        elif item == '7':
            ans = str(
                raw_input("> You are about to delete the ALL data " +
                          "of a table. \nARE YOU SURE? (yes/no) "))

            if ans.lower() == 'y' or ans.lower() == 'yes':
                table = str(raw_input("> Enter the table's name: "))
                deleteAllRecord(table)
            else:
                print("Operation aborted. Returning to menu...")
                print("--------- \n")

        elif item == '8':
            visualizeByUser()

        elif item == 'C' or item == 'c':
            os.system('cls' if os.name == 'nt' else 'clear')
            menu()

        elif item == '10':
            # TODO: Check possibility dividing this main.py in classes
            print("---------------- CREATE USER -----------")
            usr_fulname = str(raw_input("Enter user full name: "))
            usr_contact = str(raw_input("Enter user contact: "))
            username = str(raw_input("Enter user username: "******"Enter user password: "******"User create with success!")
            print("--------- \n")

        elif item == '11':
            print("\n---------------- USERs INFOs-----------")
            print("Fetching all records from table 'users'...")
            rows = database.selectAllDataFrom('users')
            if rows:
                for row in rows:
                    print row
            else:
                print("No data found!")
            print("--------- \n")

        elif item == '12':
            print("\n-------------- UPDATE USER ---------")
            usrname = str(
                raw_input(
                    "> Type the username of the user whom data will be updated: "
                ))
            field = str(
                raw_input(
                    "> Update which field(s)? Ex.: usr_fullname, active, pswd: "
                ))
            field = field.split(",")
            values = str(raw_input("> Which values? (same order): "))
            values = values.split(",")
            fv_dictio = dict(zip(field, values))
            database.updateDataFrom(table='users',
                                    condition='username',
                                    condition_value=usrname,
                                    **fv_dictio)
            print("User updated with success!")
            print("--------- \n")

        elif item == '13':
            print("\n-------------- REMOVE USER ---------")
            usrname = str(
                raw_input("> Type the username of the user to be removed: "))
            database.deleteDataFrom(table='users',
                                    condition='username',
                                    condition_value=usrname)
            print("User deleted with success!")
            print("--------- \n")

        elif item == '14':
            readAll()

        elif item == '15':
            print("\n-------------- DELETE RECORD FROM TABLE BY ID----------")
            table = str(
                raw_input(
                    "> Type the table's name from which the record will be removed: "
                ))
            id_record = str(
                raw_input("> Type the id of the record to be removed: "))
            database.deleteDataFrom(table=table,
                                    condition='id',
                                    condition_value=id_record)
            print("Data deleted with success!")
            print("--------- \n")

        else:
            print("Invalid option! Choose one option from the menu above.\n")