private void connect(String serverURI, String clientId, String zkConnect) throws MqttException { mqtt = new MqttAsyncClient(serverURI, clientId); mqtt.setCallback(this); IMqttToken token = mqtt.connect(); Properties props = new Properties(); //Updated based on Kafka v0.8.1.1 props.put("metadata.broker.list", "localhost:9092"); props.put("serializer.class", "kafka.serializer.StringEncoder"); props.put("partitioner.class", "example.producer.SimplePartitioner"); props.put("request.required.acks", "1"); ProducerConfig config = new ProducerConfig(props); kafkaProducer = new Producer<String, String>(config); token.waitForCompletion(); logger.info("Connected to MQTT and Kafka"); }
@Override public void onSuccess(final IMqttToken arg0) { // System.out.println("-------- Thread: " + Thread.currentThread().getName()+ "; MqttSession.OperationListener onSuccess: "); if (userCallback == null) { return; } Thread t = new Thread(new Runnable() { @Override public void run() { OperationMode opMode = getOperationModeFromContext((String)arg0.getUserContext()); userCallback.onSuccess(opMode, arg0); } }); t.start(); }
@Test public void whenTheConstructorIsCalledWithAValidEmitterThenGetOnErrorReturnsTheEmitter() { //Given CompletableEmitter emitter = Mockito.mock(CompletableEmitter.class); Throwable ex = Mockito.mock(Throwable.class); CompletableEmitterMqttActionListener listener = new CompletableEmitterMqttActionListener(emitter) { @Override public void onSuccess(IMqttToken asyncActionToken) { // Not invoked } }; // When OnError onError = listener.getOnError(); onError.onError(ex); // Then Mockito.verify(emitter).onError(ex); }
@Test public void whenTheConstructorIsCalledWithAValidEmitterThenGetOnErrorReturnsTheEmitter() { //Given @SuppressWarnings("unchecked") FlowableEmitter<Object> emitter = Mockito.mock(FlowableEmitter.class); Throwable ex = Mockito.mock(Throwable.class); FlowableEmitterMqttActionListener<Object> listener = new FlowableEmitterMqttActionListener<Object>(emitter) { @Override public void onSuccess(IMqttToken asyncActionToken) { // Not invoked } }; // When OnError onError = listener.getOnError(); onError.onError(ex); // Then Mockito.verify(emitter).onError(ex); }
@Override public void onSuccess(IMqttToken arg0) { client.scheduleTask(new Runnable() { @Override public void run() { if (isConnect) { client.getConnection().onConnectionSuccess(); } else { client.getConnection().onConnectionClosed(); } if (userCallback != null) { userCallback.onSuccess(); } } }); }
@Override public void onFailure(IMqttToken arg0, Throwable arg1) { LOGGER.log(Level.WARNING, (isConnect ? "Connect" : "Disconnect") + " request failure", arg1); client.scheduleTask(new Runnable() { @Override public void run() { if (isConnect) { client.getConnection().onConnectionFailure(); } else { client.getConnection().onConnectionClosed(); } if (userCallback != null) { userCallback.onFailure(); } } }); }
@Override public void onFailure(IMqttToken token, Throwable cause) { final AWSIotMessage message = (AWSIotMessage) token.getUserContext(); if (message == null) { LOGGER.warning("Request failed: " + token.getException()); return; } LOGGER.warning("Request failed for topic " + message.getTopic() + ": " + token.getException()); client.scheduleTask(new Runnable() { @Override public void run() { message.onFailure(); } }); }
/** * The action associated with this listener has been successful. * * @param asyncActionToken This argument is not used */ @Override public void onSuccess(IMqttToken asyncActionToken) { switch (action) { case CONNECT: connect(); break; case DISCONNECT: disconnect(); break; case SUBSCRIBE: subscribe(); break; case PUBLISH: publish(); break; } }
/** * The action associated with the object was a failure * * @param token This argument is not used * @param exception The exception which indicates why the action failed */ @Override public void onFailure(IMqttToken token, Throwable exception) { switch (action) { case CONNECT: connect(exception); break; case DISCONNECT: disconnect(exception); break; case SUBSCRIBE: subscribe(exception); break; case PUBLISH: publish(exception); break; } }
/** * Common processing for many notifications * * @param token * the token associated with the action being undertake * @param data * the result data */ private void simpleAction(IMqttToken token, Bundle data) { if (token != null) { Status status = (Status) data .getSerializable(MqttServiceConstants.CALLBACK_STATUS); if (status == Status.OK) { ((MqttTokenAndroid) token).notifyComplete(); } else { Exception exceptionThrown = (Exception) data.getSerializable(MqttServiceConstants.CALLBACK_EXCEPTION); ((MqttTokenAndroid) token).notifyFailure(exceptionThrown); } } else { mqttService.traceError(MqttService.TAG, "simpleAction : token is null"); } }
@Override public void onReceive(Context context, Intent intent) { // According to the docs, "Alarm Manager holds a CPU wake lock as // long as the alarm receiver's onReceive() method is executing. // This guarantees that the phone will not sleep until you have // finished handling the broadcast." int count = intent.getIntExtra(Intent.EXTRA_ALARM_COUNT, -1); //Log.d(TAG, "Ping " + count + " times."); //Log.d(TAG, "Check time :" + System.currentTimeMillis()); IMqttToken token = comms.checkForActivity(); // No ping has been sent. if (token == null) { return; } }
/** * Determine the type of callback that completed successfully. * @param token The MQTT Token for the completed action. */ @Override public void onSuccess(IMqttToken token) { Log.d(TAG, ".onSuccess() entered"); this.token = token; switch (action) { case CONNECTING: handleConnectSuccess(); break; case SUBSCRIBE: handleSubscribeSuccess(); break; case PUBLISH: handlePublishSuccess(); break; case DISCONNECTING: handleDisconnectSuccess(); break; default: break; } }
/** * Determine the type of callback that failed. * @param token The MQTT Token for the completed action. * @param throwable The exception corresponding to the failure. */ @Override public void onFailure(IMqttToken token, Throwable throwable) { Log.e(TAG, ".onFailure() entered"); switch (action) { case CONNECTING: handleConnectFailure(throwable); break; case SUBSCRIBE: handleSubscribeFailure(throwable); break; case PUBLISH: handlePublishFailure(throwable); break; case DISCONNECTING: handleDisconnectFailure(throwable); break; default: break; } }
protected boolean waitForMqttOperation(IMqttToken token, CancellationToken cancellationToken, WsdlTestStepResult testStepResult, long maxTime, String errorText) { while (!token.isComplete() && token.getException() == null) { boolean stopped = cancellationToken.cancelled(); if (stopped || (maxTime != Long.MAX_VALUE && System.nanoTime() > maxTime)) { if (stopped) { testStepResult.setStatus(TestStepResult.TestStepStatus.CANCELED); } else{ testStepResult.addMessage(TIMEOUT_EXPIRED_MSG); testStepResult.setStatus(TestStepResult.TestStepStatus.FAILED); } return false; } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } if (token.getException() != null) { testStepResult.addMessage(errorText); testStepResult.setError(token.getException()); testStepResult.setStatus(TestStepResult.TestStepStatus.FAILED); return false; } return true; }
@Override public void onFailure(IMqttToken iMqttToken, Throwable throwable) { Log.d(TAG, "Mqtt onFailure. " + throwable); // Remove the auto-connect till the failure is solved if (mMqqtClientStatus == MqqtConnectionStatus.CONNECTING) { MqttSettings.getInstance(mContext).setConnectedEnabled(false); } // Set as an error mMqqtClientStatus = MqqtConnectionStatus.ERROR; String errorText = mContext.getString(R.string.mqtt_connection_failed)+". "+throwable.getLocalizedMessage(); Toast.makeText(mContext, errorText, Toast.LENGTH_LONG).show(); // Call listener if (mListener != null) mListener.onMqttDisconnected(); }
/** * Determine the type of callback that completed successfully. * @param token The MQTT Token for the completed action. */ @Override public void onSuccess(IMqttToken token) { Log.d(TAG, ".onSuccess() entered"); switch (action) { case CONNECTING: handleConnectSuccess(); break; case SUBSCRIBE: handleSubscribeSuccess(); break; case PUBLISH: handlePublishSuccess(); break; case DISCONNECTING: handleDisconnectSuccess(); break; default: break; } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/DC/#", 1); client.subscribe("iot/tf/localhost/4223/DC/#", 1); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/DC/e1cw8v/intent/<" + UID + ">/driveMode/value", "0".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/DC/e1cw8v/intent/<" + UID + ">/enabled/value", "true".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/DC/e1cw8v/intent/<" + UID + ">/currentVelocityPeriod/period", "1".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/DC/e1cw8v/intent/<" + UID + ">/acceleration/value", "2000".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/DC/e1cw8v/intent/<" + UID + ">/velocity/value", "7000".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(DC.class.getName()).log(Level.SEVERE, null, ex); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/IMUV2/#", 1); client.subscribe("iot/tf/localhost/4223/IMUV2/#", 1); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/IMUV2/9xblji/intent/<" + UID + ">/linearAcceleration/period", "10".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(IMUV2.class.getName()).log(Level.SEVERE, null, ex); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/SoundIntensity/#", 0); client.subscribe("iot/tf/localhost/4223/SoundIntensity/#", 0); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/SoundIntensity/fnniyg/intent/<" + UID + ">/callbackThreshold/option", "g".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/SoundIntensity/fnniyg/intent/<" + UID + ">/callbackThreshold/min", "2".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/SoundIntensity/fnniyg/intent/<" + UID + ">/callbackThreshold/max", "2".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/SoundIntensity/fnniyg/intent/<" + UID + ">/callbackThreshold/enabled", "true".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(SoundIntensity.class.getName()).log(Level.SEVERE, null, ex); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/Barometer/#", 0); client.subscribe("iot/tf/localhost/4223/Barometer/#", 0); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/Barometer/dpm5s8/intent/<" + UID + ">/averaging/averagePressure", "0".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/Barometer/dpm5s8/intent/<" + UID + ">/averaging/movingAveragePressure", "0".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/Barometer/dpm5s8/intent/<" + UID + ">/averaging/averageTemperature", "0".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/Barometer/dpm5s8/intent/<" + UID + ">/averaging/enabled", "true".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/Barometer/dpm5s8/intent/<" + UID + ">/airPressureCallbackPeriod/period", "2".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(Barometer.class.getName()).log(Level.SEVERE, null, ex); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/LCD20x4/#", 1); client.subscribe("iot/tf/localhost/4223/LCD20x4/#", 1); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true); Thread.sleep(1000); } catch (Exception ex) { Logger.getLogger(Button.class.getName()).log(Level.SEVERE, null, ex); } }
@Override public void onSuccess(IMqttToken iMqttToken) { if (mMqqtClientStatus == MqqtConnectionStatus.CONNECTING) { Log.d(TAG, "Mqtt connect onSuccess"); mMqqtClientStatus = MqqtConnectionStatus.CONNECTED; if (mListener != null) mListener.onMqttConnected(); MqttSettings settings = MqttSettings.getInstance(mContext); String topic = settings.getSubscribeTopic(); int topicQos = settings.getSubscribeQos(); if (settings.isSubscribeEnabled() && topic != null) { subscribe(topic, topicQos); } } else if (mMqqtClientStatus == MqqtConnectionStatus.DISCONNECTING) { Log.d(TAG, "Mqtt disconnect onSuccess"); mMqqtClientStatus = MqqtConnectionStatus.DISCONNECTED; if (mListener != null) mListener.onMqttDisconnected(); } else { Log.d(TAG, "Mqtt unknown onSuccess"); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/RotaryPoti/#", 0); client.subscribe("iot/tf/localhost/4223/RotaryPoti/#", 0); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/RotaryPoti/etahff/intent/<" + UID + ">/positionCallbackPeriod/period", "1".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(RotaryPoti.class.getName()).log(Level.SEVERE, null, ex); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/DustDetector/#", 0); client.subscribe("iot/tf/localhost/4223/DustDetector/#", 0); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/DustDetector/dg4p7n/intent/<" + UID + ">/dustDensityCallbackPeriod/period", "1".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(DustDetector.class.getName()).log(Level.SEVERE, null, ex); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/LaserRangeFinder/#", 0); client.subscribe("iot/tf/localhost/4223/LaserRangeFinder/#", 0); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/LaserRangeFinder/eu1ew0/intent/<" + UID + ">/mode/mode", "1".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/LaserRangeFinder/eu1ew0/intent/<" + UID + ">/distanceCallbackPeriod/period", "1".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/LaserRangeFinder/eu1ew0/intent/<" + UID + ">/velocityCallbackPeriod/period", "1".getBytes(), 1, false); client.publish("iot/tf/localhost/4223/LaserRangeFinder/eu1ew0/intent/<" + UID + ">/laser/enabled", "true".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(LaserRangeFinder.class.getName()).log(Level.SEVERE, null, ex); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/TemperatureIR/#", 0); client.subscribe("iot/tf/localhost/4223/TemperatureIR/#", 0); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/TemperatureIR/fdjob6/intent/<" + UID + ">/objectTemperatureCallbackPeriod/period", "1".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(TemperatureIR.class.getName()).log(Level.SEVERE, null, ex); } }
public void connect() throws MqttException { MqttConnectOptions options = new MqttConnectOptions(); //options.setUserName("user"); //options.setPassword("pass".toCharArray()); options.setWill(UID + "/status", "connection broken".getBytes(), 1, true); options.setCleanSession(true); IMqttToken token = client.connect(options, null, new MQTTActionHandler()); token.waitForCompletion(); try { client.subscribe("iot/tf/description/CO2/#", 0); client.subscribe("iot/tf/localhost/4223/CO2/#", 0); client.subscribe("iot/tf/#", 1); client.publish("iot/tf/MQTT2TF/0/intent/<" + UID + ">/stackHandler/stackAddress", "{\"hostName\":\"localhost\",\"port\":4223}".getBytes(), 1, true).waitForCompletion(); client.publish("iot/tf/localhost/4223/CO2/e0542c/intent/<" + UID + ">/CO2ConcentrationCallbackPeriod/period", "1".getBytes(), 1, false); } catch (Exception ex) { Logger.getLogger(CO2.class.getName()).log(Level.SEVERE, null, ex); } }
@Override public void position(short s, short s1) { IMqttToken token = null; try { getEvent(PositionValueEvent.class ).update(VALUE_X, s); while (token == null) { token = getEvent(PositionValueEvent.class).update(VALUE_Y, s1); } token.waitForCompletion( 10); } catch (Exception ex) { Logger.getLogger(Joystick.class .getName()).log(Level.SEVERE, null, ex); System.out.println(token.getException()); } }
@Override public void positionReached(short s, short s1) { try { getEvent(PositionValueEvent.class ).update(VALUE_X, s); IMqttToken token = null; while (token == null) { getEvent(PositionValueEvent.class ).update(VALUE_Y, s1); } token.waitForCompletion(10); } catch (Exception ex) { Logger.getLogger(Joystick.class.getName()).log(Level.SEVERE, null, ex); } }
@Override public void onSuccess(IMqttToken asyncActionToken) { System.out.println( String.format( "%s successfully connected", name)); // try { // subscribeToken = // client.subscribe( // TOPIC, // QUALITY_OF_SERVICE, // null, // this); // } catch (MqttException e) { // e.printStackTrace(); // } }
/** * Overridden to publish a {@link MqttClientConnectionFailureEvent} message. * <p> * If a {@link ReconnectService} instance is defined, the * {@link ReconnectService#connected(boolean)} method is called with a value of false. */ @Override public void onFailure(IMqttToken token, Throwable throwable) { mqttClientEventPublisher.publishConnectionFailureEvent(getClientId(), isAutoReconnect(), throwable, applicationEventPublisher, this); if (reconnectService != null) { reconnectService.connected(false); } }
@Override public void onSuccess(IMqttToken asyncActionToken) { isLogin = true; tvName.setText("Online"); edtFriendUsername.setEnabled(true); btnLogin.setText("Logout"); MySharePreference.setUserName(LoginActivity.this, myUser); LogUtils.e("subscribeToTopic"); }
@Override public void onSuccess(IMqttToken asyncActionToken) { Log.d(TAG, "onSuccess"); String payload = ""; int mqttQos = 1; /* 0: NO QoS, 1: No Check , 2: Each Check */ MqttMessage message = new MqttMessage(payload.getBytes()); try { mqttClient.subscribe(MQTT_Req_Topic, mqttQos); } catch (MqttException e) { e.printStackTrace(); } }
@Override public void onFailure(final IMqttToken arg0, final Throwable arg1) { if (userCallback == null) { return; } Thread t = new Thread(new Runnable() { @Override public void run() { OperationMode opMode = getOperationModeFromContext((String)arg0.getUserContext()); userCallback.onFailure(opMode, arg0, arg1); } }); t.start(); }
@Override public void onSuccess(final IMqttToken mqttToken) { final PublishToken publishToken = new PublishToken() { @Override public String getClientId() { return mqttToken.getClient().getClientId(); } @Override public String[] getTopics() { return mqttToken.getTopics(); } @Override public int getMessageId() { return mqttToken.getMessageId(); } @Override public boolean getSessionPresent() { return mqttToken.getSessionPresent(); } }; this.emitter.onSuccess(publishToken); }