Esempio n. 1
0
    def MovieMaker(self):
        os.chdir(self.picsLocation)
        if self.control == 1:  ## selected a maximum duration for the video
            self.fps = self.elapsed / (int(self.interval) *
                                       float(self.max_vid_dur))
        else:
            self.fps = int(self.freq)
        command = "ffmpeg -r {} -pattern_type glob -i '*.jpg' -c:v libx264 -s 1280x960 {}/{}_{}.mp4".format(
            self.fps, self.picsLocation, self.testname, self.getDateTime())
        process = subprocess.Popen(command, shell=True,
                                   stdout=subprocess.PIPE)  # make video

        LED.whiteOn()
        while True:
            poll = process.poll()
            if poll != None:  # A None value indicates that the process hasn't terminated yet.
                print('time-lapse movie done')
                os.chdir('/home/pi/relier')
                ## go to credits page
                nxlib.nx_setcmd_1par(nxlib.ser, 'page', 'credits')
                nxlib.nx_setValue(nxlib.ser, nxApp.ID_status[0],
                                  nxApp.ID_status[1], 1)  # green flag
                nxlib.nx_setText(nxlib.ser, nxApp.ID_ip[0], nxApp.ID_ip[1],
                                 str(self.ip))

                try:
                    os.remove(os.path.join(
                        '/home/pi/relier',
                        '0'))  ## fswebcam creates this temporary file
                    os.remove(os.path.join(
                        '/home/pi/relier',
                        '10'))  ## fswebcam creates this temporary file
                    os.remove(os.path.join(
                        '/home/pi/relier',
                        '20'))  ## fswebcam creates this temporary file
                    os.remove(os.path.join(
                        '/home/pi/relier',
                        '30'))  ## fswebcam creates this temporary file
                except:
                    pass
                if self.delImages == 1:  # delete images if desired
                    for f in os.listdir(self.picsLocation):
                        if re.search(".jpg",
                                     f):  # if the file has the extension .jpg
                            os.remove(os.path.join(self.picsLocation,
                                                   f))  # remove the file
                    print('time-lapse images deleted')
                break
        LED.whiteOff()
Esempio n. 2
0
def read_display_write(
        e_rdw):  # read and display data in page "record" and write to file
    global stop, con, cur, doTimelapse, doMovie, delImages, control, freq, max_vid_dur, interval

    #first, check if HET test is selected
    if inp['test_type'] == '3':  # if HET test is the selection
        nxlib.nx_setcmd_2par(ser, 'vis', 'txt_pi', 0)
    else:
        nxlib.nx_setcmd_2par(ser, 'vis', 'txt_pi', 1)

    # inputs about timelapse, movie and delete photos are selected

    doTimelapse = nxlib.nx_getValue(ser, nxApp.ID_doTimeLapse[0],
                                    nxApp.ID_doTimeLapse[1])  # 1 = yes
    doMovie = nxlib.nx_getValue(ser, nxApp.ID_doVideo[0],
                                nxApp.ID_doVideo[1])  # 1 = yes
    delImages = nxlib.nx_getValue(ser, nxApp.ID_delImages[0],
                                  nxApp.ID_delImages[1])  # 1 = yes
    # parameters required for the video
    control = nxlib.nx_getValue(
        ser, nxApp.ID_choiceVideoDur[0],
        nxApp.ID_choiceVideoDur[1])  # 0 = freq;  1 = max duration
    freq = nxlib.nx_getText(ser, nxApp.ID_freqPics[0], nxApp.ID_freqPics[1])
    max_vid_dur = nxlib.nx_getText(ser, nxApp.ID_maxVideoDur[0],
                                   nxApp.ID_maxVideoDur[1])
    interval = nxlib.nx_getText(ser, nxApp.ID_interval[0],
                                nxApp.ID_interval[1])

    # first write in the inputs.ini file the inputs (that will be the default values next time)
    rw.write_ini(inp['filename'], inp['googlesh'], inp['share_email'],
                 inp['google_sheets'], inp['duration'], inp['interval'],
                 inp['no_reads'], inp['test_type'], inp['othername'],
                 inp['mu'], inp['bu'], inp['mi'], inp['bi'], inp['md'],
                 inp['bd'], inp['flowmeter_type'], inp['cf'], inp['timelapse'],
                 inp['video'], inp['del_images'], inp['control_video'],
                 inp['freq'], inp['max_videoDur'], inp['lastip'])

    # obtain the selected worksheet in the google spreadsheet and share it
    export_google = inp['google_sheets']
    if export_google in ('yes', 'Yes', 'YES', 'y', 'Y',
                         'yep') and gsh.google_creds == True:
        wks = gsh.spreadsheet_worksheet(ssheet_title=inp['googlesh'],
                                        wsheet_title=inp['filename'],
                                        share_email=inp['share_email'])
        #sleep(0.01)
    e_rdw.wait()
    row = 1
    zero_vol = 0

    #### required to capture the photos
    testname = inp['filename']

    if testname[-4:] != '.csv':
        testname = testname + '.csv'

    checkpath = find_dev('/media/pi/',
                         testname)  # see if usb flash drive is connected
    if checkpath == "":  # If there is no storage device, write on disk (RPi sd card) in /srv/EROSTESTS/<testname>
        path = os.path.join('/srv/EROSTESTS', testname[:-4])
    else:
        path = os.path.join(checkpath, testname[:-4])
    picsLocation = path + '_timelapse_' + str(
        datetime.datetime.now().strftime("%Y-%m-%d_%H%M"))

    # determine test type
    rg = nxlib.nx_getValue(ser, nxApp.ID_rg[0], nxApp.ID_rg[1])
    if rg == 0:
        testtype = 'FLET'
    elif rg == 1:
        testtype = 'CFET'
    elif rg == 2:
        testtype = 'HET'
    elif rg == 3:
        testtype = nxlib.nx_getText(ser, nxApp.ID_othername[0],
                                    nxApp.ID_othername[1])  # r3
    else:
        testtype = 'undetermined'
    #####

    # print(t1,t2)  # takes to long!!! why? i don't understand

    while end_rdw.is_set() == False and time.time() < stop + int(
            inp['interval']):
        if time.time() < stop + int(inp['interval']):
            t1 = time.time()
            LED.greenOn()
            e_rdw.wait()
            current = time.time()
            elapsed = current - start  # restart thread t_rdw    ### delta_calculus is to take account the time required to write and read from the nextion!!!
            elapsed = time.strftime("%H:%M:%S", time.gmtime(elapsed))
            autostop = time.strftime("%H:%M:%S", time.localtime(stop))
            print("write running")
            data = srv.get_data(int(inp['interval']), float(inp['mu']),
                                float(inp['mi']), float(inp['md']),
                                float(inp['bu']), float(inp['bi']),
                                float(inp['bd']), zerou, zeroi, zerod,
                                inp['test_type'], inp['flowmeter_type'],
                                inp['cf'])

            if inp['flowmeter_type'] != "1":  # only if the turbine flowmeter is selected
                if current < start + 2:  # zero flowrate at start.
                    zero_vol = data['liters']
                    data['flow'] = 0
                data['liters'] = data['liters'] - zero_vol

            write_csv_data.write_data(data=data, data_file=inp['filename'])
            ip = get_ip_address()
            if ip != 'No internet connection':
                #insert a new row in the database in Heroku (only when there is internet)
                cur.execute(
                    "INSERT INTO testdata(date_time, duration, mmH2O_up, mmH2O_int, mmH2O_down, turb, flow, volume) "
                    "VALUES(to_timestamp('{} {}', 'YYYY-MM-DD HH24:MI:SS') ,%s,{},{},{},{},{},{});"
                    .format(data['date'], data['time'], data['mmH2O_up'],
                            data['mmH2O_int'], data['mmH2O_down'],
                            data['turb'], data['flow'], data['liters'],
                            inp['interval']), [
                                elapsed,
                            ])

            display_sensors(data)  # display in NEXTION monitor

            ID_elapsed = nxApp.get_Ids('record', 'txt_duration')
            nxlib.nx_setText(ser, ID_elapsed[0], ID_elapsed[1], elapsed)
            ID_autostop = nxApp.get_Ids('record', 'txt_autostop')
            if int(inp['duration']) == 0:
                nxlib.nx_setText(ser, ID_autostop[0], ID_autostop[1],
                                 '--:--:--')
            else:
                nxlib.nx_setText(ser, ID_autostop[0], ID_autostop[1], autostop)
            if export_google in ['yes', 'Yes', 'YES', 'y', 'Y', 'yep'
                                 ] and gsh.google_creds == True:
                gsh.write_gsh(data, row, wks)
                row += 1
            LED.greenOff()

            # time to timelapse :)
            if doTimelapse == 1:
                # take picture in a different threat
                t_pics = cm.capture(picsLocation, testname[:-4], testtype,
                                    elapsed, data['flow'])
                t_pics.start()
                #t_pics.join()  # takes too long. don't use
            t2 = time.time()
            delta = t2 - t1

            if delta >= float(inp['interval']):
                delta = float(inp['interval'])
            sleep(float(inp['interval']) - delta)  # Interval between records

    ### time to make video
    if doMovie == 1:
        sleep(1)
        nxlib.nx_setcmd_1par(ser, 'page', 15)
        # make video in a separate thread
        ip = get_ip_address()
        t_movie = movie.makemovie(picsLocation, testname[:-4], control, freq,
                                  max_vid_dur, elapsed, interval, delImages,
                                  ip)
        t_movie.start()

    # disconnect from database
    test_end()  # morse code sounds to alert for final test

    cur.close()
    con.close()

    end_rdw.set()
    e_rdw.clear()

    if doMovie != 1:
        nxlib.nx_setcmd_1par(ser, 'page', 'credits')
        ip = get_ip_address()
        nxlib.nx_setValue(ser, nxApp.ID_status[0], nxApp.ID_status[1],
                          1)  # green flag
        nxlib.nx_setText(ser, nxApp.ID_ip[0], nxApp.ID_ip[1], ip)
Esempio n. 3
0
def reboot_pi():
    nxlib.nx_setsys(ser, 'bkcmd',
                    0)  # sets in NEXTION 'no return error/success codes'
    nxlib.nx_setcmd_1par(ser, 'page', 0)  # sends screen to page 0 "reboot"
Esempio n. 4
0
from find_usb import find_dev

# turn of the led
LED.redOff()  #R
LED.greenOff()  #G
LED.blueOff()  #B

######### make connection to serial UART to read/write NEXTION
ser = nxlib.ser

nxlib.nx_setsys(
    ser, 'bauds', nxlib.BAUD
)  # set default baud (default baud rate of nextion from fabric is 9600)
nxlib.nx_setsys(ser, 'bkcmd',
                0)  # sets in NEXTION 'no return error/success codes'
nxlib.nx_setcmd_1par(ser, 'page',
                     1)  # sets page 1  (page 0 is "not connected")
nxlib.nx_setcmd_2par(ser, 'tsw', 'b0', 0)  # disable touch events of b0
nxlib.nx_setcmd_2par(ser, 'tsw', 'txt_status',
                     0)  # disable touch events of dual button
nxlib.nx_setValue(ser, nxApp.ID_status[0], nxApp.ID_status[1],
                  0)  # red flag - not ready yet

EndCom = "\xff\xff\xff"  # 3 last bits to end serial communication

####OBTAIN DATA FROM INI FILE WITH DEFAULT INPUTS
ini = rw.read_ini()  # read inputs.ini and parse parameters

#settings page
nxlib.nx_setText(ser, nxApp.ID_filename[0], nxApp.ID_filename[1],
                 ini['filename'])
nxlib.nx_setText(ser, nxApp.ID_googlesh[0], nxApp.ID_googlesh[1],
Esempio n. 5
0
def shutdown_pi():
    nxlib.nx_setsys(ser, 'bkcmd',
                    0)  # sets in NEXTION 'no return error/success codes'
    nxlib.nx_setcmd_1par(ser, 'page', 11)  # sends screen to page 9 "shutdown"