Пример #1
0
  @Test
  public void sendMultipleDataByTimerTask() throws Exception {

    Pang httpClient = PangFactory.createHttpClient("josh", "abc2fk");
    httpClient.connect("http://localhost:9191");

    httpClient.startTimerTask(
        new MultipleDataCallback() {

          public boolean isRunning(int count) {
            return count < 10;
          }

          public void onSuccess(Object value) {}

          public Object getData() {
            sensor.setHumidity((int) (Math.random() * 30 + 30));
            sensor.setTemperature((int) (Math.random() * 20 + 20));
            sensor.setTimeStamp(new Date());
            return sensor;
          }
        },
        5,
        TimeUnit.SECONDS);

    httpClient.waitTimerTask();

    httpClient.disconnect();
  }
  @Before
  public void init() throws Exception {
    sendingDataClient = new PangMqttClient("demo", "test1");
    final CountDownLatch latch = new CountDownLatch(1);
    sendingDataClient.setConnectionCallback(
        new ConnectionCallback() {

          public void onConnectionSuccess() {
            latch.countDown();
          }

          public void onConnectionLost(Throwable cause) {}

          public void onConnectionFailure(Throwable cause) {}
        });
    sendingDataClient.connect("tcp://192.168.0.21:1884");
    latch.await(3, TimeUnit.SECONDS);
  }