重构代码

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属性每隔固定时间执行
public void reportRecord() {
log.info("运行定时任务");
log.info("运行定时转发任务");
mqttServiceimpl.reportRecord();
}

@ -37,19 +37,19 @@ public class MqttServiceImpl {
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);
}
}
// 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<Integer, Object> devIDLastTimeMap = new HashMap<>();
List<Map<String, Object>> devIDLastTimeMaps = operationDBMapper.getDeviceIDAndtime(tableName);
@ -59,26 +59,33 @@ public class MqttServiceImpl {
}
}
for (int deviceID : devIDLastTimeMap.keySet()) {
publishData(deviceID,sqlExecuting,devIDLastTimeMap,tableName,fieldMap);
for (int deviceID : dataEqmids) {
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 newSQL = devIdSQL.replace("%%KEYVALUE%%", "'"+time+"'");
System.out.println("SQL:"+newSQL);
String newSQL = devIdSQL.replace("%%KEYVALUE%%", "'" + time + "'");
List<Map<String, Object>> dataOfoneDeviceID = operationDBMapper.getData(newSQL);
String jsonStringData = FormatUtil.mqttFormatTransform(dataOfoneDeviceID, fieldMap);
try {
if (mqttUtil.publish2MQTT(jsonStringData)) {
operationDBMapper.updateSyncRecordsTable(tableName, deviceID, (String) devIDLastTimeMap.get(deviceID));
log.info("表{}设备{}推送成功:{}",tableName,deviceID,jsonStringData);
operationDBMapper.updateSyncRecordsTable(tableName, deviceID, time);
log.info("表{}设备{}推送成功:{}", tableName, deviceID, jsonStringData);
}
} catch (Exception e) {
log.info("表{}设备{}推送异常:{}", tableName,deviceID,e.getMessage());
log.info("表{}设备{}推送异常:{}", tableName, deviceID, e.getMessage());
}
}

@ -23,7 +23,7 @@ public class Subscribe {
@PostConstruct
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();
}

@ -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:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
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
profiles:
active: dev
Loading…
Cancel
Save