0

正如Apache Edgent 的文档中给出的那样, 我试图过滤掉我的传感器读数,其中温度传感器的值必须在 80 到 85 F 之间。

但是当我尝试连接传感器时,读数为 75F,并且没有显示如下消息:温度超出范围

是不是过滤方法不起作用?如果是这样,请尝试帮助我。谢谢。

范围值设置为:

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 ...

输出

4

2 回答 2

0

我认为发生这种情况是因为您的过滤器和接收器已分配给另一个 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);
于 2018-05-27T06:25:06.383 回答
0

嗯,您的代码与文档中的示例匹配,但看起来我们正在创建一个过滤流,然后在temp.print().

您可以尝试将该行更改为simpleFiltered.print()

于 2018-05-27T09:14:40.830 回答