TriggerAnd-Or json saving, loading
This commit is contained in:
parent
4ba1adabf4
commit
ca7e9d2a7f
5 changed files with 141 additions and 3 deletions
|
@ -1,6 +1,32 @@
|
|||
package info.nightscout.androidaps.plugins.general.automation.actions;
|
||||
|
||||
public abstract class Trigger {
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
abstract class Trigger {
|
||||
|
||||
Trigger() {
|
||||
}
|
||||
|
||||
Trigger(String js) {
|
||||
fromJSON(js);
|
||||
}
|
||||
|
||||
abstract boolean shouldRun();
|
||||
abstract String toJSON();
|
||||
abstract Trigger fromJSON(String data);
|
||||
|
||||
static Trigger instantiate(JSONObject object) {
|
||||
try {
|
||||
String type = object.getString("type");
|
||||
String data = object.getString("data");
|
||||
Class clazz = Class.forName(type);
|
||||
return ((Trigger) clazz.newInstance()).fromJSON(data);
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package info.nightscout.androidaps.plugins.general.automation.actions;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -17,6 +21,36 @@ public class TriggerAnd extends Trigger {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized String toJSON() {
|
||||
JSONObject o = new JSONObject();
|
||||
try {
|
||||
o.put("type", TriggerAnd.class.getName());
|
||||
JSONArray array = new JSONArray();
|
||||
for (Trigger t : list) {
|
||||
array.put(t.toJSON());
|
||||
}
|
||||
o.put("data", array.toString());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return o.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
Trigger fromJSON(String data) {
|
||||
try {
|
||||
JSONArray array = new JSONArray(data);
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
Trigger newItem = instantiate(new JSONObject(array.getString(i)));
|
||||
list.add(newItem);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
synchronized void add(Trigger t) {
|
||||
list.add(t);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package info.nightscout.androidaps.plugins.general.automation.actions;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -17,6 +21,36 @@ public class TriggerOr extends Trigger {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized String toJSON() {
|
||||
JSONObject o = new JSONObject();
|
||||
try {
|
||||
o.put("type", TriggerOr.class.getName());
|
||||
JSONArray array = new JSONArray();
|
||||
for (Trigger t : list) {
|
||||
array.put(t.toJSON());
|
||||
}
|
||||
o.put("data", array.toString());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return o.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
Trigger fromJSON(String data) {
|
||||
try {
|
||||
JSONArray array = new JSONArray(data);
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
Trigger newItem = instantiate(new JSONObject(array.getString(i)));
|
||||
list.add(newItem);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
synchronized void add(Trigger t) {
|
||||
list.add(t);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package info.nightscout.androidaps.plugins.general.automation.actions;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -28,10 +30,30 @@ public class TriggerAndTest {
|
|||
Assert.assertEquals(t2, t.get(0));
|
||||
Assert.assertEquals(t3, t.get(1));
|
||||
|
||||
t.remove(t2);
|
||||
Assert.assertTrue(t.remove(t2));
|
||||
Assert.assertTrue(t.size() == 1);
|
||||
Assert.assertEquals(t3, t.get(0));
|
||||
|
||||
Assert.assertTrue(t.shouldRun());
|
||||
}
|
||||
|
||||
String empty = "{\"data\":\"[]\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.TriggerAnd\"}";
|
||||
String oneItem = "{\"data\":\"[\\\"{\\\\\\\"data\\\\\\\":\\\\\\\"[]\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"info.nightscout.androidaps.plugins.general.automation.actions.TriggerAnd\\\\\\\"}\\\"]\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.TriggerAnd\"}";
|
||||
|
||||
@Test
|
||||
public void toJSONTest() {
|
||||
TriggerAnd t = new TriggerAnd();
|
||||
Assert.assertEquals(empty, t.toJSON());
|
||||
t.add(new TriggerAnd());
|
||||
Assert.assertEquals(oneItem, t.toJSON());
|
||||
}
|
||||
@Test
|
||||
public void fromJSONTest() throws JSONException {
|
||||
TriggerAnd t = new TriggerAnd();
|
||||
t.add(new TriggerAnd());
|
||||
|
||||
TriggerAnd t2 = (TriggerAnd) Trigger.instantiate(new JSONObject(t.toJSON()));
|
||||
Assert.assertEquals(1, t2.size());
|
||||
Assert.assertTrue(t2.get(0) instanceof TriggerAnd);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package info.nightscout.androidaps.plugins.general.automation.actions;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -28,10 +30,30 @@ public class TriggerOrTest {
|
|||
Assert.assertEquals(t2, t.get(0));
|
||||
Assert.assertEquals(t3, t.get(1));
|
||||
|
||||
t.remove(t2);
|
||||
Assert.assertTrue(t.remove(t2));
|
||||
Assert.assertTrue(t.size() == 1);
|
||||
Assert.assertEquals(t3, t.get(0));
|
||||
|
||||
Assert.assertFalse(t.shouldRun());
|
||||
}
|
||||
|
||||
String empty = "{\"data\":\"[]\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.TriggerOr\"}";
|
||||
String oneItem = "{\"data\":\"[\\\"{\\\\\\\"data\\\\\\\":\\\\\\\"[]\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"info.nightscout.androidaps.plugins.general.automation.actions.TriggerOr\\\\\\\"}\\\"]\",\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.TriggerOr\"}";
|
||||
|
||||
@Test
|
||||
public void toJSONTest() {
|
||||
TriggerOr t = new TriggerOr();
|
||||
Assert.assertEquals(empty, t.toJSON());
|
||||
t.add(new TriggerOr());
|
||||
Assert.assertEquals(oneItem, t.toJSON());
|
||||
}
|
||||
@Test
|
||||
public void fromJSONTest() throws JSONException {
|
||||
TriggerOr t = new TriggerOr();
|
||||
t.add(new TriggerOr());
|
||||
|
||||
TriggerOr t2 = (TriggerOr) Trigger.instantiate(new JSONObject(t.toJSON()));
|
||||
Assert.assertEquals(1, t2.size());
|
||||
Assert.assertTrue(t2.get(0) instanceof TriggerOr);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue