def measure(): # # MEASURE # global T_meas, T_exec, T_now, T_start, X, Y, Z, rmsACC logger.info("Measuring ACC... ") response = socket_client.request('set,state{},2'.format(iACC)) ACC.setup() T_now = time.time() T_start = getNextGridTime(T_now + 1, T_exec) logger.debug("T_cur={},{}".format(T_now, datetime_string(T_now))) logger.debug("T_st ={},{}".format(T_start, datetime_string(T_start))) logger.info("Waiting for {} seconds ...".format(T_start - T_now)) waitUntil(T_start) # MESURING pycom.heartbeat(False) pycom.rgbled(0x000F00) X, Y, Z, Xrms, Yrms, Zrms, Xavg, Yavg, Zavg = ACC.measure(Td=T_meas) rmsACC = (Xrms**2 + Yrms**2 + Zrms**2)**0.5 logger.info('Avg(X,Y,Z)=(%f,%f,%f)', Xavg, Yavg, Zavg) logger.info('RMS(X,Y,Z)=(%f,%f,%f)', Xrms, Yrms, Zrms) pycom.rgbled(0x000000) time.sleep_ms(100) pycom.heartbeat(True) return
def setup_update(request): thres = [ request.POST['acc_threshold_1'], request.POST['acc_threshold_2'], request.POST['acc_threshold_3'] ] requests = 'set,threshold,{},{},{}'.format(thres[0], thres[1], thres[2]) logger.debug('socket_client.request {}...'.format(requests)) responses = socket_client.request(requests) contexts = {'acc_threshold': thres} return render(request, 'cpanel/setup_update.html', contexts)
def config_measurement(): # # GETTING TIME AND STATE OF CONTROLLER # global logger, rtc, sd, T_exec, T_meas, logfile_new logger.info('Config_measurement...') # GETTING STATE AND TIME FROM CTLR stateCTLR = None wdt = WDT(timeout=25 * 1000) while stateCTLR not in [2, 4]: response = None while not response: requests = 'get,state;get,time;set,state{},1'.format(iACC) logger.info('sending request to CTLR: "{}"...'.format(requests)) try: response = socket_client.request(requests) except: logger.exception('1st request failed.') # try: # response = socket_client.request(requests) # except OSError as exc: # # if exc.args[0] == # pass # except: # raise time.sleep(1) response_list = response.split(';') stateCTLR = int(response_list[0]) time_gps = int(response_list[1]) rtc.init(time.localtime(time_gps)) logger.info('stateCTLR = {} and time={}'.format(stateCTLR, rtc.now())) wdt.feed() wdt = WDT(timeout=wdt_timeout) # RENAME LOGFILE logfile_new = '/sd/log/{}.log'.format(datetime_string(time.time())) logging.fclose() os.rename(logfile, logfile_new) logging.basicConfig(level=logging.DEBUG, filename=logfile_new) if stateCTLR == 2: T_exec = 60 * 5 T_meas = 20 elif stateCTLR == 4: T_exec = 60 * 60 T_meas = 60 * 3 gc.collect() return
def deep_sleep(): global T_now response = socket_client.request('set,state{},3'.format(iACC)) T_now = time.time() T_next = getNextGridTime(T_now + 43, T_exec) logger.info('time_now = {}'.format(datetime_string(T_now))) logger.info('time_next = {}'.format(datetime_string(T_next))) logger.info("Going to Sleep for {} seconds...".format(T_next - 40 - (T_now))) time.sleep(3) ACC.py.setup_sleep(T_next - 40 - (T_now + 3)) ACC.py.go_to_sleep() return
def setup(request): requests = 'get,threshold;' logger.debug('socket_client.request {}...'.format(requests)) responses = socket_client.request(requests) if responses: response_list = responses.split(';') threshold_1 = response_list[0].split(',')[0] threshold_2 = response_list[0].split(',')[1] threshold_3 = response_list[0].split(',')[2] else: threshold_1 = 'unknown' threshold_2 = 'unknown' threshold_3 = 'unknown' contexts = {'acc_threshold': [threshold_1, threshold_2, threshold_3]} return render(request, 'cpanel/setup.html', contexts)
def measure_acc(): # # MEASURE # global T_meas, T_exec, T_now, T_start, acc, acc_d, rmsdACC logger.info("=== Measuring ACC ===".format(T_meas)) ACC.setup() request = 'set,state{},2'.format(iACC) logger.info('sending request to the sever: "{}"'.format(request)) response = socket_client.request(request) T_now = time.time() logger.debug('T_now={}'.format(T_now)) logger.debug('T_exec={}'.format(T_exec)) T_start = getNextGridTime(T_now+1, T_exec) logger.debug("T_cur={},{}".format(T_now, datetime_string(T_now ))) logger.debug("T_st ={},{}".format(T_start,datetime_string(T_start))) logger.info("Waiting for {} seconds ...".format(T_start - T_now)) if debug_mode == False: waitUntil(T_start) # MESURING pycom.heartbeat(False) pycom.rgbled(0x000F00) logger.info('starting measuring for {} sec...'.format(T_meas)) ACC.measure(acc, Td=T_meas) pycom.rgbled(0x000000) time.sleep_ms(100) pycom.heartbeat(True) decimate(acc, acc_d, 4) mean_ = mean(acc_d) rmsd_ = rmsd(acc_d) rmsdACC = (rmsd_[0]**2+rmsd_[1]**2+rmsd_[2]**2)**0.5 logger.info('Avg(X,Y,Z)=(%f,%f,%f)',mean_[0],mean_[1],mean_[2]) logger.info('RMS(X,Y,Z)=(%f,%f,%f)',rmsd_[0],rmsd_[1],rmsd_[2]) gc.collect() return
def LED_control(request): try: if request.POST['pwrLED'] in 'on': pwrLED = True except: pwrLED = False if pwrLED: contexts = {'pwrLED': 'on'} requests = 'set,pwrLED,on;' else: contexts = {'pwrLED': 'off'} requests = 'set,pwrLED,off;' logger.debug('pwrLED={}'.format(pwrLED)) logger.debug('socket_client.request {}...'.format(requests)) responses = socket_client.request(requests) return render(request, 'cpanel/LED_control.html', contexts)
def config_measurement(): # # GETTING TIME AND STATE OF CONTROLLER # global logger, rtc, sd, T_exec, T_meas, logfile_new logger.info('Config_measurement...') # GETTING STATE AND TIME FROM CTLR stateCTLR = None while stateCTLR not in [2, 4]: response = None while not response: requests = 'get,state;get,time;set,state{},1'.format(iACC) response = socket_client.request(requests) logger.info('request sent to CTLR:{}...'.format(requests)) time.sleep(1) response_list = response.split(';') stateCTLR = int(response_list[0]) time_gps = int(response_list[1]) rtc.init(time.localtime(time_gps)) logger.info('stateCTLR={} and time={}'.format(stateCTLR, rtc.now())) logger.info('Starting ACC node job...') # RENAME LOGFILE logfile_new = '/sd/log/{}.log'.format(datetime_string(time.time())) logging.fclose() os.rename(logfile, logfile_new) logging.basicConfig(level=logging.DEBUG, filename=logfile_new) if stateCTLR == 2: T_exec = 60 T_meas = 5 elif stateCTLR == 4: T_exec = 3600 T_meas = 60 * 3 return
formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) logger.addHandler(ch) path = 'log/state6_video_rec.log' fh = TimedRotatingFileHandler(path, when="midnight", backupCount=0) fh.setLevel(logging.DEBUG) formatter1 = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter1) logger.addHandler(fh) logger.debug('Quering CTLR...') if 0: responses = socket_client.request('get,icam;') responses_list = responses.split(';') iCAM = int(responses_list[0]) else: iCAM = 1 logger.debug('Quering CTLR Done.') #fileName = 'videos/timestamped.h264' #fileName = 'videos/gcam{}-{}.h264'.format(iCAM,datetime.datetime.now().strftime('%Y-%m%d-%H%M%S')) fileName = 'cpanel/static/cpanel/video/{}.h264'.format(filename) logger.debug('Recording h264 video...') with picamera.PiCamera() as camera: # camera.resolution = (1920,1080) camera.resolution = (1080, 1920)
import time import pycom import logging import socket_client logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) c = 0 while True: requests = 'get,state;set,rms1,' + str(2 * float(c) + 1.0) logger.debug('socket_client.request {}...'.format(requests)) socket_client.request(requests) logger.debug('socket_client.request done...') logger.debug('mainloop: c = {}...'.format(c)) time.sleep(1) c += 1 # Packaging socket_client 2020-0403 # Done smoothly
def send_file(): _files = os.listdir('/sd/data/uploading') logger.info('{} file(s) to upload from /sd/data/uploading...'.format( len(_files))) time.sleep(15 * (iACC - 1)) for _file in _files: _file_fullpath = '/sd/data/uploading/' + _file # _file='gcam1-acc1-2020-0507-201110' # 0 0 0 _dateStr = _file[11:20] # yyyy-mmdd _timestamp = _file[11:] # yyyy-mmdd-HHMMSS _dest_path = '/sd/data/' + _dateStr logger.info("FTP to CTLR: Remote={} ...".format(_dest_path)) os.chdir('/sd/data/uploading') iter = 0 out = None while iter < 3: try: out = dropfile.dropfile(CTLR_IPADDRESS, 21, _dest_path, 'micro', 'python', _file) logger.debug('out={}'.format(out)) if out == 0: break # Successful if out == -1: logger.debug( '*************************************************') logger.debug( 'retry dropfile, current_iter={}, connection timeout; svr busy' .format(iter)) logger.debug( '*************************************************') iter += 1 time.sleep( 5) # waiting until the connection is closed by the svr continue else: logger.debug( '*************************************************') logger.debug( 'retry dropfile, current_iter={}, svr response out={}'. format(iter, out)) logger.debug( '*************************************************') iter += 1 time.sleep( 5) # waiting until the connection is closed by the svr continue except OSError as exc: # if exc.args[0] == uerrno.ECONNRESET or exc.args[0] == uerrno.ECONNABORTED or \ # exc.args[0] == uerrno.ETIMEDOUT: logger.debug( '*************************************************') logger.exception( 'retry dropfile, current_iter={}, errorcode={}'.format( iter, exc.args[0])) logger.debug( '*************************************************') time.sleep(30) iter += 1 continue except: logger.debug( '*************************************************') logger.exception( 'retry dropfile, current_iter={}, non-OSError'.format( iter)) logger.debug( '*************************************************') time.sleep(30) iter += 1 continue if out == 0: logger.info("FTP Successful.") _file_fullpath_new = _file_fullpath.replace( 'uploading', 'uploaded') os.rename(_file_fullpath, _file_fullpath_new) logger.info('file moved to uploaded folder.') request = 'set,file{},{};'.format(iACC, _timestamp) logger.info('sending request to the sever: "{}"'.format(request)) response = socket_client.request(request) else: logger.info("****** FTP Failed after all *******") # filename='/sd/data/uploading/gcam1-acc1-2020-0507-201110' # 0 0 0 0 0 timestamp = filename[30:] # yyyy-mmdd-HHMMSS request = 'set,state{},3;set,rms{},{},{:.6f};'.format( iACC, iACC, timestamp, rmsACC) logger.info('sending request to the sever: "{}"'.format(request)) response = socket_client.request(request) diskutil.house_keeping('/sd/data/uploaded', 25, 'acc') diskutil.house_keeping('/sd/log', 3, 'log') logger.info('available space = {:,} bytes'.format(os.getfree('/sd'))) gc.collect() return
def index(request): global latest_video_file stop_streaming() # return HttpResponse("Hello, world. You're at the polls index.") requests = 'get,icam;get,time;get,switch;get,state;get,astate;get,afile;get,arms;get,threshold;get,mem;get,disk;get,volt;get,pwrLED;' # 0 1 2 3 4 5 6 7 8 9 10 11 logger.debug('socket_client.request {}...'.format(requests)) responses = socket_client.request(requests) if responses: response_list = responses.split(';') icam = int(response_list[0]) time = datetime.datetime.fromtimestamp(int( response_list[1])).strftime('%Y-%m-%d %H:%M:%S') switch = int(response_list[2]) state = int(response_list[3]) acc_state = [int(i) for i in response_list[4].split(',')] acc_file = response_list[5].split(',') acc_rms = response_list[6].split(',') acc_threshold = [float(thr) for thr in response_list[7].split(',')] mem = response_list[8].split(',') volt = response_list[10] disk = response_list[9].split(',') pwrLED = 'checked' if 'on' in response_list[11] else 'unchecked' else: icam = 0 time = 'unknown' switch = 'unknown' state = 'unknown' acc_state = [None, None, None] acc_file = [None, None, None] acc_rms = ['', None, '', None, '', None] acc_threshold = [None, None, None] mem = [None, None] volt = 0 disk = [None, None] pwrLED = 'unchecked' latest_accFiles = ['', '', ''] if icam: # Database interaction: RMS logger.debug('*********************') logger.debug('acc_rms[0]={}'.format(acc_rms[0])) if acc_rms[0]: acc1_last = Acc1.objects.last() logger.debug('acc1_last={}'.format(acc1_last)) if not acc1_last: logger.debug('acc1_last None') Acc1(measure_time=acc_rms[0], rmsACC=float(acc_rms[1])).save() elif acc_rms[0] > acc1_last.measure_time: logger.debug('acc_rms[0]>acc1_last.measure_time:{}>{}'.format( acc_rms[0], acc1_last.measure_time)) Acc1(measure_time=acc_rms[0], rmsACC=float(acc_rms[1])).save() if acc_rms[2]: acc2_last = Acc2.objects.last() if not acc2_last: Acc2(measure_time=acc_rms[2], rmsACC=float(acc_rms[3])).save() elif acc_rms[2] > acc2_last.measure_time: Acc2(measure_time=acc_rms[2], rmsACC=float(acc_rms[3])).save() if acc_rms[4]: acc3_last = Acc3.objects.last() if not acc3_last: Acc3(measure_time=acc_rms[4], rmsACC=float(acc_rms[5])).save() elif acc_rms[4] > acc3_last.measure_time: Acc3(measure_time=acc_rms[4], rmsACC=float(acc_rms[5])).save() logger.debug('*************************') logger.debug('* acc_rms = [{}, {}]'.format(acc_rms[0], acc_rms[1])) logger.debug('*************************') # Database interaction: File for inode in range(3): if acc_file[inode]: if inode == 0: logger.debug('acc_file[inode]={}'.format(acc_file[inode])) acci = Acc1.objects.filter(measure_time=acc_file[inode]) elif inode == 1: acci = Acc2.objects.filter(measure_time=acc_file[inode]) elif inode == 2: acci = Acc3.objects.filter(measure_time=acc_file[inode]) if len(acci): acci_ = acci[0] if not acci_.fileUploaded: acci_.fileUploaded = True acci_.save() logger.debug('*************************') logger.debug('* acc_file = [{}, {},{}]'.format(acc_file[0], acc_file[1], acc_file[2])) logger.debug('*************************') latest_accFiles = [ Acc1.objects.filter(fileUploaded=True).last().measure_time, Acc2.objects.filter(fileUploaded=True).last().measure_time, Acc3.objects.filter(fileUploaded=True).last().measure_time ] logger.debug('*************************') logger.debug('* acc_file = [{}, {},{}]'.format(acc_file[0], acc_file[1], acc_file[2])) logger.debug('*************************') logger.debug('pwrLED={}'.format(pwrLED)) # SystemStates sysState = SystemState.objects.last() if not sysState: # initialise SystemState(icam=icam).save() else: # update if different if icam != sysState.icam: sysState.icam = icam sysState.save() latest_event = Event.objects.last() if not latest_event: latest_videoFile = 'None' else: latest_videoFile = 'gcam{}-{}.mp4'.format(icam, latest_event.measure_time) for i in range(3): if acc_rms[2 * i]: already_captured = Event.objects.filter(measure_time=acc_rms[2 * i]) if float(acc_rms[2 * i + 1]) > acc_threshold[i] and not already_captured: return redirect('/video_record?timestamp={}'.format( acc_rms[2 * i])) contexts = {'icam':icam, 'time':time, 'switch':switch, 'state':state, \ 'acc_state':acc_state,'acc_file':acc_file, \ 'acc_rms':acc_rms, 'acc_threshold': acc_threshold, \ 'mem':mem, 'disk':disk, 'volt':volt, 'pwrLED':pwrLED, \ 'latest_accFiles':latest_accFiles, 'latest_videoFile':latest_videoFile} logger.debug('socket_client.request done...') return render(request, 'cpanel/index.html', contexts)
def send_file(): logger.info("FTP to CTLR: Remote={} ...".format(remote_incoming_dir)) files = os.listdir(uploading_dir) logger.info('{} file(s) found on {}...'.format(len(files),uploading_dir)) time.sleep(15*(iACC-1)) for file in files: if 'tmp-{}'.format(date_string(today())) in file: # Skip unfinished temperature file logger.info('ftp skipped on {}.'.format(file)) continue # file='gcam1-acc1-acc-2020-0507-201110' # 0 0 0 0 timestamp = file[15:] # yyyy-mmdd-HHMMSS os.chdir(uploading_dir) iter = 0 out = None while iter <3: try: out = dropfile.dropfile(CTLR_IPADDRESS, 21, remote_incoming_dir,'micro', 'python', file) logger.debug('out={}'.format(out)) if out ==0: break # Successful if out == -1: logger.debug('*************************************************') logger.debug('retry dropfile, current_iter={}, connection timeout; svr busy'.format(iter)) logger.debug('*************************************************') iter += 1 time.sleep(5) # waiting until the connection is closed by the svr continue else: logger.debug('*************************************************') logger.debug('retry dropfile, current_iter={}, svr response out={}'.format(iter,out)) logger.debug('*************************************************') iter += 1 time.sleep(5) # waiting until the connection is closed by the svr continue except OSError as exc: # if exc.args[0] == uerrno.ECONNRESET or exc.args[0] == uerrno.ECONNABORTED or \ # exc.args[0] == uerrno.ETIMEDOUT: logger.debug('*************************************************') logger.exception('retry dropfile, current_iter={}, errorcode={}'.format(iter,exc.args[0])) logger.debug('*************************************************') time.sleep(30) iter += 1 continue except: logger.debug('*************************************************') logger.exception('retry dropfile, current_iter={}, non-OSError'.format(iter)) logger.debug('*************************************************') time.sleep(30) iter += 1 continue if out==0: logger.info("FTP Successful for {}".format(file)) # MOVE UPLOADED FILES INTO UPLOADED_DIR # yyyy-mmdd-HHMMSS # 0 0 yearString = timestamp[:4] monthdayString = timestamp[5:9] target_dir = '{}/{}/{}'.format(uploaded_dir, yearString, monthdayString) target_dir_p = '{}/{}'.format(uploaded_dir, yearString) mkdir(target_dir_p) mkdir(target_dir) os.rename('{}/{}'.format(uploading_dir,file), '{}/{}'.format(target_dir,file)) logger.info('file moved to uploaded folder.') if '-acc-' in file: request = 'set,file{},{};'.format(iACC,timestamp) logger.info('sending request to the sever: "{}"'.format(request)) response = socket_client.request(request) else: logger.info("****** FTP Failed after all *******") timestamp = datetime_string(T_start) # yyyy-mmdd-HHMMSS request = 'set,state{},3;set,rms{},{},{:.3f};'.format(iACC,iACC,timestamp,rmsdACC) logger.info('sending request to the sever: "{}"'.format(request)) response = socket_client.request(request) # diskutil.house_keeping('/sd/data',15,'acc') # diskutil.house_keeping('/sd/data/uploading',10,'acc') # diskutil.house_keeping('/sd/log',3,'log') # logger.info('available space = {:,} bytes'.format(os.getfree('/sd'))) gc.collect() return
def config_measurement(): # # GETTING TIME AND STATE OF CONTROLLER # global logger, rtc, sd, T_exec, T_meas, logfile_new global acc, acc_d, wdt_timeout logger.info('Config_measurement...') # GETTING STATE AND TIME FROM CTLR stateCTLR = None wdt = WDT(timeout=25*1000) while True: requests = 'get,state;get,time;set,state{},1'.format(iACC) logger.info('sending request to CTLR: "{}"...'.format(requests)) try: response = socket_client.request(requests) except: logger.exception('The Step1 Request failed.') if response: response_ = response.split(';') stateCTLR = int(response_[0]) if stateCTLR not in [1]: time_gps = int(response_[1]) rtc.init(time.localtime(time_gps)) break time.sleep(1) logger.info('stateCTLR = {} and time={}'.format(stateCTLR,rtc.now())) wdt.feed() # RENAME LOGFILE logfile_new = '/sd/log/{}.log'.format(datetime_string(time.time())) logging.fclose() os.rename(logfile,logfile_new) logging.basicConfig(level=logging.DEBUG,filename=logfile_new) logger.info('stateCTLR={}'.format(stateCTLR)) if stateCTLR in [2,8]: logger.info('stateCTLR in [2,8]={}'.format(stateCTLR in [2,8])) T_exec = 60*5 T_meas = 20 # T_meas = 60*3 # T_meas = 10 elif stateCTLR in [4]: T_exec = 60*10 T_meas = 60*1 elif stateCTLR in [3,5,6,7]: logger.info('stateCTLR in [3,5,6,7]={}'.format(stateCTLR in [3,5,6,7])) T_exec = 60*10 T_meas = 60*1 deep_sleep() wdt_timeout = int(T_exec*1000*1.5) wdt = WDT(timeout=wdt_timeout) acc = [array.array('f', [0.0 for j in range(T_meas*100)]) for i in range(3)] acc_d = [array.array('f', [0.0 for j in range(T_meas*100)]) for i in range(3)] gc.collect() return
wlan.ifconfig(config=('192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8')) wlan.connect('RUT230_7714', auth=(WLAN.WPA2, 'Ei09UrDg'), timeout=5000) while not wlan.isconnected(): machine.idle() # save power while waiting logger.debug('connecting to Wifi done...') # # GETTING TIME AND STATE OF CONTROLLER (state=1) # state = 1 c = 1 while c: requests = 'get,state;get,time;set,state1,{}'.format(state) logger.debug('socket_client.request {}...'.format(requests)) response = socket_client.request(requests) logger.debug('socket_client.request done. Response={}.'.format(response)) c += 1 if response: response_list = response.split(';') state = int(response_list[0]) time_gps = int(response_list[1]) rtc.init(time.localtime(time_gps)) logger.debug('state of controller = {} and time={}'.format( state, rtc.now())) if state in [2, 4]: logger.debug('CTLR state={}; starting node job...'.format(state)) c = 0 else: logger.debug( 'CTLR state={}; waiting until CTLR ready...'.format(state))