text
stringlengths
1
93.6k
#########################################################
def on_sigterm(signum, frame):
try:
if blynk != None:
blynk.disconnect()
except:
pass
try:
if cycleTimer != None:
cycleTimer.cancel()
except:
pass
syslog.syslog(syslog.LOG_NOTICE, "Exiting DD-Guard daemon")
sys.exit()
#########################################################
#
# Function: blynk_upload()
# Description: Blynk uploader
#
#########################################################
def blynk_upload(data):
global lastBolusTime
global cycleCount
if data != None:
print("Uploading data to Blynk")
# Send sensor data
if data["sensorBGL"] in sensor_exception_codes:
# Sensor exception occured
# BGL gauge
blynk.virtual_write(VPIN_SENSOR, None)
blynk.set_property(VPIN_SENSOR, "color", BLYNK_WHITE)
# Trend and active insulin
blynk.virtual_write(VPIN_ARROWS, "--"+" / "+str(data["activeInsulin"]))
# Status line
blynk.virtual_write(VPIN_STATUS, datetime.datetime.now().strftime("%H:%M")+" - "+sensor_exception_codes[data["sensorBGL"]])
blynk.set_property(VPIN_STATUS, "color", BLYNK_RED)
else:
# Regular BGL data
# BLG gauge
blynk.virtual_write(VPIN_SENSOR, data["sensorBGL"])
if data["pumpAlert"]["alertSuspend"] or data["pumpAlert"]["alertSuspendLow"]:
blynk.set_property(VPIN_SENSOR, "color", BLYNK_BLUE)
elif data["sensorBGL"] < read_config.bgl_low_val or data["sensorBGL"] > read_config.bgl_high_val or \
data["pumpAlert"]["alertOnLow"] or data["pumpAlert"]["alertOnHigh"]:
blynk.set_property(VPIN_SENSOR, "color", BLYNK_RED)
elif data["sensorBGL"] < read_config.bgl_pre_low_val or data["sensorBGL"] > read_config.bgl_pre_high_val or \
data["pumpAlert"]["alertBeforeLow"] or data["pumpAlert"]["alertBeforeHigh"]:
blynk.set_property(VPIN_SENSOR, "color", BLYNK_YELLOW)
else:
blynk.set_property(VPIN_SENSOR, "color", BLYNK_GREEN)
# Trend and active insulin
blynk.virtual_write(VPIN_ARROWS, str(data["trendArrow"])+" / "+str(data["activeInsulin"]))
# Status line
calTime = "Cal at {0}".format((data["sensorBGLTimestamp"] + datetime.timedelta(minutes=data["sensorCalMinutesRemaining"])).strftime("%H:%M"))
blynk.virtual_write(VPIN_STATUS, "Updated "+data["sensorBGLTimestamp"].strftime("%H:%M")+" - "+calTime)
blynk.set_property(VPIN_STATUS, "color", BLYNK_GREEN)
# Send pump data
# Battery bar
# Alternate pump and sensor battery
if cycleCount%2 == 0:
data_batt = data["batteryLevelPercentage"]
label_batt = "PUMP BATTERY %"
else:
data_batt = data["sensorBatteryLevelPercentage"]
label_batt = "SENSOR BATTERY %"
blynk.set_property(VPIN_BATTERY, "label", label_batt)
blynk.virtual_write(VPIN_BATTERY, data_batt)
if data_batt <= 25:
blynk.set_property(VPIN_BATTERY, "color", BLYNK_RED)
elif data_batt <= 50:
blynk.set_property(VPIN_BATTERY, "color", BLYNK_YELLOW)
else:
blynk.set_property(VPIN_BATTERY, "color", BLYNK_GREEN)
# Reservoir bar
blynk.virtual_write(VPIN_UNITS, int(round(data["insulinUnitsRemaining"])))
if data["insulinUnitsRemaining"] <= 25:
blynk.set_property(VPIN_UNITS, "color", BLYNK_RED)
elif data["insulinUnitsRemaining"] <= 75:
blynk.set_property(VPIN_UNITS, "color", BLYNK_YELLOW)
else:
blynk.set_property(VPIN_UNITS, "color", BLYNK_GREEN)
# Active insulin / last bolus graph
if int(data["lastBolusTime"].strftime("%s")) != lastBolusTime:
print("Bolus time changed")
lastBolusTime = int(data["lastBolusTime"].strftime("%s"))