重构代码

main
wenhua.zhou 2 years ago
parent 2eefee7aff
commit 3c0eea7f4c

@ -17,7 +17,7 @@ public class ReportSchedule {
@Scheduled(initialDelay = 1000, fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务使用fixedRate属性每隔固定时间执行 @Scheduled(initialDelay = 1000, fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务使用fixedRate属性每隔固定时间执行
public void reportRecord() { public void reportRecord() {
log.info("运行定时任务"); log.info("运行定时转发任务");
mqttServiceimpl.reportRecord(); mqttServiceimpl.reportRecord();
} }

@ -37,19 +37,19 @@ public class MqttServiceImpl {
String sqlExecuting = operationDBMapper.getSQL(tableName); String sqlExecuting = operationDBMapper.getSQL(tableName);
List<Integer> dataEqmids = operationDBMapper.getDataEqmids(tableName); List<Integer> dataEqmids = operationDBMapper.getDataEqmids(tableName);
List<Integer> syncDevIds = operationDBMapper.getSyncRecordDevIds(tableName); // List<Integer> syncDevIds = operationDBMapper.getSyncRecordDevIds(tableName);
if (dataEqmids.size() != syncDevIds.size()) { // if (dataEqmids.size() != syncDevIds.size()) {
List<Integer> distinctDevids = dataEqmids.stream().filter(e -> !syncDevIds.contains(e)).collect(Collectors.toList()); // List<Integer> distinctDevids = dataEqmids.stream().filter(e -> !syncDevIds.contains(e)).collect(Collectors.toList());
for (Integer devId : distinctDevids) { // for (Integer devId : distinctDevids) {
String earliestTime = null; // String earliestTime = null;
if ("data_eaif_h".equals(tableName)) { // if ("data_eaif_h".equals(tableName)) {
earliestTime = operationDBMapper.getEarliestTime4Eaif(tableName, devId); // earliestTime = operationDBMapper.getEarliestTime4Eaif(tableName, devId);
} else { // } else {
earliestTime = operationDBMapper.getEarliestTime4Other(tableName, devId); // earliestTime = operationDBMapper.getEarliestTime4Other(tableName, devId);
} // }
operationDBMapper.addEarliestTime(10, tableName, String.valueOf(devId), earliestTime); // operationDBMapper.addEarliestTime(10, tableName, String.valueOf(devId), earliestTime);
} // }
} // }
Map<Integer, Object> devIDLastTimeMap = new HashMap<>(); Map<Integer, Object> devIDLastTimeMap = new HashMap<>();
List<Map<String, Object>> devIDLastTimeMaps = operationDBMapper.getDeviceIDAndtime(tableName); List<Map<String, Object>> devIDLastTimeMaps = operationDBMapper.getDeviceIDAndtime(tableName);
@ -59,22 +59,29 @@ public class MqttServiceImpl {
} }
} }
for (int deviceID : devIDLastTimeMap.keySet()) { for (int deviceID : dataEqmids) {
publishData(deviceID,sqlExecuting,devIDLastTimeMap,tableName,fieldMap); String time = "";
if(devIDLastTimeMap.get(deviceID) !=null){
time = devIDLastTimeMap.get(deviceID).toString();
}else{
time = "2000-01-01 01:00:00";
}
publishData(deviceID, time, sqlExecuting, tableName, fieldMap);
} }
} }
} }
public void publishData(int deviceID, String sqlExecuting, Map<Integer, Object> devIDLastTimeMap, String tableName,Map<String, String> fieldMap){ //推送单个设备数据
String time = devIDLastTimeMap.get(deviceID).toString(); public void publishData(int deviceID, String time, String sqlExecuting, String tableName, Map<String, String> fieldMap) {
String devIdSQL = sqlExecuting.replace("%%DEVID%%", String.valueOf(deviceID)); String devIdSQL = sqlExecuting.replace("%%DEVID%%", String.valueOf(deviceID));
String newSQL = devIdSQL.replace("%%KEYVALUE%%", "'" + time + "'"); String newSQL = devIdSQL.replace("%%KEYVALUE%%", "'" + time + "'");
System.out.println("SQL:"+newSQL);
List<Map<String, Object>> dataOfoneDeviceID = operationDBMapper.getData(newSQL); List<Map<String, Object>> dataOfoneDeviceID = operationDBMapper.getData(newSQL);
String jsonStringData = FormatUtil.mqttFormatTransform(dataOfoneDeviceID, fieldMap); String jsonStringData = FormatUtil.mqttFormatTransform(dataOfoneDeviceID, fieldMap);
try { try {
if (mqttUtil.publish2MQTT(jsonStringData)) { if (mqttUtil.publish2MQTT(jsonStringData)) {
operationDBMapper.updateSyncRecordsTable(tableName, deviceID, (String) devIDLastTimeMap.get(deviceID)); operationDBMapper.updateSyncRecordsTable(tableName, deviceID, time);
log.info("表{}设备{}推送成功:{}", tableName, deviceID, jsonStringData); log.info("表{}设备{}推送成功:{}", tableName, deviceID, jsonStringData);
} }
} catch (Exception e) { } catch (Exception e) {

@ -23,7 +23,7 @@ public class Subscribe {
@PostConstruct @PostConstruct
private void SubscribeMqtt(){ private void SubscribeMqtt(){
log.info("开始订阅===subScribe执行一次==={}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); // log.info("开始订阅===subScribe执行一次==={}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
// mqttUtil.subScribeMQTT(); // mqttUtil.subScribeMQTT();
} }

@ -0,0 +1,51 @@
#端口号
server:
port: 8098
#数据源配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.50.200:3306/mqttcac?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
mybatis:
configuration:
map-underscore-to-camel-case: true
#mybatis:
#指定mybatis配置文件的位置
#config-location: classpath:mybatis/mybatis-config.xml
#指定映射文件的位置
# mapper-locations: classpath:mybatis/mapper/*.xml
mqtt:
publish:
clientid: publish_client
subscribe:
clientid: subscribe_client
broker: tcp://139.196.211.222:10883
topic: mqtt/test
username: test
password: AliOS%1688
qos: 0
logging:
level:
root: info
com:
xydl:
controller:
TestControler: info
service:
impl: info
schedule: info
file:
# name: E:\log\mqtt.log
name: /root/log/mqtt.log

@ -0,0 +1,51 @@
#端口号
server:
port: 8099
#数据源配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cac?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
mybatis:
configuration:
map-underscore-to-camel-case: true
#mybatis:
#指定mybatis配置文件的位置
#config-location: classpath:mybatis/mybatis-config.xml
#指定映射文件的位置
# mapper-locations: classpath:mybatis/mapper/*.xml
mqtt:
publish:
clientid: publish_client
subscribe:
clientid: subscribe_client
broker: tcp://139.196.211.222:10883
topic: mqtt/test
username: test
password: AliOS%1688
qos: 0
logging:
level:
root: info
com:
xydl:
controller:
TestControler: info
service:
impl: info
schedule: info
file:
# name: E:\log\mqtt.log
name: /root/log/mqtt.log

@ -1,54 +1,3 @@
#端口号
server:
port: 8098
#数据源配置
spring: spring:
datasource: profiles:
driver-class-name: com.mysql.cj.jdbc.Driver active: dev
url: jdbc:mysql://localhost:3306/cac?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
# url: jdbc:mysql://192.168.50.200:3306/mqttcac?characterEncoding=utf-8&serverTimezone=UTC
# username: root
# password: 123456
mybatis:
configuration:
map-underscore-to-camel-case: true
#mybatis:
#指定mybatis配置文件的位置
#config-location: classpath:mybatis/mybatis-config.xml
#指定映射文件的位置
# mapper-locations: classpath:mybatis/mapper/*.xml
mqtt:
publish:
clientid: publish_client
subscribe:
clientid: subscribe_client
broker: tcp://139.196.211.222:10883
topic: mqtt/test
username: test
password: AliOS%1688
qos: 0
logging:
level:
root: info
com:
xydl:
controller:
TestControler: info
service:
impl: info
schedule: info
file:
# name: E:\log\mqtt.log
name: /root/log/mqtt.log
Loading…
Cancel
Save