create singleton from NSstatus

This commit is contained in:
Milos Kozak 2017-06-12 11:15:03 +02:00
parent b41a942d11
commit 0f40670c24
4 changed files with 171 additions and 40 deletions

View file

@ -166,7 +166,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
if (!pm.isIgnoringBatteryOptimizations(packageName)) { if (!pm.isIgnoringBatteryOptimizations(packageName)) {
log.debug("Requesting ignore battery optimization"); log.debug("Requesting ignore battery optimization");
OKDialog.show(getParent(), getString(R.string.pleaseallowpermission), String.format(getString(R.string.needwhitelisting), getString(R.string.app_name)), new Runnable() { OKDialog.show(this, getString(R.string.pleaseallowpermission), String.format(getString(R.string.needwhitelisting), getString(R.string.app_name)), new Runnable() {
@Override @Override
public void run() { public void run() {

View file

@ -26,6 +26,7 @@ import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSMbg; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSMbg;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus;
import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
@ -230,8 +231,6 @@ public class DataService extends IntentService {
if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) { if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) {
if (Config.logIncommingData)
log.debug("Received status: " + bundles);
if (bundles.containsKey("nsclientversioncode")) { if (bundles.containsKey("nsclientversioncode")) {
ConfigBuilderPlugin.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203 ConfigBuilderPlugin.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203
ConfigBuilderPlugin.nightscoutVersionName = bundles.getString("nightscoutversionname"); ConfigBuilderPlugin.nightscoutVersionName = bundles.getString("nightscoutversionname");
@ -261,6 +260,9 @@ public class DataService extends IntentService {
if (bundles.containsKey("status")) { if (bundles.containsKey("status")) {
try { try {
JSONObject statusJson = new JSONObject(bundles.getString("status")); JSONObject statusJson = new JSONObject(bundles.getString("status"));
NSStatus.getInstance().setData(statusJson);
if (Config.logIncommingData)
log.debug("Received status: " + statusJson.toString());
if (statusJson.has("settings")) { if (statusJson.has("settings")) {
JSONObject settings = statusJson.getJSONObject("settings"); JSONObject settings = statusJson.getJSONObject("settings");
if (settings.has("thresholds")) { if (settings.has("thresholds")) {

View file

@ -5,40 +5,157 @@ import org.json.JSONObject;
import java.util.Date; import java.util.Date;
/** /*
{ {
status: 'ok' "status": "ok",
, name: env.name "name": "Nightscout",
, version: env.version "version": "0.10.0-dev-20170423",
, versionNum: versionNum (for ver 1.2.3 contains 10203) "versionNum": 1000,
, serverTime: new Date().toISOString() "serverTime": "2017-06-12T07:46:56.006Z",
, apiEnabled: apiEnabled "apiEnabled": true,
, careportalEnabled: apiEnabled && env.settings.enable.indexOf('careportal') > -1 "careportalEnabled": true,
, boluscalcEnabled: apiEnabled && env.settings.enable.indexOf('boluscalc') > -1 "boluscalcEnabled": true,
, head: env.head "head": "96ee154",
, settings: env.settings "settings": {
, extendedSettings: ctx.plugins && ctx.plugins.extendedClientSettings ? ctx.plugins.extendedClientSettings(env.extendedSettings) : {} "units": "mmol",
, activeProfile ..... calculated from treatments or missing "timeFormat": 24,
"nightMode": false,
"editMode": true,
"showRawbg": "always",
"customTitle": "Bara's CGM",
"theme": "colors",
"alarmUrgentHigh": true,
"alarmUrgentHighMins": [30, 60, 90, 120],
"alarmHigh": true,
"alarmHighMins": [30, 60, 90, 120],
"alarmLow": true,
"alarmLowMins": [15, 30, 45, 60],
"alarmUrgentLow": true,
"alarmUrgentLowMins": [15, 30, 45],
"alarmUrgentMins": [30, 60, 90, 120],
"alarmWarnMins": [30, 60, 90, 120],
"alarmTimeagoWarn": true,
"alarmTimeagoWarnMins": 15,
"alarmTimeagoUrgent": true,
"alarmTimeagoUrgentMins": 30,
"language": "cs",
"scaleY": "linear",
"showPlugins": "careportal boluscalc food bwp cage sage iage iob cob basal ar2 delta direction upbat rawbg",
"showForecast": "ar2",
"focusHours": 3,
"heartbeat": 60,
"baseURL": "http:\/\/barascgm.sysop.cz:82",
"authDefaultRoles": "readable",
"thresholds": {
"bgHigh": 252,
"bgTargetTop": 180,
"bgTargetBottom": 72,
"bgLow": 71
},
"DEFAULT_FEATURES": ["bgnow", "delta", "direction", "timeago", "devicestatus", "upbat", "errorcodes", "profile"],
"alarmTypes": ["predict"],
"enable": ["careportal", "boluscalc", "food", "bwp", "cage", "sage", "iage", "iob", "cob", "basal", "ar2", "rawbg", "pushover", "bgi", "pump", "openaps", "pushover", "treatmentnotify", "bgnow", "delta", "direction", "timeago", "devicestatus", "upbat", "profile", "ar2"]
},
"extendedSettings": {
"pump": {
"fields": "reservoir battery clock",
"urgentBattP": 26,
"warnBattP": 51
},
"openaps": {
"enableAlerts": true
},
"cage": {
"alerts": true,
"display": "days",
"urgent": 96,
"warn": 72
},
"sage": {
"alerts": true,
"urgent": 336,
"warn": 168
},
"iage": {
"alerts": true,
"urgent": 150,
"warn": 120
},
"basal": {
"render": "default"
},
"profile": {
"history": true,
"multiple": true
},
"devicestatus": {
"advanced": true
}
},
"activeProfile": "2016 +30%"
} }
*/ */
public class NSStatus { public class NSStatus {
private JSONObject data; private static NSStatus instance = null;
public NSStatus(JSONObject obj) { public static NSStatus getInstance() {
this.data = obj; if (instance == null)
instance = new NSStatus();
return instance;
} }
public String getName () { return getStringOrNull("name"); } private JSONObject data;
public String getVersion () { return getStringOrNull("version"); }
public Integer getVersionNum () { return getIntegerOrNull("versionNum"); } public NSStatus() {}
public Date getServerTime () { return getDateOrNull("versionNum"); }
public boolean getApiEnabled () { return getBooleanOrNull("apiEnabled"); } public NSStatus setData(JSONObject obj) {
public boolean getCareportalEnabled () { return getBooleanOrNull("careportalEnabled"); } this.data = obj;
public boolean getBoluscalcEnabled () { return getBooleanOrNull("boluscalcEnabled"); } return this;
public String getHead () { return getStringOrNull("head"); } }
public String getSettings () { return getStringOrNull("settings"); }
public String getExtendedSettings () { return getStringOrNull("extendedSettings"); } public String getName() {
public String getActiveProfile () { return getStringOrNull("activeProfile"); } return getStringOrNull("name");
}
public String getVersion() {
return getStringOrNull("version");
}
public Integer getVersionNum() {
return getIntegerOrNull("versionNum");
}
public Date getServerTime() {
return getDateOrNull("versionNum");
}
public boolean getApiEnabled() {
return getBooleanOrNull("apiEnabled");
}
public boolean getCareportalEnabled() {
return getBooleanOrNull("careportalEnabled");
}
public boolean getBoluscalcEnabled() {
return getBooleanOrNull("boluscalcEnabled");
}
public String getHead() {
return getStringOrNull("head");
}
public String getSettings() {
return getStringOrNull("settings");
}
public String getExtendedSettings() {
return getStringOrNull("extendedSettings");
}
public String getActiveProfile() {
return getStringOrNull("activeProfile");
}
private String getStringOrNull(String key) { private String getStringOrNull(String key) {
String ret = null; String ret = null;
@ -50,7 +167,9 @@ public class NSStatus {
} }
} }
return ret; return ret;
}; }
;
private Integer getIntegerOrNull(String key) { private Integer getIntegerOrNull(String key) {
Integer ret = null; Integer ret = null;
@ -62,7 +181,9 @@ public class NSStatus {
} }
} }
return ret; return ret;
}; }
;
private Long getLongOrNull(String key) { private Long getLongOrNull(String key) {
Long ret = null; Long ret = null;
@ -74,7 +195,9 @@ public class NSStatus {
} }
} }
return ret; return ret;
}; }
;
private Date getDateOrNull(String key) { private Date getDateOrNull(String key) {
Date ret = null; Date ret = null;
@ -86,7 +209,9 @@ public class NSStatus {
} }
} }
return ret; return ret;
}; }
;
private boolean getBooleanOrNull(String key) { private boolean getBooleanOrNull(String key) {
boolean ret = false; boolean ret = false;
@ -98,8 +223,12 @@ public class NSStatus {
} }
} }
return ret; return ret;
}; }
public JSONObject getData () { return data; } ;
public JSONObject getData() {
return data;
}
} }

View file

@ -430,15 +430,15 @@ public class NSClientService extends Service {
if (data.has("status")) { if (data.has("status")) {
JSONObject status = data.getJSONObject("status"); JSONObject status = data.getJSONObject("status");
NSStatus nsStatus = new NSStatus(status); NSStatus nsStatus = NSStatus.getInstance().setData(status);
if (!status.has("versionNum")) { if (!status.has("versionNum")) {
if (status.getInt("versionNum") < 900) { if (status.getInt("versionNum") < Config.SUPPORTEDNSVERSION) {
MainApp.bus().post(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!")); MainApp.bus().post(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!"));
} }
} else { } else {
nightscoutVersionName = status.getString("version"); nightscoutVersionName = nsStatus.getVersion();
nightscoutVersionCode = status.getInt("versionNum"); nightscoutVersionCode = nsStatus.getVersionNum();
} }
BroadcastStatus.handleNewStatus(nsStatus, MainApp.instance().getApplicationContext(), isDelta); BroadcastStatus.handleNewStatus(nsStatus, MainApp.instance().getApplicationContext(), isDelta);