如docs of 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 ...
答案 0 :(得分: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);
答案 1 :(得分:0)
嗯,您的代码与文档中的示例匹配,但看起来好像我们正在创建一个过滤后的流,然后在temp.print()
中忽略它。
您可以尝试将该行更改为simpleFiltered.print()