dbupdate/remove, saving queue

This commit is contained in:
Milos Kozak 2017-02-17 21:24:30 +01:00
parent 14e37246c2
commit f33ccf278b
9 changed files with 143 additions and 208 deletions

View file

@ -41,8 +41,8 @@ import info.nightscout.utils.SP;
public class NSClientInternalPlugin implements PluginBase {
private static Logger log = LoggerFactory.getLogger(NSClientInternalPlugin.class);
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
boolean fragmentEnabled = false;
boolean fragmentVisible = false;
static public Handler handler;
static private HandlerThread handlerThread;

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.NSClientInternal;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
@ -44,17 +45,7 @@ public class UploadQueue {
public void run() {
log.debug("QUEUE adding: " + dbr.data.toString());
queue.put(dbr.hash(), dbr);
}
});
}
public static void put(final String hash, final DbRequest dbr) {
NSClientService.handler.post(new Runnable() {
@Override
public void run() {
queue.put(hash, dbr);
BroadcastQueueStatus bs = new BroadcastQueueStatus();
bs.handleNewStatus(queue.size(), MainApp.instance().getApplicationContext());
saveMap();
}
});
}
@ -99,32 +90,52 @@ public class UploadQueue {
} catch (JSONException e) {
e.printStackTrace();
}
saveMap();
}
});
}
final String KEY = "UploadQueue";
public static void removeID(final String action, final String _id) {
NSClientService.handler.post(new Runnable() {
@Override
public void run() {
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
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();
}
});
}
private void saveMap() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
JSONObject jsonObject = new JSONObject(queue);
String jsonString = jsonObject.toString();
SharedPreferences.Editor editor = sp.edit();
editor.remove(KEY).commit();
editor.putString(KEY, jsonString);
editor.commit();
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 JSONObject()).toString());
JSONObject jsonObject = new JSONObject(jsonString);
Iterator<String> keysItr = jsonObject.keys();
while (keysItr.hasNext()) {
String key = keysItr.next();
DbRequest value = (DbRequest) jsonObject.get(key);
queue.put(key, value);
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();
@ -137,6 +148,7 @@ public class UploadQueue {
while (iter.hasNext()) {
DbRequest dbr = iter.next().getValue();
result += "<br>";
result += dbr.action.toUpperCase() + " ";
result += dbr.collection + ": ";
result += dbr.data.toString();

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.plugins.NSClientInternal.acks;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,6 +17,8 @@ import io.socket.client.Ack;
public class NSAddAck implements Ack {
private static Logger log = LoggerFactory.getLogger(NSAddAck.class);
public String _id = null;
public String nsClientID = null;
public JSONObject json = null;
public void call(Object...args) {
// Regular response
try {
@ -24,10 +27,12 @@ public class NSAddAck implements Ack {
if (responsearray.length()>0) {
response = responsearray.getJSONObject(0);
_id = response.getString("_id");
json = response;
if (response.has("NSCLIENT_ID")) {
nsClientID = response.getString("NSCLIENT_ID");
}
synchronized(this) {
this.notify();
}
MainApp.bus().post(this);
return;
} catch (Exception e) {
}
@ -37,17 +42,11 @@ public class NSAddAck implements Ack {
if (response.has("result")) {
_id = null;
if (response.getString("result").equals("Not authorized")) {
synchronized(this) {
this.notify();
}
MainApp.bus().post(new EventNSClientRestart());
return;
}
log.debug("DBACCESS " + response.getString("result"));
}
synchronized(this) {
this.notify();
}
return;
} catch (Exception e) {
e.printStackTrace();

View file

@ -5,6 +5,7 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import io.socket.client.Ack;
/**
@ -13,6 +14,8 @@ import io.socket.client.Ack;
public class NSUpdateAck implements Ack {
private static Logger log = LoggerFactory.getLogger(NSUpdateAck.class);
public boolean result = false;
public String _id = null;
public String action;
public void call(Object...args) {
JSONObject response = (JSONObject)args[0];
if (response.has("result"))
@ -23,10 +26,14 @@ public class NSUpdateAck implements Ack {
result = true;
log.debug("Internal error: Missing _id returned on dbUpdate ack");
}
MainApp.bus().post(this);
} catch (JSONException e) {
}
synchronized(this) {
this.notify();
}
public NSUpdateAck(String action, String _id) {
super();
this.action = action;
this._id = _id;
}
}

View file

@ -3,11 +3,12 @@ package info.nightscout.androidaps.plugins.NSClientInternal.data;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by mike on 27.02.2016.
*
* <p>
* Allowed actions "dbAdd" || "dbUpdate" || "dbUpdateUnset" || "dbRemove"
*/
public class DbRequest {
@ -15,32 +16,72 @@ public class DbRequest {
public String collection = null;
public JSONObject data = null;
public String _id = null;
public String nsClientID = null;
public DbRequest() {
}
// dbAdd
public DbRequest(String action, String collection, JSONObject data) {
public DbRequest(String action, String collection, String nsClientID, JSONObject data) {
this.action = action;
this.collection = collection;
this.data = data;
this.nsClientID = nsClientID;
this._id = "";
}
// dbUpdate, dbUpdateUnset
public DbRequest(String action, String collection, String _id, JSONObject data) {
public DbRequest(String action, String collection, String nsClientID, String _id, JSONObject data) {
this.action = action;
this.collection = collection;
this.data = data;
this.nsClientID = nsClientID;
this._id = _id;
}
// dbRemove
public DbRequest(String action, String collection, String _id) {
public DbRequest(String action, String collection, String nsClientID, String _id) {
this.action = action;
this.collection = collection;
this.data = new JSONObject();
this.nsClientID = nsClientID;
this._id = _id;
}
public String hash() {
return Hashing.sha1().hashString(action + collection + _id + data.toString(), Charsets.UTF_8).toString();
}
public JSONObject toJSON() {
JSONObject object = new JSONObject();
try {
object.put("action", action);
object.put("collection", collection);
object.put("data", data);
if (_id != null) object.put("_id", _id);
if (nsClientID != null) object.put("nsClientID", nsClientID);
} catch (JSONException e) {
e.printStackTrace();
}
return object;
}
public static DbRequest fromJSON(JSONObject jsonObject) {
DbRequest result = new DbRequest();
try {
if (jsonObject.has("action"))
result.action = jsonObject.getString("action");
if (jsonObject.has("collection"))
result.collection = jsonObject.getString("collection");
if (jsonObject.has("data"))
result.data = jsonObject.getJSONObject("data");
if (jsonObject.has("_id"))
result._id = jsonObject.getString("_id");
if (jsonObject.has("nsClientID"))
result.nsClientID = jsonObject.getString("nsClientID");
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
}

View file

@ -55,8 +55,9 @@ public class DBAccessReceiver extends BroadcastReceiver {
data = new JSONObject();
}
// mark by id
Long nsclientid = new Date().getTime();
try {
data.put("NSCLIENT_ID", (new Date()).getTime());
data.put("NSCLIENT_ID", nsclientid);
} catch (JSONException e) {
e.printStackTrace();
}
@ -67,10 +68,10 @@ public class DBAccessReceiver extends BroadcastReceiver {
}
if (action.equals("dbRemove")) {
DbRequest dbr = new DbRequest(action, collection, _id);
DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), _id);
UploadQueue.add(dbr);
} else {
DbRequest dbr = new DbRequest(action, collection, data);
DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), data);
UploadQueue.add(dbr);
}

View file

@ -492,181 +492,75 @@ public class NSClientService extends Service {
public void dbUpdate(DbRequest dbr, NSUpdateAck ack) {
try {
if (!isConnected) return;
if (uploading) {
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE", "Busy, adding to queue"));
return;
}
uploading = true;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("_id", dbr._id);
message.put("data", dbr.data);
mSocket.emit("dbUpdate", message, ack);
synchronized (ack) {
try {
ack.wait(timeToWaitForResponseInMs);
} catch (InterruptedException e) {
}
}
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE", "Sent " + dbr._id));
} catch (JSONException e) {
e.printStackTrace();
return;
}
uploading = false;
}
public void dbUpdate(DbRequest dbr) {
try {
if (!isConnected) return;
if (uploading) {
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE", "Busy, adding to queue"));
return;
}
uploading = true;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("_id", dbr._id);
message.put("data", dbr.data);
mSocket.emit("dbUpdate", message);
} catch (JSONException e) {
e.printStackTrace();
return;
}
uploading = false;
}
public void dbUpdateUnset(DbRequest dbr, NSUpdateAck ack) {
try {
if (!isConnected) return;
if (uploading) {
MainApp.bus().post(new EventNSClientNewLog("DBUPUNSET", "Busy, adding to queue"));
return;
}
uploading = true;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("_id", dbr._id);
message.put("data", dbr.data);
mSocket.emit("dbUpdateUnset", message, ack);
synchronized (ack) {
try {
ack.wait(timeToWaitForResponseInMs);
} catch (InterruptedException e) {
}
}
MainApp.bus().post(new EventNSClientNewLog("DBUPDATEUNSET", "Sent " + dbr._id));
} catch (JSONException e) {
e.printStackTrace();
return;
}
uploading = false;
}
public void dbUpdateUnset(DbRequest dbr) {
try {
if (!isConnected) return;
if (uploading) {
MainApp.bus().post(new EventNSClientNewLog("DBUPUNSET", "Busy, adding to queue"));
return;
}
uploading = true;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("_id", dbr._id);
message.put("data", dbr.data);
mSocket.emit("dbUpdateUnset", message);
} catch (JSONException e) {
e.printStackTrace();
return;
}
uploading = false;
}
public void dbRemove(DbRequest dbr, NSUpdateAck ack) {
try {
if (!isConnected) return;
if (uploading) {
MainApp.bus().post(new EventNSClientNewLog("DBREMOVE", "Busy, adding to queue"));
return;
}
uploading = true;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("_id", dbr._id);
mSocket.emit("dbRemove", message, ack);
synchronized (ack) {
try {
ack.wait(timeToWaitForResponseInMs);
} catch (InterruptedException e) {
}
}
MainApp.bus().post(new EventNSClientNewLog("DBREMOVE", "Sent " + dbr._id));
} catch (JSONException e) {
e.printStackTrace();
return;
}
uploading = false;
}
public void dbRemove(DbRequest dbr) {
try {
if (!isConnected) return;
if (uploading) {
MainApp.bus().post(new EventNSClientNewLog("DBREMOVE", "Busy, adding to queue"));
return;
@Subscribe
public void onStatusEvent(NSUpdateAck ack) {
if (ack.result) {
uploadQueue.removeID(ack.action, ack._id);
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked " + ack._id));
} else {
MainApp.bus().post(new EventNSClientNewLog("ERROR", "DBUPDATE/DBREMOVE Unknown response"));
}
uploading = true;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("_id", dbr._id);
mSocket.emit("dbRemove", message);
} catch (JSONException e) {
e.printStackTrace();
return;
}
uploading = false;
}
public void dbAdd(DbRequest dbr, NSAddAck ack) {
try {
if (!isConnected) return;
if (uploading) {
MainApp.bus().post(new EventNSClientNewLog("DBADD", "Busy, adding to queue"));
return;
}
uploading = true;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("data", dbr.data);
mSocket.emit("dbAdd", message, ack);
synchronized (ack) {
try {
ack.wait(timeToWaitForResponseInMs);
} catch (InterruptedException e) {
}
}
MainApp.bus().post(new EventNSClientNewLog("DBADD", "Sent " + dbr.nsClientID));
} catch (JSONException e) {
e.printStackTrace();
return;
}
uploading = false;
}
public void dbAdd(DbRequest dbr) {
try {
if (!isConnected) return;
if (uploading) {
MainApp.bus().post(new EventNSClientNewLog("DBADD", "Busy, adding to queue"));
return;
@Subscribe
public void onStatusEvent(NSAddAck ack) {
if (ack.nsClientID != null) {
uploadQueue.removeID(ack.json);
MainApp.bus().post(new EventNSClientNewLog("DBADD", "Acked " + ack.nsClientID));
} else {
MainApp.bus().post(new EventNSClientNewLog("ERROR", "DBADD Unknown response"));
}
uploading = true;
JSONObject message = new JSONObject();
message.put("collection", dbr.collection);
message.put("data", dbr.data);
mSocket.emit("dbAdd", message);
} catch (JSONException e) {
e.printStackTrace();
return;
}
uploading = false;
}
public void doPing() {
@ -735,42 +629,15 @@ public class NSClientService extends Service {
if (dbr.action.equals("dbAdd")) {
NSAddAck addAck = new NSAddAck();
dbAdd(dbr, addAck);
if (addAck._id == null) {
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "No response on dbAdd"));
return;
}
if (Config.detailedLog)
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbAdd processed: " + dbr.data.toString()));
else
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbAdd processed"));
iter.remove();
} else if (dbr.action.equals("dbRemove")) {
NSUpdateAck removeAck = new NSUpdateAck();
NSUpdateAck removeAck = new NSUpdateAck(dbr.action, dbr._id);
dbRemove(dbr, removeAck);
if (!removeAck.result) {
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "No response on dbRemove"));
return;
}
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbRemove processed: " + dbr._id));
iter.remove();
} else if (dbr.action.equals("dbUpdate")) {
NSUpdateAck updateAck = new NSUpdateAck();
NSUpdateAck updateAck = new NSUpdateAck(dbr.action, dbr._id);
dbUpdate(dbr, updateAck);
if (!updateAck.result) {
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "No response on dbUpdate"));
return;
}
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbUpdate processed: " + dbr._id));
iter.remove();
} else if (dbr.action.equals("dbUpdateUnset")) {
NSUpdateAck updateUnsetAck = new NSUpdateAck();
NSUpdateAck updateUnsetAck = new NSUpdateAck(dbr.action, dbr._id);
dbUpdateUnset(dbr, updateUnsetAck);
if (!updateUnsetAck.result) {
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "No response on dbUpdateUnset"));
return;
}
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "dbUpdateUnset processed: " + dbr._id));
iter.remove();
}
}
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend ended: " + reason));

View file

@ -1,8 +1,5 @@
package info.nightscout.androidaps.plugins.Treatments;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
@ -14,15 +11,15 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.MealData;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
import info.nightscout.utils.SafeParse;
import info.nightscout.utils.SP;
/**
* Created by mike on 05.08.2016.
@ -118,7 +115,6 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
@Override
public IobTotal getCalculationToTime(long time) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
IobTotal total = new IobTotal(time);
if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet

View file

@ -75,4 +75,16 @@ public class SP {
editor.putBoolean(MainApp.sResources.getString(resourceID), value);
editor.apply();
}
static public void putString(String key, String value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
static public void putString(int resourceID, String value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(MainApp.sResources.getString(resourceID), value);
editor.apply();
}
}