diff --git a/das/src/main/java/com/das/modules/calc/functions/FunctionAvgValue.java b/das/src/main/java/com/das/modules/calc/functions/FunctionAvgValue.java new file mode 100644 index 00000000..68742ee6 --- /dev/null +++ b/das/src/main/java/com/das/modules/calc/functions/FunctionAvgValue.java @@ -0,0 +1,76 @@ +package com.das.modules.calc.functions; + +import cn.hutool.core.date.DateField; +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.extern.slf4j.Slf4j; + +import java.util.Date; +import java.util.Map; + +/** + * Aviator扩展函数 - 获取时间维度内平均 + * 函数格式: avg(deviceId, attr, timedim) + * timedim: day - 天, month - 月, year - 年 + * 返回值:数值, nil - 获取错误 + */ +@Slf4j +public class FunctionAvgValue extends AbstractFunction { + + private DataService dataService = null; + private CacheService cacheService = null; + + + public FunctionAvgValue(DataService dataService, CacheService cacheService) { + this.dataService = dataService; + this.cacheService = cacheService; + } + + @Override + public String getName() { + return "avgv"; + } + + @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.getTimeAvgValue(deviceInfoCache.getDeviceId(), attrName, null, null); + if (value == null){ + return AviatorNil.NIL; + } + //未找到缓存,查询时序API获取数据 + return AviatorRuntimeJavaType.valueOf(value); + } + @Override + public AviatorObject call(Map env, AviatorObject deviceCode, AviatorObject attr, AviatorObject startTimeData, AviatorObject endTimeData) { + //设备Code + String code = (String)deviceCode.getValue(env); + String attrName = (String)attr.getValue(env); + Date startTime = (Date)startTimeData.getValue(env); + Date endTime = (Date)endTimeData.getValue(env); + DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheByCode(code); + if (deviceInfoCache == null) { + return AviatorNil.NIL; + } + Double value = dataService.getTimeSumValue(deviceInfoCache.getDeviceId(), attrName, startTime.getTime(), endTime.getTime()); + if (value == null){ + return AviatorNil.NIL; + } + //未找到缓存,查询时序API获取数据 + return AviatorRuntimeJavaType.valueOf(value); + } + +} diff --git a/das/src/main/java/com/das/modules/calc/functions/FunctionOffsetDate.java b/das/src/main/java/com/das/modules/calc/functions/FunctionOffsetDate.java new file mode 100644 index 00000000..67582de4 --- /dev/null +++ b/das/src/main/java/com/das/modules/calc/functions/FunctionOffsetDate.java @@ -0,0 +1,60 @@ +package com.das.modules.calc.functions; + +import cn.hutool.core.date.DateField; +import cn.hutool.core.date.DateUtil; +import com.googlecode.aviator.runtime.function.AbstractFunction; +import com.googlecode.aviator.runtime.type.AviatorObject; +import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType; +import lombok.extern.slf4j.Slf4j; + +import java.util.Date; +import java.util.Map; + +/** + * Aviator扩展函数 - 获取时间维度的起始时间 + * 函数格式: beginOf(Date, TimeDim) + * + */ +@Slf4j +public class FunctionOffsetDate extends AbstractFunction { + + public FunctionOffsetDate() { + } + + @Override + public String getName() { + return "offsetDate"; + } + + @Override + public AviatorObject call(Map env, AviatorObject dateData, AviatorObject dimData, AviatorObject offsetData) { + Date date = (Date) dateData.getValue(env); + String dim = (String) dimData.getValue(env); + Integer offset = (Integer) offsetData.getValue(env); + + Date result = null; + switch (dim) { + case "day": + result = DateUtil.offset(date, DateField.DAY_OF_MONTH, offset); + break; + case "month": + result = DateUtil.offset(date, DateField.MONTH, offset); + break; + case "year": + result = DateUtil.offset(date, DateField.YEAR, offset); + break; + case "hour": + result = DateUtil.offset(date, DateField.HOUR, offset); + break; + case "minute": + result = DateUtil.offset(date, DateField.MINUTE, offset); + break; + case "second": + result = DateUtil.offset(date, DateField.SECOND, offset); + break; + default: + log.error("不支持的维度: {}", dim); + } + return AviatorRuntimeJavaType.valueOf(result); + } +} diff --git a/das/src/main/java/com/das/modules/calc/service/CalcService.java b/das/src/main/java/com/das/modules/calc/service/CalcService.java index af47fab7..0e291a3b 100644 --- a/das/src/main/java/com/das/modules/calc/service/CalcService.java +++ b/das/src/main/java/com/das/modules/calc/service/CalcService.java @@ -128,6 +128,12 @@ public class CalcService { FunctionEndOfDate endOfDate = new FunctionEndOfDate(); aviator.addFunction(endOfDate); + + FunctionAvgValue avgValue = new FunctionAvgValue(dataService, cacheService); + aviator.addFunction(avgValue); + + FunctionOffsetDate offsetDate = new FunctionOffsetDate(); + aviator.addFunction(offsetDate); } /** 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 cf2a2d96..7628653f 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,6 +17,8 @@ public interface DataService { void updateCalFieldData(List values); - Double getTimeTopValue(Long devcieId, String attr, Long startTime, Long endTime); - Double getTimeSumValue(Long devcieId, String attr, Long startTime, Long endTime); + Double getTimeTopValue(Long deviceId, String attr, Long startTime, Long endTime); + Double getTimeSumValue(Long deviceId, String attr, Long startTime, Long endTime); + + Double getTimeAvgValue(Long deviceId, String attrName, 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 be7da2d3..639eb10b 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 @@ -905,4 +905,52 @@ public class TDEngineService { } return result; } + + public Double getTimeAvgValue(String tableName, String attr, Long startTime, Long endTime) { + StringBuffer sb = new StringBuffer(256); + sb.append("select "); + sb.append("avg("); + sb.append(attr); + sb.append(")"); + 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 = 0.0; + try (Connection conn = hikariDataSource.getConnection(); + Statement smt = conn.createStatement(); + ResultSet rs = smt.executeQuery(sb.toString())) { + if (rs.next()) { + result = rs.getDouble(1); + } + } catch (Exception e) { + log.error("获取数据异常",e); + } + return result; + } + + public Double getTimeAvgCalcValue(String tableName, String attr, Long startTime, Long endTime) { + StringBuffer sb = new StringBuffer(256); + sb.append("select "); + sb.append("avg(datavalue)"); + 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 = 0.0; + try (Connection conn = hikariDataSource.getConnection(); + Statement smt = conn.createStatement(); + ResultSet rs = smt.executeQuery(sb.toString())) { + if (rs.next()) { + result = rs.getDouble(1); + } + } 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 9e8dac56..2b9995d6 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,8 +268,8 @@ public class DataServiceImpl implements DataService { * @return */ @Override - public Double getTimeTopValue(Long devcieId, String attr, Long startTime, Long endTime){ - DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(devcieId); + public Double getTimeTopValue(Long deviceId, String attr, Long startTime, Long endTime){ + DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(deviceId); if (deviceInfoCache == null) { return null; } @@ -285,8 +285,8 @@ public class DataServiceImpl implements DataService { } @Override - public Double getTimeSumValue(Long devcieId, String attr, Long startTime, Long endTime) { - DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(devcieId); + public Double getTimeSumValue(Long deviceId, String attr, Long startTime, Long endTime) { + DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(deviceId); if (deviceInfoCache == null) { return null; } @@ -301,4 +301,22 @@ public class DataServiceImpl implements DataService { } return tdEngineService.getTimeSumValue(tableName, attr, startTime, endTime); } + + @Override + public Double getTimeAvgValue(Long deviceId, String attr, Long startTime, Long endTime) { + DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(deviceId); + if (deviceInfoCache == null) { + return null; + } + String tableName = ""; + if (cacheService.getIotModelCache().isCalculate(deviceInfoCache.getIotModelId(), attr)){ + tableName = String.format("c_%s_%s", deviceInfoCache.getDeviceId(), attr.toLowerCase()); + return tdEngineService.getTimeAvgCalcValue(tableName, attr, startTime, endTime); + } else if (cacheService.getIotModelCache().isHighSpeed(deviceInfoCache.getIotModelId(), attr.toLowerCase())){ + tableName = String.format("h_%s", deviceInfoCache.getDeviceId()); + } else if (cacheService.getIotModelCache().isLowSpeed(deviceInfoCache.getIotModelId(), attr.toLowerCase())){ + tableName = String.format("l_%s", deviceInfoCache.getDeviceId(), attr.toLowerCase()); + } + return tdEngineService.getTimeAvgValue(tableName, attr, startTime, endTime); + } }