Search in sources :

Example 31 with Sensor

use of com.eveningoutpost.dexdrip.Models.Sensor in project xDrip-plus by jamorham.

the class DexCollectionService method processNewTransmitterData.

private synchronized void processNewTransmitterData(TransmitterData transmitterData, long timestamp) {
    if (transmitterData == null) {
        return;
    }
    final Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.i(TAG, "setSerialDataToTransmitterRawData: No Active Sensor, Data only stored in Transmitter Data");
        return;
    }
    if (use_transmiter_pl_bluetooth && (transmitterData.raw_data == 100000)) {
        Log.wtf(TAG, "Ignoring probably erroneous Transmiter_PL data: " + transmitterData.raw_data);
        return;
    }
    // sensor.latest_battery_level = (sensor.latest_battery_level != 0) ? Math.min(sensor.latest_battery_level, transmitterData.sensor_battery_level) : transmitterData.sensor_battery_level;
    // allow level to go up and down
    sensor.latest_battery_level = transmitterData.sensor_battery_level;
    sensor.save();
    last_transmitter_Data = transmitterData;
    Log.d(TAG, "BgReading.create: new BG reading at " + timestamp + " with a timestamp of " + transmitterData.timestamp);
    BgReading.create(transmitterData.raw_data, transmitterData.filtered_data, this, transmitterData.timestamp);
}
Also used : Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Example 32 with Sensor

use of com.eveningoutpost.dexdrip.Models.Sensor in project xDrip-plus by jamorham.

the class Notifications method notificationSetter.

/*
 * *****************************************************************************************************************
 */
// returns weather unclear bg reading was detected
private boolean notificationSetter(Context context) {
    ReadPerfs(context);
    final long end = System.currentTimeMillis() + (60000 * 5);
    final long start = end - (60000 * 60 * 3) - (60000 * 10);
    BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context, start, end);
    // BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context);
    if (bg_ongoing && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)) {
        bgOngoingNotification(bgGraphBuilder);
    }
    if (prefs.getLong("alerts_disabled_until", 0) > new Date().getTime()) {
        Log.d("NOTIFICATIONS", "Notifications are currently disabled!!");
        return false;
    }
    boolean unclearReading = BgReading.getAndRaiseUnclearReading(context);
    if (unclearReading) {
        AlertPlayer.getPlayer().stopAlert(context, false, true);
    } else {
        FileBasedNotifications(context);
        BgReading.checkForDropAllert(context);
        BgReading.checkForRisingAllert(context);
    }
    // TODO: Add this alerts as well to depend on unclear sensor reading.
    BgReading.checkForPersistentHigh();
    // KS evaluateLowPredictionAlarm();
    // KS reportNoiseChanges();
    Sensor sensor = Sensor.currentSensor();
    // TODO need to check performance of rest of this method when in follower mode
    final List<BgReading> bgReadings = BgReading.latest(3);
    final List<Calibration> calibrations = Calibration.allForSensorLimited(3);
    if (bgReadings == null || bgReadings.size() < 3) {
        return unclearReading;
    }
    if (calibrations == null || calibrations.size() < 2) {
        return unclearReading;
    }
    BgReading bgReading = bgReadings.get(0);
    if (calibration_notifications) {
        // TODO this should only clear double calibration once after calibrations are achieved
        if (bgReadings.size() >= 3) {
            if (calibrations.size() == 0 && (new Date().getTime() - bgReadings.get(2).timestamp <= (60000 * 30)) && sensor != null) {
                if ((sensor.started_at + (60000 * 60 * 2)) < new Date().getTime()) {
                    doubleCalibrationRequest();
                } else {
                    // TODO should be aware of state
                    clearDoubleCalibrationRequest();
                }
            } else {
                clearDoubleCalibrationRequest();
            }
        } else {
            clearDoubleCalibrationRequest();
        }
        // bgreadings criteria possibly needs a review
        if (CalibrationRequest.shouldRequestCalibration(bgReading) && (new Date().getTime() - bgReadings.get(2).timestamp <= (60000 * 24))) {
            if ((!PowerStateReceiver.is_power_connected()) || (Pref.getBooleanDefaultFalse("calibration_alerts_while_charging"))) {
                if (JoH.pratelimit("calibration-request-notification", CALIBRATION_REQUEST_MAX_FREQUENCY)) {
                    extraCalibrationRequest();
                }
            }
        } else {
            // TODO should be aware of state
            clearExtraCalibrationRequest();
        }
        if (calibrations.size() >= 1 && (Math.abs((new Date().getTime() - calibrations.get(0).timestamp)) / (1000 * 60 * 60) > 12) && (CalibrationRequest.isSlopeFlatEnough(BgReading.last(true)))) {
            Log.d("NOTIFICATIONS", "Calibration difference in hours: " + ((new Date().getTime() - calibrations.get(0).timestamp)) / (1000 * 60 * 60));
            if ((!PowerStateReceiver.is_power_connected()) || (Pref.getBooleanDefaultFalse("calibration_alerts_while_charging"))) {
                // TODO check slope
                if (JoH.pratelimit("calibration-request-notification", CALIBRATION_REQUEST_MIN_FREQUENCY) || Pref.getBooleanDefaultFalse("calibration_alerts_repeat")) {
                    calibrationRequest();
                }
            }
        } else {
            // TODO should be aware of state
            clearCalibrationRequest();
        }
    } else {
        clearAllCalibrationNotifications();
    }
    return unclearReading;
}
Also used : BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) Date(java.util.Date) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Example 33 with Sensor

use of com.eveningoutpost.dexdrip.Models.Sensor in project xDrip-plus by jamorham.

the class Notifications method FileBasedNotifications.

/*
 * *************************************************************************************************************
 * Function for new notifications
 */
private void FileBasedNotifications(Context context) {
    ReadPerfs(context);
    Sensor sensor = Sensor.currentSensor();
    final BgReading bgReading = BgReading.last();
    if (bgReading == null) {
        // Sensor is stopped, or there is not enough data
        AlertPlayer.getPlayer().stopAlert(context, true, false);
        return;
    }
    final double calculated_value;
    /* KS TODO BestGlucose
        if (use_best_glucose) {
            this.dg = BestGlucose.getDisplayGlucose();
            if (dg != null) {
                bgReading.calculated_value = dg.mgdl;
                calculated_value = dg.mgdl;
            } else {
                calculated_value = bgReading.calculated_value;
                Log.wtf(TAG, "Could not obtain best glucose value!");
            }
        } else {*/
    calculated_value = bgReading.calculated_value;
    // }
    Log.d(TAG, "FileBasedNotifications called bgReading.calculated_value = " + bgReading.calculated_value + " calculated value: " + calculated_value);
    // In all this cases, bgReading.calculated_value should be 0.
    if (((sensor != null) || (Home.get_follower())) && calculated_value != 0) {
        AlertType newAlert = AlertType.get_highest_active_alert(context, calculated_value);
        if (newAlert == null) {
            Log.d(TAG, "FileBasedNotifications - No active notifcation exists, stopping all alerts");
            // No alert should work, Stop all alerts, but keep the snoozing...
            AlertPlayer.getPlayer().stopAlert(context, false, true);
            return;
        }
        AlertType activeBgAlert = ActiveBgAlert.alertTypegetOnly();
        if (activeBgAlert == null) {
            Log.d(TAG, "FileBasedNotifications we have a new alert, starting to play it... " + newAlert.name);
            // We need to create a new alert  and start playing
            boolean trendingToAlertEnd = trendingToAlertEnd(context, true, newAlert);
            // KS EditAlertActivity.
            AlertPlayer.getPlayer().startAlert(context, trendingToAlertEnd, newAlert, unitsConvert2Disp(doMgdl, calculated_value));
            return;
        }
        if (activeBgAlert.uuid.equals(newAlert.uuid)) {
            // disable alert on stale data
            if (prefs.getBoolean("disable_alerts_stale_data", false)) {
                int minutes = Integer.parseInt(prefs.getString("disable_alerts_stale_data_minutes", "15")) + 2;
                if ((new Date().getTime()) - (60000 * minutes) - BgReading.lastNoSenssor().timestamp > 0) {
                    Log.d(TAG, "FileBasedNotifications : active alert found but not replaying it because more than three readings missed :  " + newAlert.name);
                    return;
                }
            }
            Log.d(TAG, "FileBasedNotifications we have found an active alert, checking if we need to play it " + newAlert.name);
            boolean trendingToAlertEnd = trendingToAlertEnd(context, false, newAlert);
            // KS EditAlertActivity
            AlertPlayer.getPlayer().ClockTick(context, trendingToAlertEnd, unitsConvert2Disp(doMgdl, calculated_value));
            return;
        }
        // Currently the ui blocks having two alerts with the same alert value.
        boolean alertSnoozeOver = ActiveBgAlert.alertSnoozeOver();
        if (alertSnoozeOver) {
            Log.d(TAG, "FileBasedNotifications we had two alerts, the snoozed one is over, we fall down to deleting the snoozed and staring the new");
        // in such case it is not important which is higher.
        } else {
            // we have a new alert. If it is more important than the previous one. we need to stop
            // the older one and start a new one (We need to play even if we were snoozed).
            // If it is a lower level alert, we should keep being snoozed.
            // Example, if we have two alerts one for 90 and the other for 80. and we were already alerting for the 80
            // and we were snoozed. Now bg is 85, the alert for 80 is cleared, but we are alerting for 90.
            // We should not do anything if we are snoozed for the 80...
            // If one allert was high and the second one is low however, we alarm in any case (snoozing ignored).
            boolean opositeDirection = AlertType.OpositeDirection(activeBgAlert, newAlert);
            if (!opositeDirection) {
                AlertType newHigherAlert = AlertType.HigherAlert(activeBgAlert, newAlert);
                if ((newHigherAlert == activeBgAlert)) {
                    // the existing (snoozed) alert is the higher, No need to play it since it is snoozed.
                    Log.d(TAG, "FileBasedNotifications The new alert has the same direcotion, it is lower than the one snoozed, not playing it." + " newHigherAlert = " + newHigherAlert.name + "activeBgAlert = " + activeBgAlert.name);
                    return;
                }
            }
        }
        // For now, we are stopping the old alert and starting a new one.
        Log.d(TAG, "Found a new alert, that is higher than the previous one will play it. " + newAlert.name);
        AlertPlayer.getPlayer().stopAlert(context, true, false);
        boolean trendingToAlertEnd = trendingToAlertEnd(context, true, newAlert);
        // KS EditAlertActivity
        AlertPlayer.getPlayer().startAlert(context, trendingToAlertEnd, newAlert, unitsConvert2Disp(doMgdl, calculated_value));
    } else {
        AlertPlayer.getPlayer().stopAlert(context, true, false);
    }
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Date(java.util.Date) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Example 34 with Sensor

use of com.eveningoutpost.dexdrip.Models.Sensor in project xDrip-plus by jamorham.

the class SystemStatus method setSensorStatus.

private void setSensorStatus() {
    StringBuilder sensor_status = new StringBuilder();
    if (Sensor.isActive()) {
        Sensor sens = Sensor.currentSensor();
        Date date = new Date(sens.started_at);
        DateFormat df = new SimpleDateFormat();
        sensor_status.append(df.format(date));
        sensor_status.append(" (");
        sensor_status.append((System.currentTimeMillis() - sens.started_at) / (1000 * 60 * 60 * 24));
        sensor_status.append("d ");
        sensor_status.append(((System.currentTimeMillis() - sens.started_at) % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        sensor_status.append("h)");
    } else {
        sensor_status.append("not available");
    }
    sensor_status_view.setText(sensor_status.toString());
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Example 35 with Sensor

use of com.eveningoutpost.dexdrip.Models.Sensor in project xDrip-plus by jamorham.

the class SystemStatusFragment method setSensorStatus.

private void setSensorStatus() {
    StringBuilder sensor_status = new StringBuilder();
    if (Sensor.isActive()) {
        Sensor sens = Sensor.currentSensor();
        Date date = new Date(sens.started_at);
        DateFormat df = new SimpleDateFormat();
        sensor_status.append(df.format(date));
        sensor_status.append(" (");
        sensor_status.append((System.currentTimeMillis() - sens.started_at) / (1000 * 60 * 60 * 24));
        sensor_status.append("d ");
        sensor_status.append(((System.currentTimeMillis() - sens.started_at) % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        sensor_status.append("h)");
    } else {
        sensor_status.append("not available");
    }
    sensor_status_view.setText(sensor_status.toString());
}
Also used : DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Aggregations

Sensor (com.eveningoutpost.dexdrip.Models.Sensor)38 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)16 Date (java.util.Date)14 DataMap (com.google.android.gms.wearable.DataMap)12 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)8 TransmitterData (com.eveningoutpost.dexdrip.Models.TransmitterData)8 Gson (com.google.gson.Gson)6 GsonBuilder (com.google.gson.GsonBuilder)6 DateTypeAdapter (com.google.gson.internal.bind.DateTypeAdapter)6 Paint (android.graphics.Paint)4 Point (android.graphics.Point)4 DateFormat (java.text.DateFormat)4 SimpleDateFormat (java.text.SimpleDateFormat)4 ArrayList (java.util.ArrayList)4 ActivityNotFoundException (android.content.ActivityNotFoundException)2 Canvas (android.graphics.Canvas)2 ColorFilter (android.graphics.ColorFilter)2 ColorDrawable (android.graphics.drawable.ColorDrawable)2 Drawable (android.graphics.drawable.Drawable)2 DisplayMetrics (android.util.DisplayMetrics)2