NS high & low alarms with sound

This commit is contained in:
Milos Kozak 2017-06-21 07:28:04 +02:00
parent 7f92f42923
commit 3e7c4b1350
9 changed files with 104 additions and 20 deletions

View file

@ -80,9 +80,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
registerBus(); registerBus();
setUpTabs(false); setUpTabs(false);
Intent alarm = new Intent(this, AlarmSoundService.class);
alarm.putExtra("soundid", R.raw.staledataalarm);
//startService(alarm);
} }
@Subscribe @Subscribe

View file

@ -18,7 +18,7 @@ public class AlarmSoundService extends Service {
private static Logger log = LoggerFactory.getLogger(AlarmSoundService.class); private static Logger log = LoggerFactory.getLogger(AlarmSoundService.class);
MediaPlayer player; MediaPlayer player;
int resourceId = R.raw.bgalarm; int resourceId = R.raw.error;
public AlarmSoundService() { public AlarmSoundService() {
} }
@ -36,9 +36,11 @@ public class AlarmSoundService extends Service {
} }
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (player != null && player.isPlaying())
player.stop();
log.debug("onStartCommand"); log.debug("onStartCommand");
if (intent != null && intent.hasExtra("soundid")) if (intent != null && intent.hasExtra("soundid"))
resourceId = intent.getIntExtra("soundid", R.raw.bgalarm); resourceId = intent.getIntExtra("soundid", R.raw.error);
player = new MediaPlayer(); player = new MediaPlayer();
AssetFileDescriptor afd = MainApp.sResources.openRawResourceFd(resourceId); AssetFileDescriptor afd = MainApp.sResources.openRawResourceFd(resourceId);

View file

@ -262,18 +262,12 @@ public class DataService extends IntentService {
NSStatus.getInstance().setData(statusJson); NSStatus.getInstance().setData(statusJson);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("Received status: " + statusJson.toString()); log.debug("Received status: " + statusJson.toString());
if (statusJson.has("settings")) { Double targetHigh = NSStatus.getInstance().getThreshold("bgTargetTop");
JSONObject settings = statusJson.getJSONObject("settings"); Double targetlow = NSStatus.getInstance().getThreshold("bgTargetBottom");
if (settings.has("thresholds")) { if (targetHigh != null)
JSONObject thresholds = settings.getJSONObject("thresholds"); OverviewPlugin.bgTargetHigh = targetHigh;
if (thresholds.has("bgTargetTop")) { if (targetlow != null)
OverviewPlugin.bgTargetHigh = thresholds.getDouble("bgTargetTop"); OverviewPlugin.bgTargetLow = targetlow;
}
if (thresholds.has("bgTargetBottom")) {
OverviewPlugin.bgTargetLow = thresholds.getDouble("bgTargetBottom");
}
}
}
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -1,5 +1,7 @@
package info.nightscout.androidaps.plugins.NSClientInternal.data; package info.nightscout.androidaps.plugins.NSClientInternal.data;
import android.support.annotation.Nullable;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -104,9 +106,10 @@ public class NSStatus {
return instance; return instance;
} }
private JSONObject data; private JSONObject data = null;
public NSStatus() {} public NSStatus() {
}
public NSStatus setData(JSONObject obj) { public NSStatus setData(JSONObject obj) {
this.data = obj; this.data = obj;
@ -157,6 +160,32 @@ public class NSStatus {
return getStringOrNull("activeProfile"); return getStringOrNull("activeProfile");
} }
// "bgHigh": 252,
// "bgTargetTop": 180,
// "bgTargetBottom": 72,
// "bgLow": 71
@Nullable
public Double getThreshold(String what) {
try {
if (data == null)
return null;
String settings = getSettings();
if (settings != null) {
JSONObject settingsO = new JSONObject(settings);
if (settingsO.has("thresholds")) {
JSONObject tObject = settingsO.getJSONObject("thresholds");
if (tObject.has(what)) {
Double result = tObject.getDouble(what);
return result;
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private String getStringOrNull(String key) { private String getStringOrNull(String key) {
String ret = null; String ret = null;
if (data.has(key)) { if (data.has(key)) {

View file

@ -2,7 +2,12 @@ package info.nightscout.androidaps.plugins.Overview;
import java.util.Date; import java.util.Date;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSAlarm; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSAlarm;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus;
import info.nightscout.utils.SP;
/** /**
* Created by mike on 03.12.2016. * Created by mike on 03.12.2016.
@ -44,6 +49,7 @@ public class Notification {
public Date validTo = new Date(0); public Date validTo = new Date(0);
public NSAlarm nsAlarm = null; public NSAlarm nsAlarm = null;
public Integer soundId = null;
public Notification() { public Notification() {
} }
@ -87,12 +93,52 @@ public class Notification {
this.id = NSALARM; this.id = NSALARM;
this.level = NORMAL; this.level = NORMAL;
this.text = nsAlarm.getTile(); this.text = nsAlarm.getTile();
if (isAlarmForLow() && SP.getBoolean(R.string.key_nsalarm_low, false) || isAlarmForHigh() && SP.getBoolean(R.string.key_nsalarm_high, false))
this.soundId = R.raw.alarm;
break; break;
case 2: case 2:
this.id = NSURGENTALARM; this.id = NSURGENTALARM;
this.level = URGENT; this.level = URGENT;
this.text = nsAlarm.getTile(); this.text = nsAlarm.getTile();
if (isAlarmForLow() && SP.getBoolean(R.string.key_nsalarm_urgent_low, false) || isAlarmForHigh() && SP.getBoolean(R.string.key_nsalarm_urgent_high, false))
this.soundId = R.raw.urgentalarm;
break; break;
} }
} }
public boolean isEnabled() {
if (nsAlarm == null)
return true;
if (level == ANNOUNCEMENT)
return true;
if (level == NORMAL && isAlarmForLow() && SP.getBoolean(R.string.key_nsalarm_low, false) || isAlarmForHigh() && SP.getBoolean(R.string.key_nsalarm_high, false))
return true;
if (level == URGENT && isAlarmForLow() && SP.getBoolean(R.string.key_nsalarm_urgent_low, false) || isAlarmForHigh() && SP.getBoolean(R.string.key_nsalarm_urgent_high, false))
return true;
return false;
}
boolean isAlarmForLow() {
BgReading bgReading = MainApp.getDbHelper().lastBg();
if (bgReading == null)
return false;
Double threshold = NSStatus.getInstance().getThreshold("bgTargetTop");
if (threshold == null)
return false;
if (bgReading.value <= threshold)
return true;
return false;
}
boolean isAlarmForHigh() {
BgReading bgReading = MainApp.getDbHelper().lastBg();
if (bgReading == null)
return false;
Double threshold = NSStatus.getInstance().getThreshold("bgTargetBottom");
if (threshold == null)
return false;
if (bgReading.value >= threshold)
return true;
return false;
}
} }

View file

@ -1,5 +1,7 @@
package info.nightscout.androidaps.plugins.Overview; package info.nightscout.androidaps.plugins.Overview;
import android.content.Intent;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -9,6 +11,10 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.AlarmSoundService;
/** /**
* Created by mike on 03.12.2016. * Created by mike on 03.12.2016.
@ -41,6 +47,11 @@ public class NotificationStore {
return; return;
} }
} }
if (n.soundId != null) {
Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class);
alarm.putExtra("soundid", n.soundId);
MainApp.instance().startService(alarm);
}
store.add(n); store.add(n);
Collections.sort(store, new NotificationComparator()); Collections.sort(store, new NotificationComparator());
} }
@ -48,6 +59,10 @@ public class NotificationStore {
public boolean remove(int id) { public boolean remove(int id) {
for (int i = 0; i < store.size(); i++) { for (int i = 0; i < store.size(); i++) {
if (get(i).id == id) { if (get(i).id == id) {
if (get(i).soundId != null) {
Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class);
MainApp.instance().stopService(alarm);
}
store.remove(i); store.remove(i);
return true; return true;
} }

View file

@ -36,7 +36,8 @@ public class NSAlarmReceiver extends BroadcastReceiver {
case Intents.ACTION_ALARM: case Intents.ACTION_ALARM:
case Intents.ACTION_URGENT_ALARM: case Intents.ACTION_URGENT_ALARM:
Notification notification = new Notification(nsAlarm); Notification notification = new Notification(nsAlarm);
MainApp.bus().post(new EventNewNotification(notification)); if (notification.isEnabled())
MainApp.bus().post(new EventNewNotification(notification));
break; break;
case Intents.ACTION_CLEAR_ALARM: case Intents.ACTION_CLEAR_ALARM:
MainApp.bus().post(new EventDismissNotification(Notification.NSALARM)); MainApp.bus().post(new EventDismissNotification(Notification.NSALARM));