dbupdate/remove, saving queue
This commit is contained in:
parent
14e37246c2
commit
f33ccf278b
9 changed files with 143 additions and 208 deletions
|
@ -41,8 +41,8 @@ import info.nightscout.utils.SP;
|
||||||
public class NSClientInternalPlugin implements PluginBase {
|
public class NSClientInternalPlugin implements PluginBase {
|
||||||
private static Logger log = LoggerFactory.getLogger(NSClientInternalPlugin.class);
|
private static Logger log = LoggerFactory.getLogger(NSClientInternalPlugin.class);
|
||||||
|
|
||||||
boolean fragmentEnabled = true;
|
boolean fragmentEnabled = false;
|
||||||
boolean fragmentVisible = true;
|
boolean fragmentVisible = false;
|
||||||
|
|
||||||
static public Handler handler;
|
static public Handler handler;
|
||||||
static private HandlerThread handlerThread;
|
static private HandlerThread handlerThread;
|
||||||
|
|
|
@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.NSClientInternal;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -44,17 +45,7 @@ public class UploadQueue {
|
||||||
public void run() {
|
public void run() {
|
||||||
log.debug("QUEUE adding: " + dbr.data.toString());
|
log.debug("QUEUE adding: " + dbr.data.toString());
|
||||||
queue.put(dbr.hash(), dbr);
|
queue.put(dbr.hash(), dbr);
|
||||||
}
|
saveMap();
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -99,32 +90,52 @@ public class UploadQueue {
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
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() {
|
final static String KEY = "UploadQueue";
|
||||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
|
|
||||||
JSONObject jsonObject = new JSONObject(queue);
|
private static void saveMap() {
|
||||||
String jsonString = jsonObject.toString();
|
JSONArray jsonArray = new JSONArray();
|
||||||
SharedPreferences.Editor editor = sp.edit();
|
Iterator<Map.Entry<String, DbRequest>> iter = queue.entrySet().iterator();
|
||||||
editor.remove(KEY).commit();
|
while (iter.hasNext()) {
|
||||||
editor.putString(KEY, jsonString);
|
DbRequest dbr = iter.next().getValue();
|
||||||
editor.commit();
|
jsonArray.put(dbr.toJSON());
|
||||||
|
}
|
||||||
|
SP.putString(KEY, jsonArray.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadMap() {
|
private void loadMap() {
|
||||||
queue = new HashMap<String, DbRequest>();
|
queue = new HashMap<String, DbRequest>();
|
||||||
try {
|
try {
|
||||||
String jsonString = SP.getString(KEY, (new JSONObject()).toString());
|
String jsonString = SP.getString(KEY, (new JSONArray()).toString());
|
||||||
JSONObject jsonObject = new JSONObject(jsonString);
|
JSONArray jsonArray = new JSONArray(jsonString);
|
||||||
Iterator<String> keysItr = jsonObject.keys();
|
for (int i=0; i < jsonArray.length(); i++) {
|
||||||
while (keysItr.hasNext()) {
|
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
||||||
String key = keysItr.next();
|
DbRequest dbr = DbRequest.fromJSON(jsonObject);
|
||||||
DbRequest value = (DbRequest) jsonObject.get(key);
|
queue.put(dbr.hash(), dbr);
|
||||||
queue.put(key, value);
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -137,6 +148,7 @@ public class UploadQueue {
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
DbRequest dbr = iter.next().getValue();
|
DbRequest dbr = iter.next().getValue();
|
||||||
|
result += "<br>";
|
||||||
result += dbr.action.toUpperCase() + " ";
|
result += dbr.action.toUpperCase() + " ";
|
||||||
result += dbr.collection + ": ";
|
result += dbr.collection + ": ";
|
||||||
result += dbr.data.toString();
|
result += dbr.data.toString();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package info.nightscout.androidaps.plugins.NSClientInternal.acks;
|
package info.nightscout.androidaps.plugins.NSClientInternal.acks;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -16,6 +17,8 @@ import io.socket.client.Ack;
|
||||||
public class NSAddAck implements Ack {
|
public class NSAddAck implements Ack {
|
||||||
private static Logger log = LoggerFactory.getLogger(NSAddAck.class);
|
private static Logger log = LoggerFactory.getLogger(NSAddAck.class);
|
||||||
public String _id = null;
|
public String _id = null;
|
||||||
|
public String nsClientID = null;
|
||||||
|
public JSONObject json = null;
|
||||||
public void call(Object...args) {
|
public void call(Object...args) {
|
||||||
// Regular response
|
// Regular response
|
||||||
try {
|
try {
|
||||||
|
@ -24,10 +27,12 @@ public class NSAddAck implements Ack {
|
||||||
if (responsearray.length()>0) {
|
if (responsearray.length()>0) {
|
||||||
response = responsearray.getJSONObject(0);
|
response = responsearray.getJSONObject(0);
|
||||||
_id = response.getString("_id");
|
_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;
|
return;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
|
@ -37,17 +42,11 @@ public class NSAddAck implements Ack {
|
||||||
if (response.has("result")) {
|
if (response.has("result")) {
|
||||||
_id = null;
|
_id = null;
|
||||||
if (response.getString("result").equals("Not authorized")) {
|
if (response.getString("result").equals("Not authorized")) {
|
||||||
synchronized(this) {
|
|
||||||
this.notify();
|
|
||||||
}
|
|
||||||
MainApp.bus().post(new EventNSClientRestart());
|
MainApp.bus().post(new EventNSClientRestart());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log.debug("DBACCESS " + response.getString("result"));
|
log.debug("DBACCESS " + response.getString("result"));
|
||||||
}
|
}
|
||||||
synchronized(this) {
|
|
||||||
this.notify();
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -5,6 +5,7 @@ import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.MainApp;
|
||||||
import io.socket.client.Ack;
|
import io.socket.client.Ack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,6 +14,8 @@ import io.socket.client.Ack;
|
||||||
public class NSUpdateAck implements Ack {
|
public class NSUpdateAck implements Ack {
|
||||||
private static Logger log = LoggerFactory.getLogger(NSUpdateAck.class);
|
private static Logger log = LoggerFactory.getLogger(NSUpdateAck.class);
|
||||||
public boolean result = false;
|
public boolean result = false;
|
||||||
|
public String _id = null;
|
||||||
|
public String action;
|
||||||
public void call(Object...args) {
|
public void call(Object...args) {
|
||||||
JSONObject response = (JSONObject)args[0];
|
JSONObject response = (JSONObject)args[0];
|
||||||
if (response.has("result"))
|
if (response.has("result"))
|
||||||
|
@ -23,10 +26,14 @@ public class NSUpdateAck implements Ack {
|
||||||
result = true;
|
result = true;
|
||||||
log.debug("Internal error: Missing _id returned on dbUpdate ack");
|
log.debug("Internal error: Missing _id returned on dbUpdate ack");
|
||||||
}
|
}
|
||||||
|
MainApp.bus().post(this);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
}
|
}
|
||||||
synchronized(this) {
|
}
|
||||||
this.notify();
|
|
||||||
}
|
public NSUpdateAck(String action, String _id) {
|
||||||
|
super();
|
||||||
|
this.action = action;
|
||||||
|
this._id = _id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,12 @@ package info.nightscout.androidaps.plugins.NSClientInternal.data;
|
||||||
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 org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mike on 27.02.2016.
|
* Created by mike on 27.02.2016.
|
||||||
*
|
* <p>
|
||||||
* Allowed actions "dbAdd" || "dbUpdate" || "dbUpdateUnset" || "dbRemove"
|
* Allowed actions "dbAdd" || "dbUpdate" || "dbUpdateUnset" || "dbRemove"
|
||||||
*/
|
*/
|
||||||
public class DbRequest {
|
public class DbRequest {
|
||||||
|
@ -15,32 +16,72 @@ public class DbRequest {
|
||||||
public String collection = null;
|
public String collection = null;
|
||||||
public JSONObject data = null;
|
public JSONObject data = null;
|
||||||
public String _id = null;
|
public String _id = null;
|
||||||
|
public String nsClientID = null;
|
||||||
|
|
||||||
|
public DbRequest() {
|
||||||
|
}
|
||||||
|
|
||||||
// dbAdd
|
// dbAdd
|
||||||
public DbRequest(String action, String collection, 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;
|
||||||
|
this.nsClientID = nsClientID;
|
||||||
this._id = "";
|
this._id = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// dbUpdate, dbUpdateUnset
|
// 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.action = action;
|
||||||
this.collection = collection;
|
this.collection = collection;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
|
this.nsClientID = nsClientID;
|
||||||
this._id = _id;
|
this._id = _id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// dbRemove
|
// dbRemove
|
||||||
public DbRequest(String action, String collection, 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();
|
||||||
|
this.nsClientID = nsClientID;
|
||||||
this._id = _id;
|
this._id = _id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String hash() {
|
public String hash() {
|
||||||
return Hashing.sha1().hashString(action + collection + _id + data.toString(), Charsets.UTF_8).toString();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,8 +55,9 @@ public class DBAccessReceiver extends BroadcastReceiver {
|
||||||
data = new JSONObject();
|
data = new JSONObject();
|
||||||
}
|
}
|
||||||
// mark by id
|
// mark by id
|
||||||
|
Long nsclientid = new Date().getTime();
|
||||||
try {
|
try {
|
||||||
data.put("NSCLIENT_ID", (new Date()).getTime());
|
data.put("NSCLIENT_ID", nsclientid);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -67,10 +68,10 @@ public class DBAccessReceiver extends BroadcastReceiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action.equals("dbRemove")) {
|
if (action.equals("dbRemove")) {
|
||||||
DbRequest dbr = new DbRequest(action, collection, _id);
|
DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), _id);
|
||||||
UploadQueue.add(dbr);
|
UploadQueue.add(dbr);
|
||||||
} else {
|
} else {
|
||||||
DbRequest dbr = new DbRequest(action, collection, data);
|
DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), data);
|
||||||
UploadQueue.add(dbr);
|
UploadQueue.add(dbr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -492,181 +492,75 @@ public class NSClientService extends Service {
|
||||||
public void dbUpdate(DbRequest dbr, NSUpdateAck ack) {
|
public void dbUpdate(DbRequest dbr, NSUpdateAck ack) {
|
||||||
try {
|
try {
|
||||||
if (!isConnected) return;
|
if (!isConnected) return;
|
||||||
if (uploading) {
|
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE", "Busy, adding to queue"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
uploading = true;
|
|
||||||
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", dbr.data);
|
||||||
mSocket.emit("dbUpdate", message, ack);
|
mSocket.emit("dbUpdate", message, ack);
|
||||||
synchronized (ack) {
|
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE", "Sent " + dbr._id));
|
||||||
try {
|
|
||||||
ack.wait(timeToWaitForResponseInMs);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
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) {
|
public void dbUpdateUnset(DbRequest dbr, NSUpdateAck ack) {
|
||||||
try {
|
try {
|
||||||
if (!isConnected) return;
|
if (!isConnected) return;
|
||||||
if (uploading) {
|
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBUPUNSET", "Busy, adding to queue"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
uploading = true;
|
|
||||||
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", dbr.data);
|
||||||
mSocket.emit("dbUpdateUnset", message, ack);
|
mSocket.emit("dbUpdateUnset", message, ack);
|
||||||
synchronized (ack) {
|
MainApp.bus().post(new EventNSClientNewLog("DBUPDATEUNSET", "Sent " + dbr._id));
|
||||||
try {
|
|
||||||
ack.wait(timeToWaitForResponseInMs);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
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) {
|
public void dbRemove(DbRequest dbr, NSUpdateAck ack) {
|
||||||
try {
|
try {
|
||||||
if (!isConnected) return;
|
if (!isConnected) return;
|
||||||
if (uploading) {
|
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBREMOVE", "Busy, adding to queue"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
uploading = true;
|
|
||||||
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);
|
||||||
mSocket.emit("dbRemove", message, ack);
|
mSocket.emit("dbRemove", message, ack);
|
||||||
synchronized (ack) {
|
MainApp.bus().post(new EventNSClientNewLog("DBREMOVE", "Sent " + dbr._id));
|
||||||
try {
|
|
||||||
ack.wait(timeToWaitForResponseInMs);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
uploading = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dbRemove(DbRequest dbr) {
|
@Subscribe
|
||||||
try {
|
public void onStatusEvent(NSUpdateAck ack) {
|
||||||
if (!isConnected) return;
|
if (ack.result) {
|
||||||
if (uploading) {
|
uploadQueue.removeID(ack.action, ack._id);
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBREMOVE", "Busy, adding to queue"));
|
MainApp.bus().post(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked " + ack._id));
|
||||||
return;
|
} 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) {
|
public void dbAdd(DbRequest dbr, NSAddAck ack) {
|
||||||
try {
|
try {
|
||||||
if (!isConnected) return;
|
if (!isConnected) return;
|
||||||
if (uploading) {
|
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBADD", "Busy, adding to queue"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
uploading = true;
|
|
||||||
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", dbr.data);
|
||||||
mSocket.emit("dbAdd", message, ack);
|
mSocket.emit("dbAdd", message, ack);
|
||||||
synchronized (ack) {
|
MainApp.bus().post(new EventNSClientNewLog("DBADD", "Sent " + dbr.nsClientID));
|
||||||
try {
|
|
||||||
ack.wait(timeToWaitForResponseInMs);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
uploading = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dbAdd(DbRequest dbr) {
|
@Subscribe
|
||||||
try {
|
public void onStatusEvent(NSAddAck ack) {
|
||||||
if (!isConnected) return;
|
if (ack.nsClientID != null) {
|
||||||
if (uploading) {
|
uploadQueue.removeID(ack.json);
|
||||||
MainApp.bus().post(new EventNSClientNewLog("DBADD", "Busy, adding to queue"));
|
MainApp.bus().post(new EventNSClientNewLog("DBADD", "Acked " + ack.nsClientID));
|
||||||
return;
|
} 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() {
|
public void doPing() {
|
||||||
|
@ -735,42 +629,15 @@ public class NSClientService extends Service {
|
||||||
if (dbr.action.equals("dbAdd")) {
|
if (dbr.action.equals("dbAdd")) {
|
||||||
NSAddAck addAck = new NSAddAck();
|
NSAddAck addAck = new NSAddAck();
|
||||||
dbAdd(dbr, addAck);
|
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")) {
|
} else if (dbr.action.equals("dbRemove")) {
|
||||||
NSUpdateAck removeAck = new NSUpdateAck();
|
NSUpdateAck removeAck = new NSUpdateAck(dbr.action, dbr._id);
|
||||||
dbRemove(dbr, removeAck);
|
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")) {
|
} else if (dbr.action.equals("dbUpdate")) {
|
||||||
NSUpdateAck updateAck = new NSUpdateAck();
|
NSUpdateAck updateAck = new NSUpdateAck(dbr.action, dbr._id);
|
||||||
dbUpdate(dbr, updateAck);
|
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")) {
|
} else if (dbr.action.equals("dbUpdateUnset")) {
|
||||||
NSUpdateAck updateUnsetAck = new NSUpdateAck();
|
NSUpdateAck updateUnsetAck = new NSUpdateAck(dbr.action, dbr._id);
|
||||||
dbUpdateUnset(dbr, updateUnsetAck);
|
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));
|
MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend ended: " + reason));
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
package info.nightscout.androidaps.plugins.Treatments;
|
package info.nightscout.androidaps.plugins.Treatments;
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.preference.PreferenceManager;
|
|
||||||
|
|
||||||
import com.squareup.otto.Subscribe;
|
import com.squareup.otto.Subscribe;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -14,15 +11,15 @@ import java.util.List;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.R;
|
import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.data.Iob;
|
import info.nightscout.androidaps.data.Iob;
|
||||||
|
import info.nightscout.androidaps.data.IobTotal;
|
||||||
import info.nightscout.androidaps.data.MealData;
|
import info.nightscout.androidaps.data.MealData;
|
||||||
import info.nightscout.androidaps.db.Treatment;
|
import info.nightscout.androidaps.db.Treatment;
|
||||||
import info.nightscout.androidaps.events.EventTreatmentChange;
|
import info.nightscout.androidaps.events.EventTreatmentChange;
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||||
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
|
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
|
||||||
import info.nightscout.androidaps.data.IobTotal;
|
|
||||||
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
||||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
|
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.
|
* Created by mike on 05.08.2016.
|
||||||
|
@ -118,7 +115,6 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IobTotal getCalculationToTime(long time) {
|
public IobTotal getCalculationToTime(long time) {
|
||||||
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
|
|
||||||
IobTotal total = new IobTotal(time);
|
IobTotal total = new IobTotal(time);
|
||||||
|
|
||||||
if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet
|
if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet
|
||||||
|
|
|
@ -75,4 +75,16 @@ public class SP {
|
||||||
editor.putBoolean(MainApp.sResources.getString(resourceID), value);
|
editor.putBoolean(MainApp.sResources.getString(resourceID), value);
|
||||||
editor.apply();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue