Why isn't the filter method working?

断了今生、忘了曾经 提交于 2019-12-11 21:38:38

问题


As given in the docs of Apache Edgent I tried to filter out my sensor readings wherein the values of the temperature sensor must lie in between 80 to 85 F.

But when I tried connecting my sensor the readings were 75F and no message was shown like: temperature is out of range.

Is it that the filter method isn't working? if so please try help me out. thanks.

the range values are set as:

static double OPTIMAL_TEMP_LOW = 80.0;
static double OPTIMAL_TEMP_HIGH = 85.0;
static Range<Double> optimalTempRange = Ranges.closed(OPTIMAL_TEMP_LOW, OPTIMAL_TEMP_HIGH);

The sensor object is TempSensor ts

TempSensor ts = new TempSensor();
Stream<Double> temp = top.poll(ts, 1, TimeUnit.MILLISECONDS);

the filtering part:

TStream<Double> simpleFiltered = temp.filter(tuple ->
tuple < OPTIMAL_TEMP_LOW || tuple > OPTIMAL_TEMP_HIGH);
simpleFiltered.sink(tuple -> System.out.println("Temperature is out of range! "
+ "It is " + tuple + "\u00b0F!"));

/*TStream<Double> simpleFiltered = temp.filter(tuple ->
        !optimalTempRange.contains(tuple));
simpleFiltered.sink(tuple -> System.out.println("Temperature is out of range! "
            + "It is " + tuple + "\u00b0F!"));*/

// See what the temperatures look like
simpleFiltered.print();

dp.submit(top);

output:

Selet a port:
1: ttyACM0 Port opened succesefully.
7373.40
73.40
73.40 ...


回答1:


I think that happened because your filter and sink have been assigned to another TStream object, not the one you are printing out.
Probably you need to try this:

TempSensor ts = new TempSensor();
TStream<Double> temp = top.poll(ts, 1, TimeUnit.MILLISECONDS).filter(tuple ->
tuple < OPTIMAL_TEMP_LOW || tuple > OPTIMAL_TEMP_HIGH);
temp.sink(tuple -> System.out.println("Temperature is out of range! "
+ "It is " + tuple + "\u00b0F!"));

// See what the temperatures look like
temp.print();

dp.submit(top);



回答2:


Hmm, your code matches the example in the documentation, but it looks as if we’re creating a filtered stream, and then ignoring it in temp.print().

You could try changing that line to simpleFiltered.print()



来源:https://stackoverflow.com/questions/50549019/why-isnt-the-filter-method-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!