新增缓存值函数

This commit is contained in:
谷成伟 2024-11-28 13:44:24 +08:00
parent b9ded9def6
commit 4ade32744e
3 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,65 @@
package com.das.modules.calc.functions;
import com.das.common.utils.AdminRedisTemplate;
import com.das.modules.cache.domain.DeviceInfoCache;
import com.das.modules.cache.service.CacheService;
import com.das.modules.data.domain.SnapshotValueQueryParam;
import com.das.modules.data.service.DataService;
import com.googlecode.aviator.exception.StandardError;
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.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Aviator扩展函数 - 获取设备实时数据
* 函数格式: cacheValue(key, value) 设置缓存值
* 函数格式: cacheValue(key) 读取缓存值
*/
@Slf4j
public class FunctionGetCacheValue extends AbstractFunction {
private ConcurrentHashMap<String,Double> cacheValues = new ConcurrentHashMap<>();
public FunctionGetCacheValue( ) {
}
@Override
public String getName() {
return "cacheValue";
}
@SneakyThrows
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject keyData, AviatorObject valData) {
//设备Code
String key = (String)keyData.getValue(env);
Double value = (Double) valData.getValue(env);
String scriptName = (String)env.get("G_SCRIPTNAME");
String cacheKey = String.format("%s_%s", scriptName, key);
cacheValues.put(cacheKey, value);
return AviatorRuntimeJavaType.valueOf(0);
}
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject keyData) {
//设备Code
String key = (String)keyData.getValue(env);
String scriptName = (String)env.get("G_SCRIPTNAME");
String cacheKey = String.format("%s_%s", scriptName, key);
Double value = cacheValues.get(cacheKey);
if (value == null) {
return AviatorNil.NIL;
}
return AviatorRuntimeJavaType.valueOf(value);
}
}

View File

@ -38,6 +38,8 @@ public class CalcJob implements Job {
}
//准备全局变量
Map<String,Object> envs = expression.newEnv();
//脚本名称
envs.put("G_SCRIPTNAME", calcModule.getName());
//风场信息
envs.put("G_WF", cacheService.getEquipmentCache().getDevicesCache().stream().filter(c->c.getObjectType().equals(10001)).findFirst().get());
//风机信息

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.das.modules.cache.service.CacheService;
import com.das.modules.calc.domain.entity.CalcModule;
import com.das.modules.calc.domain.vo.CalcModuleVo;
import com.das.modules.calc.functions.FunctionGetCacheValue;
import com.das.modules.calc.functions.FunctionRealData;
import com.das.modules.calc.functions.FunctionSaveCalcData;
import com.das.modules.calc.mapper.CalcModuleMapper;
@ -110,6 +111,9 @@ public class CalcService {
//
FunctionSaveCalcData save = new FunctionSaveCalcData(dataService, cacheService);
aviator.addFunction(save);
FunctionGetCacheValue cache = new FunctionGetCacheValue();
aviator.addFunction(cache);
}
/**