Added rule validation

pull/189/head
M66B 4 years ago
parent c686d64a7b
commit b42d6bbb84

@ -307,7 +307,7 @@ public class EntityRule {
return executed;
}
private boolean _execute(Context context, EntityMessage message) throws JSONException {
private boolean _execute(Context context, EntityMessage message) throws JSONException, IllegalArgumentException {
JSONObject jaction = new JSONObject(action);
int type = jaction.getInt("type");
Log.i("Executing rule=" + type + ":" + name + " message=" + message.id);
@ -346,6 +346,65 @@ public class EntityRule {
}
}
void validate(Context context) throws JSONException, IllegalArgumentException {
JSONObject jargs = new JSONObject(action);
int type = jargs.getInt("type");
DB db = DB.getInstance(context);
switch (type) {
case TYPE_NOOP:
return;
case TYPE_SEEN:
return;
case TYPE_UNSEEN:
return;
case TYPE_HIDE:
return;
case TYPE_IGNORE:
return;
case TYPE_SNOOZE:
return;
case TYPE_FLAG:
return;
case TYPE_IMPORTANCE:
return;
case TYPE_KEYWORD:
String keyword = jargs.getString("keyword");
if (TextUtils.isEmpty(keyword))
throw new IllegalArgumentException("Keyword missing");
case TYPE_MOVE:
case TYPE_COPY:
long target = jargs.optLong("target", -1);
if (target < 0)
throw new IllegalArgumentException("Folder missing");
EntityFolder folder = db.folder().getFolder(target);
if (folder == null)
throw new IllegalArgumentException("Folder not found");
return;
case TYPE_ANSWER:
long iid = jargs.optLong("identity", -1);
if (iid < 0)
throw new IllegalArgumentException("Identity missing");
EntityIdentity identity = db.identity().getIdentity(iid);
if (identity == null)
throw new IllegalArgumentException("Identity not found");
long aid = jargs.optLong("answer", -1);
if (aid < 0)
throw new IllegalArgumentException("Answer missing");
EntityAnswer answer = db.answer().getAnswer(aid);
if (answer == null)
throw new IllegalArgumentException("Answer not found");
return;
case TYPE_TTS:
return;
case TYPE_AUTOMATION:
return;
default:
throw new IllegalArgumentException("Unknown rule type=" + type);
}
}
private boolean onActionSeen(Context context, EntityMessage message, boolean seen) {
EntityOperation.queue(context, message, EntityOperation.SEEN, seen);
@ -443,7 +502,7 @@ public class EntityRule {
try {
answer(context, EntityRule.this, message, jargs);
} catch (Throwable ex) {
Log.e(ex);
Log.w(ex);
}
}
});
@ -596,7 +655,7 @@ public class EntityRule {
try {
speak(context, EntityRule.this, message);
} catch (Throwable ex) {
Log.e(ex);
Log.w(ex);
}
}
});
@ -691,10 +750,8 @@ public class EntityRule {
private boolean onActionKeyword(Context context, EntityMessage message, JSONObject jargs) throws JSONException {
String keyword = jargs.getString("keyword");
if (TextUtils.isEmpty(keyword)) {
Log.w("Keyword empty");
return false;
}
if (TextUtils.isEmpty(keyword))
throw new IllegalArgumentException("Keyword missing rule=" + name);
EntityOperation.queue(context, message, EntityOperation.KEYWORD, keyword, true);

@ -1051,6 +1051,7 @@ public class FragmentRule extends FragmentBase {
rule.stop = stop;
rule.condition = condition;
rule.action = action;
rule.validate(context);
rule.id = db.rule().insertRule(rule);
} else {
EntityRule rule = db.rule().getRule(id);
@ -1061,6 +1062,7 @@ public class FragmentRule extends FragmentBase {
rule.stop = stop;
rule.condition = condition;
rule.action = action;
rule.validate((context));
db.rule().updateRule(rule);
}

Loading…
Cancel
Save