diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CareDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CareDialog.kt index 946a3fec5b..1881e4fb77 100644 --- a/app/src/main/java/info/nightscout/androidaps/dialogs/CareDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CareDialog.kt @@ -13,6 +13,7 @@ import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.db.CareportalEvent +import info.nightscout.androidaps.db.Source import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions import info.nightscout.androidaps.plugins.general.nsclient.NSUpload import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus @@ -24,6 +25,7 @@ import info.nightscout.androidaps.utils.Translator import kotlinx.android.synthetic.main.dialog_care.* import kotlinx.android.synthetic.main.notes.* import kotlinx.android.synthetic.main.okcancel.* +import org.json.JSONException import org.json.JSONObject import java.text.DecimalFormat import java.util.* @@ -126,10 +128,13 @@ class CareDialog : DialogFragmentWithDate() { actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) json.put("notes", notes) } + eventTime -= eventTime % 1000 + if (eventTimeChanged) actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime)) json.put("created_at", DateUtil.toISOString(eventTime)) + json.put("mills", eventTime) json.put("eventType", when (options) { EventType.BGCHECK -> CareportalEvent.BGCHECK EventType.SENSOR_INSERT -> CareportalEvent.SENSORCHANGE @@ -141,7 +146,16 @@ class CareDialog : DialogFragmentWithDate() { activity?.let { activity -> OKDialog.showConfirmation(activity, MainApp.gs(event), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { - MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(json) + val careportalEvent = CareportalEvent() + careportalEvent.date = eventTime + careportalEvent.source = Source.USER + careportalEvent.eventType = when (options) { + EventType.BGCHECK -> CareportalEvent.BGCHECK + EventType.SENSOR_INSERT -> CareportalEvent.SENSORCHANGE + EventType.BATTERY_CHANGE -> CareportalEvent.PUMPBATTERYCHANGE + } + careportalEvent.json = json.toString() + MainApp.getDbHelper().createOrUpdate(careportalEvent) NSUpload.uploadCareportalEntryToNS(json) }, null) } diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/FillDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/FillDialog.kt index 8d55df9bc1..3eb4b0e379 100644 --- a/app/src/main/java/info/nightscout/androidaps/dialogs/FillDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/FillDialog.kt @@ -20,6 +20,8 @@ import info.nightscout.androidaps.utils.* import kotlinx.android.synthetic.main.dialog_fill.* import kotlinx.android.synthetic.main.notes.* import kotlinx.android.synthetic.main.okcancel.* +import org.json.JSONException +import org.json.JSONObject import java.util.* import kotlin.math.abs @@ -91,6 +93,8 @@ class FillDialog : DialogFragmentWithDate() { val notes = notes.text.toString() if (notes.isNotEmpty()) actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) + eventTime -= eventTime % 1000 + if (eventTimeChanged) actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime)) @@ -117,8 +121,21 @@ class FillDialog : DialogFragmentWithDate() { } }) } - if (siteChange) NSUpload.uploadEvent(CareportalEvent.SITECHANGE, eventTime, notes) - if (insulinChange) NSUpload.uploadEvent(CareportalEvent.INSULINCHANGE, eventTime + 1000, notes) + val careportalEvent = CareportalEvent() + careportalEvent.date = eventTime + careportalEvent.source = Source.USER + if (siteChange) { + careportalEvent.json = generateJson(CareportalEvent.SITECHANGE, eventTime, notes).toString() + careportalEvent.eventType = CareportalEvent.SITECHANGE + NSUpload.uploadEvent(CareportalEvent.SITECHANGE, eventTime, notes) + } + if (insulinChange) { + // add a second for case of both checked + careportalEvent.json = generateJson(CareportalEvent.INSULINCHANGE, eventTime + 1000, notes).toString() + careportalEvent.eventType = CareportalEvent.INSULINCHANGE + NSUpload.uploadEvent(CareportalEvent.INSULINCHANGE, eventTime + 1000, notes) + } + MainApp.getDbHelper().createOrUpdate(careportalEvent) }, null) } } else { @@ -129,4 +146,18 @@ class FillDialog : DialogFragmentWithDate() { dismiss() return true } + + fun generateJson(careportalEvent: String, time: Long, notes: String): JSONObject { + val data = JSONObject() + try { + data.put("eventType", careportalEvent) + data.put("created_at", DateUtil.toISOString(time)) + data.put("mills", time) + data.put("enteredBy", SP.getString("careportal_enteredby", MainApp.gs(R.string.app_name))) + if (notes.isNotEmpty()) data.put("notes", notes) + } catch (ignored: JSONException) { + } + return data + } + } \ No newline at end of file