def upload_live_data(): global cycleCount # Guard against multiple threads if upload_live_data.active: return upload_live_data.active = True print("read live data from pump") hasFailed = True numRetries = MAX_RETRIES_AT_FAILURE while hasFailed and numRetries > 0: try: liveData = cnl24driverlib.readLiveData() hasFailed = False except: print("unexpected ERROR occured while reading live data") syslog.syslog(syslog.LOG_ERR, "Unexpected ERROR occured while reading live data") liveData = None numRetries -= 1 if numRetries > 0: time.sleep(5) # Upload data to Blynk server if blynk != None: try: blynk_upload(liveData) except: syslog.syslog(syslog.LOG_ERR, "Blynk upload ERROR") # TEST #liveData = {"actins":0.5, #"bgl":778, #"time":"111", #"trend":2, #"unit":60, #"batt":25 #} # Upload data to Nighscout server if nightscout != None: try: nightscout.upload(liveData) except: syslog.syslog(syslog.LOG_ERR, "Nightscout upload ERROR") cycleCount += 1 upload_live_data.active = False
def upload_live_data(): global cycleCount global cycleTimer # Guard against multiple threads if upload_live_data.active: return upload_live_data.active = True print("read live data from pump") hasFailed = True numRetries = MAX_RETRIES_AT_FAILURE while hasFailed and numRetries > 0: try: liveData = cnl24driverlib.readLiveData() hasFailed = False except: print("unexpected ERROR occured while reading live data") syslog.syslog(syslog.LOG_ERR, "Unexpected ERROR occured while reading live data") liveData = None numRetries -= 1 if numRetries > 0: time.sleep(RETRY_DELAY) # Account for pump RTC drift if liveData != None: print("account for pump RTC drift:") print(" before: pumpTime {0}, sensorBGLTimestamp {1}".format( liveData["pumpTime"], liveData["sensorBGLTimestamp"])) liveData["pumpTime"] += liveData["pumpTimeDrift"] if liveData["sensorBGL"] != SENSOR_EXCEPTIONS.SENSOR_LOST: liveData["sensorBGLTimestamp"] += liveData["pumpTimeDrift"] print(" after : pumpTime {0}, sensorBGLTimestamp {1}".format( liveData["pumpTime"], liveData["sensorBGLTimestamp"])) # Upload data to Blynk server if blynk != None: try: blynk_upload(liveData) except: syslog.syslog(syslog.LOG_ERR, "Blynk upload ERROR") # Upload data to Nighscout server if nightscout != None: try: nightscout.upload(liveData) except: syslog.syslog(syslog.LOG_ERR, "Nightscout upload ERROR") # Calculate time until next reading if liveData != None: nextReading = liveData["sensorBGLTimestamp"] + datetime.timedelta( seconds=UPDATE_INTERVAL) tmoSeconds = int((nextReading - datetime.datetime.now( liveData["pumpTime"].tzinfo)).total_seconds()) print("Next reading at {0}, {1} seconds from now\n".format( nextReading, tmoSeconds)) if tmoSeconds < 0: tmoSeconds = RETRY_INTERVAL else: tmoSeconds = RETRY_INTERVAL print("Retry reading {0} seconds from now\n".format(tmoSeconds)) # Start timer for next cycle cycleTimer = threading.Timer(tmoSeconds + 10, upload_live_data) cycleTimer.start() cycleCount += 1 upload_live_data.active = False