text
stringlengths
1
93.6k
# Check if last bolus time is recent
if int(time.time()) - lastBolusTime < 2*UPDATE_INTERVAL:
print("Bolus time is recent")
blynk.virtual_write(VPIN_LASTBOLUS, data["lastBolusAmount"])
else:
blynk.virtual_write(VPIN_ACTINS, data["activeInsulin"])
else:
syslog.syslog(syslog.LOG_ERR, "Unable to get data from pump")
blynk.set_property(VPIN_STATUS, "color", BLYNK_RED)
#########################################################
#
# Function: upload_live_data()
# Description: Read live data from pump and upload it
# to the enabled cloud services
# This runs once at startup and then as a
# periodic timer every 5min
#
#########################################################
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
##########################################################
# Setup
##########################################################
# read configuration parameters
if read_config(CONFIG_FILE) == False:
sys.exit()
blynk_enabled = (read_config.blynk_token != "") and (read_config.blynk_server != "")
nightscout_enabled = (read_config.nightscout_server != "") and (read_config.nightscout_api_secret != "")