You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
5.4 KiB
Java

2 years ago
package com.xydl.service.impl;
import com.xydl.mapper.OperationDB;
import com.xydl.util.FormatUtil;
import com.xydl.util.MqttUtil;
2 years ago
import com.xydl.util.Subscribe;
2 years ago
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
2 years ago
import java.util.stream.Collectors;
2 years ago
@Service
public class MqttServiceImpl {
2 years ago
private static final Logger logger = LoggerFactory.getLogger(MqttServiceImpl.class);
2 years ago
@Autowired
OperationDB operationDBMapper;
2 years ago
// @Scheduled(initialDelay = 1000, fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务使用fixedRate属性每隔固定时间执行
2 years ago
public void reportRecord() {
2 years ago
logger.info("开始执行");
2 years ago
Subscribe.getInstance();
2 years ago
List<String> allTableNames = operationDBMapper.getAllTable();
for (String tableName : allTableNames) {
Map<String,String> fieldMap = new HashMap<>();
List<Map<String, String>> fieldMaps = operationDBMapper.getFieldMap(tableName);
for(Map<String,String> map: fieldMaps){
for(String key : map.keySet()){
fieldMap.put(map.get("field_name"),map.get("dest_field_name"));
2 years ago
}
}
2 years ago
String sqlExecuting = operationDBMapper.getSQL(tableName);
2 years ago
String preSQL = sqlExecuting.substring(0,sqlExecuting.indexOf("?"));
String midSQL = sqlExecuting.substring(sqlExecuting.indexOf("?")+1,sqlExecuting.lastIndexOf("?"));
String lastSQL = sqlExecuting.substring(sqlExecuting.lastIndexOf("?")+1);
2 years ago
List<Integer> dataEqmids = operationDBMapper.getDataEqmids(tableName);
List<Integer> syncDevIds = operationDBMapper.getSyncRecordDevIds(tableName);
if (dataEqmids.size() != syncDevIds.size()) {
List<Integer> distinctDevids = dataEqmids.stream().filter(e -> !syncDevIds.contains(e)).collect(Collectors.toList());
for (Integer devId : distinctDevids) {
String earliestTime = null;
if ("data_eaif_h".equals(tableName)) {
earliestTime = operationDBMapper.getEarliestTime4Eaif(tableName, devId);
} else {
earliestTime = operationDBMapper.getEarliestTime4Other(tableName, devId);
}
operationDBMapper.addEarliestTime(10,tableName, String.valueOf(devId), earliestTime);
}
}
2 years ago
2 years ago
Map<Integer,Object> devIDLastTimeMap = new HashMap<>();
List<Map<String, Object>> devIDLastTimeMaps = operationDBMapper.getDeviceIDAndtime(tableName);
for(Map<String,Object> map : devIDLastTimeMaps){
for(String devId : map.keySet()){
devIDLastTimeMap.put(Integer.parseInt((String) map.get("devid_val")), map.get("field_val2"));
}
}
for (int deviceID : devIDLastTimeMap.keySet()) {
String time = devIDLastTimeMap.get(deviceID).toString();
System.out.println(time);
String sql = preSQL+deviceID+midSQL+"'"+time+"'"+lastSQL;
System.out.println("sql :"+sql);
List<Map<String, Object>> dataOfoneDeviceID = operationDBMapper.getData(sql);
2 years ago
String jsonStringData = FormatUtil.mqttFormatTransform(dataOfoneDeviceID, fieldMap);
logger.info("表{}设备{}推送数据:{}", tableName, deviceID, jsonStringData);
2 years ago
if (new MqttUtil().publish2MQTT(jsonStringData)) {
operationDBMapper.updateSyncRecordsTable(tableName, deviceID, (String) devIDLastTimeMap.get(deviceID));
2 years ago
logger.info("推送成功");
} else {
logger.info("消息推送失败");
2 years ago
}
}
}
}
2 years ago
// @Scheduled(fixedDelay = Long.MAX_VALUE) // 用一个非常大的延迟值,确保只执行一次
// public void subScribeSamle() {
// logger.info("开始订阅===subScribe执行一次==={}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
// MqttUtil.subScribeMQTT();
// }
// @Scheduled(fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务使用fixedRate属性每隔固定时间执行
// public void checkDevIdTimer() {
// logger.info("每小时检测一次同步的表是否在‘同步记录表’");
// List<String> allTableNames = getAllTableNameFromSyncTable();
// for (String tableName : allTableNames) {
// if (!tableNameIfExitsSyncRec(tableName)) {
// logger.info("有不存在的表{},把所有的devId及最早的时间更新到'同步记录表'", tableName);
// List<String> devIds = operationDBMapper.getAllDevId(tableName);
// for (String devId : devIds) {
// String earliestTime = null;
// if ("data_eaif_h".equals(tableName)) {
// earliestTime = operationDBMapper.getEarliestTime4Eaif(tableName, devId);
// } else {
// earliestTime = operationDBMapper.getEarliestTime4Other(tableName, devId);
// }
// addEarliestTime2SyncRecord(tableName, devId, earliestTime);
// }
// }
// }
// }
2 years ago
}