diff --git a/das/src/main/java/com/das/modules/calc/functions/FunctionSumValue.java b/das/src/main/java/com/das/modules/calc/functions/FunctionSumValue.java new file mode 100644 index 00000000..99455b22 --- /dev/null +++ b/das/src/main/java/com/das/modules/calc/functions/FunctionSumValue.java @@ -0,0 +1,59 @@ +package com.das.modules.calc.functions; + +import cn.hutool.core.date.DateUtil; +import com.das.modules.cache.domain.DeviceInfoCache; +import com.das.modules.cache.service.CacheService; +import com.das.modules.data.service.DataService; +import com.googlecode.aviator.runtime.function.AbstractFunction; +import com.googlecode.aviator.runtime.type.AviatorNil; +import com.googlecode.aviator.runtime.type.AviatorObject; +import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; + +import java.util.Date; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Aviator扩展函数 - 获取时间维度内最早的一条数据 + * 函数格式: topv(deviceId, attr, timedim) + * timedim: day - 天, month - 月, year - 年 + * 返回值:数值, nil - 获取错误 + */ +@Slf4j +public class FunctionSumValue extends AbstractFunction { + + private DataService dataService = null; + private CacheService cacheService = null; + + + public FunctionSumValue(DataService dataService, CacheService cacheService) { + this.dataService = dataService; + this.cacheService = cacheService; + } + + @Override + public String getName() { + return "sumv"; + } + + @Override + public AviatorObject call(Map env, AviatorObject deviceCode, AviatorObject attr) { + //设备Code + String code = (String)deviceCode.getValue(env); + String attrName = (String)attr.getValue(env); + + DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheByCode(code); + if (deviceInfoCache == null) { + return AviatorNil.NIL; + } + Double value = dataService.getTimeTopValue(deviceInfoCache.getDeviceId(), attrName, null, null); + if (value == null){ + return AviatorNil.NIL; + } + //未找到缓存,查询时序API获取数据 + return AviatorRuntimeJavaType.valueOf(value); + } + +} diff --git a/das/src/main/java/com/das/modules/data/service/DataService.java b/das/src/main/java/com/das/modules/data/service/DataService.java index 445090e1..cf2a2d96 100644 --- a/das/src/main/java/com/das/modules/data/service/DataService.java +++ b/das/src/main/java/com/das/modules/data/service/DataService.java @@ -17,5 +17,6 @@ public interface DataService { void updateCalFieldData(List values); - Double getTimeTopValue(Long devcieId, String attr, long startTime, long endTime); + Double getTimeTopValue(Long devcieId, String attr, Long startTime, Long endTime); + Double getTimeSumValue(Long devcieId, String attr, Long startTime, Long endTime); } diff --git a/das/src/main/java/com/das/modules/data/service/TDEngineService.java b/das/src/main/java/com/das/modules/data/service/TDEngineService.java index 5ee1044b..f4ba5e7d 100644 --- a/das/src/main/java/com/das/modules/data/service/TDEngineService.java +++ b/das/src/main/java/com/das/modules/data/service/TDEngineService.java @@ -784,7 +784,7 @@ public class TDEngineService { } } - public Double getTimeTopValue(String tableName, String attr, long startTime, long endTime) { + public Double getTimeTopValue(String tableName, String attr, Long startTime, Long endTime) { StringBuffer sb = new StringBuffer(256); sb.append("select "); sb.append("first("); @@ -792,8 +792,10 @@ public class TDEngineService { sb.append(") as value"); sb.append(" from "); sb.append(tableName); - sb.append(" where "); - sb.append(String.format(" updatetime >= %d and updatetime < %d ", startTime, endTime)); + if (startTime != null && endTime != null) { + sb.append(" where "); + sb.append(String.format(" updatetime >= %d and updatetime < %d ", startTime, endTime)); + } Double result = null; try (Connection conn = hikariDataSource.getConnection(); Statement smt = conn.createStatement(); @@ -855,4 +857,29 @@ public class TDEngineService { log.error("修改Tag值失败", e); } } + + public Double getTimeSumValue(String tableName, String attr, Long startTime, Long endTime) { + StringBuffer sb = new StringBuffer(256); + sb.append("select "); + sb.append("sum("); + sb.append(attr); + sb.append(") as value"); + sb.append(" from "); + sb.append(tableName); + if (startTime != null && endTime != null) { + sb.append(" where "); + sb.append(String.format(" updatetime >= %d and updatetime < %d ", startTime, endTime)); + } + Double result = null; + try (Connection conn = hikariDataSource.getConnection(); + Statement smt = conn.createStatement(); + ResultSet rs = smt.executeQuery(sb.toString())) { + if (rs.next()) { + result = rs.getDouble("value"); + } + } catch (Exception e) { + log.error("获取数据异常", e); + } + return result; + } } diff --git a/das/src/main/java/com/das/modules/data/service/impl/DataServiceImpl.java b/das/src/main/java/com/das/modules/data/service/impl/DataServiceImpl.java index 22c4ccca..e265420e 100644 --- a/das/src/main/java/com/das/modules/data/service/impl/DataServiceImpl.java +++ b/das/src/main/java/com/das/modules/data/service/impl/DataServiceImpl.java @@ -268,7 +268,7 @@ public class DataServiceImpl implements DataService { * @return */ @Override - public Double getTimeTopValue(Long devcieId, String attr, long startTime, long endTime){ + public Double getTimeTopValue(Long devcieId, String attr, Long startTime, Long endTime){ DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(devcieId); if (deviceInfoCache == null) { return null; @@ -283,4 +283,21 @@ public class DataServiceImpl implements DataService { } return tdEngineService.getTimeTopValue(tableName, attr, startTime, endTime); } + + @Override + public Double getTimeSumValue(Long devcieId, String attr, Long startTime, Long endTime) { + DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(devcieId); + if (deviceInfoCache == null) { + return null; + } + String tableName = ""; + if (cacheService.getIotModelCache().isHighSpeed(deviceInfoCache.getIotModelId(), attr)){ + tableName = String.format("h_%s", deviceInfoCache.getDeviceId()); + } else if (cacheService.getIotModelCache().isLowSpeed(deviceInfoCache.getIotModelId(), attr)){ + tableName = String.format("l_%s", deviceInfoCache.getDeviceId()); + } else if (cacheService.getIotModelCache().isCalculate(deviceInfoCache.getIotModelId(), attr)){ + tableName = String.format("c_%s", deviceInfoCache.getDeviceId()); + } + return tdEngineService.getTimeSumValue(tableName, attr, startTime, endTime); + } }