more logging

This commit is contained in:
Milos Kozak 2020-01-27 23:07:48 +01:00
parent 404bf7e633
commit a4fa64efe2
3 changed files with 20 additions and 52 deletions

View file

@ -41,79 +41,55 @@ public class DbRequest {
}
// dbAdd
public DbRequest(String action, String collection, JSONObject data) {
public DbRequest(String action, String collection, JSONObject json) {
this.action = action;
this.collection = collection;
this.nsClientID = "" + DateUtil.now();
try {
data.put("NSCLIENT_ID", nsClientID);
json.put("NSCLIENT_ID", nsClientID);
} catch (JSONException e) {
e.printStackTrace();
}
this.data = data.toString();
this.data = json.toString();
this._id = "";
}
// dbUpdate, dbUpdateUnset
public DbRequest(String action, String collection, String _id, JSONObject data) {
public DbRequest(String action, String collection, String _id, JSONObject json) {
this.action = action;
this.collection = collection;
this.nsClientID = "" + DateUtil.now();
try {
data.put("NSCLIENT_ID", nsClientID);
json.put("NSCLIENT_ID", nsClientID);
} catch (JSONException e) {
e.printStackTrace();
}
this.data = data.toString();
this.data = json.toString();
this._id = _id;
}
// dbRemove
public DbRequest(String action, String collection,
String _id) {
JSONObject data = new JSONObject();
JSONObject json = new JSONObject();
this.action = action;
this.collection = collection;
this.nsClientID = "" + DateUtil.now();
try {
data.put("NSCLIENT_ID", nsClientID);
json.put("NSCLIENT_ID", nsClientID);
} catch (JSONException e) {
e.printStackTrace();
}
this.data = data.toString();
this.data = json.toString();
this._id = _id;
}
public JSONObject toJSON() {
JSONObject object = new JSONObject();
try {
object.put("action", action);
object.put("collection", collection);
object.put("data", new JSONObject(data));
if (_id != null) object.put("_id", _id);
if (nsClientID != null) object.put("nsClientID", nsClientID);
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
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").toString();
if (jsonObject.has("_id"))
result._id = jsonObject.getString("_id");
if (jsonObject.has("nsClientID"))
result.nsClientID = jsonObject.getString("nsClientID");
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return result;
public String log() {
return
"\nnsClientID:" + nsClientID +
"\naction:" + action +
"\ncollection:" + collection +
"\ndata:" + data +
"\n_id:" + _id;
}
}

View file

@ -323,7 +323,9 @@ public class NSUpload {
prebolus.put("created_at", DateUtil.toISOString(preBolusDate));
uploadCareportalEntryToNS(prebolus);
}
UploadQueue.add(new DbRequest("dbAdd", "treatments", data));
DbRequest dbr = new DbRequest("dbAdd", "treatments", data);
log.debug("Prepared: " + dbr.log());
UploadQueue.add(dbr);
} catch (Exception e) {
log.error("Unhandled exception", e);
}

View file

@ -43,27 +43,17 @@ public class UploadQueue {
public static void add(final DbRequest dbr) {
startService();
if (NSClientService.handler != null) {
NSClientService.handler.post(() -> {
if (L.isEnabled(L.NSCLIENT))
log.debug("Adding to queue: " + dbr.data);
log.debug("Adding to queue: " + dbr.log());
try {
MainApp.getDbHelper().create(dbr);
} catch (Exception e) {
log.error("Unhandled exception", e);
dbr.nsClientID += "1";
try {
MainApp.getDbHelper().create(dbr);
} catch (Exception e1) {
log.error("Unhandled exception", e1);
}
}
NSClientPlugin plugin = NSClientPlugin.getPlugin();
if (plugin != null) {
plugin.resend("newdata");
}
});
}
}
public static void clearQueue() {