2021与蓝度共同重构项目,服务端
Van333
2022-12-29 727a69f859060093e685582fa10e5de82dcc138a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.sandu.ximon.admin.manager.iot.amqp;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sandu.ximon.admin.manager.iot.amqp.processor.*;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5AtmosphereHeartbeatReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5C3CommonReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5C3ErrorCodeReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5C3HeartbeatReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonReportMessage;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.*;
import com.sandu.ximon.admin.manager.iot.rrpc.util.FrameUtils;
import com.sandu.ximon.admin.service.C3ChargingService;
import com.sandu.ximon.admin.service.C3mOrderService;
import com.sandu.ximon.admin.service.PoleBindingService;
import com.sandu.ximon.admin.service.PoleService;
import com.sandu.ximon.admin.utils.LogUtils;
import com.sandu.ximon.admin.utils.RedisUtils;
import com.sandu.ximon.admin.vo.C3mOrderVO;
import com.sandu.ximon.dao.domain.C3mCharging;
import com.sandu.ximon.dao.domain.C3mOrder;
import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.enums.OrderStatus;
import com.sandu.ximon.dao.enums.OrderType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
 
import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * @author chenjiantian
 * @date 2021/12/2 17:33
 * 处理amqp订阅消息
 */
@Slf4j
public class AmqpMessageListener implements MessageListener {
 
 
    protected final static ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors()  * 2,
            Runtime.getRuntime().availableProcessors() * 4, 60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(50000), new NameTreadFactory());
 
    static class NameTreadFactory implements ThreadFactory {
 
        private final AtomicInteger mThreadNum = new AtomicInteger(1);
 
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "ampq-msg-thread-" + mThreadNum.getAndIncrement());
        }
    }
 
    @Override
    public void onMessage(Message message) {
        EXECUTOR_SERVICE.submit(() -> processMessage(message));
    }
 
    /**
     * 在这里处理您收到消息后的具体业务逻辑。
     */
    private void processMessage(Message message) {
        try {
 
            byte[] body = message.getBody(byte[].class);
            String content = new String(body);
            Map map = JSON.parseObject(content, Map.class);
            String topic = message.getStringProperty("topic");
            String messageId = message.getStringProperty("messageId");
 
//            log.info("收到订阅" + topic + "," + messageId);
//            log.info(content);
 
            if (null != map.get("status")) {
                // 上下线上报处理
            } else {
                // 设备数据上报
                CommonReportMessage commonReportMessage = JSON.parseObject(content, CommonReportMessage.class);
 
                CommonFrame connectFrame = FrameUtils.transformMessageToFrame(commonReportMessage.getItems().getParams().getValue());
 
                processTask(commonReportMessage.getProductKey(), commonReportMessage.getDeviceName(), connectFrame);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
 
        }
    }
 
    /**
     * 处理任务
     *
     * @param productKey 产品码
     * @param deviceName 产品名称
     * @param frame      上报帧
     */
    private void processTask(String productKey, String deviceName, CommonFrame frame) {
        if (frame == null) {
            return;
        }
//        if (!deviceName.equals("3930364d485010ff803affff")){
//            return;
//        }
        log.info("处理订阅:\nmac:{},frame:{}",deviceName,frame.toString());
        if (frame.getFunctionCode().equals("A5")){
            log.info("A5");
            if (frame.getOrderType().equals(A5OrderEnum.RESPONSE_LIGHT_DATA.getCode())) {
                // 单灯数据上报处理
                LightDataProcessor.getInstance().process(productKey, deviceName, frame);
            } else if (frame.getOrderType().equals(A5OrderEnum.RESPONSE_C3_DATA.getCode())) {
                // C3充电桩上报处理
                c3ChargingProcessor.c3ChargingProcessorgetInstance().process(productKey, deviceName, frame);
            } else if (frame.getOrderType().equals(A5OrderEnum.RESPONSE_ATMOSPHERE_DATA.getCode())) {
                // 大气数据指令上报
                AirDataProcessor.getInstance().process(productKey, deviceName, frame);
            } else if (frame.getOrderType().equals(A5OrderEnum.RESPONSE_POLE_MONITOR_DATA.getCode())) {
                PoleMonitorDataProcessor.getInstance().process(productKey, deviceName, frame);
            }
        } else if (frame.getFunctionCode().equals("A7")) {
            log.info("A7");
 
            if (frame.getOrderType().equals(A7OrderEnum.RESPONSE_PLC_DATA.getCode())){
//                PLC
                PlcDataProcessor.getInstance().process(productKey, deviceName,frame);
            }
        }
 
    }
}