为风速系数增加缓存

This commit is contained in:
谷成伟 2024-12-05 17:40:38 +08:00
parent 6eedf5826f
commit bc675664f7
5 changed files with 83 additions and 24 deletions

View File

@ -2,7 +2,9 @@ package com.das.modules.cache.service;
import com.das.modules.cache.domain.WindSpeedCoefValue; import com.das.modules.cache.domain.WindSpeedCoefValue;
import java.util.List;
public interface CalcCache { public interface CalcCache {
WindSpeedCoefValue getWindSpeedCoef(Long deviceId); List<WindSpeedCoefValue> getWindSpeedCoef(Long deviceId);
void putWindSpeedCoef(Long deviceId, WindSpeedCoefValue windSpeedCoefValue); void refreshWindSpeedCoef();
} }

View File

@ -18,10 +18,20 @@ public class CalcCacheImpl implements CalcCache {
@Autowired @Autowired
SysPowerCurveFactorMapper sysPowerCurveFactorMapper; SysPowerCurveFactorMapper sysPowerCurveFactorMapper;
private ConcurrentHashMap<Long, WindSpeedCoefValue> windSpeedCoefMap = new ConcurrentHashMap<>(); private ConcurrentHashMap<Long, List<WindSpeedCoefValue>> windSpeedCoefMap = new ConcurrentHashMap<>();
@PostConstruct @PostConstruct
private void init(){ private void init(){
refreshWindSpeedCoef();
}
@Override
public List<WindSpeedCoefValue> getWindSpeedCoef(Long deviceId) {
return windSpeedCoefMap.get(deviceId);
}
@Override
public void refreshWindSpeedCoef() {
List<SysPowerCurveFactorVo> sysPowerCurveFactorVos = sysPowerCurveFactorMapper.querySysPowerCurveFactorList(new SysPowerCurveFactorDto()); List<SysPowerCurveFactorVo> sysPowerCurveFactorVos = sysPowerCurveFactorMapper.querySysPowerCurveFactorList(new SysPowerCurveFactorDto());
for (SysPowerCurveFactorVo sysPowerCurveFactorVo : sysPowerCurveFactorVos) { for (SysPowerCurveFactorVo sysPowerCurveFactorVo : sysPowerCurveFactorVos) {
WindSpeedCoefValue windSpeedCoefValue = new WindSpeedCoefValue(); WindSpeedCoefValue windSpeedCoefValue = new WindSpeedCoefValue();
@ -29,17 +39,12 @@ public class CalcCacheImpl implements CalcCache {
windSpeedCoefValue.setMaxValue(sysPowerCurveFactorVo.getSpeedMax()); windSpeedCoefValue.setMaxValue(sysPowerCurveFactorVo.getSpeedMax());
windSpeedCoefValue.setCoef(sysPowerCurveFactorVo.getFactorK()); windSpeedCoefValue.setCoef(sysPowerCurveFactorVo.getFactorK());
windSpeedCoefValue.setBase(sysPowerCurveFactorVo.getFactorB()); windSpeedCoefValue.setBase(sysPowerCurveFactorVo.getFactorB());
windSpeedCoefMap.put(sysPowerCurveFactorVo.getTurbineId(), windSpeedCoefValue); List<WindSpeedCoefValue> windSpeedCoefValues = windSpeedCoefMap.get(sysPowerCurveFactorVo.getTurbineId());
if (windSpeedCoefValues == null) {
windSpeedCoefValues = new java.util.ArrayList<>();
}
windSpeedCoefValues.add(windSpeedCoefValue);
windSpeedCoefMap.put(sysPowerCurveFactorVo.getTurbineId(), windSpeedCoefValues);
} }
} }
@Override
public WindSpeedCoefValue getWindSpeedCoef(Long deviceId) {
return windSpeedCoefMap.get(deviceId);
}
@Override
public void putWindSpeedCoef(Long deviceId, WindSpeedCoefValue windSpeedCoefValue) {
windSpeedCoefMap.put(deviceId, windSpeedCoefValue);
}
} }

View File

@ -0,0 +1,59 @@
package com.das.modules.calc.functions;
import cn.hutool.core.collection.CollectionUtil;
import com.das.modules.cache.domain.DeviceInfoCache;
import com.das.modules.cache.domain.WindSpeedCoefValue;
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.List;
import java.util.Map;
/**
* Aviator扩展函数 - 获取时间维度内平均
* 函数格式: avg(deviceId, attr, timedim)
* timedim: day - month - year -
* 返回值数值 nil - 获取错误
*/
@Slf4j
public class FunctionWindSpeedFactor extends AbstractFunction {
private DataService dataService = null;
private CacheService cacheService = null;
public FunctionWindSpeedFactor(DataService dataService, CacheService cacheService) {
this.dataService = dataService;
this.cacheService = cacheService;
}
@Override
public String getName() {
return "windSpeedFactor";
}
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject deviceCode) {
//设备Code
String code = (String)deviceCode.getValue(env);
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheByCode(code);
if (deviceInfoCache == null) {
return AviatorNil.NIL;
}
List<WindSpeedCoefValue> windSpeedCoef = cacheService.getCalcCache().getWindSpeedCoef(deviceInfoCache.getDeviceId());
if (CollectionUtil.isEmpty(windSpeedCoef)){
return AviatorNil.NIL;
}
//未找到缓存查询时序API获取数据
return AviatorRuntimeJavaType.valueOf(windSpeedCoef);
}
}

View File

@ -96,14 +96,7 @@ public class WindSpeedCorrectServiceImpl implements WindSpeedCorrectService {
//批量插入数据库 //批量插入数据库
sysPowerCurveFactorMapper.insertBatch(listData); sysPowerCurveFactorMapper.insertBatch(listData);
//更新缓存数据 //更新缓存数据
listData.forEach(sysPowerCurveFactor -> { cacheService.getCalcCache().refreshWindSpeedCoef();
WindSpeedCoefValue windSpeedCoefValue = new WindSpeedCoefValue();
windSpeedCoefValue.setMaxValue(sysPowerCurveFactor.getSpeedMax());
windSpeedCoefValue.setMinValue(sysPowerCurveFactor.getSpeedMin());
windSpeedCoefValue.setCoef(sysPowerCurveFactor.getFactorK());
windSpeedCoefValue.setBase(sysPowerCurveFactor.getFactorB());
cacheService.getCalcCache().putWindSpeedCoef(sysPowerCurveFactor.getTurbineId(),windSpeedCoefValue);
});
} }
@Override @Override

View File

@ -24,7 +24,7 @@
and t.turbine_id = #{info.turbineId} and t.turbine_id = #{info.turbineId}
</if> </if>
</where> </where>
order by sq.name asc order by sq.name asc, t.speed_min asc
</select> </select>
<select id="querySysPowerCurveFactorList" resultMap="resultMap"> <select id="querySysPowerCurveFactorList" resultMap="resultMap">
@ -46,7 +46,7 @@
</foreach> </foreach>
</if> </if>
</where> </where>
order by sq.name asc order by sq.name asc, t.speed_min asc
</select> </select>