Browse Source

任务来源:机场

任务描述:告警日志功能开发、工程代码整理
develop
wubin 1 year ago
parent
commit
59da4f2bf9
11 changed files with 49 additions and 243 deletions
  1. +8
    -0
      tuoheng-admin/README.md
  2. +1
    -1
      tuoheng-admin/src/main/java/com/tuoheng/admin/AdminApplication.java
  3. +4
    -39
      tuoheng-admin/src/main/java/com/tuoheng/admin/enums/MQTTTopicEnum.java
  4. +1
    -1
      tuoheng-admin/src/main/java/com/tuoheng/admin/service/airport/IAirportService.java
  5. +2
    -2
      tuoheng-admin/src/main/java/com/tuoheng/admin/service/airport/impl/AirportServiceImpl.java
  6. +24
    -194
      tuoheng-admin/src/main/java/com/tuoheng/admin/service/mqttService/consumer/MqttConsumerCallBack.java
  7. +5
    -2
      tuoheng-admin/src/main/java/com/tuoheng/admin/service/mqttService/send/MqttProviderConfig.java
  8. +1
    -1
      tuoheng-admin/src/main/resources/application-dev.yml
  9. +1
    -1
      tuoheng-admin/src/main/resources/application-local.yml
  10. +1
    -1
      tuoheng-admin/src/main/resources/application-prod.yml
  11. +1
    -1
      tuoheng-admin/src/main/resources/application-test.yml

+ 8
- 0
tuoheng-admin/README.md View File

@@ -2,4 +2,12 @@

## 统一管理和硬件相关,机场地面站以及控制面板的交互
## 对业务系统提供Restful API暴漏飞行相关的能力

## 代码约束
- 1、状态变量或者一些常量,必须定义成枚举或者常量类
- 2、核心方法必须写上业务逻辑注释
- 3、和三方对接的方法单独类维护,不要糅杂在系统业务类里面
- 4、运行时异常统一定义在异常枚举类中
- 5、redis里面的key统一命名,项目:业务:[子业务:]变量,如:airport:airportstatus:123456789
- 6、前端后端请求体/返回体,针对多字段必须封装对应的VO,不允许用JSONObject接收或者Map返回

+ 1
- 1
tuoheng-admin/src/main/java/com/tuoheng/admin/AdminApplication.java View File

@@ -9,7 +9,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@SpringBootApplication(scanBasePackages = {"com.tuoheng.*"})
@SpringBootApplication(scanBasePackages = {"com.tuoheng.**"})
@EnableDiscoveryClient
@MapperScan("com.tuoheng.**.mapper")
@EnableTransactionManagement

+ 4
- 39
tuoheng-admin/src/main/java/com/tuoheng/admin/enums/MQTTTopicEnum.java View File

@@ -11,45 +11,10 @@ import lombok.Getter;
*/
public enum MQTTTopicEnum {

TOPIC_DATA_DOORMOTOR("/v1/%s/data/DoorMotor", 0, ""),
TOPIC_DATA_LIFTMOTOR("/v1/%s/data/LiftMotor", 0, ""),
TOPIC_DATA_FIXEDMOTOR("/v1/%s/data/FixedMotor", 0, ""),
TOPIC_DATA_DRONEGOAWAY("/v1/%s/data/DroneGoAway", 0, ""),
TOPIC_DATA_DRONEGOHOME("/v1/%s/data/DroneGoHome", 0, ""),
TOPIC_DATA_DRONEGOSTOP("/v1/%s/data/DroneGoStop", 0, ""),
TOPIC_DATA_DRONESTOP("/v1/%s/data/DroneStop", 0, ""),
TOPIC_CONFIRM_DOORMOTOR("/v1/%s/confirm/DoorMotor", 0, ""),
TOPIC_CONFIRM_LIFTMOTOR("/v1/%s/confirm/LiftMotor", 0, ""),
TOPIC_CONFIRM_FIXEDMOTOR1("/v1/%s/confirm/FixedMotor1", 0, ""),
TOPIC_CONFIRM_FIXEDMOTOR2("/v1/%s/confirm/FixedMotor2", 0, ""),
TOPIC_CONFIRM_DRONEGOAWAY("/v1/%s/confirm/DroneGoAway", 0, ""),
TOPIC_CONFIRM_DRONEGOHOME("/v1/%s/confirm/DroneGoHome", 0, ""),
TOPIC_CONFIRM_DRONEGOSTOP("/v1/%s/confirm/DroneGoStop", 0, ""),
TOPIC_CONFIRM_DRONESTOP("/v1/%s/confirm/DroneStop", 0, ""),
TOPIC_CONFIRM_EDGE("/v1/%s/confirm/Edge", 0, ""),
TOPIC_CONFIRM_CHGR("/v1/%s/confirm/CHGR", 0, ""),
TOPIC_CONFIRM_LIGHT("/v1/%s/confirm/Light", 0, ""),
TOPIC_CONTROL_DOORMOTOR("/v1/%s/control/DoorMotor", 0, ""),
TOPIC_CONTROL_LIFTMOTOR("/v1/%s/control/LiftMotor", 0, ""),
TOPIC_CONTROL_FIXEDMOTOR("/v1/%s/control/FixedMotor", 0, ""),
TOPIC_CONTROL_DRONEGOAWAY("/v1/%s/control/DroneGoAway", 0, ""),
TOPIC_CONTROL_DRONEGOHOME("/v1/%s/control/DroneGoHome", 0, ""),
TOPIC_CONTROL_DRONEGOSTOP("/v1/%s/control/DroneGoStop", 0, ""),
TOPIC_CONTROL_DRONESTOP("/v1/%s/control/DroneStop", 0, ""),
TOPIC_CONTROL_CHGR("/v1/%s/control/CHGR", 0, ""),
TOPIC_CONTROL_LIGHT("/v1/%s/control/Light", 0, ""),
TOPIC_DATA_TAH("/v1/%s/data/TAH", 0, ""),
TOPIC_DATA_WTH("/v1/%s/data/WTH", 0, ""),
TOPIC_DATA_ACD("/v1/%s/data/ACD", 0, ""),
TOPIC_DATA_UPS("/v1/%s/data/UPS", 0, ""),
TOPIC_DATA_CHGR("/v1/%s/data/CHGR", 0, ""),
TOPIC_DATA_LIGHT("/v1/%s/data/Light", 0, ""),
TOPIC_ALARM_TAH("/v1/%s/alarm/TAH", 0, ""),
TOPIC_ALARM_WTH("/v1/%s/alarm/WTH", 0, ""),
TOPIC_ALARM_ACD("/v1/%s/alarm/ACD", 0, ""),
TOPIC_ALARM_UPS("/v1/%s/alarm/UPS", 0, ""),
TOPIC_PULL_WTH("/v1/%s/pull/WTH", 0, ""),
TOPIC_POST_WTH("/v1/%s/post/WTH", 0, "");
/**
* 机场日志记录
*/
TOPIC_POST_WTH("/v1/%s/log", 2, "机场日志记录");

MQTTTopicEnum(String topic, int qos, String desc) {
this.topic = topic;

tuoheng-admin/src/main/java/com/tuoheng/admin/service/IAirportService.java → tuoheng-admin/src/main/java/com/tuoheng/admin/service/airport/IAirportService.java View File

@@ -1,4 +1,4 @@
package com.tuoheng.admin.service;
package com.tuoheng.admin.service.airport;

import com.tuoheng.admin.pojo.entity.Airport;
import com.tuoheng.common.common.IBaseService;

tuoheng-admin/src/main/java/com/tuoheng/admin/service/impl/AirportServiceImpl.java → tuoheng-admin/src/main/java/com/tuoheng/admin/service/airport/impl/AirportServiceImpl.java View File

@@ -1,8 +1,8 @@
package com.tuoheng.admin.service.impl;
package com.tuoheng.admin.service.airport.impl;

import com.tuoheng.admin.mapper.AirportMapper;
import com.tuoheng.admin.pojo.entity.Airport;
import com.tuoheng.admin.service.IAirportService;
import com.tuoheng.admin.service.airport.IAirportService;
import com.tuoheng.common.common.BaseServiceImpl;
import org.springframework.stereotype.Service;


+ 24
- 194
tuoheng-admin/src/main/java/com/tuoheng/admin/service/mqttService/consumer/MqttConsumerCallBack.java View File

@@ -1,33 +1,31 @@
package com.tuoheng.admin.service.mqttService.consumer;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tuoheng.admin.pojo.entity.Airport;
import com.tuoheng.admin.service.IAirportService;
import com.tuoheng.admin.utils.SpringUtil;
import com.tuoheng.common.utils.RedisUtils;
import com.tuoheng.admin.service.mqttService.consumer.topicHandle.ITopicHandleService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
* @Author: 吴彬
* @CreateTime: 2023-05-22 19:09
* @Description: MQTT 消息监听回调
* @Version: 1.0
*/
@Component
@Slf4j
public class MqttConsumerCallBack implements MqttCallback{

@Autowired
private List<ITopicHandleService> iTopicHandleServices;

/**
* 客户端断开连接的回调
*/
@@ -41,182 +39,32 @@ public class MqttConsumerCallBack implements MqttCallback{
System.out.println("重连成功!");
}*/
}
DateFormat bf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 消息到达的回调
* 消费消息回调
* @param topic name of the topic on the message was published to
* @param message the actual message.
*/
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
public void messageArrived(String topic, MqttMessage message){
try {
log.info("接收消息主题 : {}\n{}",topic,JSONObject.parse(message.getPayload()));
RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
String edgId = topic.split("/")[2];
String command = topic.substring(edgId.length() + 4, topic.length());
if (!(command.contains("data/")||command.contains("alarm/"))) {
redisUtils.hset(edgId, command, message, 100);
}else{
//上报数据换算
message = getMathValue(message,command,redisUtils,edgId);
redisUtils.hset(edgId, command, message);
redisUtils.hset(edgId+"_data", command, message);
if(command.contains("alarm")) {
command = command.replace("alarm", "data");
redisUtils.hset(edgId, command, message);
redisUtils.hset(edgId+"_data", command, message);
//将topic中的设备编号替换成通用标识
String topicSource = topic.replace(edgId,"%s");

/**
* 根据topic获取对应的消息处理类,后期扩展只需定义对应topic的处理类即可,注意:每个处理类对应的topic全局唯一
*/
for(ITopicHandleService iTopicHandleService : iTopicHandleServices){
if (topicSource.equals(iTopicHandleService.getTopic())){
iTopicHandleService.topicHandle(message,edgId);
break;
}
getWth(command,redisUtils,edgId, message);
}
//saveStatus(edgId,command,message);
//消息日志入库
//saveLog(edgId, command, message);
//保存机场的运行状态
saveAirportRedisStatus(command,redisUtils,edgId, message);
}catch (Exception e){
log.error("",e);
e.printStackTrace();
}
}

/**
* 保存机场的运行状态
* @param command
* @param redisUtils
* @param edgId
* @param message
*/
private void saveAirportRedisStatus(String command, RedisUtils redisUtils, String edgId, MqttMessage message) {
JSONObject jsonObject = (JSONObject)JSONObject.parse(message.getPayload());
IAirportService airportService = SpringUtil.getBean(IAirportService.class);
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("edge_id",edgId);
queryWrapper.eq("mark",1);
List<Airport> list = airportService.list(queryWrapper);

if (list.size()>0){
edgId = list.get(0).getCode();
}
/**
* 1,2 舱门(命令接收,动作完成)
* 3,4 升降器(命令接收,动作完成)
* 5,6 固定器(命令接收,动作完成)
* 7,8 出舱(命令接收,动作完成)
* 9,10 回舱(命令接收,动作完成)
* 11 任务中
* 12 返航中
* 13 悬停中
*/
if(command.contains("confirm/DoorMotor")&&jsonObject.get("msg").toString().contains("SUCCESS")) {
if(jsonObject.get("msg")!=null&&jsonObject.get("msg").toString().contains("SUCCESS")){
redisUtils.hset("airportRunStatus",edgId,"1");
}
}else if (command.contains("data/DoorMotor")){
if(jsonObject.get("parm")!=null&&(JSONObject.toJSONString(jsonObject.get("parm"))).contains("true")){
redisUtils.hset("airportRunStatus",edgId,"2");
}
}else if(command.contains("confirm/LiftMotor")) {
if(jsonObject.get("msg")!=null&&jsonObject.get("msg").toString().contains("SUCCESS")){
redisUtils.hset("airportRunStatus",edgId,"3");
}
}else if (command.contains("data/LiftMotor")){
if(jsonObject.get("parm")!=null&&(JSONObject.toJSONString(jsonObject.get("parm"))).contains("true")){
redisUtils.hset("airportRunStatus",edgId,"4");
}
}else if(command.contains("confirm/FixedMotor")) {
if(jsonObject.get("msg")!=null&&jsonObject.get("msg").toString().contains("SUCCESS")){
redisUtils.hset("airportRunStatus",edgId,"5");
}
}else if (command.contains("data/FixedMotor")){
if(jsonObject.get("parm")!=null&&(JSONObject.toJSONString(jsonObject.get("parm"))).contains("true")){
redisUtils.hset("airportRunStatus",edgId,"6");
}
}else if(command.contains("confirm/DroneGoAway")) {
if(jsonObject.get("msg")!=null&&jsonObject.get("msg").toString().contains("SUCCESS")){
redisUtils.hset("airportRunStatus",edgId,"7");
}
}else if (command.contains("data/DroneGoAway")){
if(jsonObject.get("parm")!=null&&(JSONObject.toJSONString(jsonObject.get("parm"))).contains("true")){
redisUtils.hset("airportRunStatus",edgId,"8");
}
}else if(command.contains("confirm/DroneGoHome")) {
if(jsonObject.get("msg")!=null&&jsonObject.get("msg").toString().contains("SUCCESS")){
redisUtils.hset("airportRunStatus",edgId,"9");
}
}else if (command.contains("data/DroneGoHome")){
if(jsonObject.get("parm")!=null&&(JSONObject.toJSONString(jsonObject.get("parm"))).contains("true")){
redisUtils.hset("airportRunStatus",edgId,"10");
}
}
}

//上报数据换算封装
private MqttMessage getMathValue(MqttMessage message,String command,RedisUtils redisUtils,String edgeId) {
JSONObject jsonObject = (JSONObject)JSONObject.parse(message.getPayload());

JSONObject parmObjectList = (JSONObject)jsonObject.get("parm");
if(command.contains("/WTH")) {
JSONObject parmNew = (JSONObject) parmObjectList.clone();
parmNew.put("WDIRNAME", getMathValueWDIR(parmObjectList.get("WDIR")==null?"0":parmObjectList.get("WDIR").toString()));
parmNew.put("WDIR", String.format("%.2f",Double.parseDouble(parmObjectList.get("WDIR")==null?"0":parmObjectList.get("WDIR").toString())/10));
parmNew.put("WSPD", String.format("%.2f", Double.parseDouble(parmObjectList.get("WSPD")==null?"0":parmObjectList.get("WSPD").toString()) / 100));
parmNew.put("Rainfull", String.format("%.2f",Double.parseDouble(parmObjectList.get("Rainfull")==null?"0":parmObjectList.get("Rainfull").toString())*0.3));
parmNew.put("Hpa", String.format("%.2f",Double.parseDouble(parmObjectList.get("Hpa")==null?"0":parmObjectList.get("Hpa").toString())*0.001));
parmNew.put("Hum", String.format("%.2f",Double.parseDouble(parmObjectList.get("Hum")==null?"0":parmObjectList.get("Hum").toString())*0.1));
parmNew.put("Tmp", String.format("%.2f",Double.parseDouble(parmObjectList.get("Tmp")==null?"0":parmObjectList.get("Tmp").toString())*0.1));
parmNew.put("Lux", String.format("%.2f",Double.parseDouble(parmObjectList.get("Lux")==null?"0":parmObjectList.get("Lux").toString())*0.1));
parmNew.put("Nosie", String.format("%.2f",Double.parseDouble(parmObjectList.get("Nosie")==null?"0":parmObjectList.get("Nosie").toString())*0.1));
parmNew.put("Dew", String.format("%.2f",Double.parseDouble(parmObjectList.get("Dew")==null?"0":parmObjectList.get("Dew").toString())*0.1));
String acd = redisUtils.hget("acd",edgeId)==null?"":redisUtils.hget("acd",edgeId).toString();
parmNew.put("ACDTmp",acd);
jsonObject.put("parmNew",parmNew);
}else if(command.contains("/TAH")) {
JSONObject parmNew = (JSONObject) parmObjectList.clone();
parmNew.put("Hum", String.format("%.2f",Double.parseDouble(parmObjectList.get("Hum")==null?"0":parmObjectList.get("Hum").toString())*0.1));
parmNew.put("Tmp", String.format("%.2f",Double.parseDouble(parmObjectList.get("Tmp")==null?"0":parmObjectList.get("Tmp").toString())*0.1));
jsonObject.put("parmNew",parmNew);
}
if (command.contains("/ACD")){
JSONObject parmNew = (JSONObject) parmObjectList.clone();
parmNew.put("ACDTmp", String.format("%.2f",Double.parseDouble(parmObjectList.get("Tmp")==null?"0":parmObjectList.get("Tmp").toString())*0.1));
jsonObject.put("parmNew",parmNew);

redisUtils.hset("acd",edgeId,String.format("%.2f",Double.parseDouble(parmObjectList.get("Tmp")==null?"0":parmObjectList.get("Tmp").toString())*0.1),180);
}
if (command.contains("DoorMotor")||command.contains("DroneGoAway")){

}

message.setPayload(jsonObject.toJSONString().getBytes());
return message;
}

private Object getMathValueWDIR(String wdir) {
double dr = Double.parseDouble(wdir)/10;
double resultde=dr-180;
if (resultde==0){
return "北风";
}
if (resultde>0&&resultde<90){
return "东北风";
log.error("接收消息主题 : {},异常:{}",topic,e);
}
if (resultde==90){
return "东风";
}
if (resultde>90&&resultde<180){
return "东南风";
}
if (resultde==180){
return "南风";
}
if (resultde>-90&&resultde<0){
return "西北风";
}
if (resultde==-90){
return "西风";
}
if (resultde>-180&&resultde<-90){
return "西南风";
}
return "";
}

/**
@@ -227,23 +75,5 @@ public class MqttConsumerCallBack implements MqttCallback{

}

public void getWth(String command,RedisUtils redisUtils,String edgId,MqttMessage message){
if(command.contains("data/WTH")||command.contains("data/TAH")){

JSONObject json = (JSONObject) JSON.parse(message.getPayload());
List list = (List) redisUtils.get(edgId+command+"list");
if(list==null) {
list = new ArrayList();
}
if(list.size()>3){
list.remove(0);
}
JSONObject map = (JSONObject)json.get("parm");
map.put("date", new Date());
list.add(map);
redisUtils.set(edgId+command+"list",list);
System.out.println(json.get("parm"));
}
}
}


+ 5
- 2
tuoheng-admin/src/main/java/com/tuoheng/admin/service/mqttService/send/MqttProviderConfig.java View File

@@ -3,7 +3,7 @@ package com.tuoheng.admin.service.mqttService.send;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tuoheng.admin.enums.MQTTTopicEnum;
import com.tuoheng.admin.pojo.entity.Airport;
import com.tuoheng.admin.service.IAirportService;
import com.tuoheng.admin.service.airport.IAirportService;
import com.tuoheng.admin.service.mqttService.consumer.MqttConsumerCallBack;
import com.tuoheng.common.utils.IpUtils;
import lombok.extern.slf4j.Slf4j;
@@ -48,6 +48,9 @@ public class MqttProviderConfig {

@Autowired
private IAirportService airportService;

@Autowired
private MqttConsumerCallBack mqttConsumerCallBack;
/**
* 在bean初始化后连接到服务器
*/
@@ -86,7 +89,7 @@ public class MqttProviderConfig {
//mqttClient.reconnect(); 这个方法或者回调已经设置了重连
//options.setAutomaticReconnect(true);
//设置回调
client.setCallback(new MqttConsumerCallBack());
client.setCallback(mqttConsumerCallBack);
client.connect(options);



+ 1
- 1
tuoheng-admin/src/main/resources/application-dev.yml View File

@@ -33,7 +33,7 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
# 填写你数据库的url、登录名、密码和数据库名
url: jdbc:mysql://192.168.11.13:3306/tuoheng_airport?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false
url: jdbc:mysql://192.168.11.13:3306/tuoheng_airport_platform?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false
username: root
password: idontcare
druid:

+ 1
- 1
tuoheng-admin/src/main/resources/application-local.yml View File

@@ -12,7 +12,7 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
# 填写你数据库的url、登录名、密码和数据库名
url: jdbc:mysql://127.0.0.1:3306/tuoheng_airport?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false
url: jdbc:mysql://127.0.0.1:3306/tuoheng_airport_platform?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false
username: th
password: th
druid:

+ 1
- 1
tuoheng-admin/src/main/resources/application-prod.yml View File

@@ -32,7 +32,7 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
# 填写你数据库的url、登录名、密码和数据库名
url: jdbc:mysql://rm-uf6x76i111rb1eo48.mysql.rds.aliyuncs.com:3306/tuoheng_airport?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false
url: jdbc:mysql://rm-uf6x76i111rb1eo48.mysql.rds.aliyuncs.com:3306/tuoheng_airport_platform?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false
username: root
password: TH22#2022
druid:

+ 1
- 1
tuoheng-admin/src/main/resources/application-test.yml View File

@@ -33,7 +33,7 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
# 填写你数据库的url、登录名、密码和数据库名
url: jdbc:mysql://rm-uf6z740323e8053pj.mysql.rds.aliyuncs.com:3306/tuoheng_airport?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false
url: jdbc:mysql://rm-uf6z740323e8053pj.mysql.rds.aliyuncs.com:3306/tuoheng_airport_platform?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false
username: root
password: TH22#2022
druid:

Loading…
Cancel
Save