64850858
2021-07-26 ea32cd2673b83b9481e8cc45705d2d3a84e884bb
重启后对心跳超时的设备设置为离线。
3个文件已修改
1个文件已添加
82 ■■■■ 已修改文件
src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java 30 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java
New file
@@ -0,0 +1,36 @@
package com.genersoft.iot.vmp.conf;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
/**
 * 系统启动时控制设备离线
 */
@Component
@Order(value=4)
public class SipDeviceRunner implements CommandLineRunner {
    @Autowired
    private IVideoManagerStorager storager;
    @Autowired
    private IRedisCatchStorage redisCatchStorage;
    @Override
    public void run(String... args) throws Exception {
        // 读取redis没有心跳信息的则设置为离线,等收到下次心跳设置为在线
        // 设置所有设备离线
        storager.outlineForAll();
        List<String> onlineForAll = redisCatchStorage.getOnlineForAll();
        for (String deviceId : onlineForAll) {
            storager.online(deviceId);
        }
    }
}
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java
@@ -771,19 +771,31 @@
            Element rootElement = getRootElement(evt);
            String deviceId = XmlUtil.getText(rootElement, "DeviceID");
            Device device = storager.queryVideoDevice(deviceId);
            // 检查设备是否存在并在线, 不存在则不回复
            if (device != null && device.getOnline() == 1) {
            // 检查设备是否存在并在线, 不在线则设置为在线
            if (device != null ) {
                // 回复200 OK
                responseAck(evt);
                if (offLineDetector.isOnline(deviceId)) {
                    publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
                } else {
                }
            }else {
                logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备" + (device == null? "不存在":"离线") + ", 回复401");
                Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, evt.getRequest());
                publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
            }else{
                logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备不存在, 回复404");
                Response response = getMessageFactory().createResponse(Response.NOT_FOUND, evt.getRequest());
                getServerTransaction(evt).sendResponse(response);
            }
//            if (device != null && device.getOnline() == 1) {
//
//                if (offLineDetector.isOnline(deviceId)) {
//                    publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
//                } else {
//                }
//            }else {
////                logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备" + (device == null? "不存在":"离线") + ", 回复401");
////                Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, evt.getRequest());
////                getServerTransaction(evt).sendResponse(response);
//                publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
//
//            }
        } catch (ParseException | SipException | InvalidArgumentException | DocumentException e) {
            e.printStackTrace();
        }
src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
@@ -113,6 +113,11 @@
    void outlineForAll();
    /**
     * 获取所有在线的
     */
    List<String> getOnlineForAll();
    /**
     * 在redis添加wvp的信息
     */
    void updateWVPInfo(JSONObject jsonObject);
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
@@ -284,6 +284,17 @@
    }
    @Override
    public List<String> getOnlineForAll() {
        List<String> result = new ArrayList<>();
        List<Object> onlineDevices = redis.scan(VideoManagerConstants.KEEPLIVEKEY_PREFIX + "*" );
        for (int i = 0; i < onlineDevices.size(); i++) {
            String key = (String) onlineDevices.get(i);
            result.add((String) redis.get(key));
        }
        return result;
    }
    @Override
    public void updateWVPInfo(JSONObject jsonObject) {
    }