Remove some duplicate code lines

This commit is contained in:
Markus M. May 2018-05-29 20:57:55 +02:00
parent 7e57c30ecf
commit 41503800e8
4 changed files with 72 additions and 98 deletions

View file

@ -427,9 +427,7 @@ public class DataService extends IntentService {
if (bundles.containsKey("sgv")) { if (bundles.containsKey("sgv")) {
String sgvstring = bundles.getString("sgv"); String sgvstring = bundles.getString("sgv");
JSONObject sgvJson = new JSONObject(sgvstring); JSONObject sgvJson = new JSONObject(sgvstring);
NSSgv nsSgv = new NSSgv(sgvJson); storeSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
} }
if (bundles.containsKey("sgvs")) { if (bundles.containsKey("sgvs")) {
@ -437,9 +435,7 @@ public class DataService extends IntentService {
JSONArray jsonArray = new JSONArray(sgvstring); JSONArray jsonArray = new JSONArray(sgvstring);
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject sgvJson = jsonArray.getJSONObject(i); JSONObject sgvJson = jsonArray.getJSONObject(i);
NSSgv nsSgv = new NSSgv(sgvJson); storeSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -452,11 +448,7 @@ public class DataService extends IntentService {
if (bundles.containsKey("mbg")) { if (bundles.containsKey("mbg")) {
String mbgstring = bundles.getString("mbg"); String mbgstring = bundles.getString("mbg");
JSONObject mbgJson = new JSONObject(mbgstring); JSONObject mbgJson = new JSONObject(mbgstring);
NSMbg nsMbg = new NSMbg(mbgJson); storeMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log());
} }
if (bundles.containsKey("mbgs")) { if (bundles.containsKey("mbgs")) {
@ -464,11 +456,7 @@ public class DataService extends IntentService {
JSONArray jsonArray = new JSONArray(sgvstring); JSONArray jsonArray = new JSONArray(sgvstring);
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mbgJson = jsonArray.getJSONObject(i); JSONObject mbgJson = jsonArray.getJSONObject(i);
NSMbg nsMbg = new NSMbg(mbgJson); storeMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log());
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -549,6 +537,20 @@ public class DataService extends IntentService {
} }
} }
private void storeMbg(JSONObject mbgJson) {
NSMbg nsMbg = new NSMbg(mbgJson);
CareportalEvent careportalEvent = new CareportalEvent(nsMbg);
MainApp.getDbHelper().createOrUpdate(careportalEvent);
if (Config.logIncommingData)
log.debug("Adding/Updating new MBG: " + careportalEvent.log());
}
private void storeSgv(JSONObject sgvJson) {
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
}
private void handleNewSMS(Intent intent) { private void handleNewSMS(Intent intent) {
Bundle bundle = intent.getExtras(); Bundle bundle = intent.getExtras();
if (bundle == null) return; if (bundle == null) return;

View file

@ -218,50 +218,19 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli
double iageUrgent = nsSettings.getExtendedWarnValue("iage", "urgent", 96); double iageUrgent = nsSettings.getExtendedWarnValue("iage", "urgent", 96);
double iageWarn = nsSettings.getExtendedWarnValue("iage", "warn", 72); double iageWarn = nsSettings.getExtendedWarnValue("iage", "warn", 72);
handleAge(iage, CareportalEvent.INSULINCHANGE, iageWarn, iageUrgent);
double cageUrgent = nsSettings.getExtendedWarnValue("cage", "urgent", 72); double cageUrgent = nsSettings.getExtendedWarnValue("cage", "urgent", 72);
double cageWarn = nsSettings.getExtendedWarnValue("cage", "warn", 48); double cageWarn = nsSettings.getExtendedWarnValue("cage", "warn", 48);
handleAge(sage, CareportalEvent.SITECHANGE, cageWarn, cageUrgent);
double sageUrgent = nsSettings.getExtendedWarnValue("sage", "urgent", 166); double sageUrgent = nsSettings.getExtendedWarnValue("sage", "urgent", 166);
double sageWarn = nsSettings.getExtendedWarnValue("sage", "warn", 164); double sageWarn = nsSettings.getExtendedWarnValue("sage", "warn", 164);
handleAge(sage, CareportalEvent.SITECHANGE, sageWarn, sageUrgent);
double pbageUrgent = nsSettings.getExtendedWarnValue("pgage", "urgent", 360); double pbageUrgent = nsSettings.getExtendedWarnValue("pgage", "urgent", 360);
double pbageWarn = nsSettings.getExtendedWarnValue("pgage", "warn", 240); double pbageWarn = nsSettings.getExtendedWarnValue("pgage", "warn", 240);
handleAge(pbage, CareportalEvent.PUMPBATTERYCHANGE, pbageWarn, pbageUrgent);
String notavailable = OverviewFragment.shorttextmode ? "-" : MainApp.gs(R.string.notavailable);
if (sage != null) {
careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.SENSORCHANGE);
if (careportalEvent != null) {
sage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, sageWarn, sageUrgent));
sage.setText(careportalEvent.age());
} else {
sage.setText(notavailable);
}
}
if (iage != null) {
careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.INSULINCHANGE);
if (careportalEvent != null) {
iage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, iageWarn, iageUrgent));
iage.setText(careportalEvent.age());
} else {
iage.setText(notavailable);
}
}
if (cage != null) {
careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.SITECHANGE);
if (careportalEvent != null) {
cage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, cageWarn, cageUrgent));
cage.setText(careportalEvent.age());
} else {
cage.setText(notavailable);
}
}
if (pbage != null) {
careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(CareportalEvent.PUMPBATTERYCHANGE);
if (careportalEvent != null) {
pbage.setTextColor(CareportalFragment.determineTextColor(careportalEvent, pbageWarn, pbageUrgent));
pbage.setText(careportalEvent.age());
} else {
pbage.setText(notavailable);
}
}
} }
); );
} }
@ -277,5 +246,21 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli
} }
} }
private static TextView handleAge(final TextView age, String eventType, double warnThreshold, double urgentThreshold) {
String notavailable = OverviewFragment.shorttextmode ? "-" : MainApp.gs(R.string.notavailable);
if (age != null) {
CareportalEvent careportalEvent = MainApp.getDbHelper().getLastCareportalEvent(eventType);
if (careportalEvent != null) {
age.setTextColor(CareportalFragment.determineTextColor(careportalEvent, warnThreshold, urgentThreshold));
age.setText(careportalEvent.age());
} else {
age.setText(notavailable);
}
}
return age;
}
} }

View file

@ -26,14 +26,24 @@ public class BroadcastStatus {
private static Logger log = LoggerFactory.getLogger(BroadcastStatus.class); private static Logger log = LoggerFactory.getLogger(BroadcastStatus.class);
public static void handleNewStatus(NSSettingsStatus status, Context context, boolean isDelta) { public static void handleNewStatus(NSSettingsStatus status, Context context, boolean isDelta) {
Intent intent = createIntent(status, isDelta);
LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) {
context.sendBroadcast(createIntent(status, isDelta));
}
}
private static Intent createIntent(NSSettingsStatus status, boolean isDelta) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
try { try {
bundle.putString("nsclientversionname", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionName); bundle.putString("nsclientversionname", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionName);
bundle.putInt("nsclientversioncode", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode); bundle.putInt("nsclientversioncode", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode);
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
bundle.putString("nightscoutversionname", NSClientService.nightscoutVersionName); bundle.putString("nightscoutversionname", NSClientService.nightscoutVersionName);
bundle.putInt("nightscoutversioncode", NSClientService.nightscoutVersionCode); bundle.putInt("nightscoutversioncode", NSClientService.nightscoutVersionCode);
bundle.putString("status", status.getData().toString()); bundle.putString("status", status.getData().toString());
@ -41,24 +51,7 @@ public class BroadcastStatus {
Intent intent = new Intent(Intents.ACTION_NEW_STATUS); Intent intent = new Intent(Intents.ACTION_NEW_STATUS);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent);
if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, true)) { return intent;
bundle = new Bundle();
try {
bundle.putString("nsclientversionname", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionName);
bundle.putInt("nsclientversioncode", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode);
} catch (PackageManager.NameNotFoundException e) {
log.error("Unhandled exception", e);
}
bundle.putString("nightscoutversionname", NSClientService.nightscoutVersionName);
bundle.putInt("nightscoutversioncode", NSClientService.nightscoutVersionCode);
bundle.putString("status", status.getData().toString());
bundle.putBoolean("delta", isDelta);
intent = new Intent(Intents.ACTION_NEW_STATUS);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
}
} }
} }

View file

@ -289,19 +289,7 @@ public class NSUpload {
public static void uploadProfileSwitch(ProfileSwitch profileSwitch) { public static void uploadProfileSwitch(ProfileSwitch profileSwitch) {
try { try {
JSONObject data = new JSONObject(); JSONObject data = getJson(profileSwitch);
data.put("eventType", CareportalEvent.PROFILESWITCH);
data.put("duration", profileSwitch.durationInMinutes);
data.put("profile", profileSwitch.getCustomizedName());
data.put("profileJson", profileSwitch.profileJson);
data.put("profilePlugin", profileSwitch.profilePlugin);
if (profileSwitch.isCPP) {
data.put("CircadianPercentageProfile", true);
data.put("timeshift", profileSwitch.timeshift);
data.put("percentage", profileSwitch.percentage);
}
data.put("created_at", DateUtil.toISOString(profileSwitch.date));
data.put("enteredBy", MainApp.gs(R.string.app_name));
uploadCareportalEntryToNS(data); uploadCareportalEntryToNS(data);
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -334,19 +322,7 @@ public class NSUpload {
public static void updateProfileSwitch(ProfileSwitch profileSwitch) { public static void updateProfileSwitch(ProfileSwitch profileSwitch) {
try { try {
JSONObject data = new JSONObject(); JSONObject data = getJson(profileSwitch);
data.put("eventType", CareportalEvent.PROFILESWITCH);
data.put("duration", profileSwitch.durationInMinutes);
data.put("profile", profileSwitch.getCustomizedName());
data.put("profileJson", profileSwitch.profileJson);
data.put("profilePlugin", profileSwitch.profilePlugin);
if (profileSwitch.isCPP) {
data.put("CircadianPercentageProfile", true);
data.put("timeshift", profileSwitch.timeshift);
data.put("percentage", profileSwitch.percentage);
}
data.put("created_at", DateUtil.toISOString(profileSwitch.date));
data.put("enteredBy", MainApp.gs(R.string.app_name));
if (profileSwitch._id != null) { if (profileSwitch._id != null) {
Context context = MainApp.instance().getApplicationContext(); Context context = MainApp.instance().getApplicationContext();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
@ -365,6 +341,24 @@ public class NSUpload {
} }
} }
private static JSONObject getJson(ProfileSwitch profileSwitch) throws JSONException {
JSONObject data = new JSONObject();
data.put("eventType", CareportalEvent.PROFILESWITCH);
data.put("duration", profileSwitch.durationInMinutes);
data.put("profile", profileSwitch.getCustomizedName());
data.put("profileJson", profileSwitch.profileJson);
data.put("profilePlugin", profileSwitch.profilePlugin);
if (profileSwitch.isCPP) {
data.put("CircadianPercentageProfile", true);
data.put("timeshift", profileSwitch.timeshift);
data.put("percentage", profileSwitch.percentage);
}
data.put("created_at", DateUtil.toISOString(profileSwitch.date));
data.put("enteredBy", MainApp.gs(R.string.app_name));
return data;
}
public static void uploadCareportalEntryToNS(JSONObject data) { public static void uploadCareportalEntryToNS(JSONObject data) {
try { try {
if (data.has("preBolus") && data.has("carbs")) { if (data.has("preBolus") && data.has("carbs")) {