Ejemplo n.º 1
0
  public static void main(String[] args) throws Exception {

    final Pang httpClient =
        //        PreverClientFactory.createHttpClient("demo", "Fj8QBK",
        // "http://192.168.56.101:9191");
        PangFactory.createHttpClient("demo", "Fj8QBK", "http://192.168.0.4:9191");
    final Random generator = new Random();
    // 2pi = 6.28....
    final double period = 1000;
    final double amplitude = 10;

    Timer timer = new Timer();
    timer.schedule(
        new TimerTask() {
          @Override
          public void run() {
            String value = null;
            double sin = Math.sin(System.currentTimeMillis() / period);
            value = ((Double) (amplitude * sin)).toString();
            System.out.println((new Date()).toString() + "@" + value);

            try {
              //          httpClient.sendData("test_sin", value);
            } catch (Throwable e) {

            }
          }
        },
        0,
        1 * 1000);
    timer.wait();
    httpClient.disconnect();
  }
Ejemplo n.º 2
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();
  }
Ejemplo n.º 3
0
  @Test
  public void sendByTimerTask() throws Exception {
    final Random r = new Random();

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

    httpClient.startTimerTask(
        "temperature",
        new SingleDataCallback() {

          public void onSuccess(String data) {
            System.out.println(data);
          }

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

          public String getData() {
            return Integer.toString(r.nextInt(10));
          }
        },
        5,
        TimeUnit.SECONDS);

    httpClient.waitTimerTask();

    httpClient.disconnect();
  }
Ejemplo n.º 4
0
  @Test
  public void senddata() throws Exception {
    Pang httpClient = PangFactory.createHttpClient("josh", "abc2fk", "http://localhost:9191");

    httpClient.sendData("temperature", "12");

    httpClient.disconnect();
  }
Ejemplo n.º 5
0
  @Test
  public void sendMultipledata() throws Exception {
    Pang httpClient = PangFactory.createHttpClient("josh", "abc2fk", "http://localhost:9191");

    sensor.setHumidity((int) (Math.random() * 30 + 30));
    sensor.setTemperature((int) (Math.random() * 20 + 20));
    sensor.setTimeStamp(new Date());

    httpClient.sendData(sensor);

    httpClient.disconnect();
  }
  @Test
  public void sendmanydata() throws Exception {
    Random r = new Random();

    for (int i = 0; i < 1000; i++) {
      TimeUnit.MILLISECONDS.sleep(5);
      sendingDataClient.sendData("pi_temp", "" + r.nextInt(50));
    }
  }
  @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);
  }
 @Test
 public void sendone() throws Exception {
   Random r = new Random();
   sendingDataClient.sendData("test1", "" + 95);
 }