|
|
@ -6,15 +6,19 @@ import com.xydl.mapper.OperationDB;
|
|
|
|
import com.xydl.util.DataSourceUtils;
|
|
|
|
import com.xydl.util.DataSourceUtils;
|
|
|
|
import com.xydl.util.FormatUtil;
|
|
|
|
import com.xydl.util.FormatUtil;
|
|
|
|
import com.xydl.util.MqttUtil;
|
|
|
|
import com.xydl.util.MqttUtil;
|
|
|
|
|
|
|
|
import com.xydl.util.Subscribe;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.*;
|
|
|
|
import java.sql.*;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -150,7 +154,6 @@ public class MqttServiceImpl {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> getData(String sqlExecuting, String deviceId, String time) {
|
|
|
|
public List<Map<String, Object>> getData(String sqlExecuting, String deviceId, String time) {
|
|
|
|
Connection conn = null;
|
|
|
|
Connection conn = null;
|
|
|
|
PreparedStatement pstmt = null;
|
|
|
|
PreparedStatement pstmt = null;
|
|
|
@ -250,6 +253,53 @@ public class MqttServiceImpl {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Integer> getDataEqmids(String tableName) {
|
|
|
|
|
|
|
|
Connection conn = null;
|
|
|
|
|
|
|
|
PreparedStatement pstmt = null;
|
|
|
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
|
|
|
|
List<Integer> eqmids = new ArrayList<>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
conn = DataSourceUtils.getConnection();
|
|
|
|
|
|
|
|
String sql = "select distinct eqmid from ?";
|
|
|
|
|
|
|
|
pstmt = conn.prepareStatement(sql);
|
|
|
|
|
|
|
|
pstmt.setString(1, tableName);
|
|
|
|
|
|
|
|
rs = pstmt.executeQuery();
|
|
|
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
|
|
|
eqmids.add(Integer.valueOf(rs.getString("eqmid")));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
logger.error("execute sql exception:", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
DataSourceUtils.closeResource(rs, pstmt, conn);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return eqmids;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Integer> getSyncRecordDevIds(String tableName) {
|
|
|
|
|
|
|
|
Connection conn = null;
|
|
|
|
|
|
|
|
PreparedStatement pstmt = null;
|
|
|
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
|
|
|
|
List<Integer> syncEqmids = new ArrayList<>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
conn = DataSourceUtils.getConnection();
|
|
|
|
|
|
|
|
String sql = "select devid_val from sync_records where table_name = ?";
|
|
|
|
|
|
|
|
pstmt = conn.prepareStatement(sql);
|
|
|
|
|
|
|
|
pstmt.setString(1, tableName);
|
|
|
|
|
|
|
|
rs = pstmt.executeQuery();
|
|
|
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
|
|
|
syncEqmids.add(Integer.valueOf(rs.getString("devid_val")));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
logger.error("execute sql exception:", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
DataSourceUtils.closeResource(rs, pstmt, conn);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return syncEqmids;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean updateSyncRecordsTable(String tableName, String deviceID, String time) {
|
|
|
|
public boolean updateSyncRecordsTable(String tableName, String deviceID, String time) {
|
|
|
|
Connection conn = null;
|
|
|
|
Connection conn = null;
|
|
|
@ -276,13 +326,35 @@ public class MqttServiceImpl {
|
|
|
|
@Scheduled(initialDelay = 1000, fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
|
|
|
|
@Scheduled(initialDelay = 1000, fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
|
|
|
|
public void reportRecord() {
|
|
|
|
public void reportRecord() {
|
|
|
|
logger.info("开始执行");
|
|
|
|
logger.info("开始执行");
|
|
|
|
List<String> allTableNames = getAllTableNameFromSyncTable();
|
|
|
|
// Subscribe.getInstance();
|
|
|
|
|
|
|
|
List<String> allTableNames = operationDBMapper.getAllTable();
|
|
|
|
for (String tableName : allTableNames) {
|
|
|
|
for (String tableName : allTableNames) {
|
|
|
|
|
|
|
|
|
|
|
|
Map<String,String> fieldMap = getFieldMap(tableName);
|
|
|
|
Map<String,String> fieldMap = new HashMap<>();
|
|
|
|
String sqlExecuting = getSQL(tableName);
|
|
|
|
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"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String sqlExecuting = operationDBMapper.getSQL(tableName);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Map<String,Object> devIDLastTimeMap = getDeviceIDAndtime(tableName);
|
|
|
|
Map<String, Object> devIDLastTimeMap = operationDBMapper.getDeviceIDAndtime(tableName);
|
|
|
|
for (String deviceID : devIDLastTimeMap.keySet()) {
|
|
|
|
for (String deviceID : devIDLastTimeMap.keySet()) {
|
|
|
|
List<Map<String, Object>> dataOfoneDeviceID = getData(sqlExecuting, deviceID, (String) devIDLastTimeMap.get(deviceID));
|
|
|
|
List<Map<String, Object>> dataOfoneDeviceID = getData(sqlExecuting, deviceID, (String) devIDLastTimeMap.get(deviceID));
|
|
|
|
String jsonStringData = FormatUtil.mqttFormatTransform(dataOfoneDeviceID, fieldMap);
|
|
|
|
String jsonStringData = FormatUtil.mqttFormatTransform(dataOfoneDeviceID, fieldMap);
|
|
|
@ -298,28 +370,32 @@ public class MqttServiceImpl {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Scheduled(fixedDelay = Long.MAX_VALUE) // 用一个非常大的延迟值,确保只执行一次
|
|
|
|
// @Scheduled(fixedDelay = Long.MAX_VALUE) // 用一个非常大的延迟值,确保只执行一次
|
|
|
|
public void subScribeSamle() {
|
|
|
|
// public void subScribeSamle() {
|
|
|
|
logger.info("开始订阅===subScribe执行一次==={}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
|
// logger.info("开始订阅===subScribe执行一次==={}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
|
MqttUtil.subScribeMQTT();
|
|
|
|
// MqttUtil.subScribeMQTT();
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
@Scheduled(fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
|
|
|
|
// @Scheduled(fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
|
|
|
|
public void checkDevIdTimer() {
|
|
|
|
// public void checkDevIdTimer() {
|
|
|
|
logger.info("每小时检测一次同步的表是否在‘同步记录表’");
|
|
|
|
// logger.info("每小时检测一次同步的表是否在‘同步记录表’");
|
|
|
|
List<String> allTableNames = getAllTableNameFromSyncTable();
|
|
|
|
// List<String> allTableNames = getAllTableNameFromSyncTable();
|
|
|
|
for(String tableName : allTableNames){
|
|
|
|
// for (String tableName : allTableNames) {
|
|
|
|
if(!tableNameIfExitsSyncRec(tableName)){
|
|
|
|
// if (!tableNameIfExitsSyncRec(tableName)) {
|
|
|
|
logger.info("有不存在的表,把所有的devId及最早的时间更新到'同步记录表'");
|
|
|
|
// logger.info("有不存在的表{},把所有的devId及最早的时间更新到'同步记录表'", tableName);
|
|
|
|
List<String> devIds = operationDBMapper.getAllDevId(tableName);
|
|
|
|
// List<String> devIds = operationDBMapper.getAllDevId(tableName);
|
|
|
|
for(String devId : devIds){
|
|
|
|
// for (String devId : devIds) {
|
|
|
|
String lastTime = operationDBMapper.getLastTime(tableName,devId);
|
|
|
|
// String earliestTime = null;
|
|
|
|
addEarliestTime2SyncRecord(tableName,devId,lastTime);
|
|
|
|
// if ("data_eaif_h".equals(tableName)) {
|
|
|
|
}
|
|
|
|
// earliestTime = operationDBMapper.getEarliestTime4Eaif(tableName, devId);
|
|
|
|
}
|
|
|
|
// } else {
|
|
|
|
}
|
|
|
|
// earliestTime = operationDBMapper.getEarliestTime4Other(tableName, devId);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// addEarliestTime2SyncRecord(tableName, devId, earliestTime);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|