|
@@ -1,130 +0,0 @@
|
|
|
-package com.platform.opc.servie;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.platform.common.util.DateUtils;
|
|
|
-import com.platform.common.util.RedisUtils;
|
|
|
-import com.platform.common.util.StringUtils;
|
|
|
-import com.platform.dao.dto.check.CheckProjectDTO;
|
|
|
-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.opc.entity.OpcResult;
|
|
|
-import com.platform.opc.util.OpcDAClient;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
-import tk.mybatis.mapper.weekend.Weekend;
|
|
|
-import tk.mybatis.mapper.weekend.WeekendCriteria;
|
|
|
-
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-@Service("opcTaskService")
|
|
|
-@AllArgsConstructor
|
|
|
-@Slf4j
|
|
|
-@EnableScheduling // 1.开启定时任务
|
|
|
-public class OpcTaskService {
|
|
|
-
|
|
|
- private final RemoteOpcMapper remoteOpcMapper;
|
|
|
- private final RemoteOpcLogMapper remoteOpcLogMapper;
|
|
|
-
|
|
|
- /**
|
|
|
- * 1: 分组获取数据
|
|
|
- * a:保存到redis,前端页面实时从数据库获取数据,5秒刷新一次
|
|
|
- */
|
|
|
- @Scheduled(fixedDelay = 2000) //间隔2秒
|
|
|
- public void getValue() {
|
|
|
- log.info("启动拉取");
|
|
|
- String key = RedisUtils.getString(OpcDAClient.redis_opc_update_flag);
|
|
|
- if(StringUtils.isBlank(key)){
|
|
|
- log.info("开始拉取数据");
|
|
|
- List<OpcResult> resultList = OpcDAClient.getItemValuesList();
|
|
|
- if (!CollectionUtils.isEmpty(resultList)) {
|
|
|
- log.info("拉取数量:" + resultList.size());
|
|
|
- RedisUtils.setString(OpcDAClient.redis_opc_item_values, JSON.toJSONString(resultList));
|
|
|
- // 更新数据库实时数据
|
|
|
- List<RemoteOpc> remoteOpcList = new ArrayList<>();
|
|
|
- LocalDateTime localDateTime = LocalDateTime.now();
|
|
|
- for (OpcResult result : resultList) {
|
|
|
- RemoteOpc remoteOpc = new RemoteOpc();
|
|
|
- remoteOpc.setResult(new BigDecimal(result.getValue()).setScale(2));
|
|
|
- remoteOpc.setPositionNum(result.getId());
|
|
|
- remoteOpc.setUpdateTime(localDateTime);
|
|
|
- remoteOpcList.add(remoteOpc);
|
|
|
- }
|
|
|
- remoteOpcMapper.updateBatch(remoteOpcList);
|
|
|
- } else {
|
|
|
- log.info("初始化启动分组错误,等待下次重新启动分组");
|
|
|
- }
|
|
|
- log.info("结束拉取数据");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 1: 保存获取的数据
|
|
|
- * b: 开启新线程,队列写入数据库,每2分钟启动一次
|
|
|
- * c: 每个点位,每条数据保存一天的
|
|
|
- * 1)循环查询点位在当天是否存在记录,如果存在,则追加
|
|
|
- * 2)批量写入数据库,每天的数据追加到一条里面
|
|
|
- */
|
|
|
- @Scheduled(fixedDelay = 300000) //间隔300秒,5分钟保存一次数据到数据库,确保每天不超过700万数据
|
|
|
- public void saveValue() {
|
|
|
- log.info("启动保存");
|
|
|
- String key = RedisUtils.getString(OpcDAClient.redis_opc_update_flag);
|
|
|
- if(StringUtils.isBlank(key)){
|
|
|
- log.info("开始保存点位");
|
|
|
- String jsonStr = RedisUtils.getString(OpcDAClient.redis_opc_item_values);
|
|
|
- if (StringUtils.isNotBlank(jsonStr)) {
|
|
|
- List<OpcResult> resultList = JSON.parseArray(jsonStr, OpcResult.class);
|
|
|
- List<RemoteOpcLog> addOpcLogList = new ArrayList<>();
|
|
|
- List<RemoteOpcLog> updateRemoteOpcLogList = new ArrayList<>();
|
|
|
- Weekend<RemoteOpcLog> weekend = new Weekend<>(RemoteOpcLog.class);
|
|
|
- WeekendCriteria<RemoteOpcLog, Object> weekendCriteria = weekend.weekendCriteria();
|
|
|
- // 查询当天是否已经存在了,存在则追加,否则更新
|
|
|
- LocalDateTime time = LocalDateTime.now();
|
|
|
- weekendCriteria.andIn(RemoteOpcLog::getPositionNum, resultList.stream().map(OpcResult::getId).collect(Collectors.toList()));
|
|
|
- weekendCriteria.andEqualTo(RemoteOpcLog::getYear, time.getYear());
|
|
|
- weekendCriteria.andEqualTo(RemoteOpcLog::getMonth, time.getMonthValue());
|
|
|
- weekendCriteria.andEqualTo(RemoteOpcLog::getDay, time.getDayOfMonth());
|
|
|
- List<RemoteOpcLog> checkList = remoteOpcLogMapper.selectByExample(weekend);
|
|
|
- for (OpcResult result : resultList) {
|
|
|
- RemoteOpcLog remoteOpcLog = new RemoteOpcLog();
|
|
|
- remoteOpcLog.setPositionNum(result.getId());
|
|
|
- remoteOpcLog.setResult(new BigDecimal(result.getValue()));
|
|
|
- LocalDateTime localDateTime = DateUtils.strToLocalDateTime(result.getTime(), DateUtils.PATTERN_YMD_HMS);
|
|
|
- remoteOpcLog.setCreatedTime(localDateTime);
|
|
|
- remoteOpcLog.setYear(localDateTime.getYear());
|
|
|
- remoteOpcLog.setMonth(localDateTime.getMonthValue());
|
|
|
- remoteOpcLog.setDay(localDateTime.getDayOfMonth());
|
|
|
- // remoteOpcLog.setHour(localDateTime.getHour());
|
|
|
- // remoteOpcLog.setMinute(localDateTime.getMinute());
|
|
|
- remoteOpcLog.setRemark(result.getTime().split(" ")[1] + "," + result.getValue() + ";");
|
|
|
- List<RemoteOpcLog> findItemList = checkList.stream().filter(remoteOpcLog1 -> remoteOpcLog1.getPositionNum().equals(result.getId())).collect(Collectors.toList());
|
|
|
- if (!CollectionUtils.isEmpty(findItemList) && findItemList.size()>0) {
|
|
|
- updateRemoteOpcLogList.add(remoteOpcLog);
|
|
|
- } else {
|
|
|
- addOpcLogList.add(remoteOpcLog);
|
|
|
- }
|
|
|
- }
|
|
|
- if(!CollectionUtils.isEmpty(updateRemoteOpcLogList)){
|
|
|
- remoteOpcLogMapper.updateBatch(updateRemoteOpcLogList);
|
|
|
- log.info("更新数量:" + resultList.size());
|
|
|
- }
|
|
|
- if(!CollectionUtils.isEmpty(addOpcLogList)){
|
|
|
- remoteOpcLogMapper.insertListforComplex(addOpcLogList);
|
|
|
- log.info("写入数量:" + resultList.size());
|
|
|
- }
|
|
|
- log.info("写入/更新数据库数量:" + resultList.size());
|
|
|
- }
|
|
|
- log.info("结束保存点位");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|