def main():
    global cancel_count

    # Remove old shutdown file
    try:
        os.remove("/home/pi/accelerometer_raspi/source/shutdown")
    except (OSError):
        pass

    myLCD.updateLCD(str2="WELCOME", str3="REXNORD EDGE DEVICE")
    sleep(2)

    myLCD.clearLine(2)
    myLCD.clearLine(3)

    while True:
        select_option()
        cutie.getTime()
        if cancel_count >= 3:
            myLCD.clear_all()
            options = ['EXIT', 'SHUTDOWN']
            selected_option = cutie.select(options, selected_index=0)
            if selected_option == 0:
                myLCD.updateLCD(str2='EXITING PROGRAM', str3='GOODBYE')
                sleep(2)
                exit()
            elif selected_option == 1:
                myLCD.updateLCD(str2='EXITING PROGRAM', str3='GOODBYE')
                sleep(2)
                os.system('sudo shutdown now -h')
def set_time():

    myLCD.clear_all()

    myLCD.updateLCD(str2='SET TIME')
    year = cutie.get_number_arrows('YEAR', 1, 2050, 2018)
    if year == -1:
        return

    myLCD.updateLCD(str2='SET TIME')
    month = cutie.get_number_arrows('MONTH', 1, 13, 1)
    if year == -1:
        return

    invalidDay = True
    day = 0
    while invalidDay:
        myLCD.updateLCD(str2='SET TIME')
        day = cutie.get_number_arrows('DAY', 1, 32, 1)

        if day == -1:
            return

        if day == 31 and (month in [2, 4, 6, 9, 11]):
            invalidDay = True
            myLCD.updateLCD(str2='INVALID DAY')

        elif day == 30 and (month in [2]):
            myLCD.updateLCD(str2='INVALID DAY')
            invalidDay = True
        else:
            invalidDay = False

    myLCD.updateLCD(str2='SET TIME')
    hour = cutie.get_number_arrows('HOUR', 1, 13, 1)
    if hour == -1:
        return

    myLCD.updateLCD(str2='SET TIME')
    minute = cutie.get_number_arrows('MINUTE', 1, 60, 1)
    if hour == -1:
        return

    time_tuple = (
        year,
        month,
        day,
        hour,
        minute,
    )

    _set_time_helper(time_tuple)

    myLCD.clear_all()
def delete_file():
    myLCD.clear_all()

    # find all *.csv files and display select
    home = expanduser('~')
    csv_files = find_all_files('*.csv', home)

    if len(csv_files) == 0:
        myLCD.updateLCD(str2='NO FILES FOUND')

    csv_files_lcd = []
    count = 1
    for file in csv_files:
        strs = file.split('_')
        tmp = str(count) + '. ' + strs[1] + ' ' + strs[2]
        #tmp = tmp[:tmp.rfind(':')]
        csv_files_lcd.append(tmp)
        count += 1

    #transfer selected file to usb
    myLCD.clear_all()
    selected_index = cutie.select(csv_files_lcd, selected_index=0)
    if selected_index == -1:
        return
    selected_csv = csv_files[selected_index]

    print('SELECTED CSV: ' + selected_csv)

    myLCD.updateLCD(str2='SELECTED CSV: ',
                    str3=selected_csv.split('Device')[1][2:],
                    str4='DELETING FILE')

    os.remove(selected_csv)
    sleep(1)

    myLCD.updateLCD(str4='FILE DELETED')
    sleep(1)

    return
def transfer_usb():

    myLCD.clear_all()

    #find usb and confirm hash
    usb = get_usb_devices()

    while len(usb) == 0:
        myLCD.printLine(1, 'NO USB CONNECTED')
        print('NO USB CONNECTED')
        sleep(1)
        options = ['RETRY USB CONNECTION']
        selected_option = cutie.select(options, selected_index=0)
        if selected_option == -1:
            return  # back to main menu
        usb = get_usb_devices()

    # mount usb device if necessary
    usb_mount_pt = get_mount_points()
    if len(usb_mount_pt) == 0:
        # mount should iterate through all file system types in /proc/filesystems but fails itermittently
        bash_mount_cmd = 'sudo mount /dev/{}1 /media/usb/'.format(
            usb.keys()[0])
        print(bash_mount_cmd)
        subprocess.check_output(bash_mount_cmd.split())

    sleep(1)
    hashfile = find_file('hash.key', '/media/usb/')

    if hashfile is None:
        print('INVALID USB')
        myLCD.printLine(1, 'INVALID USB')
        sleep(1)
        return  # back to main menu

    # find all *.csv files and display select
    home = expanduser('~')
    csv_files = find_all_files('*.csv', home)

    csv_files_lcd = []
    count = 1
    for file in csv_files:
        strs = file.split('_')
        tmp = str(count) + '. ' + strs[1] + ' ' + strs[2]
        #tmp = tmp[:tmp.rfind(':')]
        csv_files_lcd.append(tmp)
        count += 1

    #transfer selected file to usb
    myLCD.clear_all()
    selected_index = cutie.select(csv_files_lcd, selected_index=0)
    if selected_index == -1:
        return
    selected_csv = csv_files[selected_index]

    print('SELECTED CSV: ' + selected_csv)

    myLCD.updateLCD(str2='SELECTED CSV: ' + selected_csv,
                    str3='COPYING FILE',
                    str4='DO NOT UNPLUG USB')
    #shutil.copy(selected_csv, '/media/usb'+selected_csv)

    # hacky workaround using bash executed in python
    cmd = 'sudo chmod 777 /media/usb/'
    subprocess.check_output(cmd.split())

    selected_csv_file = selected_csv.split('/')[-1]
    selected_csv_file = selected_csv_file.replace(' ', '')
    indexColon = selected_csv_file.find(':')
    selected_csv_file = selected_csv_file[:
                                          indexColon] + 'h' + selected_csv_file[
                                              indexColon + 1]
    indexColon = selected_csv_file.find(':')
    selected_csv_file = selected_csv_file[:
                                          indexColon] + 'm' + selected_csv_file[
                                              indexColon + 1]
    indexSecAbrv = selected_csv_file.find('.csv')
    selected_csv_file = selected_csv_file[:
                                          indexSecAbrv] + 's' + selected_csv_file[
                                              indexSecAbrv:]

    cmd = 'sudo touch /media/usb/{}'.format(selected_csv_file)
    #subprocess.check_output(cmd.split())

    cmd = 'sudo cp {} /media/usb/{}'.format(selected_csv, selected_csv_file)
    cmd = cmd.split()
    cmd = cmd[:2] + [cmd[2] + ' ' + cmd[3]] + cmd[4:]
    subprocess.check_output(cmd)

    sleep(1)

    #done
    myLCD.updateLCD(str2='TRANSFER COMPLETE')
    print('DONE')
    sleep(1)
    return
Example #5
0
def record_data():
    # lcd = Adafruit_CharLCD()
    # lcd.clear()

    hours = 0
    minutes = 0

    myLCD.clear_all()
    myLCD.printLine(1, 'ENTER DURATION:')
    hours = cutie.get_number_arrows('HOURS', 1, 13, 0)
    if hours == -1:
        return
    minutes = cutie.get_number_arrows('MIN', 1, 60, 0)
    if minutes == -1:
        return

    if minutes + hours == 0:
        #lcd.clear()
        #lcd.message('NO TIME ENTERED')
        myLCD.updateLCD(str2='NO TIME ENTERED')
        print('NO TIME ENTERED')
        sleep(1)
        return

    endTime = time() + (3600 * float(hours)) + (60 * float(minutes))

    # check if sensor is attached via /dev/ttyUSB0
    sensor_connected = os.path.exists('/dev/ttyUSB0')
    while not sensor_connected:
        #lcd.clear()
        #lcd.message('NO SENSOR FOUND')
        myLCD.updateLCD(str2='NO SENSOR FOUND')
        print('NO SENSOR FOUND')
        sleep(1)
        options = ['RETRY SENSOR PAIRING']
        selected_option = cutie.select(options, selected_index=0)
        if selected_option == -1:
            return  # back to main menu
        sensor_connected = os.path.exists('/dev/ttyUSB0')

    myLCD.updateLCD(str2='ENTER TO START')
    button_pressed = None
    while True:
        myLCD.getTime()
        button_pressed = cutie.wait_for_button()
        print(button_pressed)
        if button_pressed == 'red' or button_pressed == 'green':
            break

    if button_pressed == 'red':
        myLCD.updateLCD(str2='CANCELING...')
        sleep(1)
        return

    # calculate the finish time after the user confirms the start
    curTime = time()
    finish = datetime.datetime.fromtimestamp(endTime)

    myLCD.updateLCD(str2='RECORDING...',
                    str3='FINISH: {:02d}:{:02d}'.format(
                        finish.hour, finish.minute))
    print('FINISH: {:02d}:{:02d}\nRECORDING...'.format(finish.hour,
                                                       finish.minute))

    # excute following script: java -jar ~/BannerQM42TestApplication.jar -config 1000RPM-5Hz_1Device.JSON -logfile test.csv -port /dev/ttyUSB0
    os.chdir('/home/pi')
    cmd = 'java -jar BannerQM42TestApplication.jar -config 1000RPM-5Hz_1Device.JSON -logfile {}.csv -port /dev/ttyUSB0'
    cmd = cmd.format(datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S'))
    #subprocess.check_output(cmd.split())

    # see StackO: https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true
    pro = subprocess.Popen(cmd.split(),
                           stdout=subprocess.PIPE,
                           preexec_fn=os.setsid)

    while True:
        myLCD.getTime()
        button_pressed = cutie.wait_for_button()
        if (time() >= endTime) or button_pressed == 'red':
            break
        elif button_pressed == 'red':
            myLCD.updateLCD(str2='CANCELING...')
            os.killpg(os.getpgid(pro.pid), signal.SIGTERM)
            os.chdir('/home/pi/accelerometer_raspi/source')
            sleep(1)
            return

    os.killpg(os.getpgid(pro.pid), signal.SIGTERM)

    os.chdir('/home/pi/accelerometer_raspi/source')

    #lcd.message('\nDONE')
    myLCD.updateLCD(str2='DONE')
    sleep(1)
    print('DONE')