def get_and_store_data():
    '''
    Get data from the usb port connected with UNO board
    Then record and process the data collected
    '''
    try:
        while True:   
            file = open("./water_level.txt", "a+")
            conn = server_conn.connect_db(conf.DB['database'])
            cur = conn.cursor()
            create_table(cur)
            
            curTime = datetime.now()
            curTime = curTime.strftime('%Y_%m_%d_%H_%M_%S')
            
            seri = serial.Serial(conf.sc_port, conf.sc_baud)
            line = seri.readline()
            data = line.split(',')
            if len(data) != 2:
                print "Wrong Values! Beginning the next collection!"
                continue
            else:
                res = float(data[0])
                vol = float(data[1])
                print "Resistance: " + str(res) + ", Vol: " + str(vol)
            
            status = determineWaterLevel(res)
            print "Water Level: " + str(status)

            store_data_to_local(file, str(status), str(vol), curTime)
            store_data_to_server(cur, str(status), str(vol))
            
            conn.close()
            seri.close()
            file.close()
            print "================================================"
            time.sleep(10)
    except IOError:
        print "I/O Error in opening the file!"
    except MySQLdb.Error, e:
        print "MySQL Error [{0}]: {1}".format(e.args[0], e.args[1]) 
def get_and_store_data():
    pre_power = -1
    
    # Build connection and get current data from USB port
    seri = serial.Serial(conf.ct_port, conf.ct_baudrate)
    
    # Open the file for storing data locally
    try:
        file = open("./res_data/ct_on_data.txt", "a+")
    except IOError:
        print "I/O Error in opening the file!"

    # Connect to database for storing data remotely to the server
    try:
        conn = server_conn.connect_db(conf.DB['database_c'])
        cur = conn.cursor()
        create_table(cur)
        while True:    
            curTime = datetime.now()
            curTime = curTime.strftime('%Y_%m_%d_%H_%M_%S')
            line = seri.readline()
            data = line.split()
            
            if len(data) != 2:
                print "Wrong Values!"
                continue
            
            status = determineIfOn(pre_power, float(data[0]))
            
            print data[0] + ", " + data[1]
            print status

            store_data_to_local(file, str(status), curTime)
            store_data_to_server(cur, str(status))
            
            pre_power = data[0]
            time.sleep(2)
    except MySQLdb.Error, e:
        print "MySQL Error [%d]: %s".format(e.args[0], e.args[1]) 
        return False