ctl+alt+L格式化

main
wenhua.zhou 2 years ago
parent d62480e4c6
commit 1d2b18c5e9

@ -31,6 +31,4 @@ public interface OperationDB {
boolean updateSyncRecordsTable(String tableName, int deviceID, String time); boolean updateSyncRecordsTable(String tableName, int deviceID, String time);
} }

@ -26,7 +26,7 @@ public class MqttServiceImpl {
public void reportRecord() { public void reportRecord() {
List<String> allTableNames = operationDBMapper.getAllTable(); List<String> allTableNames = operationDBMapper.getAllTable();
for (String tableName : allTableNames) { for (String tableName : allTableNames) {
Map<String,String> fieldMap = getTableFieldMap(tableName); Map<String, String> fieldMap = getTableFieldMap(tableName);
String sqlExecuting = operationDBMapper.getSQL(tableName); String sqlExecuting = operationDBMapper.getSQL(tableName);
List<Integer> dataEqmids = operationDBMapper.getDataEqmids(tableName); List<Integer> dataEqmids = operationDBMapper.getDataEqmids(tableName);
Map<Integer, Object> devIDLastTimeMap = new HashMap<>(); Map<Integer, Object> devIDLastTimeMap = new HashMap<>();
@ -38,19 +38,19 @@ public class MqttServiceImpl {
} }
for (int deviceID : dataEqmids) { for (int deviceID : dataEqmids) {
String time = ""; String time = "";
if(devIDLastTimeMap.get(deviceID) !=null){ if (devIDLastTimeMap.get(deviceID) != null) {
time = devIDLastTimeMap.get(deviceID).toString(); time = devIDLastTimeMap.get(deviceID).toString();
}else{ } else {
time = "2000-01-01 01:00:00"; time = "2000-01-01 01:00:00";
operationDBMapper.addEarliestTime(10, tableName, String.valueOf(deviceID), time); operationDBMapper.addEarliestTime(10, tableName, String.valueOf(deviceID), time);
} }
publishData(deviceID, time, sqlExecuting, tableName, fieldMap); publishData(deviceID, time, sqlExecuting, tableName, fieldMap);
} }
} }
} }
//单个表的字段映射 //单个表的字段映射
public Map<String,String> getTableFieldMap(String tableName){ public Map<String, String> getTableFieldMap(String tableName) {
Map<String, String> fieldMap = new HashMap<>(); Map<String, String> fieldMap = new HashMap<>();
List<Map<String, String>> fieldMaps = operationDBMapper.getFieldMap(tableName); List<Map<String, String>> fieldMaps = operationDBMapper.getFieldMap(tableName);
for (Map<String, String> map : fieldMaps) { for (Map<String, String> map : fieldMaps) {

@ -9,11 +9,11 @@ import java.util.stream.Collectors;
public class FormatUtil { public class FormatUtil {
public static String mqttFormatTransform(List<Map<String,Object>> list, Map<String,String> fieldMap){ public static String mqttFormatTransform(List<Map<String, Object>> list, Map<String, String> fieldMap) {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
Map<String,Object> resultMap = new HashMap<String,Object>(){{ Map<String, Object> resultMap = new HashMap<String, Object>() {{
put("AssetList",list.stream().map(e -> recordTransform(e,fieldMap)).collect(Collectors.toList())); put("AssetList", list.stream().map(e -> recordTransform(e, fieldMap)).collect(Collectors.toList()));
}}; }};
String jsonString = null; String jsonString = null;
@ -25,25 +25,24 @@ public class FormatUtil {
return jsonString; return jsonString;
} }
public static Map<String,Object> recordTransform(Map<String, Object> record, Map<String, String> fieldMap){ public static Map<String, Object> recordTransform(Map<String, Object> record, Map<String, String> fieldMap) {
Map<String,Object> mqttRecord = new TreeMap<>(); Map<String, Object> mqttRecord = new TreeMap<>();
List<Map<String,Object>> attribuiteList = new ArrayList<>(); List<Map<String, Object>> attribuiteList = new ArrayList<>();
for(String key : record.keySet()){ for (String key : record.keySet()) {
if(fieldMap.containsKey(key)){ if (fieldMap.containsKey(key)) {
attribuiteList.add(new HashMap<String,Object>(){{ attribuiteList.add(new HashMap<String, Object>() {{
put("AttributeCode", fieldMap.get(key)); put("AttributeCode", fieldMap.get(key));
put("DataValue",record.get(key)); put("DataValue", record.get(key));
}}); }});
} }
} }
mqttRecord.put("AssetCode",record.get("sensorid")); mqttRecord.put("AssetCode", record.get("sensorid"));
mqttRecord.put("AttributeList",attribuiteList); mqttRecord.put("AttributeList", attribuiteList);
String captureTime = record.get("d_time") !=null ? record.get("d_time").toString() : (String) record.get("capturetime").toString(); String captureTime = record.get("d_time") != null ? record.get("d_time").toString() : (String) record.get("capturetime").toString();
mqttRecord.put("Timestamp",Timestamp.valueOf(captureTime).getTime()); mqttRecord.put("Timestamp", Timestamp.valueOf(captureTime).getTime());
return mqttRecord; return mqttRecord;
} }
} }

@ -35,8 +35,7 @@ public class MqttUtil {
private int qos; private int qos;
public boolean publish2MQTT(String content) {
public boolean publish2MQTT(String content){
MqttClient client; MqttClient client;
MqttDeliveryToken token; MqttDeliveryToken token;
try { try {
@ -70,7 +69,7 @@ public class MqttUtil {
} }
log.info("Message published"); log.info("Message published");
log.info("topic: {}", topic); log.info("topic: {}", topic);
log.info("{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+ " message content推送: " + content); log.info("{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " message content推送: " + content);
// disconnect // disconnect
try { try {
client.disconnect(); client.disconnect();
@ -87,7 +86,7 @@ public class MqttUtil {
return token.isComplete(); return token.isComplete();
} }
public void subScribeMQTT(){ public void subScribeMQTT() {
// String broker = "tcp://139.196.211.222:10883"; // String broker = "tcp://139.196.211.222:10883";
// String topic = "mqtt/test"; // String topic = "mqtt/test";
@ -108,12 +107,14 @@ public class MqttUtil {
public void connectionLost(Throwable cause) { public void connectionLost(Throwable cause) {
log.info("connectionLost:{}", cause.getMessage()); log.info("connectionLost:{}", cause.getMessage());
} }
public void messageArrived(String topic, MqttMessage message) { public void messageArrived(String topic, MqttMessage message) {
log.info("topic:{} ", topic); log.info("topic:{} ", topic);
log.info("Qos:{} ", message.getQos()); log.info("Qos:{} ", message.getQos());
log.info("{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" message content接收: " + new String(message.getPayload())); log.info("{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " message content接收: " + new String(message.getPayload()));
} }
public void deliveryComplete(IMqttDeliveryToken token) { public void deliveryComplete(IMqttDeliveryToken token) {
log.info("deliveryComplete---------{}", token.isComplete()); log.info("deliveryComplete---------{}", token.isComplete());
} }
@ -121,7 +122,8 @@ public class MqttUtil {
client.connect(options); client.connect(options);
client.subscribe(topic, qos); client.subscribe(topic, qos);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); } e.printStackTrace();
}
} }

@ -21,12 +21,12 @@ public class Subscribe {
private static Subscribe single = new Subscribe(); private static Subscribe single = new 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();
} }
public static Subscribe getInstance(){ public static Subscribe getInstance() {
return single; return single;
} }

Loading…
Cancel
Save