|
@@ -3,16 +3,19 @@ package com.platform.opc.servie;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.platform.common.constant.UpmsRedisKeyConstants;
|
|
|
import com.platform.common.util.BeanUtils;
|
|
|
+import com.platform.common.util.DateUtils;
|
|
|
import com.platform.common.util.RedisUtils;
|
|
|
import com.platform.common.util.StringUtils;
|
|
|
import com.platform.dao.entity.remote.RemoteOpc;
|
|
|
import com.platform.dao.entity.remote.RemoteOpcLog;
|
|
|
+import com.platform.dao.mapper.remote.RemoteOpcLogMapper;
|
|
|
import com.platform.dao.mapper.remote.RemoteOpcMapper;
|
|
|
import com.platform.dao.mapper.upms.SysDictMapper;
|
|
|
import com.platform.opc.entity.OpcResult;
|
|
|
import com.platform.opc.util.OpcDAClient;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.DependsOn;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
@@ -22,6 +25,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -34,16 +38,18 @@ import java.util.stream.Collectors;
|
|
|
@EnableAsync
|
|
|
public class OpcTaskService {
|
|
|
|
|
|
+ private final RemoteOpcLogMapper remoteOpcLogMapper;
|
|
|
+
|
|
|
/**
|
|
|
* 1: 分组获取数据
|
|
|
- * a:保存到redis,前端页面实时从数据库获取数据,2秒刷新一次
|
|
|
+ * a:保存到redis,前端页面实时从数据库获取数据,5秒刷新一次
|
|
|
*/
|
|
|
@Async
|
|
|
@Scheduled(fixedDelay = 5000) //间隔5秒
|
|
|
- public void getValue(){
|
|
|
+ public void getValue() {
|
|
|
log.info("开始定时任务");
|
|
|
List<OpcResult> resultList = OpcDAClient.getItemValuesList();
|
|
|
- if(!CollectionUtils.isEmpty(resultList)){
|
|
|
+ if (!CollectionUtils.isEmpty(resultList)) {
|
|
|
log.info("resultList数量:" + resultList.size());
|
|
|
String str = JSON.toJSONString(resultList);
|
|
|
//log.info("str: " + str);
|
|
@@ -54,30 +60,34 @@ public class OpcTaskService {
|
|
|
|
|
|
/**
|
|
|
* 1: 保存获取的数据
|
|
|
- * b: 开启新线程,队列写入数据库,每一分钟启动一次
|
|
|
- * c: 每个点位,每条数据保存一天的
|
|
|
- * 1)循环查询点位在当天是否存在记录,如果存在,则追加
|
|
|
- * 2)批量写入数据库
|
|
|
+ * b: 开启新线程,队列写入数据库,每一分钟启动一次
|
|
|
+ * c: 每个点位,每条数据保存一天的
|
|
|
+ * 1)循环查询点位在当天是否存在记录,如果存在,则追加
|
|
|
+ * 2)批量写入数据库
|
|
|
*/
|
|
|
@Async
|
|
|
@Scheduled(fixedDelay = 60000) //间隔60秒
|
|
|
- public void saveValue(){
|
|
|
+ public void saveValue() {
|
|
|
log.info("开始读取redis");
|
|
|
- String jsonStr= RedisUtils.getString("opcList");
|
|
|
- if(StringUtils.isNotBlank(jsonStr)){
|
|
|
+ String jsonStr = RedisUtils.getString("opcList");
|
|
|
+ if (StringUtils.isNotBlank(jsonStr)) {
|
|
|
log.info("jsonStr2:" + jsonStr);
|
|
|
- List<OpcResult> resultList = JSON.parseArray(jsonStr, OpcResult.class);
|
|
|
+ List<OpcResult> resultList = JSON.parseArray(jsonStr, OpcResult.class);
|
|
|
log.info("resultList数量:" + resultList.size());
|
|
|
/*for(OpcResult result: resultList){
|
|
|
log.info("id: " + result.getId() + ", value: " + result.getValue() + ", time: " + result.getTime());
|
|
|
}*/
|
|
|
// 保存入库
|
|
|
- for(OpcResult result: resultList){
|
|
|
+ List<RemoteOpcLog> remoteOpcLogList = new ArrayList<>();
|
|
|
+ for (OpcResult result : resultList) {
|
|
|
RemoteOpcLog remoteOpcLog = new RemoteOpcLog();
|
|
|
remoteOpcLog.setPositionNum(result.getId());
|
|
|
- log.info("id: " + result.getId() + ", value: " + result.getValue() + ", time: " + result.getTime());
|
|
|
+ remoteOpcLog.setResult(result.getValue());
|
|
|
+ remoteOpcLog.setCreatedTime(DateUtils.strToLocalDateTime(result.getTime(), DateUtils.PATTERN_YMD_HMS));
|
|
|
+ remoteOpcLogList.add(remoteOpcLog);
|
|
|
+ //log.info("id: " + result.getId() + ", value: " + result.getValue() + ", time: " + result.getTime());
|
|
|
}
|
|
|
-
|
|
|
+ remoteOpcLogMapper.insertListforComplex(remoteOpcLogList);
|
|
|
}
|
|
|
log.info("结束读取redis");
|
|
|
}
|