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()));
}
}
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()));
}
}
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());
}
}
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);
}
}
}
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();
}
}
Aggregations