Search in sources :

Example 11 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project android-mdm-agent by flyve-mdm.

the class PoliciesController method sendKeepAlive.

/**
 * Send PING to the MQTT server
 * payload: !
 */
public void sendKeepAlive() {
    String topic = mTopic + "/Status/Ping";
    String payload = "!";
    byte[] encodedPayload = new byte[0];
    try {
        encodedPayload = payload.getBytes(UTF_8);
        MqttMessage message = new MqttMessage(encodedPayload);
        IMqttDeliveryToken token = client.publish(topic, message);
        broadcastReceivedLog(Helpers.broadCastMessage(MQTT_SEND, "PING", "ID: " + token.getMessageId()));
    } catch (Exception ex) {
        FlyveLog.e(ex.getMessage());
        broadcastReceivedLog(Helpers.broadCastMessage(ERROR, "Error on sendKeepAlive", ex.getMessage()));
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)

Example 12 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project android-mdm-agent by flyve-mdm.

the class PoliciesController method sendInventory.

/**
 * Send INVENTORY to the MQTT server
 * payload: XML FusionInventory
 */
public void sendInventory(String payload) {
    String topic = mTopic + "/Status/Inventory";
    byte[] encodedPayload = new byte[0];
    try {
        encodedPayload = payload.getBytes(UTF_8);
        MqttMessage message = new MqttMessage(encodedPayload);
        IMqttDeliveryToken token = client.publish(topic, message);
        // send broadcast
        broadcastReceivedLog(Helpers.broadCastMessage(MQTT_SEND, "Send Inventory", "ID: " + token.getMessageId()));
    } catch (Exception ex) {
        FlyveLog.e(ex.getMessage());
        // send broadcast
        broadcastReceivedLog(Helpers.broadCastMessage(ERROR, "Error on sendKeepAlive", ex.getMessage()));
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)

Example 13 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project ha-bridge by bwssytems.

the class MQTTHandler method publishMessage.

public void publishMessage(String topic, String content) {
    MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes());
    message.setQos(qos);
    try {
        myClient.publish(topic, message);
    } catch (MqttPersistenceException e) {
        log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
    } catch (MqttException e) {
        log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttPersistenceException(org.eclipse.paho.client.mqttv3.MqttPersistenceException)

Example 14 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project ignite by apache.

the class IgniteMqttStreamerTest method sendMessages.

/**
 * @param topics Topics.
 * @param fromIdx From index.
 * @param cnt Count.
 * @param singleMsg Single message flag.
 * @throws MqttException If failed.
 */
private void sendMessages(final List<String> topics, int fromIdx, int cnt, boolean singleMsg) throws MqttException {
    if (singleMsg) {
        final List<StringBuilder> sbs = new ArrayList<>(topics.size());
        // initialize String Builders for each topic
        F.forEach(topics, new IgniteInClosure<String>() {

            @Override
            public void apply(String s) {
                sbs.add(new StringBuilder());
            }
        });
        // fill String Builders for each topic
        F.forEach(F.range(fromIdx, fromIdx + cnt), new IgniteInClosure<Integer>() {

            @Override
            public void apply(Integer integer) {
                sbs.get(integer % topics.size()).append(integer.toString() + "," + TEST_DATA.get(integer) + "\n");
            }
        });
        // send each buffer out
        for (int i = 0; i < topics.size(); i++) {
            MqttMessage msg = new MqttMessage(sbs.get(i).toString().getBytes());
            client.publish(topics.get(i % topics.size()), msg);
        }
    } else {
        for (int i = fromIdx; i < fromIdx + cnt; i++) {
            byte[] payload = (i + "," + TEST_DATA.get(i)).getBytes();
            MqttMessage msg = new MqttMessage(payload);
            client.publish(topics.get(i % topics.size()), msg);
        }
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) ArrayList(java.util.ArrayList)

Example 15 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project anki-battle-showcase by adessoAG.

the class MqttService method connect.

public void connect() {
    try {
        // test.mosquitto.org   broker.hivemq.com
        mqttClient = new MqttClient("tcp://localhost:1883", "anki-battle", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(false);
        log.info("Connecting to MQTT broker: localhost:1883");
        mqttClient.connect(connOpts);
        log.info("Connected to MQTT broker");
        mqttClient.setCallback(new MqttCallback() {

            @Override
            public void connectionLost(Throwable throwable) {
                log.info("MQTT connection lost");
            }

            @Override
            public void messageArrived(String s, MqttMessage mqttMessage) {
                try {
                    log.debug("MQTT message arrived: topic=" + s + "; message=" + mqttMessage.toString());
                    String temp = mqttMessage.toString();
                    byte[] hm = mqttMessage.getPayload();
                    JSONObject json = new JSONObject(mqttMessage.toString());
                    // get topic, where topic is identifier for vehicle
                    // get corresponding vehicle v
                    // v.exexute command
                    String topic = s.split("_")[0];
                    List<DynamicBody> vehicles = world.getDynamicBodies();
                    Vehicle wantedVehicle = null;
                    for (int i = 0; i < vehicles.size(); i++) {
                        Vehicle currentVehicle = ((Vehicle) vehicles.get(i));
                        if (topic.equals(currentVehicle.getName())) {
                            wantedVehicle = currentVehicle;
                            break;
                        }
                    }
                    String commandType = (String) json.get("type");
                    int speed;
                    Command command = null;
                    int track;
                    switch(commandType) {
                        case ("accelerate"):
                            speed = Integer.parseInt((String) json.get("velocity"));
                            command = new AccelerateCommand(speed);
                            break;
                        case ("brake"):
                            speed = Integer.parseInt((String) json.get("velocity"));
                            command = new BrakeCommand(speed);
                            break;
                        case ("change track"):
                            val lane = Double.parseDouble((String) json.get("track"));
                            command = new ChangeLaneCommand(lane);
                            break;
                        case ("uTurn"):
                            command = new UTurnCommand();
                            break;
                        case ("fireRocket"):
                            command = new FireRocketCommand("");
                            break;
                        case ("putMine"):
                            command = new PutMineCommand();
                            break;
                    }
                    if (command != null) {
                        command.execute(wantedVehicle);
                    } else {
                        log.debug("No Command");
                    }
                } catch (Exception e) {
                    log.error("Error while parsing MQTT message", e);
                }
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
            }
        });
    } catch (MqttException e) {
        log.debug("exception during initialising service");
        e.printStackTrace();
    }
}
Also used : lombok.val(lombok.val) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) Vehicle(de.adesso.anki.battle.world.bodies.Vehicle) JSONObject(org.json.JSONObject) List(java.util.List)

Aggregations

MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)65 MqttException (org.eclipse.paho.client.mqttv3.MqttException)28 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)19 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)16 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)16 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)15 Test (org.junit.Test)14 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)8 CountDownLatch (java.util.concurrent.CountDownLatch)4 JsonParser (com.google.gson.JsonParser)3 IOException (java.io.IOException)3 MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)3 JSONObject (org.json.JSONObject)3 Test (org.testng.annotations.Test)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 MISTEdge (edu.snu.mist.common.graph.MISTEdge)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 CamelContext (org.apache.camel.CamelContext)2