def fifteen_sec_updates(): ''' These are the things to do once every 15 Seconds Collect Data ''' global cont global to_modem global modem global modem_status global head_left global head_right global run_cyc global dt global now2 save_controller() ## Save current contrloller state data_to_store = {key:cont[key] for key in ['gmid','seq','pressure','runcycles','faults','mode','temp','current']} # Reset Sequence Count afer 4 entries (test) (*** 40 entries in production) cont['pressure'] = round(cont['pressure'],2) data = {'id' : cont['gmid'],'s':cont['seq'], 'p':cont['pressure'], 'r':cont['runcycles'], 'f':cont['faults'], 'm':cont['mode'], 't':cont['temp'], 'c':cont['current']} modem.append(data) if not now2: now2 = get_time() Eastern_tz = tz.gettz('EST5EDT') dt = now2.now(tz=Eastern_tz) UTC_tz = tz.tzutc() dt_utc = now2.now(tz=UTC_tz) to_file = data to_file['dt'] = dt.strftime("%m/%d/%Y %H:%M:%S%Z") to_file['utc'] = dt_utc.strftime("%m/%d/%Y %H:%M:%S%Z") if DEBUGGING: print(f"Logging data #:{cont['seq']}") print(f'15 sec run_cyc: {run_cyc}') ### ### CHANGE NOTICE: ### Changed comparison below from == to >= on 7/27/21 to ensure that records are pass AFTER reaching 40 records. ### It would have been possible to miss a 10 minute wndow if the system was in the process of downloading a new ### program. ### last_cycle = cont['cycles_per_block'] - 1 if (cont['seq'] >= last_cycle) and (not file_download): req = {"req" :"note.add"} req["file"] = "vstdata.qo" req["sync"] = True req["body"] = {"data" : modem} try: rsp = card.Transaction(req) except: modem_status = 'ERR:OFFLINE' if DEBUGGING: print(f'Modem Not Responding... ') logging.info(f'Modem Not Responding... ') req = {"req" : "hub.status"} try: rsp = card.Transaction(req) except: modem_status = 'ERR:OFFLINE' if DEBUGGING: print(f'Modem Not Responding... ') logging.info(f'Modem Not Responding... ') else: print(f'Hub Status:') print(rsp) req = {"req": "hub.sync.status"} rsp = card.Transaction(req) print(f'Hub SYNC Status:') print(rsp) modem_status = 'TRANSMITTING' #print(rsp) #pprint(req) ## Only empty the modem array if there was a successful transmission, ## If not, continue to accumulate modem = [] cont['seq'] = 0 else: modem_status = f"ADDING REC: {cont['seq']}" cont['seq'] = cont['seq'] + 1 ### ### Write data to a file ### try: f = open("/media/pi/DATA/logfile.csv","a") except IOError as e: #except Exception as e: print(e) #except: logging.error("USB Flash media not found.") alarms['sd_card_alarm'] = True else: f.write(f'{to_file}\n') f.close() to_file = [] alarms['sd_card_alarm'] = False