|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package com.xydl.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.xydl.mapper.OperationDB;
|
|
|
|
|
import com.xydl.util.DataSourceUtils;
|
|
|
|
|
import com.xydl.util.FormatUtil;
|
|
|
|
@ -14,6 +15,7 @@ import java.sql.*;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
@ -149,7 +151,7 @@ public class MqttServiceImpl {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Map<String,Object>> getData(String sqlExecuting, String deviceId, String timeStamp) {
|
|
|
|
|
public List<Map<String,Object>> getData(String sqlExecuting, String deviceId, String time) {
|
|
|
|
|
Connection conn = null;
|
|
|
|
|
PreparedStatement pstmt = null;
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
@ -159,14 +161,14 @@ public class MqttServiceImpl {
|
|
|
|
|
String sql = sqlExecuting;
|
|
|
|
|
pstmt = conn.prepareStatement(sql);
|
|
|
|
|
pstmt.setString(1, deviceId);
|
|
|
|
|
pstmt.setString(2,timeStamp);
|
|
|
|
|
pstmt.setString(2, time);
|
|
|
|
|
rs = pstmt.executeQuery();
|
|
|
|
|
int columnCount = rs.getMetaData().getColumnCount(); //获取列的数量
|
|
|
|
|
while(rs.next()){
|
|
|
|
|
Map<String,Object> record = new HashMap<>();
|
|
|
|
|
for (int col = 0; col < columnCount; col++) {
|
|
|
|
|
String columnName = rs.getMetaData().getColumnName(col + 1);
|
|
|
|
|
String columnValue = rs.getString(columnName);
|
|
|
|
|
Object columnValue = rs.getString(columnName);
|
|
|
|
|
record.put(columnName,columnValue);
|
|
|
|
|
}
|
|
|
|
|
records.add(record);
|
|
|
|
@ -249,27 +251,6 @@ public class MqttServiceImpl {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<Map<String,Object>> transformList(Map<String,String> fieldMap, List<Map<String,Object>> deviceIDData) {
|
|
|
|
|
List<Map<String,Object>> newDeviceIDData = new ArrayList<>();
|
|
|
|
|
for(Map<String,Object> fieldValueMap : deviceIDData){
|
|
|
|
|
newDeviceIDData.add(transformOneRecord(fieldMap,fieldValueMap));
|
|
|
|
|
}
|
|
|
|
|
return newDeviceIDData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String,Object> transformOneRecord(Map<String,String> fieldMap, Map<String,Object> fieldValueMap) {
|
|
|
|
|
Map<String,Object> newFieldValueMap = new HashMap<>();
|
|
|
|
|
for(String field : fieldMap.keySet()){
|
|
|
|
|
if(fieldValueMap.containsKey(field)){
|
|
|
|
|
newFieldValueMap.put(fieldMap.get(field),fieldValueMap.get(field) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newFieldValueMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean updateSyncRecordsTable(String tableName, String deviceID, String time) {
|
|
|
|
|
Connection conn = null;
|
|
|
|
|
PreparedStatement pstmt = null;
|
|
|
|
@ -297,16 +278,14 @@ public class MqttServiceImpl {
|
|
|
|
|
logger.info("开始执行");
|
|
|
|
|
List<String> allTableNames = getAllTableNameFromSyncTable();
|
|
|
|
|
for(String tableName : allTableNames){
|
|
|
|
|
|
|
|
|
|
Map<String,String> fieldMap = getFieldMap(tableName);
|
|
|
|
|
String sqlExecuting = getSQL(tableName);
|
|
|
|
|
|
|
|
|
|
Map<String,Object> devIDLastTimeMap = getDeviceIDAndtime(tableName);
|
|
|
|
|
for(String deviceID : devIDLastTimeMap.keySet()){
|
|
|
|
|
List<Map<String,Object>> dataOfoneDeviceID = getData(sqlExecuting,deviceID, (String) devIDLastTimeMap.get(deviceID));
|
|
|
|
|
|
|
|
|
|
List<Map<String,Object>> newDataOfoneDeviceID = transformList(fieldMap,dataOfoneDeviceID);
|
|
|
|
|
|
|
|
|
|
String jsonStringData = FormatUtil.list2Json(newDataOfoneDeviceID);
|
|
|
|
|
String jsonStringData = FormatUtil.mqttFormatTransform(dataOfoneDeviceID,fieldMap);
|
|
|
|
|
logger.info("表{}设备{}推送数据:{}",tableName,deviceID,jsonStringData);
|
|
|
|
|
if(MqttUtil.publish2MQTT(jsonStringData)){
|
|
|
|
|
updateSyncRecordsTable(tableName,deviceID, (String) devIDLastTimeMap.get(deviceID));
|
|
|
|
@ -340,9 +319,7 @@ public class MqttServiceImpl {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|