-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(设备管理): 新增在reactorQL中获取设备属性上报时间 (#592)
Co-authored-by: fighter-wang <11291691+fighter-wang@user.noreply.gitee.com>
- Loading branch information
1 parent
b890f4e
commit 9c26d7d
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...ager/src/main/java/org/jetlinks/community/device/function/DevicePropertyTimeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jetlinks.community.device.function; | ||
|
||
import org.jetlinks.core.message.DeviceDataManager; | ||
import org.jetlinks.reactor.ql.supports.map.FunctionMapFeature; | ||
import org.jetlinks.reactor.ql.utils.CastUtils; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* 在reactorQL中获取设备属性上报时间 | ||
* <pre>{@code | ||
* select device.property_time.recent(deviceId,'temperature',timestamp) recent from ... | ||
* | ||
* select * from ... where device.property_time.recent(deviceId,'temperature',timestamp) = 'xxx' | ||
* }</pre> | ||
* | ||
* @author zhouhao | ||
* @since 2.2 | ||
*/ | ||
@Component | ||
public class DevicePropertyTimeFunction extends FunctionMapFeature { | ||
public DevicePropertyTimeFunction(DeviceDataManager dataManager) { | ||
super("device.property_time.recent", 3, 2, flux -> flux | ||
.collectList() | ||
.flatMap(args -> { | ||
if (args.size() < 2) { | ||
return Mono.empty(); | ||
} | ||
String deviceId = String.valueOf(args.get(0)); | ||
String property = String.valueOf(args.get(1)); | ||
long timestamp = args.size() > 2 | ||
? CastUtils.castNumber(args.get(2)) | ||
.longValue() | ||
: System.currentTimeMillis(); | ||
|
||
return dataManager | ||
.getLastProperty(deviceId, property, timestamp) | ||
.map(DeviceDataManager.PropertyValue::getTimestamp); | ||
})); | ||
} | ||
} |