use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip-plus by jamorham.
the class ListenerService method createTreatment.
public static synchronized void createTreatment(DataMap dataMap, Context context) {
Log.d(TAG, "createTreatment dataMap=" + dataMap);
double timeoffset = dataMap.getDouble("timeoffset", 0);
double carbs = dataMap.getDouble("carbs", 0);
double insulin = dataMap.getDouble("insulin", 0);
double bloodtest = dataMap.getDouble("bloodtest", 0);
String notes = dataMap.getString("notes", "");
long timestamp_ms = Treatments.getTimeStampWithOffset(timeoffset);
Treatments treatment = Treatments.create(carbs, insulin, notes, timestamp_ms);
if (bloodtest > 0) {
Log.d(TAG, "createTreatment bloodtest=" + bloodtest);
BloodTest.createFromCal(bloodtest, timeoffset, "Manual Entry", treatment.uuid);
} else
Log.d(TAG, "createTreatment bloodtest=0 " + bloodtest);
showTreatments(context, "all");
SendData(context, SYNC_TREATMENTS_PATH, null);
// requestData(context);//send to phone if connected
}
use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip-plus by jamorham.
the class ListenerService method getWearTreatmentsData.
private synchronized DataMap getWearTreatmentsData(int count, long last_send_time, int min_count) {
forceGoogleApiConnect();
Log.d(TAG, "getWearTreatmentsData last_send_time:" + JoH.dateTimeText(last_send_time) + " max count=" + count + " min_count=" + min_count);
Treatments last_log = Treatments.lastSystime();
if (last_log != null) {
Log.d(TAG, "getWearTreatmentsData last systimestamp: " + last_log.systimestamp + " " + JoH.dateTimeText((long) last_log.systimestamp));
}
if (last_log != null && last_log.systimestamp > 0 && last_send_time <= last_log.systimestamp) {
// startTime
long last_send_success = last_send_time;
Log.d(TAG, "getWearTreatmentsData last_send_time < last_log.timestamp:" + JoH.dateTimeText((long) last_log.systimestamp));
List<Treatments> logs = Treatments.latestForGraphSystime(count, last_send_time);
if (!logs.isEmpty() && logs.size() > min_count) {
// Log.d(TAG, "getWearLogData count = " + logs.size());
DataMap entries = dataMap(last_log);
final ArrayList<DataMap> dataMaps = new ArrayList<>(logs.size());
for (Treatments log : logs) {
dataMaps.add(dataMap(log));
last_send_success = (long) log.systimestamp;
// Log.d(TAG, "getWearTreatmentsData set last_send_sucess:" + JoH.dateTimeText(last_send_sucess) + " Log:" + log.toString());
}
// MOST IMPORTANT LINE FOR TIMESTAMP
entries.putLong("time", new Date().getTime());
entries.putDataMapArrayList("entries", dataMaps);
Log.i(TAG, "getWearTreatmentsData SYNCED treatments up to " + JoH.dateTimeText(last_send_success) + " count = " + logs.size());
return entries;
} else
Log.i(TAG, "getWearTreatmentsData SYNCED treatments up to " + JoH.dateTimeText(last_send_success) + " count = 0");
}
return null;
}
use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip-plus by jamorham.
the class ListenerService method getTreatments.
public static DataMap getTreatments(long startTime) {
Treatments last = Treatments.last();
if (last != null) {
Log.d(TAG, "getTreatments last.timestamp:" + JoH.dateTimeText(last.timestamp));
}
List<Treatments> graph = Treatments.latestForGraph(60, startTime);
if (!graph.isEmpty()) {
Log.d(TAG, "getTreatments graph size=" + graph.size());
final ArrayList<DataMap> dataMaps = new ArrayList<>(graph.size());
DataMap entries = null;
// if (includeTreatment(last)) entries = dataMapForWatchface(last);
for (Treatments data : graph) {
if (includeTreatment(data)) {
if (entries == null) {
entries = dataMapForWatchface(data);
dataMaps.add(dataMapForWatchface(data));
} else
dataMaps.add(dataMapForWatchface(data));
}
}
if (entries != null) {
entries.putDataMapArrayList("entries", dataMaps);
Log.d(TAG, "getTreatments entries=" + entries);
}
return entries;
} else
return null;
}
use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip-plus by jamorham.
the class ListenerService method syncTreatmentsData.
private synchronized void syncTreatmentsData(DataMap dataMap, Context context) {
Log.d(TAG, "syncTreatmentsData");
boolean changed = false;
String action = dataMap.getString("action");
if (action.equals("delete")) {
Log.d(TAG, "syncTreatmentsData Delete Treatments");
deleteTreatment(dataMap);
showTreatments(context, "treats");
} else {
ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
if (entries != null) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
Log.d(TAG, "syncTreatmentsData add Treatments Table entries count=" + entries.size());
// ensure database has already been initialized
Sensor.InitDb(context);
for (DataMap entry : entries) {
if (entry != null) {
String record = entry.getString("data");
if (record != null) {
Treatments data = gson.fromJson(record, Treatments.class);
Treatments exists = Treatments.byuuid(data.uuid);
if (exists != null) {
Log.d(TAG, "syncTreatmentsData save existing Treatments for action insert uuid=" + data.uuid + " timestamp=" + data.timestamp + " timeString=" + JoH.dateTimeText(data.timestamp) + " carbs=" + data.carbs + " insulin=" + data.insulin + " exists.systime=" + JoH.dateTimeText(exists.systimestamp));
if (exists.timestamp != data.timestamp) {
// currently only tracking timestamp on watch
changed = true;
}
exists.enteredBy = data.enteredBy;
exists.eventType = data.eventType;
exists.insulin = data.insulin;
exists.carbs = data.carbs;
exists.created_at = data.created_at;
exists.notes = data.notes;
exists.timestamp = data.timestamp;
exists.systimestamp = exists.systimestamp > 0 ? exists.systimestamp : data.timestamp < last_send_previous_treatments ? data.timestamp : last_send_previous_treatments > 0 ? last_send_previous_treatments - 1 : JoH.tsl();
exists.save();
} else {
changed = true;
data.systimestamp = data.timestamp < last_send_previous_treatments ? data.timestamp : last_send_previous_treatments > 0 ? last_send_previous_treatments - 1 : JoH.tsl();
data.save();
Log.d(TAG, "syncTreatmentsData create new treatment for action insert uuid=" + data.uuid + " timestamp=" + data.timestamp + " timeString=" + JoH.dateTimeText(data.timestamp) + " carbs=" + data.carbs + " insulin=" + data.insulin + " systime=" + JoH.dateTimeText(data.systimestamp));
}
}
}
}
if (changed) {
showTreatments(context, "treats");
}
}
}
}
use of com.eveningoutpost.dexdrip.Models.Treatments in project xDrip-plus by jamorham.
the class TreatmentsTable method getData.
private void getData() {
// 3 days
final long startTime = new Date().getTime() - (60000 * 60 * 24 * 3);
final List<Treatments> latest = Treatments.latestForGraph(60, startTime);
ListAdapter adapter = new thisAdapter(this, latest);
this.setListAdapter(adapter);
String msg = "";
int size = 0;
if (latest != null)
size = latest.size();
if (size == 0) {
msg = getResources().getString(R.string.notify_table_size, "Treatments", size);
JoH.static_toast(xdrip.getAppContext(), msg, Toast.LENGTH_SHORT);
}
}
Aggregations