mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.panzhihua.service_property.config;
 
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;;
import com.panzhihua.service_property.dao.ComPropertyAlarmDao;
import com.panzhihua.service_property.dto.DeviceDto;
import com.panzhihua.service_property.entity.ComPropertyAlarm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.websocket.*;
import java.io.IOException;
import java.util.Date;
 
/**
 * 应用模块名称
 * <p>
 * 一键报警设备消息 代码描述
 * <p>
 * Copyright: Copyright (C) 2021 XXX, Inc. All rights reserved.
 * <p>
 * Company: 成都呐喊信息技术有限公司
 * <p>
 *
 * @author manailin
 * @since 2021/9/27 16:31
 */
@Slf4j
@Component
@ClientEndpoint
public class WebSocketClient {
 
    private static final Logger logger = LoggerFactory.getLogger(WebSocketClient.class);
 
    /**
     * 保存报警记录服务
     */
    @Resource
    private ComPropertyAlarmDao comPropertyAlarmDao;
    /**
     * session
     */
    private Session session;
 
    /**
     * <beforeInit>
     *
     * @throws
     */
    public static void beforeInit() {
        // 在socket配置类中调用此方法可以完成一些需要初始化注入的操作
    }
 
    @OnOpen
    public void onOpen(Session session) {
        logger.info("连接开启...");
        this.session = session;
    }
 
    @OnMessage
    public void onMessage(String message, Session session) {
        // 提取数据
        logger.info("收到的原始数据:{}", message);
        dealNewData(message);
    }
 
    @Deprecated
    @Async
    void dealNewData(String message) {
        comPropertyAlarmDao = SpringUtil.getBean(ComPropertyAlarmDao.class);
        JSONObject deviceMess = JSONObject.parseObject(message);
        boolean checkFlag =
                ("trace_call".equals(deviceMess.getString("cmd")) && (StringUtils.isNotEmpty(deviceMess.getString("userid"))));
        if (checkFlag) {
            log.info("已经接收到报警记录,记录信息:{}",deviceMess.toJSONString());
            boolean panzhihuaDevice = deviceMess.getString("userid").startsWith("7") || deviceMess.getString("userid").startsWith("8");
            if(panzhihuaDevice) {
                log.info("报警记录是攀枝花设备7-8开头的设备");
                logger.info("收到设备报警信息:{}", message);
                for (int i = 1; i < 10; i++) {
                    if (org.apache.commons.lang.StringUtils.isNotEmpty(deviceMess.getString("num" + i))) {
                        ComPropertyAlarm comPropertyAlarm = new ComPropertyAlarm();
                        comPropertyAlarm.setSerialNo(deviceMess.getString("userid"));
                        comPropertyAlarm.setReceiveNo(deviceMess.getString("num" + i));
                        comPropertyAlarm.setStatus(0);
                        // 10位的秒级别的时间戳
                        Date createDate = new Date(deviceMess.getLong("ts" + i) * 1000);
                        //对应的就是时间戳对应的Date
                        comPropertyAlarm.setCreateTime(createDate);
                        comPropertyAlarm.setType(1);
                        comPropertyAlarmDao.insert(comPropertyAlarm);
                    } else {
                        break;
                    }
                }
            }
        }
    }
 
    @Deprecated
    @Async
    void dealData(String message) {
        comPropertyAlarmDao = SpringUtil.getBean(ComPropertyAlarmDao.class);
        DeviceDto deviceInfo = JSONObject.parseObject(message, DeviceDto.class);
        boolean checkFlag =
                "RINGING".equals(deviceInfo.getCallstatus()) && StringUtils.isNotEmpty(deviceInfo.getOri_number());
        if (checkFlag) {
            logger.info("收到设备报警信息:{}", message);
            ComPropertyAlarm comPropertyAlarm = new ComPropertyAlarm();
            comPropertyAlarm.setSerialNo(deviceInfo.getOri_number());
            comPropertyAlarm.setReceiveNo(deviceInfo.getUserid());
            comPropertyAlarm.setStatus(0);
            comPropertyAlarm.setCreateTime(new Date());
            comPropertyAlarm.setType(1);
            comPropertyAlarmDao.insert(comPropertyAlarm);
        }
    }
 
    @OnClose
    public void onClose() {
        logger.info("长连接关闭...");
    }
 
    @OnError
    public void onError(Session session, Throwable t) {
        logger.error("error, cause: ", t);
    }
 
    /**
     * <异步发送message>
     *
     * @param message message @throws
     */
    public void send(String message) {
        this.session.getAsyncRemote().sendText(message);
    }
 
    /**
     * <发送message>
     *
     * @param message message @throws
     */
    public void sendMessage(String message) {
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException ex) {
            logger.error("error, cause: ", ex);
        }
    }
 
    /**
     * <关闭连接>
     *
     * @throws
     */
    public void close() throws IOException {
        if (this.session.isOpen()) {
            this.session.close();
        }
    }
}