簡體   English   中英

為什么過濾器方法不起作用?

[英]Why isn't the filter method working?

Apache Edgent文檔所述,我嘗試過濾掉傳感器讀數,其中溫度傳感器的值必須介於80至85 F之間。

但是,當我嘗試連接傳感器時,讀數為75F,沒有顯示如下消息: 溫度超出范圍

是filter方法不起作用? 如果是這樣,請嘗試幫助我。 謝謝。

范圍值設置為:

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);

傳感器對象是TempSensor ts

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

過濾部分:

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);

輸出:

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

輸出

我認為發生這種情況是因為您的過濾器和接收器已分配給另一個TStream對象,而不是您要打印的對象。
可能您需要嘗試以下方法:

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);

嗯,您的代碼與文檔中的示例匹配,但是看起來好像我們在創建過濾后的流,然后在temp.print()忽略它。

您可以嘗試將該行更改為simpleFiltered.print()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM