diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java index b7e222df..458afc2f 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java @@ -1,5 +1,7 @@ package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.genersoft.iot.vmp.conf.SipConfig; import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.gb28181.bean.*; @@ -12,11 +14,14 @@ import com.genersoft.iot.vmp.gb28181.utils.NumericUtil; import com.genersoft.iot.vmp.service.IDeviceAlarmService; import com.genersoft.iot.vmp.storager.IRedisCatchStorage; import com.genersoft.iot.vmp.storager.IVideoManagerStorage; +import okhttp3.*; import org.dom4j.Element; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; @@ -25,7 +30,9 @@ import javax.sip.RequestEvent; import javax.sip.SipException; import javax.sip.message.Response; +import java.io.IOException; import java.text.ParseException; +import java.util.UUID; import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.*; @@ -56,6 +63,9 @@ public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent impleme @Autowired private IDeviceAlarmService deviceAlarmService; + @Value("${user-settings.alarmCallbackHttp:}") + private String alarmCallbackHttp; + @Override public void afterPropertiesSet() throws Exception { notifyMessageHandler.addHandler(cmdType, this); @@ -151,6 +161,8 @@ public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent impleme if (redisCatchStorage.deviceIsOnline(device.getDeviceId())) { publisher.deviceAlarmEventPublish(deviceAlarm); } + + alarmCallback("device",device,null,deviceAlarm); } @Override @@ -211,5 +223,53 @@ public class AlarmNotifyMessageHandler extends SIPRequestProcessorParent impleme redisCatchStorage.sendAlarmMsg(alarmChannelMessage); return; } + + alarmCallback("platform",null,parentPlatform,deviceAlarm); + } + + /** + * 处理报警回调外部接口处理 + * @param alarmSource 报警来源,device/platfrom + * @param device 报警设备 + * @param device 报警平台 + * @param deviceAlarm 报警内容 + */ + private void alarmCallback(String alarmSource, Device device, ParentPlatform parentPlatform, DeviceAlarm deviceAlarm){ + if (StringUtils.isEmpty(alarmCallbackHttp)) return; + + JSONObject alarmData = new JSONObject(); + alarmData.put("alarmSource",alarmSource); + alarmData.put("sourcesInfo",device != null ? JSON.toJSONString(device) : JSON.toJSONString(parentPlatform)); + alarmData.put("alarmContent",JSON.toJSONString(deviceAlarm)); + + try { + OkHttpClient okHttpClient = new OkHttpClient(); + RequestBody requestBody = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("alarmData",alarmData.toJSONString()) + .build(); + Request request = new Request.Builder() + .header("Authorization", "Client-ID " + UUID.randomUUID()) + .url(alarmCallbackHttp) + .post(requestBody) + .build(); + Call call = okHttpClient.newCall(request); + call.enqueue(new Callback() { + @Override + public void onResponse(@NotNull Call call, @NotNull okhttp3.Response response) throws IOException { + if (response.isSuccessful()) { + logger.debug("报警回调外部接口返回成功"); + }else{ + logger.debug("报警回调外部接口返回失败:{}", response.message()); + } + } + @Override + public void onFailure(Call call, IOException e) { + logger.error("处理报警回调外部接口错误:{}", e.getMessage()); + } + }); + }catch (Exception e){ + logger.error("处理报警回调外部接口错误,请检查配置文件的http地址是否正确:{}", e.getMessage()); + } } } diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java index 0144e83b..3eba5db1 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java @@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.service.impl; import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction; +import com.genersoft.iot.vmp.gb28181.event.SipSubscribe; import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander; import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd.CatalogResponseMessageHandler; @@ -17,6 +18,7 @@ import com.genersoft.iot.vmp.utils.DateUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -58,6 +60,9 @@ public class DeviceServiceImpl implements IDeviceService { @Autowired private IMediaServerService mediaServerService; + @Value("${user-settings.guard:false}") + private boolean guard; + @Override public void online(Device device) { logger.info("[设备上线] deviceId:{}->{}:{}", device.getDeviceId(), device.getIp(), device.getPort()); @@ -96,6 +101,11 @@ public class DeviceServiceImpl implements IDeviceService { String registerExpireTaskKey = registerExpireTaskKeyPrefix + device.getDeviceId(); dynamicTask.stop(registerExpireTaskKey); dynamicTask.startDelay(registerExpireTaskKey, ()-> offline(device.getDeviceId()), device.getExpires() * 1000); + // 上线添加布防 + if(guard){ + logger.info("[启用布防] deviceId: {}", device.getDeviceId()); + sipCommander.guardCmd(device, "SetGuard", eventResult -> logger.info("[启用布防] 启用布防失败,deviceId: {},错误信息: {}", device.getDeviceId(), eventResult.toString())); + } } @Override diff --git a/src/main/resources/all-application.yml b/src/main/resources/all-application.yml index 1233a890..fce2e7ef 100644 --- a/src/main/resources/all-application.yml +++ b/src/main/resources/all-application.yml @@ -187,6 +187,10 @@ user-settings: logInDatebase: true # 第三方匹配,用于从stream钟获取有效信息 thirdPartyGBIdReg: "[\\s\\S]*" + # 设备上线时自动启用布防功能 + guard: true + # 设备报警时http回调,POST方法,接收String类型alarmData字段 + alarmCallbackHttp: http://ip:port/... # 在线文档: swagger-ui(生产环境建议关闭) swagger-ui: