Search in sources :

Example 1 with QDataPaymentsCallbackMessage

use of life.genny.qwanda.message.QDataPaymentsCallbackMessage in project rulesservice by genny-project.

the class EBCHandlers method registerHandlers.

public static void registerHandlers(final EventBus eventBus) {
    Consumer.getFromCmds().subscribe(arg -> {
        JsonObject payload = processMessage("Service Command", arg);
        if ("CMD_RELOAD_RULES".equals(payload.getString("cmd_type"))) {
            if ("RELOAD_RULES_FROM_FILES".equals(payload.getString("code"))) {
                String rulesDir = payload.getString("rulesDir");
                RulesLoader.loadInitialRules(rulesDir);
            }
        }
    });
    Consumer.getFromEvents().subscribe(arg -> {
        JsonObject payload = processMessage("Event", arg);
        QEventMessage eventMsg = null;
        String evtMsg = "Event:";
        if (payload.getString("event_type").equals("EVT_ATTRIBUTE_VALUE_CHANGE")) {
            eventMsg = JsonUtils.fromJson(payload.toString(), QEventAttributeValueChangeMessage.class);
        } else if (payload.getString("event_type").equals("BTN_CLICK")) {
            eventMsg = JsonUtils.fromJson(payload.toString(), QEventBtnClickMessage.class);
        } else if (payload.getString("event_type").equals("EVT_LINK_CHANGE")) {
            eventMsg = JsonUtils.fromJson(payload.toString(), QEventLinkChangeMessage.class);
        } else {
            try {
                eventMsg = JsonUtils.fromJson(payload.toString(), QEventMessage.class);
            } catch (NoClassDefFoundError e) {
                log.error("No class def found [" + payload.toString() + "]");
            }
        }
        processMsg("Event:" + payload.getString("event_type"), payload.getString("ruleGroup"), eventMsg, eventBus, payload.getString("token"));
    });
    Consumer.getFromData().subscribe(arg -> {
        JsonObject payload = processMessage("Data", arg);
        if (payload.getString("msg_type").equalsIgnoreCase("DATA_MSG")) {
            // should always be data if coming through
            // this channel
            QDataAnswerMessage dataMsg = null;
            // Is it a Rule?
            if (payload.getString("data_type").equals(Rule.class.getSimpleName())) {
                JsonArray ja = payload.getJsonArray("items");
                String ruleGroup = ja.getJsonObject(0).getString("ruleGroup");
                String ruleText = ja.getJsonObject(0).getString("rule");
                String ruleCode = ja.getJsonObject(0).getString("code");
                // QDataRuleMessage ruleMsg = gson3.fromJson(json, QDataRuleMessage.class);
                System.out.println("Incoming Rule :" + ruleText);
                if (rulesDir == null) {
                    rulesDir = "rules";
                }
                String rulesGroup = rulesDir;
                List<Tuple3<String, String, String>> rules = new ArrayList<Tuple3<String, String, String>>();
                rules.add(Tuple.of(ruleGroup, ruleCode, ruleText));
                RulesLoader.setupKieRules(rulesGroup, rules);
            } else if (payload.getString("data_type").equals(Answer.class.getSimpleName())) {
                try {
                    dataMsg = JsonUtils.fromJson(payload.toString(), QDataAnswerMessage.class);
                    processMsg("Data:" + dataMsg.getData_type(), payload.getString("ruleGroup"), dataMsg, eventBus, payload.getString("token"));
                } catch (com.google.gson.JsonSyntaxException e) {
                    log.error("BAD Syntax converting to json from " + dataMsg);
                    JsonObject json = new JsonObject(payload.toString());
                    JsonObject answerData = json.getJsonObject("items");
                    JsonArray jsonArray = new JsonArray();
                    jsonArray.add(answerData);
                    json.put("items", jsonArray);
                    dataMsg = JsonUtils.fromJson(json.toString(), QDataAnswerMessage.class);
                    processMsg("Data:" + dataMsg.getData_type(), payload.getString("ruleGroup"), dataMsg, eventBus, payload.getString("token"));
                }
            } else if (payload.getString("data_type").equals(GPS.class.getSimpleName())) {
                QDataGPSMessage dataGPSMsg = null;
                try {
                    dataGPSMsg = JsonUtils.fromJson(payload.toString(), QDataGPSMessage.class);
                    processMsg("GPS", payload.getString("ruleGroup"), dataGPSMsg, eventBus, payload.getString("token"));
                } catch (com.google.gson.JsonSyntaxException e) {
                    log.error("BAD Syntax converting to json from " + dataGPSMsg);
                    JsonObject json = new JsonObject(payload.toString());
                    JsonObject answerData = json.getJsonObject("items");
                    JsonArray jsonArray = new JsonArray();
                    jsonArray.add(answerData);
                    json.put("items", jsonArray);
                    dataGPSMsg = JsonUtils.fromJson(json.toString(), QDataGPSMessage.class);
                    processMsg("GPS:" + dataGPSMsg.getData_type(), payload.getString("ruleGroup"), dataGPSMsg, eventBus, payload.getString("token"));
                }
            } else if (payload.getString("data_type").equals(QDataPaymentsCallbackMessage.class.getSimpleName())) {
                QDataPaymentsCallbackMessage dataCallbackMsg = null;
                try {
                    dataCallbackMsg = JsonUtils.fromJson(payload.toString(), QDataPaymentsCallbackMessage.class);
                    processMsg("Data:" + dataCallbackMsg.getData_type(), payload.getString("ruleGroup"), dataCallbackMsg, eventBus, payload.getString("token"));
                } catch (com.google.gson.JsonSyntaxException e) {
                    log.error("BAD Syntax converting to json from " + dataCallbackMsg);
                    JsonObject json = new JsonObject(payload.toString());
                    dataCallbackMsg = JsonUtils.fromJson(json.toString(), QDataPaymentsCallbackMessage.class);
                    processMsg("Callback:" + dataCallbackMsg.getData_type(), payload.getString("ruleGroup"), dataCallbackMsg, eventBus, payload.getString("token"));
                }
            }
        }
    });
}
Also used : QEventLinkChangeMessage(life.genny.qwanda.message.QEventLinkChangeMessage) QEventAttributeValueChangeMessage(life.genny.qwanda.message.QEventAttributeValueChangeMessage) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) QDataAnswerMessage(life.genny.qwanda.message.QDataAnswerMessage) GPS(life.genny.qwanda.GPS) QEventMessage(life.genny.qwanda.message.QEventMessage) JsonArray(io.vertx.core.json.JsonArray) QDataGPSMessage(life.genny.qwanda.message.QDataGPSMessage) QDataPaymentsCallbackMessage(life.genny.qwanda.message.QDataPaymentsCallbackMessage) Tuple3(io.vavr.Tuple3) Rule(life.genny.qwanda.rule.Rule)

Aggregations

Tuple3 (io.vavr.Tuple3)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 ArrayList (java.util.ArrayList)1 GPS (life.genny.qwanda.GPS)1 QDataAnswerMessage (life.genny.qwanda.message.QDataAnswerMessage)1 QDataGPSMessage (life.genny.qwanda.message.QDataGPSMessage)1 QDataPaymentsCallbackMessage (life.genny.qwanda.message.QDataPaymentsCallbackMessage)1 QEventAttributeValueChangeMessage (life.genny.qwanda.message.QEventAttributeValueChangeMessage)1 QEventLinkChangeMessage (life.genny.qwanda.message.QEventLinkChangeMessage)1 QEventMessage (life.genny.qwanda.message.QEventMessage)1 Rule (life.genny.qwanda.rule.Rule)1