How to filter on Apache Edgent and also show the values which were filtered?












0















I am using Apache Edgent (Java framework) to poll values from a HCSR04 ultrasonic sensor on a Raspberry Pi every 3 seconds. I use a filter to not get values from 50cm to 80cm.



    UltrasonicStream sensor = new UltrasonicStream();
DirectProvider dp = new DirectProvider();
Topology topology = dp.newTopology();
TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);
TStream<Double> filteredReadings = tempReadings.filter(reading -> reading < 50 || reading > 80);
System.out.println("filter added: tempReadings.filter(reading -> reading < 50 || reading > 80);");
filteredReadings.print();
dp.submit(topology);


I want to show some message when the values are filtered. When the values do not match with my filter I can poll them, but when they match I am not returning, that is ok. However, I want just to show that a value was filtered using Apache Edgent libraries. I know that I can do something on the public double get() method, but I wonder if I could do this trick with some method of the Apache Edgent.



public class UltrasonicStream implements Supplier {



private static final long serialVersionUID = -6511218542753341056L;

private static GpioPinDigitalOutput sensorTriggerPin;
private static GpioPinDigitalInput sensorEchoPin;
private static final GpioController gpio = GpioFactory.getInstance();
private double currentDistance = -1.0;

/**
* The HCSR04 Ultrasonic sensor is connected on the physical pin 16 and 18 which
* correspond to the GPIO 04 and 05 of the WiringPi library.
*/
public UltrasonicStream() {
// Trigger pin as OUTPUT
sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
// Echo pin as INPUT
sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, PinPullResistance.PULL_DOWN);
}

/**
* This is the override method of the Supplier interface from Apache Edgent
*/
@Override
public Double get() {
try {
System.out.print("Distance in centimeters: ");
currentDistance = getDistance();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return currentDistance;
}

/**
* Retrieve the distance measured by the HCSR04 Ultrasonic sensor connected on a
* Raspberry Pi 3+B
*
* @return the distance in centimeters
* @throws InterruptedException
*/
public double getDistance() throws InterruptedException {

double distanceCM = -1;
try {
// Thread.sleep(2000);
sensorTriggerPin.high(); // Make trigger pin HIGH
Thread.sleep((long) 0.01);// Delay for 10 microseconds
sensorTriggerPin.low(); // Make trigger pin LOW

// Wait until the ECHO pin gets HIGH
while (sensorEchoPin.isLow()) {

}
// Store the current time to calculate ECHO pin HIGH time.
long startTime = System.nanoTime();
// Wait until the ECHO pin gets LOW
while (sensorEchoPin.isHigh()) {

}
// Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
long endTime = System.nanoTime();

distanceCM = ((((endTime - startTime) / 1e3) / 2) / 29.1);
// Printing out the distance in centimeters
// System.out.println("Distance: " + distanceCM + " centimeters");

return distanceCM;
} catch (InterruptedException e) {
e.printStackTrace();
}
return distanceCM;
}


}










share|improve this question



























    0















    I am using Apache Edgent (Java framework) to poll values from a HCSR04 ultrasonic sensor on a Raspberry Pi every 3 seconds. I use a filter to not get values from 50cm to 80cm.



        UltrasonicStream sensor = new UltrasonicStream();
    DirectProvider dp = new DirectProvider();
    Topology topology = dp.newTopology();
    TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);
    TStream<Double> filteredReadings = tempReadings.filter(reading -> reading < 50 || reading > 80);
    System.out.println("filter added: tempReadings.filter(reading -> reading < 50 || reading > 80);");
    filteredReadings.print();
    dp.submit(topology);


    I want to show some message when the values are filtered. When the values do not match with my filter I can poll them, but when they match I am not returning, that is ok. However, I want just to show that a value was filtered using Apache Edgent libraries. I know that I can do something on the public double get() method, but I wonder if I could do this trick with some method of the Apache Edgent.



    public class UltrasonicStream implements Supplier {



    private static final long serialVersionUID = -6511218542753341056L;

    private static GpioPinDigitalOutput sensorTriggerPin;
    private static GpioPinDigitalInput sensorEchoPin;
    private static final GpioController gpio = GpioFactory.getInstance();
    private double currentDistance = -1.0;

    /**
    * The HCSR04 Ultrasonic sensor is connected on the physical pin 16 and 18 which
    * correspond to the GPIO 04 and 05 of the WiringPi library.
    */
    public UltrasonicStream() {
    // Trigger pin as OUTPUT
    sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
    // Echo pin as INPUT
    sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, PinPullResistance.PULL_DOWN);
    }

    /**
    * This is the override method of the Supplier interface from Apache Edgent
    */
    @Override
    public Double get() {
    try {
    System.out.print("Distance in centimeters: ");
    currentDistance = getDistance();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return currentDistance;
    }

    /**
    * Retrieve the distance measured by the HCSR04 Ultrasonic sensor connected on a
    * Raspberry Pi 3+B
    *
    * @return the distance in centimeters
    * @throws InterruptedException
    */
    public double getDistance() throws InterruptedException {

    double distanceCM = -1;
    try {
    // Thread.sleep(2000);
    sensorTriggerPin.high(); // Make trigger pin HIGH
    Thread.sleep((long) 0.01);// Delay for 10 microseconds
    sensorTriggerPin.low(); // Make trigger pin LOW

    // Wait until the ECHO pin gets HIGH
    while (sensorEchoPin.isLow()) {

    }
    // Store the current time to calculate ECHO pin HIGH time.
    long startTime = System.nanoTime();
    // Wait until the ECHO pin gets LOW
    while (sensorEchoPin.isHigh()) {

    }
    // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
    long endTime = System.nanoTime();

    distanceCM = ((((endTime - startTime) / 1e3) / 2) / 29.1);
    // Printing out the distance in centimeters
    // System.out.println("Distance: " + distanceCM + " centimeters");

    return distanceCM;
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    return distanceCM;
    }


    }










    share|improve this question

























      0












      0








      0








      I am using Apache Edgent (Java framework) to poll values from a HCSR04 ultrasonic sensor on a Raspberry Pi every 3 seconds. I use a filter to not get values from 50cm to 80cm.



          UltrasonicStream sensor = new UltrasonicStream();
      DirectProvider dp = new DirectProvider();
      Topology topology = dp.newTopology();
      TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);
      TStream<Double> filteredReadings = tempReadings.filter(reading -> reading < 50 || reading > 80);
      System.out.println("filter added: tempReadings.filter(reading -> reading < 50 || reading > 80);");
      filteredReadings.print();
      dp.submit(topology);


      I want to show some message when the values are filtered. When the values do not match with my filter I can poll them, but when they match I am not returning, that is ok. However, I want just to show that a value was filtered using Apache Edgent libraries. I know that I can do something on the public double get() method, but I wonder if I could do this trick with some method of the Apache Edgent.



      public class UltrasonicStream implements Supplier {



      private static final long serialVersionUID = -6511218542753341056L;

      private static GpioPinDigitalOutput sensorTriggerPin;
      private static GpioPinDigitalInput sensorEchoPin;
      private static final GpioController gpio = GpioFactory.getInstance();
      private double currentDistance = -1.0;

      /**
      * The HCSR04 Ultrasonic sensor is connected on the physical pin 16 and 18 which
      * correspond to the GPIO 04 and 05 of the WiringPi library.
      */
      public UltrasonicStream() {
      // Trigger pin as OUTPUT
      sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
      // Echo pin as INPUT
      sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, PinPullResistance.PULL_DOWN);
      }

      /**
      * This is the override method of the Supplier interface from Apache Edgent
      */
      @Override
      public Double get() {
      try {
      System.out.print("Distance in centimeters: ");
      currentDistance = getDistance();
      } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      return currentDistance;
      }

      /**
      * Retrieve the distance measured by the HCSR04 Ultrasonic sensor connected on a
      * Raspberry Pi 3+B
      *
      * @return the distance in centimeters
      * @throws InterruptedException
      */
      public double getDistance() throws InterruptedException {

      double distanceCM = -1;
      try {
      // Thread.sleep(2000);
      sensorTriggerPin.high(); // Make trigger pin HIGH
      Thread.sleep((long) 0.01);// Delay for 10 microseconds
      sensorTriggerPin.low(); // Make trigger pin LOW

      // Wait until the ECHO pin gets HIGH
      while (sensorEchoPin.isLow()) {

      }
      // Store the current time to calculate ECHO pin HIGH time.
      long startTime = System.nanoTime();
      // Wait until the ECHO pin gets LOW
      while (sensorEchoPin.isHigh()) {

      }
      // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
      long endTime = System.nanoTime();

      distanceCM = ((((endTime - startTime) / 1e3) / 2) / 29.1);
      // Printing out the distance in centimeters
      // System.out.println("Distance: " + distanceCM + " centimeters");

      return distanceCM;
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      return distanceCM;
      }


      }










      share|improve this question














      I am using Apache Edgent (Java framework) to poll values from a HCSR04 ultrasonic sensor on a Raspberry Pi every 3 seconds. I use a filter to not get values from 50cm to 80cm.



          UltrasonicStream sensor = new UltrasonicStream();
      DirectProvider dp = new DirectProvider();
      Topology topology = dp.newTopology();
      TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);
      TStream<Double> filteredReadings = tempReadings.filter(reading -> reading < 50 || reading > 80);
      System.out.println("filter added: tempReadings.filter(reading -> reading < 50 || reading > 80);");
      filteredReadings.print();
      dp.submit(topology);


      I want to show some message when the values are filtered. When the values do not match with my filter I can poll them, but when they match I am not returning, that is ok. However, I want just to show that a value was filtered using Apache Edgent libraries. I know that I can do something on the public double get() method, but I wonder if I could do this trick with some method of the Apache Edgent.



      public class UltrasonicStream implements Supplier {



      private static final long serialVersionUID = -6511218542753341056L;

      private static GpioPinDigitalOutput sensorTriggerPin;
      private static GpioPinDigitalInput sensorEchoPin;
      private static final GpioController gpio = GpioFactory.getInstance();
      private double currentDistance = -1.0;

      /**
      * The HCSR04 Ultrasonic sensor is connected on the physical pin 16 and 18 which
      * correspond to the GPIO 04 and 05 of the WiringPi library.
      */
      public UltrasonicStream() {
      // Trigger pin as OUTPUT
      sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
      // Echo pin as INPUT
      sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, PinPullResistance.PULL_DOWN);
      }

      /**
      * This is the override method of the Supplier interface from Apache Edgent
      */
      @Override
      public Double get() {
      try {
      System.out.print("Distance in centimeters: ");
      currentDistance = getDistance();
      } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      return currentDistance;
      }

      /**
      * Retrieve the distance measured by the HCSR04 Ultrasonic sensor connected on a
      * Raspberry Pi 3+B
      *
      * @return the distance in centimeters
      * @throws InterruptedException
      */
      public double getDistance() throws InterruptedException {

      double distanceCM = -1;
      try {
      // Thread.sleep(2000);
      sensorTriggerPin.high(); // Make trigger pin HIGH
      Thread.sleep((long) 0.01);// Delay for 10 microseconds
      sensorTriggerPin.low(); // Make trigger pin LOW

      // Wait until the ECHO pin gets HIGH
      while (sensorEchoPin.isLow()) {

      }
      // Store the current time to calculate ECHO pin HIGH time.
      long startTime = System.nanoTime();
      // Wait until the ECHO pin gets LOW
      while (sensorEchoPin.isHigh()) {

      }
      // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
      long endTime = System.nanoTime();

      distanceCM = ((((endTime - startTime) / 1e3) / 2) / 29.1);
      // Printing out the distance in centimeters
      // System.out.println("Distance: " + distanceCM + " centimeters");

      return distanceCM;
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      return distanceCM;
      }


      }







      apache-edgent






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 10:38









      Felipe Oliveira GutierrezFelipe Oliveira Gutierrez

      95521228




      95521228
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You can use TStream.split() to create two streams: one whose tuples match your filter predicate and one for those that don't. You can then do whatever you want with either stream. e.g. TStream.peek(t -> System.out.println("excluded: " + t)), or TStream.print(...)






          share|improve this answer































            0














            I implemented like this:



            UltrasonicStream sensor = new UltrasonicStream();

            DirectProvider dp = new DirectProvider();

            Topology topology = dp.newTopology();

            TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);

            TStream<Double> filteredReadings = tempReadings.filter(reading -> {
            boolean threshold = reading < 20 || reading > 80;
            if (!threshold) {
            System.out.println(String.format("Threshold reached: %s cm", reading));
            }
            return threshold;
            });
            filteredReadings.print();

            dp.submit(topology);





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410238%2fhow-to-filter-on-apache-edgent-and-also-show-the-values-which-were-filtered%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You can use TStream.split() to create two streams: one whose tuples match your filter predicate and one for those that don't. You can then do whatever you want with either stream. e.g. TStream.peek(t -> System.out.println("excluded: " + t)), or TStream.print(...)






              share|improve this answer




























                0














                You can use TStream.split() to create two streams: one whose tuples match your filter predicate and one for those that don't. You can then do whatever you want with either stream. e.g. TStream.peek(t -> System.out.println("excluded: " + t)), or TStream.print(...)






                share|improve this answer


























                  0












                  0








                  0







                  You can use TStream.split() to create two streams: one whose tuples match your filter predicate and one for those that don't. You can then do whatever you want with either stream. e.g. TStream.peek(t -> System.out.println("excluded: " + t)), or TStream.print(...)






                  share|improve this answer













                  You can use TStream.split() to create two streams: one whose tuples match your filter predicate and one for those that don't. You can then do whatever you want with either stream. e.g. TStream.peek(t -> System.out.println("excluded: " + t)), or TStream.print(...)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 2 '18 at 21:42









                  dlabossdlaboss

                  212




                  212

























                      0














                      I implemented like this:



                      UltrasonicStream sensor = new UltrasonicStream();

                      DirectProvider dp = new DirectProvider();

                      Topology topology = dp.newTopology();

                      TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);

                      TStream<Double> filteredReadings = tempReadings.filter(reading -> {
                      boolean threshold = reading < 20 || reading > 80;
                      if (!threshold) {
                      System.out.println(String.format("Threshold reached: %s cm", reading));
                      }
                      return threshold;
                      });
                      filteredReadings.print();

                      dp.submit(topology);





                      share|improve this answer




























                        0














                        I implemented like this:



                        UltrasonicStream sensor = new UltrasonicStream();

                        DirectProvider dp = new DirectProvider();

                        Topology topology = dp.newTopology();

                        TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);

                        TStream<Double> filteredReadings = tempReadings.filter(reading -> {
                        boolean threshold = reading < 20 || reading > 80;
                        if (!threshold) {
                        System.out.println(String.format("Threshold reached: %s cm", reading));
                        }
                        return threshold;
                        });
                        filteredReadings.print();

                        dp.submit(topology);





                        share|improve this answer


























                          0












                          0








                          0







                          I implemented like this:



                          UltrasonicStream sensor = new UltrasonicStream();

                          DirectProvider dp = new DirectProvider();

                          Topology topology = dp.newTopology();

                          TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);

                          TStream<Double> filteredReadings = tempReadings.filter(reading -> {
                          boolean threshold = reading < 20 || reading > 80;
                          if (!threshold) {
                          System.out.println(String.format("Threshold reached: %s cm", reading));
                          }
                          return threshold;
                          });
                          filteredReadings.print();

                          dp.submit(topology);





                          share|improve this answer













                          I implemented like this:



                          UltrasonicStream sensor = new UltrasonicStream();

                          DirectProvider dp = new DirectProvider();

                          Topology topology = dp.newTopology();

                          TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);

                          TStream<Double> filteredReadings = tempReadings.filter(reading -> {
                          boolean threshold = reading < 20 || reading > 80;
                          if (!threshold) {
                          System.out.println(String.format("Threshold reached: %s cm", reading));
                          }
                          return threshold;
                          });
                          filteredReadings.print();

                          dp.submit(topology);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 3 '18 at 10:39









                          Felipe Oliveira GutierrezFelipe Oliveira Gutierrez

                          95521228




                          95521228






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410238%2fhow-to-filter-on-apache-edgent-and-also-show-the-values-which-were-filtered%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Costa Masnaga

                              Fotorealismo

                              Sidney Franklin