Search in sources :

Example 1 with MqttMessage

use of org.eclipse.kapua.transport.message.mqtt.MqttMessage in project kapua by eclipse.

the class MqttClientTest method testMqttClientPublish.

/**
 * Ignoring this test for a while. We should fix the build in the first place and then use embedded ActiveMQ
 * broker for tests.
 */
@Ignore
@Test
public void testMqttClientPublish() throws Exception {
    MqttClientConnectionOptions clientConnectOptions = new MqttClientConnectionOptions();
    clientConnectOptions.setClientId(ClientIdGenerator.getInstance().next(MqttClientTest.class.getSimpleName()));
    clientConnectOptions.setUsername(username);
    clientConnectOptions.setPassword(password.toCharArray());
    clientConnectOptions.setEndpointURI(SystemUtils.getBrokerURI());
    // 
    // Connect
    MqttClient mqttClient = new MqttClient();
    try {
        mqttClient.connectClient(clientConnectOptions);
    } catch (Exception e) {
        fail(e.getMessage());
    }
    assertTrue("client.connected", mqttClient.isConnected());
    // 
    // Send
    String sendTopic = "$EDC/kapua-sys/" + mqttClient.getClientId() + "/" + MqttClientTest.class.getSimpleName() + "/testMqttClientSendTopic";
    MqttTopic mqttTopic = new MqttTopic(sendTopic);
    MqttPayload mqttPayload = new MqttPayload("testMqttClientSendPayload".getBytes());
    MqttMessage mqttMessage = new MqttMessage(mqttTopic, new Date(), mqttPayload);
    try {
        mqttClient.publish(mqttMessage);
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Disconnect
    try {
        mqttClient.disconnectClient();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    assertFalse("client.connected", mqttClient.isConnected());
}
Also used : MqttClient(org.eclipse.kapua.transport.mqtt.MqttClient) MqttMessage(org.eclipse.kapua.transport.message.mqtt.MqttMessage) MqttTopic(org.eclipse.kapua.transport.message.mqtt.MqttTopic) MqttPayload(org.eclipse.kapua.transport.message.mqtt.MqttPayload) MqttClientConnectionOptions(org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions) Date(java.util.Date) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with MqttMessage

use of org.eclipse.kapua.transport.message.mqtt.MqttMessage in project kapua by eclipse.

the class MqttFacadeTest method testMqttClientSend.

/**
 * Ignoring this test for a while. We should fix the build in the first place and then use embedded ActiveMQ
 * broker for tests.
 */
@Ignore
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testMqttClientSend() throws Exception {
    // 
    // Get facade
    KapuaLocator locator = KapuaLocator.getInstance();
    TransportClientFactory transportFacadeFactory = locator.getFactory(TransportClientFactory.class);
    TransportFacade transportFacade = transportFacadeFactory.getFacade();
    assertNotNull("client.clientId", transportFacade.getClientId());
    // 
    // Send
    String sendTopic = "$EDC/kapua-sys/" + transportFacade.getClientId() + "/" + MqttClientTest.class.getSimpleName() + "/testTransportFacadeSend";
    MqttTopic mqttTopic = new MqttTopic(sendTopic);
    MqttPayload mqttPayload = new MqttPayload("testTransportFacadeSendPayload".getBytes());
    MqttMessage mqttMessage = new MqttMessage(mqttTopic, new Date(), mqttPayload);
    TransportMessage responseMessage = null;
    try {
        responseMessage = transportFacade.sendSync(mqttMessage, null);
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // 
    // Verify
    assertNull("responseMessage", responseMessage);
    // Clean
    try {
        transportFacade.clean();
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) MqttMessage(org.eclipse.kapua.transport.message.mqtt.MqttMessage) MqttTopic(org.eclipse.kapua.transport.message.mqtt.MqttTopic) MqttPayload(org.eclipse.kapua.transport.message.mqtt.MqttPayload) TransportFacade(org.eclipse.kapua.transport.TransportFacade) TransportClientFactory(org.eclipse.kapua.transport.TransportClientFactory) Date(java.util.Date) TransportMessage(org.eclipse.kapua.transport.message.TransportMessage) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with MqttMessage

use of org.eclipse.kapua.transport.message.mqtt.MqttMessage in project kapua by eclipse.

the class MqttClientCallback method messageArrived.

/**
 * Implements the API signature {@link MqttCallback#messageArrived(String, org.eclipse.paho.client.mqttv3.MqttMessage)}.
 * <p>
 * Each {@link org.eclipse.paho.client.mqttv3.MqttMessage} arrived is converted into a
 * {@link MqttMessage} and added to the list of received messages.
 * </p>
 * <p>
 * If the total number of received reaches the limit set for expected response messages
 * the {@code notifyAll()} is invoked
 * </p>
 *
 * @since 1.0.0
 */
@Override
public void messageArrived(String stringTopic, org.eclipse.paho.client.mqttv3.MqttMessage message) throws Exception {
    MqttTopic mqttTopic = new MqttTopic(stringTopic);
    MqttPayload mqttPayload = new MqttPayload(message.getPayload());
    MqttMessage mqttMessage = new MqttMessage(mqttTopic, new Date(), mqttPayload);
    // Add to the received responses
    if (responses == null) {
        responses = new ArrayList<MqttMessage>();
    }
    // 
    // Convert MqttMessage to the given device-levelMessage
    responses.add(mqttMessage);
    // notify if all expected responses arrived
    if (expectedResponses == responses.size()) {
        synchronized (this) {
            notifyAll();
        }
    }
}
Also used : MqttMessage(org.eclipse.kapua.transport.message.mqtt.MqttMessage) MqttTopic(org.eclipse.kapua.transport.message.mqtt.MqttTopic) MqttPayload(org.eclipse.kapua.transport.message.mqtt.MqttPayload) Date(java.util.Date)

Example 4 with MqttMessage

use of org.eclipse.kapua.transport.message.mqtt.MqttMessage in project kapua by eclipse.

the class TranslatorDataKuraMqtt method translate.

@Override
public MqttMessage translate(KuraMessage kuraMessage) throws KapuaException {
    // 
    // Mqtt request topic
    MqttTopic mqttRequestTopic = translate(kuraMessage.getChannel());
    // 
    // Mqtt payload
    MqttPayload mqttPayload = translate(kuraMessage.getPayload());
    // Return Mqtt message
    return new MqttMessage(mqttRequestTopic, new Date(), mqttPayload);
}
Also used : MqttMessage(org.eclipse.kapua.transport.message.mqtt.MqttMessage) MqttTopic(org.eclipse.kapua.transport.message.mqtt.MqttTopic) MqttPayload(org.eclipse.kapua.transport.message.mqtt.MqttPayload) Date(java.util.Date)

Example 5 with MqttMessage

use of org.eclipse.kapua.transport.message.mqtt.MqttMessage in project kapua by eclipse.

the class TranslatorRequestKuraMqtt method translate.

@Override
public MqttMessage translate(KuraRequestMessage kuraMessage) throws KapuaException {
    // 
    // Mqtt request topic
    MqttTopic mqttRequestTopic = translate(kuraMessage.getChannel());
    // 
    // Mqtt response topic
    MqttTopic mqttResponseTopic = generateResponseTopic(kuraMessage.getChannel());
    // 
    // Mqtt payload
    MqttPayload mqttPayload = translate(kuraMessage.getPayload());
    // Return Mqtt message
    return new MqttMessage(mqttRequestTopic, mqttResponseTopic, mqttPayload);
}
Also used : MqttMessage(org.eclipse.kapua.transport.message.mqtt.MqttMessage) MqttTopic(org.eclipse.kapua.transport.message.mqtt.MqttTopic) MqttPayload(org.eclipse.kapua.transport.message.mqtt.MqttPayload)

Aggregations

MqttMessage (org.eclipse.kapua.transport.message.mqtt.MqttMessage)5 MqttPayload (org.eclipse.kapua.transport.message.mqtt.MqttPayload)5 MqttTopic (org.eclipse.kapua.transport.message.mqtt.MqttTopic)5 Date (java.util.Date)4 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 TransportClientFactory (org.eclipse.kapua.transport.TransportClientFactory)1 TransportFacade (org.eclipse.kapua.transport.TransportFacade)1 TransportMessage (org.eclipse.kapua.transport.message.TransportMessage)1 MqttClient (org.eclipse.kapua.transport.mqtt.MqttClient)1 MqttClientConnectionOptions (org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions)1