Ejemplo n.º 1
0
    def __init__(self):
        #print "BGReadings_Data initialisiert"
        self._id = 0
        self.timestamp = 0
        self.DateTime = ''
        self.bg = 0
        self.raw_value = 0
        self.raw_timestamp = 0
        self.age_adjusted_raw_value = 0
        self.filtered_value = 0
        self.sensor_age_at_time_of_estimation = 0
        self.possible_bad = False
        self.slope = 0
        self.intercept = 0
        self.sensor_confidence = 0
        self.uploaded = 0
        self.a = 0
        self.b = 0
        self.c = 0
        self.ra = 0
        self.rb = 0
        self.rc = 0

        self.calculated_value = 0

        # print "Klasse BGReadings wurde erzeugt"
        if sensor.SensorisActive():
            SensorData = sensor.currentSensor()
            self.sensor = SensorData['_id']
        else:
            print "Kein Aktiver Sensor vorhanden "
	def __init__(self):
		#print "BGReadings_Data initialisiert"
		self._id=0
		self.timestamp=0
		self.DateTime=''
		self.bg=0 
		self.raw_value=0 
		self.raw_timestamp=0 
		self.age_adjusted_raw_value=0
		self.filtered_value=0 
		self.sensor_age_at_time_of_estimation=0 
		self.possible_bad=False 
		self.slope=0 
		self.intercept=0  
		self.sensor_confidence=0 
		self.uploaded=0
		self.a=0  
		self.b=0 
		self.c=0
		self.ra=0  
		self.rb=0 
		self.rc=0
		
		self.calculated_value=0
		
		# print "Klasse BGReadings wurde erzeugt"
		if sensor.SensorisActive():
			SensorData=sensor.currentSensor()
			self.sensor=SensorData['_id']
		else:
			print "Kein Aktiver Sensor vorhanden "
Ejemplo n.º 3
0
def oldinsertIntoWixeldata_(data) :
	
	if sensor.SensorisActive():
		CurSensor=sensor.currentSensor()
		print "CurSensor->" + str(CurSensor['started_at'])
		TimeDelta=((long(data['CaptureDateTime'])-long(CurSensor['started_at']))*1.0)/1000/60/60
		Adjusted_raw=xdriplib.calculateAgeAdjustedRawValue(TimeDelta,int(data['RawValue']))
		print "BGReadings AgeAdjustedRaw 		-> " + str(Adjusted_raw)
		Adjusted_raw=xdriplib.calculateAgeAdjustedRawValue(TimeDelta,int(data['RawValue']))
	else:
		print "No Sensor Active"
		Adjusted_raw=0
		
	conn = sqlite3.connect(db.openapsDBName)
	sql='insert into ' + db.tableNameWixeldata
	sql+='(TransmitterId, CaptureDateTime, RelativeTime, ReceivedSignalStrength, RawValue, TransmissionId, BatteryLife, UploadAttempts, Uploaded, UploaderBatteryLife, FilteredValue, age_adjusted_raw_value ) VALUES ('
	sql+="  '" + str(data['TransmitterId']) + "'"
	sql+=', ' + str(data['CaptureDateTime']) 
	sql+=', ' + str(data['RelativeTime']) 
	sql+=', ' + str(data['ReceivedSignalStrength'])
	sql+=', ' + str(data['RawValue']) 
	sql+=', ' + str(data['TransmissionId']) 
	sql+=', ' + str(data['BatteryLife']) 
	sql+=', ' + str(data['UploadAttempts']) 
	sql+=', ' + str(data['Uploaded']) 
	sql+=', ' + str(data['UploaderBatteryLife']) 
	sql+=', ' + str(Adjusted_raw)
	sql+=', ' + str(data['FilteredValue']) + ' )'
	#print "(BGReadings)(insertIntoWixel)  SQL->" + sql
	conn.execute(sql)
	conn.commit()
	print "Records created successfully";
	conn.close()
	def __init__(self):
		self._id=0
		self.uuid='uid'
		self.sensor_uuid='SensorUID'
		self.timestamp=0
		self.sensor_age_at_time_of_estimation=0 
		self.sensorid=0
		self.SensorData=sensor.SensorData
		self.bg=0
		self.filtered_value=0
		self.raw_timestamp=0
		self.raw_value=0
		self.age_adjusted_raw_value=0
		
		self.sensor_confidence=0
		self.slope_confidence=0
		self.slope=0
		self.intercept=0
		
		self.distance_from_estimate=0
		self.estimate_raw_at_time_of_calibration=0
		self.estimate_bg_at_time_of_calibration=0
		self.possible_bad=False
		
		self.first_decay=0
		self.second_decay=0
		self.first_slope=0
		self.second_slope=0
		self.first_intercept=0
		self.second_intercept=0
		self.first_scale=0
		self.second_scale=0
		self.check_in=False
		self.uploaded=0


		#print "Klasse " + " wurde erzeugt"
		if sensor.SensorisActive():
			SensorData=sensor.currentSensor()
			self.sensorid=SensorData['_id']
			#print "Aktiver Sensor vorhanden _id->" + str(self.sensor)
		else:
			print "Kein Aktiver Sensor vorhanden "
Ejemplo n.º 5
0
    def __init__(self):
        self._id = 0
        self.uuid = 'uid'
        self.sensor_uuid = 'SensorUID'
        self.timestamp = 0
        self.sensor_age_at_time_of_estimation = 0
        self.sensorid = 0
        self.SensorData = sensor.SensorData
        self.bg = 0
        self.filtered_value = 0
        self.raw_timestamp = 0
        self.raw_value = 0
        self.age_adjusted_raw_value = 0

        self.sensor_confidence = 0
        self.slope_confidence = 0
        self.slope = 0
        self.intercept = 0

        self.distance_from_estimate = 0
        self.estimate_raw_at_time_of_calibration = 0
        self.estimate_bg_at_time_of_calibration = 0
        self.possible_bad = False

        self.first_decay = 0
        self.second_decay = 0
        self.first_slope = 0
        self.second_slope = 0
        self.first_intercept = 0
        self.second_intercept = 0
        self.first_scale = 0
        self.second_scale = 0
        self.check_in = False
        self.uploaded = 0

        #print "Klasse " + " wurde erzeugt"
        if sensor.SensorisActive():
            SensorData = sensor.currentSensor()
            self.sensorid = SensorData['_id']
            #print "Aktiver Sensor vorhanden _id->" + str(self.sensor)
        else:
            print "Kein Aktiver Sensor vorhanden "
Ejemplo n.º 6
0
def oldinsertIntoWixeldata_(data):

    if sensor.SensorisActive():
        CurSensor = sensor.currentSensor()
        print "CurSensor->" + str(CurSensor['started_at'])
        TimeDelta = (
            (long(data['CaptureDateTime']) - long(CurSensor['started_at'])) *
            1.0) / 1000 / 60 / 60
        Adjusted_raw = xdriplib.calculateAgeAdjustedRawValue(
            TimeDelta, int(data['RawValue']))
        print "BGReadings AgeAdjustedRaw 		-> " + str(Adjusted_raw)
        Adjusted_raw = xdriplib.calculateAgeAdjustedRawValue(
            TimeDelta, int(data['RawValue']))
    else:
        print "No Sensor Active"
        Adjusted_raw = 0

    conn = sqlite3.connect(db.openapsDBName)
    sql = 'insert into ' + db.tableNameWixeldata
    sql += '(TransmitterId, CaptureDateTime, RelativeTime, ReceivedSignalStrength, RawValue, TransmissionId, BatteryLife, UploadAttempts, Uploaded, UploaderBatteryLife, FilteredValue, age_adjusted_raw_value ) VALUES ('
    sql += "  '" + str(data['TransmitterId']) + "'"
    sql += ', ' + str(data['CaptureDateTime'])
    sql += ', ' + str(data['RelativeTime'])
    sql += ', ' + str(data['ReceivedSignalStrength'])
    sql += ', ' + str(data['RawValue'])
    sql += ', ' + str(data['TransmissionId'])
    sql += ', ' + str(data['BatteryLife'])
    sql += ', ' + str(data['UploadAttempts'])
    sql += ', ' + str(data['Uploaded'])
    sql += ', ' + str(data['UploaderBatteryLife'])
    sql += ', ' + str(Adjusted_raw)
    sql += ', ' + str(data['FilteredValue']) + ' )'
    #print "(BGReadings)(insertIntoWixel)  SQL->" + sql
    conn.execute(sql)
    conn.commit()
    print "Records created successfully"
    conn.close()
import os
from thread import *
from struct import *

import serial

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

import Image
import ImageFont
import ImageDraw
import wixellib
import xdriplib
import BGReadings
import db
import sensor


from calibration import *


db.initDB()


ID=sensor.StartSensor()


if sensor.SensorisActive():
	CurSensor=sensor.currentSensor()
Ejemplo n.º 8
0
#!/usr/bin/python

import json
import socket
import sys
import time
import os
from thread import *
from struct import *

import serial

import wixellib
import xdriplib
import BGReadings
import db
import sensor

from calibration import *

db.initDB()

sensor.StopSensor()

if sensor.SensorisActive():
    CurSensor = sensor.currentSensor()
Ejemplo n.º 9
0
def initialCalibration(bg1, bg2):

    # calib = calibration_Data()
    # calib.clear_all_existing_calibrations()

    higherCalibration = calibration_Data()
    lowerCalibration = calibration_Data()
    sens = sensor.currentSensor()

    higher_bg = max(bg1, bg2)
    lower_bg = min(bg1, bg2)

    BgReading1 = BGReadings_Data()
    BgReading1.getlatest()
    BgReading2 = BGReadings_Data()
    BgReading2.getsecondlatest()

    if BgReading2.raw_value == None:
        print "(initialCalibration) Hmm benoetige ersteinmal 2 BG Readings"
        return False

    if BgReading1.raw_value > BgReading2.raw_value:
        highBgReading = BGReadings_Data()
        highBgReading.getlatest()

        lowBgReading = BGReadings_Data()
        lowBgReading.getsecondlatest()
    else:
        highBgReading = BGReadings_Data()
        highBgReading.getsecondlatest()

        lowBgReading = BGReadings_Data()
        lowBgReading.getlatest()

    print "highBgReading.raw_value -> " + str(highBgReading.raw_value)
    print "lowBgReading.raw_value -> " + str(lowBgReading.raw_value)

    higherCalibration.bg = higher_bg
    higherCalibration.slope = 1
    higherCalibration.intercept = higher_bg

    print "cal high intercept " + str(higherCalibration.intercept)
    print "cal high adjraw    " + str(highBgReading.age_adjusted_raw_value)
    print "cal high higherbg  " + str(higher_bg)

    higherCalibration.sensor = sens
    higherCalibration.estimate_raw_at_time_of_calibration = highBgReading.age_adjusted_raw_value
    higherCalibration.age_adjusted_raw_value = highBgReading.age_adjusted_raw_value
    higherCalibration.raw_value = highBgReading.raw_value
    higherCalibration.raw_timestamp = highBgReading.raw_timestamp
    higherCalibration.save()

    highBgReading.bg = higher_bg
    highBgReading.calibration_flag = True
    highBgReading.calibration = higherCalibration
    higherCalibration.save()

    lowerCalibration.bg = lower_bg
    lowerCalibration.slope = 1

    lowerCalibration.intercept = lower_bg
    print "cal low intercept " + str(lowerCalibration.intercept)
    print "cal low adjraw    " + str(lowBgReading.age_adjusted_raw_value)
    print "cal low higherbg  " + str(lower_bg)

    lowerCalibration.sensor = sens
    lowerCalibration.estimate_raw_at_time_of_calibration = lowBgReading.age_adjusted_raw_value
    lowerCalibration.age_adjusted_raw_value = lowBgReading.age_adjusted_raw_value
    lowerCalibration.raw_value = lowBgReading.raw_value
    lowerCalibration.raw_timestamp = lowBgReading.raw_timestamp

    lowBgReading.bg = lower_bg
    lowBgReading.calibration_flag = True
    lowBgReading.calibration = lowerCalibration
    lowerCalibration.save()

    # lowBgReading.timestamp=long(str(int(time.time()))+"000")
    lowBgReading.sensor_uuid = "Sensor_uuid"
    lowBgReading.slope_confidence = 0.5
    lowBgReading.distance_from_estimate = 0
    lowBgReading.check_in = False
    lowBgReading.sensor_confidence = (
        (-0.0018 * lowBgReading.bg * lowBgReading.bg) + (0.6657 * lowBgReading.bg) + 36.7505
    ) / 100
    lowBgReading.sensor_age_at_time_of_estimation = lowBgReading.timestamp - sens["started_at"]
    lowBgReading.uuid = "randomUUID"
    lowBgReading.write2db()

    # highBgReading.timestamp=long(str(int(time.time()))+"000")
    highBgReading.sensor_uuid = "Sensor_uuid"
    highBgReading.slope_confidence = 0.5
    highBgReading.distance_from_estimate = 0
    highBgReading.check_in = False
    highBgReading.sensor_confidence = (
        (-0.0018 * highBgReading.bg * highBgReading.bg) + (0.6657 * highBgReading.bg) + 36.7505
    ) / 100
    highBgReading.sensor_age_at_time_of_estimation = highBgReading.timestamp - sens["started_at"]
    highBgReading.uuid = "randomUUID"
    highBgReading.write2db()

    find_new_curve()
    find_new_raw_curve()

    # highBgReading.find_new_raw_curve();
    # lowBgReading.find_new_curve();
    # lowBgReading.find_new_raw_curve();

    lowerCalibration.timestamp = long(str(int(time.time())) + "000")
    lowerCalibration.sensor_uuid = sens["uuid"]
    lowerCalibration.slope_confidence = 0.5
    lowerCalibration.distance_from_estimate = 0
    lowerCalibration.check_in = False
    lowerCalibration.sensor_confidence = (
        (-0.0018 * lowerCalibration.bg * lowerCalibration.bg) + (0.6657 * lowerCalibration.bg) + 36.7505
    ) / 100
    lowerCalibration.sensor_age_at_time_of_estimation = lowerCalibration.timestamp - sens["started_at"]
    lowerCalibration.uuid = "CALIBRATION UUID"
    # 	lowerCalibration.calculate_w_l_s()
    lowerCalibration.save()

    higherCalibration.timestamp = long(str(int(time.time())) + "000")
    higherCalibration.sensor_uuid = sens["uuid"]
    higherCalibration.slope_confidence = 0.5
    higherCalibration.distance_from_estimate = 0
    higherCalibration.check_in = False
    higherCalibration.sensor_confidence = (
        (-0.0018 * higherCalibration.bg * higherCalibration.bg) + (0.6657 * higherCalibration.bg) + 36.7505
    ) / 100
    higherCalibration.sensor_age_at_time_of_estimation = higherCalibration.timestamp - sens["started_at"]
    higherCalibration.uuid = "CALIBRATION UUID"
    higherCalibration.save()

    calculate_w_l_s()
Ejemplo n.º 10
0
def create(bg):

    calibration = calibration_Data()
    # sens = sensor();
    bgReading = BGReadings_Data()
    bgReadingsec = BGReadings_Data()

    if sensor.SensorisActive():
        sens = sensor.currentSensor()
        bgReading.getlatest()
        bgReadingsec.getsecondlatest()
        if bgReading._id <> 0:
            calibration.sensor = sensor
            calibration.bg = bg
            calibration.check_in = False
            calibration.timestamp = long(str(int(time.time())) + "000")
            calibration.raw_value = bgReading.raw_value
            calibration.age_adjusted_raw_value = bgReading.age_adjusted_raw_value
            calibration.sensor_uuid = "Sensor_uuid"

            print "(create) bgReading.bg    -> " + str(bgReading.bg)
            print "(create) bgReadingsec.bg -> " + str(bgReadingsec.bg)
            print "(create) abs(bgReading.bg - bgReadingsec.bg) -> " + str(abs(bgReading.bg - bgReadingsec.bg))

            calibration.slope_confidence = min(max(((4 - (abs(bgReading.bg - bgReadingsec.bg) * 60000)) / 4), 0), 1)

            print "(create) calibration.slope_confidence  -> " + str(calibration.slope_confidence)
            calibration.slope_confidence = (100 - abs(bgReading.bg - bgReadingsec.bg)) / 100
            print "(create) calibration.slope_confidence  -> " + str(calibration.slope_confidence)

            estimated_raw_bg_value = estimated_raw_bg(long(str(int(time.time())) + "000"))

            calibration.raw_timestamp = bgReading.timestamp

            print "(create) estimated_raw_bg_value ->" + str(estimated_raw_bg_value)
            print "(create) bgReading.age_adjusted_raw_value ->" + str(bgReading.age_adjusted_raw_value)

            if abs(estimated_raw_bg_value - bgReading.age_adjusted_raw_value) > 20:
                print "create(bg) bgReading.age_adjusted_raw_value ->" + str(bgReading.age_adjusted_raw_value)
                calibration.estimate_raw_at_time_of_calibration = bgReading.age_adjusted_raw_value
            else:
                print "create(bg)estimated_raw_bg_value ->" + str(estimated_raw_bg_value)
                calibration.estimate_raw_at_time_of_calibration = estimated_raw_bg_value

            calibration.distance_from_estimate = abs(calibration.bg - bgReading.bg)
            calibration.sensor_confidence = max(((-0.0018 * bg * bg) + (0.6657 * bg) + 36.7505) / 100, 0)
            calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sens["started_at"]
            calibration.uuid = "CalUUI"
            calibration.save()

            bgReading.calibration = calibration
            bgReading.calibration_flag = True
            bgReading.write2db()
            # BgSendQueue.handleNewBgReading(bgReading, "update", context);

            calculate_w_l_s()
            # adjustRecentBgReadings();
            # CalibrationSendQueue.addToQueue(calibration, context);
            # context.startService(new Intent(context, Notifications.class));
            # Calibration.requestCalibrationIfRangeTooNarrow();
    else:
        print "CALIBRATION", "No sensor, cant save!"
Ejemplo n.º 11
0
def find_new_raw_curve():
    CurSensor = sensor.currentSensor()
    first_latest = BGReadings_Data()
    first_latest.getlatest()

    second_latest = BGReadings_Data()
    second_latest.getsecondlatest()

    third_latest = BGReadings_Data()
    third_latest.getthirdlatest()

    if third_latest.raw_value <> 0:
        y3 = 1.0 * calculateAgeAdjustedRawValue(getTimeDelta(first_latest, CurSensor), first_latest.raw_value) / 1000
        x3 = first_latest.timestamp
        y2 = calculateAgeAdjustedRawValue(getTimeDelta(second_latest, CurSensor), second_latest.raw_value) / 1000
        x2 = second_latest.timestamp
        y1 = calculateAgeAdjustedRawValue(getTimeDelta(third_latest, CurSensor), third_latest.raw_value) / 1000
        x1 = third_latest.timestamp
        print "x1->" + str(x1) + ", y1->" + str(y1) + ", "
        print "x2->" + str(x2) + ", y2->" + str(y2) + ", "
        print "x3->" + str(x3) + ", y3->" + str(y3) + ", "

        first_latest.ra = y1 / ((x1 - x2) * (x1 - x3)) + y2 / ((x2 - x1) * (x2 - x3)) + y3 / ((x3 - x1) * (x3 - x2))
        first_latest.rb = (
            -y1 * (x2 + x3) / ((x1 - x2) * (x1 - x3))
            - y2 * (x1 + x3) / ((x2 - x1) * (x2 - x3))
            - y3 * (x1 + x2) / ((x3 - x1) * (x3 - x2))
        )
        first_latest.rc = (
            y1 * x2 * x3 / ((x1 - x2) * (x1 - x3))
            + y2 * x1 * x3 / ((x2 - x1) * (x2 - x3))
            + y3 * x1 * x2 / ((x3 - x1) * (x3 - x2))
        )

        print "RAW PARABOLIC RATES: " + str(first_latest.ra) + "x^2 + " + str(first_latest.rb) + "x + " + str(
            first_latest.rc
        )
        first_latest.write2db()

    elif second_latest.raw_value <> 0:
        y2 = calculateAgeAdjustedRawValue(getTimeDelta(first_latest, CurSensor), first_latest.raw_value)
        x2 = first_latest.timestamp
        y1 = calculateAgeAdjustedRawValue(getTimeDelta(second_latest, CurSensor), second_latest.raw_value)
        x1 = second_latest.timestamp

        if y1 == y2:
            first_latest.rb = 0
        else:
            first_latest.rb = (y2 - y1) / (x2 - x1)

        first_latest.ra = 0
        first_latest.rc = -1 * ((first_latest.rb * x1) - y1)

        print "Not enough data to calculate parabolic rates - assume Linear data"
        print "RAW PARABOLIC RATES: " + str(first_latest.ra) + "x^2 + " + str(first_latest.rb) + "x + " + str(
            first_latest.rc
        )
        first_latest.write2db()
    else:
        first_latest.ra = 0
        first_latest.rb = 0
        first_latest.rc = first_latest.age_adjusted_raw_value
        print "Not enough data to calculate parabolic rates - assume static data"
        print "RAW PARABOLIC RATES: " + str(first_latest.ra) + "x^2 + " + str(first_latest.rb) + "x + " + str(
            first_latest.rc
        )
        first_latest.write2db()