监听重连-sjx

main
sunjianxi 2 years ago
parent 23ff537a22
commit fe73cae165
  1. 135
      src/main/java/org/springblade/hospital/hik/alarm/Alarm.java
  2. 45
      src/main/java/org/springblade/hospital/newalarm/communicationCom/HTTPClientUtil.java

@ -6,25 +6,26 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.springblade.hospital.hik.CommonMethod.osSelect;
import org.springblade.hospital.hik.NetSDKDemo.HCNetSDK;
import org.springblade.hospital.newalarm.communicationCom.HTTPClientUtil;
import org.springblade.hospital.utils.CornJobUtil;
import org.springblade.hospital.utils.ExternalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author AAA
*/
@Slf4j
@Component
public class Alarm implements CommandLineRunner {
@ -41,6 +42,8 @@ public class Alarm implements CommandLineRunner {
public static Map<String, String> portMap;
public static Map<String, String> macMap;
public static ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(10);
@Autowired
ExternalUtils externalUtils;
@ -484,57 +487,26 @@ public class Alarm implements CommandLineRunner {
}
// 物联网平台获取报警主机设备信息
List<Map> list = externalUtils.getList(0, 0, null, null, "105", null);
// 物联网平台获取的报警主机列表
if (CollectionUtils.isNotEmpty(list)) {
HTTPClientUtil clientUtil = new HTTPClientUtil();
for (Map map : list) {
// System.out.println(map.get("tags"));
String s = JSON.toJSONString(map.get("tags")).replace("\\", "");
s = s.substring(1, s.length() - 1);
Map tagsMap = JSONObject.parseObject(s, Map.class);
// 报警主机信息
String number = tagsMap.get("hostNumber").toString();//序列号
String ip = tagsMap.get("ip").toString();
String port = tagsMap.get("port").toString();
String mac = tagsMap.get("mac").toString();
HttpClient client = new HttpClient();
clientMap.put(number, client);
ipMap.put(number, ip);
portMap.put(number, port);
macMap.put(mac, number);
new Thread(() -> {
// 登录认证
String rzUrl = "http://" + ip + ":" + port + "/ISAPI/System/deviceInfo";
String renzheng = HTTPClientUtil.renzheng(rzUrl, client);
System.out.println("登录信息:" + renzheng);
if (renzheng.contains(number)) {
// 获取设备系统能力集
String jtUrl = "http://" + ip + ":" + port + "/ISAPI/System/capabilities";
HTTPClientUtil.doGet(jtUrl, client);
// 获取布防订阅能力
String strUrl = "http://" + ip + ":" + port + "/ISAPI/Event/notification/subscribeEventCap";
HTTPClientUtil.doGet(strUrl, client);
// 建立布防订阅连接
String jlUrl = "http://" + ip + ":" + port + "/ISAPI/Event/notification/subscribeEvent?deployID=1";
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"\n" +
"<SubscribeEvent xmlns=\"http://www.isapi.org/ver20/XMLSchema\" version=\"2.0\">\n" +
" <heartbeat>1</heartbeat>\n" +
" <eventMode>all</eventMode>\n" +
"</SubscribeEvent>";
System.out.println("jl:" + clientUtil.doPost(jlUrl, xml, number, client, mac));
AtomicInteger size = new AtomicInteger(0);
List<Thread> threadList = new ArrayList<>();
ScheduledFuture<?> scheduledFuture = scheduled.scheduleAtFixedRate(() -> {
List<Map> list = externalUtils.getList(0, 0, null, null, "105", null);
log.info("获取报警主机列表数量为:" + list.size());
if(list.size() != size.get()){
size.set(list.size());
if(threadList.size() > 0){
for(Thread thread : threadList){
thread.interrupt();
}
}).start();
}
threadList.clear();
// 物联网平台获取的报警主机列表
getList(list,threadList);
}
}
}, 0, 1, TimeUnit.DAYS);
} catch (Exception e) {
e.printStackTrace();
}
@ -635,4 +607,57 @@ public class Alarm implements CommandLineRunner {
// //释放SDK
// hCNetSDK.NET_DVR_Cleanup();
}
public void getList(List<Map> list,List<Thread> threadList){
if (CollectionUtils.isNotEmpty(list)) {
HTTPClientUtil clientUtil = new HTTPClientUtil();
for (Map map : list) {
// System.out.println(map.get("tags"));
String s = JSON.toJSONString(map.get("tags")).replace("\\", "");
s = s.substring(1, s.length() - 1);
Map tagsMap = JSONObject.parseObject(s, Map.class);
// 报警主机信息
String number = tagsMap.get("hostNumber").toString();//序列号
String ip = tagsMap.get("ip").toString();
String port = tagsMap.get("port").toString();
String mac = tagsMap.get("mac").toString();
HttpClient client = new HttpClient();
clientMap.put(number, client);
ipMap.put(number, ip);
portMap.put(number, port);
macMap.put(mac, number);
Thread thread = new Thread(() -> {
// 登录认证
String rzUrl = "http://" + ip + ":" + port + "/ISAPI/System/deviceInfo";
String renzheng = HTTPClientUtil.renzheng(rzUrl, client);
System.out.println("登录信息:" + renzheng);
if (renzheng.contains(number)) {
// 获取设备系统能力集
String jtUrl = "http://" + ip + ":" + port + "/ISAPI/System/capabilities";
HTTPClientUtil.doGet(jtUrl, client);
// 获取布防订阅能力
String strUrl = "http://" + ip + ":" + port + "/ISAPI/Event/notification/subscribeEventCap";
HTTPClientUtil.doGet(strUrl, client);
// 建立布防订阅连接
String jlUrl = "http://" + ip + ":" + port + "/ISAPI/Event/notification/subscribeEvent?deployID=1";
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"\n" +
"<SubscribeEvent xmlns=\"http://www.isapi.org/ver20/XMLSchema\" version=\"2.0\">\n" +
" <heartbeat>1</heartbeat>\n" +
" <eventMode>all</eventMode>\n" +
"</SubscribeEvent>";
System.out.println("jl:" + clientUtil.doPost(jlUrl, xml, number, client, mac));
}
});
thread.start();
threadList.add(thread);
}
}
}
}

@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.protobuf.ByteString;
import com.hisense.device.agent.grpc.Point;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
@ -32,7 +34,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Slf4j
public class HTTPClientUtil {
public static HttpClient client = new HttpClient();
@ -55,14 +57,22 @@ public class HTTPClientUtil {
byte[] responseData = method.getResponseBodyAsString().getBytes(method.getResponseCharSet());
strResponseData = new String(responseData, "utf-8");
} catch (IOException e) {
e.printStackTrace();
log.error("url:"+rzUrl+",通讯出现错误,错误信息是:" + e.toString());
//出现错误,每隔1s,重新尝试调用
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
renzheng(rzUrl, client);
//e.printStackTrace();
}
method.releaseConnection();
// 输出接收的消息
return strResponseData;
}
public static String doGet(String url, HttpClient client) {
public static String doGet(String url, HttpClient client){
GetMethod method = new GetMethod(url);
method.setDoAuthentication(true);
@ -75,7 +85,15 @@ public class HTTPClientUtil {
// Use encoding in the Return response message (utf-8 or gb2312)
response = new String(responseBody, "utf-8");
} catch (IOException e) {
e.printStackTrace();
log.error("url:"+url+",通讯出现错误,错误信息是:" + e.toString());
//出现错误,每隔1s,重新尝试调用
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
doGet(url, client);
//e.printStackTrace();
}
// Release the connection
method.releaseConnection();
@ -117,8 +135,13 @@ public class HTTPClientUtil {
String str = "";
List<Map> list = new ArrayList<>();
while ((str = br.readLine()) != null) {
while ((str = br.readLine()) != null && !Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("Thread 停止!");
break;
}
if (str.contains("--boundary")) {
Map resMap = JSON.parseObject(str.replaceAll("--boundary", ""), Map.class);
@ -252,7 +275,15 @@ public class HTTPClientUtil {
method.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
log.error("url:"+url+",通讯出现错误,错误信息是:" + e.toString());
//出现错误,每隔1s,重新尝试调用
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
doPost(url, inbound, sSerialNumber, client, mac);
//e.printStackTrace();
}
return stringBuffer.toString();

Loading…
Cancel
Save