|
|
|
|
package com.xydl.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.xydl.mapper.OperationDB;
|
|
|
|
|
import com.xydl.util.FormatUtil;
|
|
|
|
|
import com.xydl.util.MqttUtil;
|
|
|
|
|
import com.xydl.util.Subscribe;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class MqttServiceImpl {
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(MqttServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
OperationDB operationDBMapper;
|
|
|
|
|
|
|
|
|
|
// @Scheduled(initialDelay = 1000, fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
|
|
|
|
|
public void reportRecord() {
|
|
|
|
|
logger.info("开始执行");
|
|
|
|
|
Subscribe.getInstance();
|
|
|
|
|
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"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String sqlExecuting = operationDBMapper.getSQL(tableName);
|
|
|
|
|
String preSQL = sqlExecuting.substring(0,sqlExecuting.indexOf("?"));
|
|
|
|
|
String midSQL = sqlExecuting.substring(sqlExecuting.indexOf("?")+1,sqlExecuting.lastIndexOf("?"));
|
|
|
|
|
String lastSQL = sqlExecuting.substring(sqlExecuting.lastIndexOf("?")+1);
|
|
|
|
|
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<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);
|
|
|
|
|
String jsonStringData = FormatUtil.mqttFormatTransform(dataOfoneDeviceID, fieldMap);
|
|
|
|
|
logger.info("表{}设备{}推送数据:{}", tableName, deviceID, jsonStringData);
|
|
|
|
|
if (new MqttUtil().publish2MQTT(jsonStringData)) {
|
|
|
|
|
operationDBMapper.updateSyncRecordsTable(tableName, deviceID, (String) devIDLastTimeMap.get(deviceID));
|
|
|
|
|
logger.info("推送成功");
|
|
|
|
|
} else {
|
|
|
|
|
logger.info("消息推送失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// @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);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|