package com.ruoyi.dataInterchange.util.haikang; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hikvision.artemis.sdk.ArtemisHttpUtil; import com.hikvision.artemis.sdk.config.ArtemisConfig; import com.ruoyi.dataInterchange.util.haikang.model.EventSubscriptionByEventTypesRequest; import com.ruoyi.dataInterchange.util.haikang.model.EventSubscriptionViewRequest; import com.ruoyi.dataInterchange.util.haikang.model.EventUnSubscriptionByEventTypesRequest; import lombok.extern.slf4j.Slf4j; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 事件订阅工具类 * @author zhibing.pu * @Date 2025/5/20 16:17 */ @Slf4j @WebListener public class Artemis implements ServletContextListener { /** * STEP2:设置OpenAPI接口的上下文 */ private static final String ARTEMIS_PATH = "/artemis"; /** * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数. * * ip:port : 平台门户/nginx的IP和端口(必须使用https协议,https端口默认为443) * appKey : 请填入appKey * appSecret : 请填入appSecret */ private static ArtemisConfig artemisConfig = new ArtemisConfig("112.18.106.230:443", "27273246", "vjvZA7X4hHUc0SbONht9"); //按事件类型取消订阅 public static String eventUnSubscriptionByEventTypes(EventUnSubscriptionByEventTypesRequest eventUnSubscriptionByEventTypesRequest) throws Exception { String eventUnSubscriptionByEventTypesDataApi = ARTEMIS_PATH +"/api/eventService/v1/eventUnSubscriptionByEventTypes"; Map path = new HashMap(2){ { put("https://",eventUnSubscriptionByEventTypesDataApi); } }; String body= JSON.toJSONString(eventUnSubscriptionByEventTypesRequest); String result =ArtemisHttpUtil.doPostStringArtemis(artemisConfig,path,body,null,null,"application/json"); return result; } @Override public void contextInitialized(ServletContextEvent sce) { try { //先查询是否订阅事件 EventSubscriptionViewRequest eventSubscriptionViewRequest = new EventSubscriptionViewRequest(); eventSubscriptionViewRequest.setSubWay(1); String eventSubscriptionView = Artemis.eventSubscriptionView(eventSubscriptionViewRequest); JSONObject jsonObject = JSON.parseObject(eventSubscriptionView); String code = jsonObject.getString("code"); if("200".equals(code)){ JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("detail"); List list = new ArrayList<>(); for (int i = 0; i < jsonArray.size(); i++) { JSONArray eventTypes = jsonArray.getJSONObject(i).getJSONArray("eventTypes"); eventTypes.forEach(eventType -> { list.add(eventType.toString()); }); } //告警事件类型 if(!list.contains("5201154049")){ //订阅事件 EventSubscriptionByEventTypesRequest eventSubscriptionByEventTypesRequest = new EventSubscriptionByEventTypesRequest(); eventSubscriptionByEventTypesRequest.setSubWay(1); eventSubscriptionByEventTypesRequest.setEventDest("http://221.182.45.100:1000/dataInterchange/warnMsgAdptInfo/alarmNotice"); eventSubscriptionByEventTypesRequest.setEventTypes(new ArrayList(){{ add("5201154049"); }}); String subscription = Artemis.eventSubscriptionByEventTypes(eventSubscriptionByEventTypesRequest); jsonObject = JSON.parseObject(subscription); code = jsonObject.getString("code"); if(!"200".equals(code)){ log.error("告警事件订阅失败"); }else { log.info("告警事件订阅成功"); } } }else{ log.error("查询事件订阅信息失败"); } }catch (Exception e){ e.printStackTrace(); } } //查询事件订阅信息 public static String eventSubscriptionView(EventSubscriptionViewRequest eventSubscriptionViewRequest) throws Exception { String eventSubscriptionViewDataApi = ARTEMIS_PATH +"/api/eventService/v1/eventSubscriptionView"; Map path = new HashMap(2){ { put("https://",eventSubscriptionViewDataApi); } }; String body=JSON.toJSONString(eventSubscriptionViewRequest); String result =ArtemisHttpUtil.doPostStringArtemis(artemisConfig,path,body,null,null,"application/json"); return result; } //按事件类型订阅事件 public static String eventSubscriptionByEventTypes(EventSubscriptionByEventTypesRequest eventSubscriptionByEventTypesRequest) throws Exception { String eventSubscriptionByEventTypesDataApi = ARTEMIS_PATH +"/api/eventService/v1/eventSubscriptionByEventTypes"; Map path = new HashMap(2){ { put("https://",eventSubscriptionByEventTypesDataApi); } }; String body=JSON.toJSONString(eventSubscriptionByEventTypesRequest); String result = ArtemisHttpUtil.doPostStringArtemis(artemisConfig,path,body,null,null,"application/json"); return result; } @Override public void contextDestroyed(ServletContextEvent sce) { } }