Java Code Examples for com.eveningoutpost.dexdrip.Models.Calibration#last()

The following examples show how to use com.eveningoutpost.dexdrip.Models.Calibration#last() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListenerService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static DataMap getCalibrations(long startTime) {
    Calibration last = Calibration.last();
    if (last != null) {
        Log.d(TAG, "getCalibrations last.timestamp:" +  JoH.dateTimeText(last.timestamp));
    }
    List<Calibration> graph = Calibration.latestForGraph(60, startTime, Long.MAX_VALUE);
    //calibrations = Calibration.latestForGraph(numValues, start - (3 * Constants.DAY_IN_MS), end);
    if (!graph.isEmpty()) {
        Log.d(TAG, "getCalibrations graph size=" + graph.size());
        final ArrayList<DataMap> dataMaps = new ArrayList<>(graph.size());
        DataMap entries = null;
        //if (last.slope_confidence != 0) entries = dataMapForWatchface(last);
        for (Calibration data : graph) {
            if (data.slope_confidence != 0) {
                if (entries == null) {
                    entries = dataMapForWatchface(data);
                    dataMaps.add(dataMapForWatchface(data));
                }
                else
                    dataMaps.add(dataMapForWatchface(data));
            }
        }
        if (entries != null) {
            entries.putDataMapArrayList("entries", dataMaps);
            Log.d(TAG, "getCalibrations entries=" + entries);
        }
        return entries;
    }
    else return null;
}
 
Example 2
Source File: ListenerService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static DataMap getCalibrations(long startTime) {
    Calibration last = Calibration.last();
    if (last != null) {
        Log.d(TAG, "getCalibrations last.timestamp:" +  JoH.dateTimeText(last.timestamp));
    }
    List<Calibration> graph = Calibration.latestForGraph(60, startTime, Long.MAX_VALUE);
    //calibrations = Calibration.latestForGraph(numValues, start - (3 * Constants.DAY_IN_MS), end);
    if (!graph.isEmpty()) {
        Log.d(TAG, "getCalibrations graph size=" + graph.size());
        final ArrayList<DataMap> dataMaps = new ArrayList<>(graph.size());
        DataMap entries = null;
        //if (last.slope_confidence != 0) entries = dataMapForWatchface(last);
        for (Calibration data : graph) {
            if (data.slope_confidence != 0) {
                if (entries == null) {
                    entries = dataMapForWatchface(data);
                    dataMaps.add(dataMapForWatchface(data));
                }
                else
                    dataMaps.add(dataMapForWatchface(data));
            }
        }
        if (entries != null) {
            entries.putDataMapArrayList("entries", dataMaps);
            Log.d(TAG, "getCalibrations entries=" + entries);
        }
        return entries;
    }
    else return null;
}
 
Example 3
Source File: XDripViewer.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
private void readCalData(String baseUrl, String key) {

        NsRestApiReader nsRestApiReader = new NsRestApiReader();
        Long LastReportedTime = 0L;

        Calibration lastCalibration = Calibration.last();

        if(lastCalibration != null) {
            LastReportedTime = (long)lastCalibration.timestamp;
        }
        Log.e(TAG, "readCalData  LastReportedTime = " + LastReportedTime);

        List<NightscoutMbg> nightscoutMbgs = nsRestApiReader.readCalDataFromNs(baseUrl, key, LastReportedTime, 10 );
        if(nightscoutMbgs == null) {
            Log.e(TAG, "readCalDataFromNs returned null");
            return;
        }

        ListIterator<NightscoutMbg> li = nightscoutMbgs.listIterator(nightscoutMbgs.size());
        long lastInserted = 0;
        while(li.hasPrevious()) {
            NightscoutMbg nightscoutMbg = li.previous();
            Log.e(TAG, "NightscoutMbg " + nightscoutMbg.mbg + " " + nightscoutMbg.date);
            if(nightscoutMbg.date < lastInserted) {
                Log.e(TAG, "not inserting calibratoin, since order is wrong. ");
                continue;
            }
            
            verifyViewerNightscoutMode(mContext, nightscoutMbg);
            
            Calibration.createUpdate(nightscoutMbg.xDrip_sensor_uuid, nightscoutMbg.mbg, nightscoutMbg.date, nightscoutMbg.xDrip_intercept, nightscoutMbg.xDrip_slope, nightscoutMbg.xDrip_estimate_raw_at_time_of_calibration,
                    nightscoutMbg.xDrip_slope_confidence , nightscoutMbg.xDrip_sensor_confidence, nightscoutMbg.xDrip_raw_timestamp);
            lastInserted = nightscoutMbg.date;
        }
    }
 
Example 4
Source File: WixelReader.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
@Override
  public void readData()
  {
      if(!WixelReader.IsConfigured(mContext)) {
          return;
      }
      Long LastReportedTime = 0L;
  	TransmitterData lastTransmitterData = TransmitterData.last();
  	if(lastTransmitterData != null) {
  	    LastReportedTime = lastTransmitterData.timestamp;

          // jamorham fix to avoid going twice to network when we just got a packet
          if ((new Date().getTime() - LastReportedTime) < DEXCOM_PERIOD-2000) {
          Log.d(TAG, "Already have a recent packet - returning");
          return;
          }


      }
  	Long startReadTime = LastReportedTime;

  	TransmitterRawData LastReportedReading = null;
  	Log.d(TAG, "Starting... LastReportedReading " + LastReportedReading);
  	// try to read one object...
      TransmitterRawData[] LastReadingArr = null;

      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
      String recieversIpAddresses = prefs.getString("wifi_recievers_addresses", "");
      
      // How many packets should we read? we look at the maximum time between last calibration and last reading time
      // and calculate how much are needed.
      
      Calibration lastCalibration = Calibration.last();
      if(lastCalibration != null) {
          startReadTime = Math.max(startReadTime, (long)(lastCalibration.timestamp));
      }
      Long gapTime = new Date().getTime() - startReadTime + 120000;
      int packetsToRead = (int) (gapTime / (5 * 60000));
      packetsToRead = Math.min(packetsToRead, 200); // don't read too much, but always read 1.
      packetsToRead = Math.max(packetsToRead, 1); 
      
      Log.d(TAG,"reading " + packetsToRead + " packets");
LastReadingArr = Read(recieversIpAddresses ,packetsToRead);

if (LastReadingArr == null || LastReadingArr.length  == 0) {
    return;
}

for(TransmitterRawData LastReading : LastReadingArr ) {
  		// Last in the array is the most updated reading we have.
  		//TransmitterRawData LastReading = LastReadingArr[LastReadingArr.length -1];
    

  		//if (LastReading.CaptureDateTime > LastReportedReading + 5000) {
  		// Make sure we do not report packets from the far future...
  		if ((LastReading.CaptureDateTime > LastReportedTime + 120000 ) &&
  		        (!almostEquals(LastReading, LastReportedReading)) &&
  		        LastReading.CaptureDateTime < new Date().getTime() + 120000) {
  			// We have a real new reading...
  			Log.d(TAG, "calling setSerialDataToTransmitterRawData " + LastReading.RawValue +
  			        " LastReading.CaptureDateTime " + LastReading.CaptureDateTime + " " + LastReading.TransmissionId);
  			setSerialDataToTransmitterRawData(LastReading.RawValue,  LastReading.FilteredValue, LastReading.BatteryLife, LastReading.CaptureDateTime);
  			LastReportedReading = LastReading;
  			LastReportedTime = LastReading.CaptureDateTime;
  		}
  	}
  }
 
Example 5
Source File: BgSendQueue.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public static void handleNewBgReading(BgReading bgReading, String operation_type, Context context) {
    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "sendQueue");
    wakeLock.acquire();
    try {
    	
   		addToQueue(bgReading, operation_type);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

        Intent updateIntent = new Intent(Intents.ACTION_NEW_BG_ESTIMATE_NO_DATA);
        context.sendBroadcast(updateIntent);

        if(AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, xDripWidget.class)).length > 0){
            context.startService(new Intent(context, WidgetUpdateService.class));
        }


        if (prefs.getBoolean("broadcast_data_through_intents", false)) {

            //prepare data
            double calculated_value = bgReading.calculated_value;
            boolean hide_slope = bgReading.hide_slope;
            String slopeName = hide_slope?null:bgReading.slopeName();
            int batteryLevel = getBatteryLevel(context);
            final long timestamp = bgReading.timestamp;
            Calibration cal = Calibration.last();
            double raw = NightscoutUploader.getNightscoutRaw(bgReading, cal);
            double slope = BgReading.currentSlope();

            //send broadcast
            BgEstimateBroadcaster.broadcastBgEstimate(calculated_value, raw, timestamp, slope, slopeName, batteryLevel, context);

            //just keep it alive for 3 more seconds to allow the watch to be updated
            // TODO: change NightWatch to not allow the system to sleep.
            powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "broadcastNightWatch").acquire(3000);

        }

        // send to wear
        if (prefs.getBoolean("wear_sync", false)) {

            /*By integrating the watch part of Nightwatch we inherited the same wakelock
                problems NW had - so adding the same quick fix for now.
                TODO: properly "wakelock" the wear (and probably pebble) services
             */
            context.startService(new Intent(context, WatchUpdaterService.class));
            powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "quickFix3").acquire(15000);
        }

        // send to pebble
        if(prefs.getBoolean("broadcast_to_pebble", false)) {
            context.startService(new Intent(context, PebbleSync.class));
        }



        if (prefs.getBoolean("share_upload", false)) {
            Log.d("ShareRest", "About to call ShareRest!!");
            String receiverSn = prefs.getString("share_key", "SM00000000").toUpperCase();
            BgUploader bgUploader = new BgUploader(context);
            bgUploader.upload(new ShareUploadPayload(receiverSn, bgReading));
        }
        context.startService(new Intent(context, SyncService.class));

        //Text to speech
        Log.d("BgToSpeech", "gonna call speak");
        BgToSpeech.speak(bgReading.calculated_value, bgReading.timestamp);


    } finally {
        wakeLock.release();
    }
}
 
Example 6
Source File: Home.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
public static String extraStatusLine(SharedPreferences prefs) {
    StringBuilder extraline = new StringBuilder();
    Calibration lastCalibration = Calibration.last();
    if (prefs.getBoolean("status_line_calibration_long", true) && lastCalibration != null){
        if(extraline.length()!=0) extraline.append(' ');
        extraline.append("slope = ");
        extraline.append(String.format("%.2f",lastCalibration.slope));
        extraline.append(' ');
        extraline.append("inter = ");
        extraline.append(String.format("%.2f", lastCalibration.intercept));
    }

    if(prefs.getBoolean("status_line_calibration_short", false) && lastCalibration != null) {
        if(extraline.length()!=0) extraline.append(' ');
        extraline.append("s:");
        extraline.append(String.format("%.2f",lastCalibration.slope));
        extraline.append(' ');
        extraline.append("i:");
        extraline.append(String.format("%.2f", lastCalibration.intercept));
    }

    if(prefs.getBoolean("status_line_avg", false)
            || prefs.getBoolean("status_line_a1c_dcct", false)
            || prefs.getBoolean("status_line_a1c_ifcc", false
            || prefs.getBoolean("status_line_in", false))
            || prefs.getBoolean("status_line_high", false)
            || prefs.getBoolean("status_line_low", false)){

        StatsResult statsResult = new StatsResult(prefs);

        if(prefs.getBoolean("status_line_avg", false)) {
            if(extraline.length()!=0) extraline.append(' ');
            extraline.append(statsResult.getAverageUnitised());
        }
        if(prefs.getBoolean("status_line_a1c_dcct", false)) {
            if(extraline.length()!=0) extraline.append(' ');
            extraline.append(statsResult.getA1cDCCT());
        }
        if(prefs.getBoolean("status_line_a1c_ifcc", false)) {
            if(extraline.length()!=0) extraline.append(' ');
            extraline.append(statsResult.getA1cIFCC());
        }
        if(prefs.getBoolean("status_line_in", false)) {
            if(extraline.length()!=0) extraline.append(' ');
            extraline.append(statsResult.getInPercentage());
        }
        if(prefs.getBoolean("status_line_high", false)) {
            if(extraline.length()!=0) extraline.append(' ');
            extraline.append(statsResult.getHighPercentage());
        }
        if(prefs.getBoolean("status_line_low", false)) {
            if(extraline.length()!=0) extraline.append(' ');
            extraline.append(statsResult.getLowPercentage());
        }
    }
    if(prefs.getBoolean("status_line_time", false)) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        if(extraline.length()!=0) extraline.append(' ');
        extraline.append(sdf.format(new Date()));
    }
    return extraline.toString();
}
 
Example 7
Source File: CalibrationGraph.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public void setupCharts() {
    chart = (LineChartView) findViewById(R.id.chart);
    List<Line> lines = new ArrayList<Line>();

    Calibration calibration = Calibration.last();
    if(calibration != null) {
        //set header
        DecimalFormat df = new DecimalFormat("#");
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        String Header = "slope = " + df.format(calibration.slope) + " intercept = " + df.format(calibration.intercept);
        GraphHeader.setText(Header);

        //red line
        List<PointValue> lineValues = new ArrayList<PointValue>();
        lineValues.add(new PointValue((float) start_x, (float) (start_x * calibration.slope + calibration.intercept)));
        lineValues.add(new PointValue((float) end_x, (float) (end_x * calibration.slope + calibration.intercept)));
        Line calibrationLine = new Line(lineValues);
        calibrationLine.setColor(ChartUtils.COLOR_RED);
        calibrationLine.setHasLines(true);
        calibrationLine.setHasPoints(false);

        //calibration values
        List<Calibration> calibrations = Calibration.allForSensor();
        Line greyLine = getCalibrationsLine(calibrations, Color.parseColor("#66FFFFFF"));
        calibrations = Calibration.allForSensorInLastFourDays();
        Line blueLine = getCalibrationsLine(calibrations, ChartUtils.COLOR_BLUE);

        //add lines in order
        lines.add(greyLine);
        lines.add(blueLine);
        lines.add(calibrationLine);

    }
    Axis axisX = new Axis();
    Axis axisY = new Axis().setHasLines(true);
    axisX.setName("Raw Value");
    axisY.setName("BG");


    data = new LineChartData(lines);
    data.setAxisXBottom(axisX);
    data.setAxisYLeft(axisY);
    chart.setLineChartData(data);

}