songww
2020-05-07 70091f29f2c3c3f974ffc2cc8f140db9f992ca0e
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.genersoft.iot.vmp.gb28181.transmit.request.impl;
 
import java.io.ByteArrayInputStream;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
import javax.sip.InvalidArgumentException;
import javax.sip.RequestEvent;
import javax.sip.ServerTransaction;
import javax.sip.SipException;
import javax.sip.message.Request;
import javax.sip.message.Response;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.gb28181.SipLayer;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.genersoft.iot.vmp.gb28181.transmit.request.ISIPRequestProcessor;
import com.genersoft.iot.vmp.gb28181.utils.XmlUtil;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
 
/**    
 * @Description:MESSAGE请求处理器
 * @author: songww
 * @date:   2020年5月3日 下午5:32:41     
 */
@Component
public class MessageRequestProcessor implements ISIPRequestProcessor {
 
    private ServerTransaction transaction;
    
    private SipLayer layer;
    
    @Autowired
    private SIPCommander cmder;
    
    @Autowired
    private IVideoManagerStorager storager;
    
    @Autowired
    private EventPublisher publisher;
    
    /**   
     * 处理MESSAGE请求
     *  
     * @param evt
     * @param layer
     * @param transaction  
     */  
    @Override
    public void process(RequestEvent evt, SipLayer layer, ServerTransaction transaction) {
        
        this.layer = layer;
        this.transaction = transaction;
        
        Request request = evt.getRequest();
        
        if (new String(request.getRawContent()).contains("<CmdType>Keepalive</CmdType>")) {
            processMessageKeepAlive(evt);
        } else if (new String(request.getRawContent()).contains("<CmdType>Catalog</CmdType>")) {
            processMessageCatalogList(evt);
        } else if (new String(request.getRawContent()).contains("<CmdType>DeviceInfo</CmdType>")) {
            processMessageDeviceInfo(evt);
        } else if (new String(request.getRawContent()).contains("<CmdType>Alarm</CmdType>")) {
            processMessageAlarm(evt);
        }
        
    }
    
    /***
     * 收到catalog设备目录列表请求 处理
     * @param evt
     */
    private void processMessageCatalogList(RequestEvent evt) {
        try {
            Request request = evt.getRequest();
            SAXReader reader = new SAXReader();
            reader.setEncoding("GB2312");
            Document xml = reader.read(new ByteArrayInputStream(request.getRawContent()));
            Element rootElement = xml.getRootElement();
            Element deviceIdElement = rootElement.element("DeviceID");
            String deviceId = deviceIdElement.getText().toString();
            Element deviceListElement = rootElement.element("DeviceList");
            if (deviceListElement == null) {
                return;
            }
            Iterator<Element> deviceListIterator = deviceListElement.elementIterator();
            if (deviceListIterator != null) {
                Device device = storager.queryVideoDevice(deviceId);
                if (device == null) {
                    return;
                }
                Map<String, DeviceChannel> channelMap = device.getChannelMap();
                if (channelMap == null) {
                    channelMap = new HashMap<String, DeviceChannel>(5);
                    device.setChannelMap(channelMap);
                }
                // 遍历DeviceList
                while (deviceListIterator.hasNext()) {
                    Element itemDevice = deviceListIterator.next();
                    Element channelDeviceElement = itemDevice.element("DeviceID");
                    if (channelDeviceElement == null) {
                        continue;
                    }
                    String channelDeviceId = channelDeviceElement.getText().toString();
                    Element channdelNameElement = itemDevice.element("Name");
                    String channelName = channdelNameElement != null ? channdelNameElement.getText().toString() : "";
                    Element statusElement = itemDevice.element("Status");
                    String status = statusElement != null ? statusElement.getText().toString() : "ON";
                    DeviceChannel deviceChannel = channelMap.containsKey(channelDeviceId) ? channelMap.get(channelDeviceId) : new DeviceChannel();
                    deviceChannel.setName(channelName);
                    deviceChannel.setChannelId(channelDeviceId);
                    if(status.equals("ON")) {
                        deviceChannel.setStatus(1);
                    }
                    if(status.equals("OFF")) {
                        deviceChannel.setStatus(0);
                    }
 
                    deviceChannel.setManufacture(XmlUtil.getText(itemDevice,"Manufacturer"));
                    deviceChannel.setModel(XmlUtil.getText(itemDevice,"Model"));
                    deviceChannel.setOwner(XmlUtil.getText(itemDevice,"Owner"));
                    deviceChannel.setCivilCode(XmlUtil.getText(itemDevice,"CivilCode"));
                    deviceChannel.setBlock(XmlUtil.getText(itemDevice,"Block"));
                    deviceChannel.setAddress(XmlUtil.getText(itemDevice,"Address"));
                    deviceChannel.setParental(itemDevice.element("Parental") == null? 0:Integer.parseInt(XmlUtil.getText(itemDevice,"Parental")));
                    deviceChannel.setParentId(XmlUtil.getText(itemDevice,"ParentId"));
                    deviceChannel.setSafetyWay(itemDevice.element("SafetyWay") == null? 0:Integer.parseInt(XmlUtil.getText(itemDevice,"SafetyWay")));
                    deviceChannel.setRegisterWay(itemDevice.element("RegisterWay") == null? 1:Integer.parseInt(XmlUtil.getText(itemDevice,"RegisterWay")));
                    deviceChannel.setCertNum(XmlUtil.getText(itemDevice,"CertNum"));
                    deviceChannel.setCertifiable(itemDevice.element("Certifiable") == null? 0:Integer.parseInt(XmlUtil.getText(itemDevice,"Certifiable")));
                    deviceChannel.setErrCode(itemDevice.element("ErrCode") == null? 0:Integer.parseInt(XmlUtil.getText(itemDevice,"ErrCode")));
                    deviceChannel.setEndTime(XmlUtil.getText(itemDevice,"EndTime"));
                    deviceChannel.setSecrecy(XmlUtil.getText(itemDevice,"Secrecy"));
                    deviceChannel.setIpAddress(XmlUtil.getText(itemDevice,"IPAddress"));
                    deviceChannel.setPort(itemDevice.element("Port") == null? 0:Integer.parseInt(XmlUtil.getText(itemDevice,"Port")));
                    deviceChannel.setPassword(XmlUtil.getText(itemDevice,"Password"));
                    deviceChannel.setLongitude(itemDevice.element("Longitude") == null? 0.00:Double.parseDouble(XmlUtil.getText(itemDevice,"Longitude")));
                    deviceChannel.setLatitude(itemDevice.element("Latitude") == null? 0.00:Double.parseDouble(XmlUtil.getText(itemDevice,"Latitude")));
                    channelMap.put(channelDeviceId, deviceChannel);
                }
                // 更新
                storager.update(device);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    /***
     * 收到deviceInfo设备信息请求 处理
     * @param evt
     */
    private void processMessageDeviceInfo(RequestEvent evt) {
        try {
            Request request = evt.getRequest();
            SAXReader reader = new SAXReader();
            // reader.setEncoding("GB2312");
            Document xml = reader.read(new ByteArrayInputStream(request.getRawContent()));
            Element rootElement = xml.getRootElement();
            Element deviceIdElement = rootElement.element("DeviceID");
            String deviceId = deviceIdElement.getText().toString();
            
            Device device = storager.queryVideoDevice(deviceId);
            if (device == null) {
                return;
            }
            device.setName(XmlUtil.getText(rootElement,"DeviceName"));
            device.setManufacturer(XmlUtil.getText(rootElement,"Manufacturer"));
            device.setModel(XmlUtil.getText(rootElement,"Model"));
            device.setFirmware(XmlUtil.getText(rootElement,"Firmware"));
            storager.update(device);
            cmder.catalogQuery(device);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    /***
     * 收到alarm设备报警信息 处理
     * @param evt
     */
    private void processMessageAlarm(RequestEvent evt) {
        try {
            Request request = evt.getRequest();
            SAXReader reader = new SAXReader();
            // reader.setEncoding("GB2312");
            Document xml = reader.read(new ByteArrayInputStream(request.getRawContent()));
            Element rootElement = xml.getRootElement();
            Element deviceIdElement = rootElement.element("DeviceID");
            String deviceId = deviceIdElement.getText().toString();
            
            Device device = storager.queryVideoDevice(deviceId);
            if (device == null) {
                return;
            }
            device.setName(XmlUtil.getText(rootElement,"DeviceName"));
            device.setManufacturer(XmlUtil.getText(rootElement,"Manufacturer"));
            device.setModel(XmlUtil.getText(rootElement,"Model"));
            device.setFirmware(XmlUtil.getText(rootElement,"Firmware"));
            storager.update(device);
            cmder.catalogQuery(device);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    /***
     * 收到keepalive请求 处理
     * @param evt
     */
    private void processMessageKeepAlive(RequestEvent evt){
        try {
            Request request = evt.getRequest();
            Response response = layer.getMessageFactory().createResponse(Response.OK,request);
            SAXReader reader = new SAXReader();
            Document xml = reader.read(new ByteArrayInputStream(request.getRawContent()));
            // reader.setEncoding("GB2312");
            Element rootElement = xml.getRootElement();
            Element deviceIdElement = rootElement.element("DeviceID");
            transaction.sendResponse(response);
            publisher.onlineEventPublish(deviceIdElement.getText(), VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
        } catch (ParseException | SipException | InvalidArgumentException | DocumentException e) {
            e.printStackTrace();
        }
    }
 
}