commit
3a19dd1bb6
6 changed files with 169 additions and 116 deletions
|
@ -39,6 +39,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
public static final String DATABASE_TEMPTARGETS = "TempTargets";
|
public static final String DATABASE_TEMPTARGETS = "TempTargets";
|
||||||
public static final String DATABASE_TREATMENTS = "Treatments";
|
public static final String DATABASE_TREATMENTS = "Treatments";
|
||||||
public static final String DATABASE_DANARHISTORY = "DanaRHistory";
|
public static final String DATABASE_DANARHISTORY = "DanaRHistory";
|
||||||
|
public static final String DATABASE_DBREQUESTS = "DBRequests";
|
||||||
|
|
||||||
private static final int DATABASE_VERSION = 5;
|
private static final int DATABASE_VERSION = 5;
|
||||||
|
|
||||||
|
@ -62,6 +63,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
||||||
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
|
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
|
||||||
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
|
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
|
||||||
|
TableUtils.createTableIfNotExists(connectionSource, DbRequest.class);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error("Can't create database", e);
|
log.error("Can't create database", e);
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
@ -77,6 +79,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
||||||
TableUtils.dropTable(connectionSource, BgReading.class, true);
|
TableUtils.dropTable(connectionSource, BgReading.class, true);
|
||||||
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
|
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
|
||||||
|
TableUtils.dropTable(connectionSource, DbRequest.class, true);
|
||||||
onCreate(database, connectionSource);
|
onCreate(database, connectionSource);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error("Can't drop databases", e);
|
log.error("Can't drop databases", e);
|
||||||
|
@ -122,6 +125,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
||||||
TableUtils.dropTable(connectionSource, BgReading.class, true);
|
TableUtils.dropTable(connectionSource, BgReading.class, true);
|
||||||
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
|
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
|
||||||
|
//DbRequests can be cleared from NSClient fragment
|
||||||
TableUtils.createTableIfNotExists(connectionSource, TempBasal.class);
|
TableUtils.createTableIfNotExists(connectionSource, TempBasal.class);
|
||||||
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
|
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
|
||||||
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
||||||
|
@ -172,6 +176,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
return getDao(DanaRHistoryRecord.class);
|
return getDao(DanaRHistoryRecord.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Dao<DbRequest, String> getDaoDbRequest() throws SQLException {
|
||||||
|
return getDao(DbRequest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long size(String database) {
|
||||||
|
return DatabaseUtils.queryNumEntries(getReadableDatabase(), database);
|
||||||
|
}
|
||||||
|
|
||||||
public List<BgReading> getBgreadingsDataFromTime(long mills, boolean ascending) {
|
public List<BgReading> getBgreadingsDataFromTime(long mills, boolean ascending) {
|
||||||
try {
|
try {
|
||||||
Dao<BgReading, Long> daoBgreadings = getDaoBgReadings();
|
Dao<BgReading, Long> daoBgreadings = getDaoBgReadings();
|
||||||
|
@ -189,6 +201,62 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
return new ArrayList<BgReading>();
|
return new ArrayList<BgReading>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DbRequests handling
|
||||||
|
|
||||||
|
public void create(DbRequest dbr) {
|
||||||
|
try {
|
||||||
|
getDaoDbRequest().create(dbr);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int delete(DbRequest dbr) {
|
||||||
|
try {
|
||||||
|
return getDaoDbRequest().delete(dbr);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int deleteDbRequest(String nsClientId) {
|
||||||
|
try {
|
||||||
|
return getDaoDbRequest().deleteById(nsClientId);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int deleteDbRequestbyMongoId(String action, String id) {
|
||||||
|
try {
|
||||||
|
QueryBuilder<DbRequest, String> queryBuilder = getDaoDbRequest().queryBuilder();
|
||||||
|
Where where = queryBuilder.where();
|
||||||
|
where.eq("_id", id).and().eq("action", action);
|
||||||
|
queryBuilder.limit(10L);
|
||||||
|
PreparedQuery<DbRequest> preparedQuery = queryBuilder.prepare();
|
||||||
|
List<DbRequest> dbList = getDaoDbRequest().query(preparedQuery);
|
||||||
|
if (dbList.size() != 1) {
|
||||||
|
log.error("deleteDbRequestbyMongoId query size: " + dbList.size());
|
||||||
|
} else {
|
||||||
|
//log.debug("Treatment findTreatmentById found: " + trList.get(0).log());
|
||||||
|
return delete(dbList.get(0));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAllDbRequests() {
|
||||||
|
try {
|
||||||
|
TableUtils.clearTable(connectionSource, DbRequest.class);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TREATMENT HANDLING
|
// TREATMENT HANDLING
|
||||||
|
|
||||||
public boolean isDataUnchanged(long time) {
|
public boolean isDataUnchanged(long time) {
|
||||||
|
|
|
@ -1,23 +1,47 @@
|
||||||
package info.nightscout.androidaps.plugins.NSClientInternal.data;
|
package info.nightscout.androidaps.db;
|
||||||
|
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
import com.google.common.hash.Hashing;
|
import com.google.common.hash.Hashing;
|
||||||
|
import com.j256.ormlite.field.DatabaseField;
|
||||||
|
import com.j256.ormlite.table.DatabaseTable;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mike on 27.02.2016.
|
* Created by mike on 27.02.2016.
|
||||||
* <p>
|
* <p>
|
||||||
* Allowed actions "dbAdd" || "dbUpdate" || "dbUpdateUnset" || "dbRemove"
|
* Allowed actions "dbAdd" || "dbUpdate" || "dbUpdateUnset" || "dbRemove"
|
||||||
*/
|
*/
|
||||||
|
@DatabaseTable(tableName = DatabaseHelper.DATABASE_DBREQUESTS)
|
||||||
public class DbRequest {
|
public class DbRequest {
|
||||||
public String action = null;
|
private static Logger log = LoggerFactory.getLogger(DbRequest.class);
|
||||||
public String collection = null;
|
|
||||||
public JSONObject data = null;
|
public String getNsClientID() {
|
||||||
public String _id = null;
|
return nsClientID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNsClientID(String nsClientID) {
|
||||||
|
this.nsClientID = nsClientID;
|
||||||
|
}
|
||||||
|
|
||||||
|
@DatabaseField(id = true, useGetSet = true)
|
||||||
public String nsClientID = null;
|
public String nsClientID = null;
|
||||||
|
|
||||||
|
@DatabaseField
|
||||||
|
public String action = null;
|
||||||
|
|
||||||
|
@DatabaseField
|
||||||
|
public String collection = null;
|
||||||
|
|
||||||
|
@DatabaseField
|
||||||
|
public String data = null;
|
||||||
|
|
||||||
|
@DatabaseField
|
||||||
|
public String _id = null;
|
||||||
|
|
||||||
public DbRequest() {
|
public DbRequest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +49,7 @@ public class DbRequest {
|
||||||
public DbRequest(String action, String collection, String nsClientID, JSONObject data) {
|
public DbRequest(String action, String collection, String nsClientID, JSONObject data) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.collection = collection;
|
this.collection = collection;
|
||||||
this.data = data;
|
this.data = data.toString();
|
||||||
this.nsClientID = nsClientID;
|
this.nsClientID = nsClientID;
|
||||||
this._id = "";
|
this._id = "";
|
||||||
}
|
}
|
||||||
|
@ -34,7 +58,7 @@ public class DbRequest {
|
||||||
public DbRequest(String action, String collection, String nsClientID, String _id, JSONObject data) {
|
public DbRequest(String action, String collection, String nsClientID, String _id, JSONObject data) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.collection = collection;
|
this.collection = collection;
|
||||||
this.data = data;
|
this.data = data.toString();
|
||||||
this.nsClientID = nsClientID;
|
this.nsClientID = nsClientID;
|
||||||
this._id = _id;
|
this._id = _id;
|
||||||
}
|
}
|
||||||
|
@ -43,7 +67,7 @@ public class DbRequest {
|
||||||
public DbRequest(String action, String collection, String nsClientID, String _id) {
|
public DbRequest(String action, String collection, String nsClientID, String _id) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.collection = collection;
|
this.collection = collection;
|
||||||
this.data = new JSONObject();
|
this.data = new JSONObject().toString();
|
||||||
this.nsClientID = nsClientID;
|
this.nsClientID = nsClientID;
|
||||||
this._id = _id;
|
this._id = _id;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +81,7 @@ public class DbRequest {
|
||||||
try {
|
try {
|
||||||
object.put("action", action);
|
object.put("action", action);
|
||||||
object.put("collection", collection);
|
object.put("collection", collection);
|
||||||
object.put("data", data);
|
object.put("data", new JSONObject(data));
|
||||||
if (_id != null) object.put("_id", _id);
|
if (_id != null) object.put("_id", _id);
|
||||||
if (nsClientID != null) object.put("nsClientID", nsClientID);
|
if (nsClientID != null) object.put("nsClientID", nsClientID);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
@ -74,7 +98,7 @@ public class DbRequest {
|
||||||
if (jsonObject.has("collection"))
|
if (jsonObject.has("collection"))
|
||||||
result.collection = jsonObject.getString("collection");
|
result.collection = jsonObject.getString("collection");
|
||||||
if (jsonObject.has("data"))
|
if (jsonObject.has("data"))
|
||||||
result.data = jsonObject.getJSONObject("data");
|
result.data = jsonObject.getJSONObject("data").toString();
|
||||||
if (jsonObject.has("_id"))
|
if (jsonObject.has("_id"))
|
||||||
result._id = jsonObject.getString("_id");
|
result._id = jsonObject.getString("_id");
|
||||||
if (jsonObject.has("nsClientID"))
|
if (jsonObject.has("nsClientID"))
|
|
@ -106,7 +106,7 @@ public class NSClientInternalFragment extends Fragment implements FragmentBase,
|
||||||
getPlugin().clearLog();
|
getPlugin().clearLog();
|
||||||
break;
|
break;
|
||||||
case R.id.nsclientinternal_clearqueue:
|
case R.id.nsclientinternal_clearqueue:
|
||||||
getPlugin().queue().reset();
|
getPlugin().queue().clearQueue();
|
||||||
break;
|
break;
|
||||||
case R.id.nsclientinternal_showqueue:
|
case R.id.nsclientinternal_showqueue:
|
||||||
MainApp.bus().post(new EventNSClientNewLog("QUEUE", getPlugin().queue().textList()));
|
MainApp.bus().post(new EventNSClientNewLog("QUEUE", getPlugin().queue().textList()));
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package info.nightscout.androidaps.plugins.NSClientInternal;
|
package info.nightscout.androidaps.plugins.NSClientInternal;
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
import com.j256.ormlite.dao.CloseableIterator;
|
||||||
import android.preference.PreferenceManager;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
@ -9,13 +8,14 @@ import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastQueueStatus;
|
import info.nightscout.androidaps.db.DatabaseHelper;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest;
|
import info.nightscout.androidaps.db.DbRequest;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService;
|
import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService;
|
||||||
import info.nightscout.utils.SP;
|
import info.nightscout.utils.SP;
|
||||||
|
|
||||||
|
@ -25,37 +25,30 @@ import info.nightscout.utils.SP;
|
||||||
public class UploadQueue {
|
public class UploadQueue {
|
||||||
private static Logger log = LoggerFactory.getLogger(UploadQueue.class);
|
private static Logger log = LoggerFactory.getLogger(UploadQueue.class);
|
||||||
|
|
||||||
public static HashMap<String, DbRequest> queue = null;
|
|
||||||
|
|
||||||
public UploadQueue() {
|
|
||||||
loadMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String status() {
|
public static String status() {
|
||||||
return "QUEUE: " + queue.size();
|
return "QUEUE: " + MainApp.getDbHelper().size(DatabaseHelper.DATABASE_DBREQUESTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int size() {
|
public static long size() {
|
||||||
return queue.size();
|
return MainApp.getDbHelper().size(DatabaseHelper.DATABASE_DBREQUESTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void add(final DbRequest dbr) {
|
public static void add(final DbRequest dbr) {
|
||||||
NSClientService.handler.post(new Runnable() {
|
NSClientService.handler.post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
log.debug("QUEUE adding: " + dbr.data.toString());
|
log.debug("QUEUE adding: " + dbr.data);
|
||||||
queue.put(dbr.hash(), dbr);
|
MainApp.getDbHelper().create(dbr);
|
||||||
saveMap();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void reset() {
|
public static void clearQueue() {
|
||||||
NSClientService.handler.post(new Runnable() {
|
NSClientService.handler.post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
log.debug("QUEUE Reset");
|
log.debug("QUEUE ClearQueue");
|
||||||
queue.clear();
|
MainApp.getDbHelper().deleteAllDbRequests();
|
||||||
log.debug(status());
|
log.debug(status());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -66,31 +59,18 @@ public class UploadQueue {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
long id = -1L;
|
String id;
|
||||||
if (record.has("NSCLIENT_ID")) {
|
if (record.has("NSCLIENT_ID")) {
|
||||||
id = record.getLong("NSCLIENT_ID");
|
id = record.getString("NSCLIENT_ID");
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
|
if (MainApp.getDbHelper().deleteDbRequest(id) == 1) {
|
||||||
while (iter.hasNext()) {
|
log.debug("Removed item from UploadQueue. " + UploadQueue.status());
|
||||||
DbRequest dbr = iter.next().getValue();
|
|
||||||
JSONObject data = dbr.data;
|
|
||||||
long nsclientId = -1;
|
|
||||||
if (data.has("NSCLIENT_ID")) {
|
|
||||||
nsclientId = data.getLong("NSCLIENT_ID");
|
|
||||||
if (nsclientId == id) {
|
|
||||||
log.debug("Removing item from UploadQueue");
|
|
||||||
iter.remove();
|
|
||||||
log.debug(UploadQueue.status());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
saveMap();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -99,59 +79,29 @@ public class UploadQueue {
|
||||||
NSClientService.handler.post(new Runnable() {
|
NSClientService.handler.post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
|
MainApp.getDbHelper().deleteDbRequestbyMongoId(action, _id);
|
||||||
while (iter.hasNext()) {
|
|
||||||
DbRequest dbr = iter.next().getValue();
|
|
||||||
if (dbr.action.equals(action) && dbr._id.equals(_id)) {
|
|
||||||
log.debug("Removing item from UploadQueue");
|
|
||||||
iter.remove();
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
log.debug("Failed removing item from UploadQueue");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
saveMap();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final static String KEY = "UploadQueue";
|
|
||||||
|
|
||||||
private static void saveMap() {
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
|
||||||
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
|
|
||||||
while (iter.hasNext()) {
|
|
||||||
DbRequest dbr = iter.next().getValue();
|
|
||||||
jsonArray.put(dbr.toJSON());
|
|
||||||
}
|
|
||||||
SP.putString(KEY, jsonArray.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadMap() {
|
|
||||||
queue = new HashMap<String, DbRequest>();
|
|
||||||
try {
|
|
||||||
String jsonString = SP.getString(KEY, (new JSONArray()).toString());
|
|
||||||
JSONArray jsonArray = new JSONArray(jsonString);
|
|
||||||
for (int i=0; i < jsonArray.length(); i++) {
|
|
||||||
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
||||||
DbRequest dbr = DbRequest.fromJSON(jsonObject);
|
|
||||||
queue.put(dbr.hash(), dbr);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String textList() {
|
public String textList() {
|
||||||
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
|
|
||||||
String result = "";
|
String result = "";
|
||||||
|
CloseableIterator<DbRequest> iterator = null;
|
||||||
while (iter.hasNext()) {
|
try {
|
||||||
DbRequest dbr = iter.next().getValue();
|
iterator = MainApp.getDbHelper().getDaoDbRequest().closeableIterator();
|
||||||
|
try {
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
DbRequest dbr = iterator.next();
|
||||||
result += "<br>";
|
result += "<br>";
|
||||||
result += dbr.action.toUpperCase() + " ";
|
result += dbr.action.toUpperCase() + " ";
|
||||||
result += dbr.collection + ": ";
|
result += dbr.collection + ": ";
|
||||||
result += dbr.data.toString();
|
result += dbr.data;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
iterator.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin;
|
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue;
|
import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest;
|
import info.nightscout.androidaps.db.DbRequest;
|
||||||
|
|
||||||
public class DBAccessReceiver extends BroadcastReceiver {
|
public class DBAccessReceiver extends BroadcastReceiver {
|
||||||
private static Logger log = LoggerFactory.getLogger(DBAccessReceiver.class);
|
private static Logger log = LoggerFactory.getLogger(DBAccessReceiver.class);
|
||||||
|
|
|
@ -13,6 +13,7 @@ import android.preference.PreferenceManager;
|
||||||
|
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
import com.google.common.hash.Hashing;
|
import com.google.common.hash.Hashing;
|
||||||
|
import com.j256.ormlite.dao.CloseableIterator;
|
||||||
import com.squareup.otto.Subscribe;
|
import com.squareup.otto.Subscribe;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
@ -22,6 +23,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -47,7 +49,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastP
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastSgvs;
|
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastSgvs;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastStatus;
|
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastStatus;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastTreatment;
|
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastTreatment;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest;
|
import info.nightscout.androidaps.db.DbRequest;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSCal;
|
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSCal;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
|
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
|
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
|
||||||
|
@ -518,7 +520,7 @@ public class NSClientService extends Service {
|
||||||
JSONObject message = new JSONObject();
|
JSONObject message = new JSONObject();
|
||||||
message.put("collection", dbr.collection);
|
message.put("collection", dbr.collection);
|
||||||
message.put("_id", dbr._id);
|
message.put("_id", dbr._id);
|
||||||
message.put("data", dbr.data);
|
message.put("data", new JSONObject(dbr.data));
|
||||||
mSocket.emit("dbUpdate", message, ack);
|
mSocket.emit("dbUpdate", message, ack);
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE " + dbr.collection, "Sent " + dbr._id));
|
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE " + dbr.collection, "Sent " + dbr._id));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
@ -532,7 +534,7 @@ public class NSClientService extends Service {
|
||||||
JSONObject message = new JSONObject();
|
JSONObject message = new JSONObject();
|
||||||
message.put("collection", dbr.collection);
|
message.put("collection", dbr.collection);
|
||||||
message.put("_id", dbr._id);
|
message.put("_id", dbr._id);
|
||||||
message.put("data", dbr.data);
|
message.put("data", new JSONObject(dbr.data));
|
||||||
mSocket.emit("dbUpdateUnset", message, ack);
|
mSocket.emit("dbUpdateUnset", message, ack);
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBUPDATEUNSET " + dbr.collection, "Sent " + dbr._id));
|
MainApp.bus().post(new EventNSClientNewLog("DBUPDATEUNSET " + dbr.collection, "Sent " + dbr._id));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
@ -568,7 +570,7 @@ public class NSClientService extends Service {
|
||||||
if (!isConnected || !hasWriteAuth) return;
|
if (!isConnected || !hasWriteAuth) return;
|
||||||
JSONObject message = new JSONObject();
|
JSONObject message = new JSONObject();
|
||||||
message.put("collection", dbr.collection);
|
message.put("collection", dbr.collection);
|
||||||
message.put("data", dbr.data);
|
message.put("data", new JSONObject(dbr.data));
|
||||||
mSocket.emit("dbAdd", message, ack);
|
mSocket.emit("dbAdd", message, ack);
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBADD " + dbr.collection, "Sent " + dbr.nsClientID));
|
MainApp.bus().post(new EventNSClientNewLog("DBADD " + dbr.collection, "Sent " + dbr.nsClientID));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
@ -632,7 +634,7 @@ public class NSClientService extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resend(final String reason) {
|
public void resend(final String reason) {
|
||||||
if (UploadQueue.queue.size() == 0)
|
if (UploadQueue.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!isConnected || !hasWriteAuth) return;
|
if (!isConnected || !hasWriteAuth) return;
|
||||||
|
@ -643,12 +645,14 @@ public class NSClientService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Logger log = LoggerFactory.getLogger(UploadQueue.class);
|
Logger log = LoggerFactory.getLogger(UploadQueue.class);
|
||||||
Iterator<Map.Entry<String, DbRequest>> iter = UploadQueue.queue.entrySet().iterator();
|
|
||||||
|
|
||||||
if (mSocket == null || !mSocket.connected()) return;
|
if (mSocket == null || !mSocket.connected()) return;
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
CloseableIterator<DbRequest> iterator = null;
|
||||||
DbRequest dbr = iter.next().getValue();
|
try {
|
||||||
|
iterator = MainApp.getDbHelper().getDaoDbRequest().closeableIterator();
|
||||||
|
try {
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
DbRequest dbr = iterator.next();
|
||||||
if (dbr.action.equals("dbAdd")) {
|
if (dbr.action.equals("dbAdd")) {
|
||||||
NSAddAck addAck = new NSAddAck();
|
NSAddAck addAck = new NSAddAck();
|
||||||
dbAdd(dbr, addAck);
|
dbAdd(dbr, addAck);
|
||||||
|
@ -663,6 +667,13 @@ public class NSClientService extends Service {
|
||||||
dbUpdateUnset(dbr, updateUnsetAck);
|
dbUpdateUnset(dbr, updateUnsetAck);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
iterator.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend ended: " + reason));
|
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend ended: " + reason));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue