commit
26ff7084e1
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,146 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-parent</artifactId> |
||||
<version>2.7.13</version> |
||||
<relativePath/> <!-- lookup parent from repository --> |
||||
</parent> |
||||
<groupId>com.cjy</groupId> |
||||
<artifactId>access-control-listen</artifactId> |
||||
<version>1.0-SNAPSHOT</version> |
||||
|
||||
<properties> |
||||
<maven.compiler.source>8</maven.compiler.source> |
||||
<maven.compiler.target>8</maven.compiler.target> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-web</artifactId> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>org.projectlombok</groupId> |
||||
<artifactId>lombok</artifactId> |
||||
<optional>true</optional> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>com.alibaba</groupId> |
||||
<artifactId>fastjson</artifactId> |
||||
<version>1.2.47</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>httpClient</groupId> |
||||
<artifactId>httpClient</artifactId> |
||||
<version>5.1.3</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/httpclient5-5.1.3.jar</systemPath> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>httpClient-cache</groupId> |
||||
<artifactId>httpClient-cache</artifactId> |
||||
<version>5.1.3</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/httpclient5-cache-5.1.3.jar</systemPath> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>httpClient-fluent</groupId> |
||||
<artifactId>httpClient-fluent</artifactId> |
||||
<version>5.1.3</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/httpclient5-fluent-5.1.3.jar</systemPath> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>httpcore</groupId> |
||||
<artifactId>httpcore</artifactId> |
||||
<version>5.1.3</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/httpcore5-5.1.3.jar</systemPath> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>jackson-annotations</groupId> |
||||
<artifactId>jackson-annotations</artifactId> |
||||
<version>2.13.0</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/jackson-annotations-2.13.0-rc2.jar</systemPath> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>jackson-core</groupId> |
||||
<artifactId>jackson-core</artifactId> |
||||
<version>2.13.0</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/jackson-core-2.13.0-rc2.jar</systemPath> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>jackson-databind</groupId> |
||||
<artifactId>jackson-databind</artifactId> |
||||
<version>2.13.2</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/jackson-databind-2.13.2.2.jar</systemPath> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>java-webSocket</groupId> |
||||
<artifactId>java-webSocket</artifactId> |
||||
<version>1.5.3</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/Java-WebSocket-1.5.3.jar</systemPath> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>slf4j-api</groupId> |
||||
<artifactId>slf4j-api</artifactId> |
||||
<version>2.0.0</version> |
||||
<scope>system</scope> |
||||
<systemPath>${project.basedir}/lib/slf4j-api-2.0.0-alpha7.jar</systemPath> |
||||
</dependency> |
||||
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> |
||||
<dependency> |
||||
<groupId>org.apache.commons</groupId> |
||||
<artifactId>commons-lang3</artifactId> |
||||
<version>3.10</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.httpcomponents</groupId> |
||||
<artifactId>httpasyncclient</artifactId> |
||||
<version>4.1.5</version> |
||||
</dependency> |
||||
</dependencies> |
||||
<build> |
||||
<finalName>${project.artifactId}-${project.version}</finalName> |
||||
<resources> |
||||
<resource> |
||||
<directory>src/main/resources</directory> |
||||
</resource> |
||||
|
||||
<resource> |
||||
<directory>lib</directory> |
||||
<targetPath>/BOOT-INF/lib/</targetPath> |
||||
<includes> |
||||
<include>**/*.jar</include> |
||||
</includes> |
||||
</resource> |
||||
</resources> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
<configuration> |
||||
<excludes> |
||||
<exclude> |
||||
<groupId>org.projectlombok</groupId> |
||||
<artifactId>lombok</artifactId> |
||||
</exclude> |
||||
</excludes> |
||||
</configuration> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
</project> |
@ -0,0 +1,24 @@ |
||||
|
||||
## 门禁监听程序(适用于微耕门禁的板子) |
||||
<p>通过webSocket监听形式 对门状态定时查询输出</p> |
||||
<p>对进门数据(刷卡)进行记录或处理 该程序目前只对刷卡成功进门的数据进行推送至相应程序,不进行远程非法开门</p> |
||||
<p>socket启动在ServerSocketConfig.java 随主程序同步启动</p> |
||||
<p>门禁监听地址和数据推送地址需在applicaiton.yml中配置 controller.url 和push.url</p> |
||||
<p>数据推送调用三方http接口 目前只写了GET请求形式</p> |
||||
<p> http推送采用httClient</p> |
||||
|
||||
#### 版本号 |
||||
<table> |
||||
<tr> |
||||
<td>时间</td> |
||||
<td>版本号</td> |
||||
<td>更新说明</td> |
||||
</tr> |
||||
<tr> |
||||
<td>2023-07-26</td> |
||||
<td>V1.0.0</td> |
||||
<td>监听进门状态推送到指定http</td> |
||||
</tr> |
||||
</table> |
||||
|
||||
|
@ -0,0 +1,20 @@ |
||||
package com.cjy; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
||||
/** |
||||
* 说明: 门禁设备监听程序 |
||||
* |
||||
* @Author 清风煮酒QaQ |
||||
* @Date 2023-7-24 11:11 |
||||
* @ClassName: AccessControlListenApplication |
||||
* @Version 1.0 |
||||
*/ |
||||
@SpringBootApplication |
||||
public class AccessControlListenApplication { |
||||
public static void main(String[] args) { |
||||
SpringApplication.run(AccessControlListenApplication.class, args); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,169 @@ |
||||
package com.cjy.config; |
||||
|
||||
import com.cjy.util.HttpClientUtil; |
||||
import com.cjy.util.WebSocket; |
||||
import com.cjy.util.WgWebapi; |
||||
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
import com.fasterxml.jackson.databind.JsonNode; |
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.net.URI; |
||||
import java.net.URISyntaxException; |
||||
import java.text.ParseException; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.HashMap; |
||||
import java.util.LinkedList; |
||||
import java.util.Map; |
||||
import java.util.Queue; |
||||
|
||||
/** |
||||
* 说明: |
||||
* |
||||
* @Author 清风煮酒QaQ |
||||
* @Date 2023-7-25 13:58 |
||||
* @ClassName: ServerSocketConfig |
||||
* @Version 1.0 |
||||
*/ |
||||
@Component |
||||
public class ServerSocketConfig { |
||||
@Value("${controller.server}") |
||||
private String SERVER; |
||||
@Value("${push.url}") |
||||
private String pushUrl; |
||||
/** |
||||
* The timeout value in milliseconds for socket connection. |
||||
*/ |
||||
private static final int TIMEOUT = 5000; |
||||
|
||||
private static Queue<String> queue = new LinkedList<String>(); //1001数据缓存队列
|
||||
|
||||
static int wsRunning = 0; //用于连接失败后 重连接 建立连接超时为2秒
|
||||
|
||||
@Bean |
||||
public void socketStart() throws URISyntaxException, InterruptedException { |
||||
WgWebapi.logInfo("实时数据监听"); |
||||
while (true) //引入循环
|
||||
{ |
||||
if (wsRunning == 0) { |
||||
WgWebapi.logInfo("尝试连接ws服务器 " + SERVER); |
||||
WebSocket webSocket = new WebSocket(new URI(SERVER)); |
||||
webSocket.queue = queue; |
||||
webSocket.connect(); |
||||
Thread.sleep(2000); |
||||
if (webSocket.isOpen()) { |
||||
wsRunning = 1; |
||||
int chkcount = 0; |
||||
long heartcycle = 120 * 1000; //2分钟 与1001连接通信. 否则关闭重启
|
||||
long endTicks = System.currentTimeMillis() + heartcycle; |
||||
while (endTicks > System.currentTimeMillis()) //(text = in.readLine()) != null)
|
||||
{ |
||||
if (!queue.isEmpty()) { |
||||
dealDataFrom1001Server(webSocket,pushUrl); //处理来自1001数据
|
||||
endTicks = System.currentTimeMillis() + heartcycle; //刷新超时点
|
||||
} else { |
||||
try { |
||||
Thread.sleep(2000); |
||||
chkcount++; |
||||
if (chkcount > 1000) { |
||||
chkcount = 0; |
||||
if (!webSocket.isOpen()) { |
||||
break; |
||||
} |
||||
} |
||||
} catch (InterruptedException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
webSocket.close(); |
||||
WgWebapi.logInfo("已经关闭和服务端的channel"); |
||||
} |
||||
if (!webSocket.isClosed()) { |
||||
webSocket.close(); |
||||
} |
||||
wsRunning = 0; |
||||
} |
||||
//循环
|
||||
try { |
||||
Thread.sleep(5000); |
||||
} catch (InterruptedException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
static void dealDataFrom1001Server(WebSocket ws,String pushUrl) //处理接收到的数据信息
|
||||
{ |
||||
String msg; |
||||
try { |
||||
synchronized (queue) { |
||||
msg = queue.poll(); |
||||
} |
||||
ObjectMapper infojsn = new ObjectMapper(); |
||||
JsonNode node = infojsn.readTree(msg); |
||||
if (!node.findPath("刷新时间").isMissingNode()) { //表示 门信息
|
||||
//门的同步信息 门状态
|
||||
WgWebapi.logInfo("定时推送的信息(默认30秒周期) 门状态"); |
||||
WgWebapi.logInfoInJson(msg); |
||||
for (int i = 0; i < node.size(); i++) { |
||||
|
||||
JsonNode doorinfo = node.get(i); |
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
||||
java.util.Date refreshtime = sdf.parse(doorinfo.get("刷新时间").asText()); |
||||
java.util.Date curtime = new java.util.Date(); |
||||
int controllerSN = doorinfo.get("SN").asInt(); |
||||
if (Math.abs(refreshtime.getTime() - curtime.getTime()) > 60 * 1000) //超过60秒没同步 通信异常
|
||||
{ |
||||
WgWebapi.logInfo(String.format("控制器SN = %d 通信异常????", controllerSN)); |
||||
} |
||||
//doorinfo.get("1号门"); //获取门状态
|
||||
} |
||||
} else { |
||||
WgWebapi.logInfoInJson(msg); //加入显示 可以关闭
|
||||
for (int i = 0; i < node.size(); i++) { |
||||
JsonNode recinfo = node.get(i); |
||||
if (!recinfo.findPath("记录类型").isMissingNode()) { |
||||
if (recinfo.get("记录类型").asInt() == 1) { //只关注记录
|
||||
//
|
||||
String idCard = recinfo.get("卡号").asText(); |
||||
String openDoorTime = recinfo.get("时间").asText(); |
||||
if("进门".equals(recinfo.get("进出").asText()) &&"通过".equals(recinfo.get("有效").asText())){ |
||||
System.out.println("》》》》》》》"); |
||||
System.out.println("需要发送数据openDoorTime:"+openDoorTime+",idCard:"+idCard); |
||||
Map<String,Object> map = new HashMap<>(); |
||||
map.put("time",openDoorTime ); |
||||
map.put("idCard",idCard); |
||||
String s = HttpClientUtil.doGet(pushUrl,map,null); |
||||
System.out.println("》》》》》》》》》接收数据:"+s); |
||||
} |
||||
|
||||
} else if (recinfo.get("记录类型").asInt() == 2) { //远程开门
|
||||
if (recinfo.get("描述").asText().indexOf("远程开门") > 0) { |
||||
int controllerSN = recinfo.get("SN").asInt(); |
||||
long vcard = recinfo.get("卡号").asLong(); |
||||
int doorNO = recinfo.get("门号").asInt(); |
||||
System.out.println(""); |
||||
WgWebapi.logInfo(String.format("远程开门成功--卡号=%d, SN=%d, 门号=%d", vcard, controllerSN, doorNO)); |
||||
} |
||||
} |
||||
} else if (!recinfo.findPath("原始十六进制数据").isMissingNode()) //透传数据
|
||||
{ |
||||
//二维码信息
|
||||
String qrinfoHex = recinfo.get("原始十六进制数据").asText(); |
||||
String qrinfo = recinfo.get("转换为字符串").asText(); |
||||
|
||||
|
||||
} |
||||
} |
||||
} |
||||
//logInfo((new ObjectMapper()).readTree(info).toPrettyString());
|
||||
} catch (JsonProcessingException e) { |
||||
//throw new RuntimeException(e);
|
||||
} catch (ParseException e) { |
||||
//throw new RuntimeException(e);
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
package com.cjy.controller; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
/** |
||||
* 说明: |
||||
* |
||||
* @Author 清风煮酒QaQ |
||||
* @Date 2023-7-25 8:37 |
||||
* @ClassName: IndexController |
||||
* @Version 1.0 |
||||
*/ |
||||
@RestController |
||||
public class IndexController { |
||||
|
||||
@GetMapping() |
||||
public String showIndex() { |
||||
return "hello word"; |
||||
} |
||||
} |
@ -0,0 +1,207 @@ |
||||
package com.cjy.entity; |
||||
|
||||
/** |
||||
* 说明: |
||||
* |
||||
* @Author 清风煮酒QaQ |
||||
* @Date 2023-7-24 11:32 |
||||
* @ClassName: Access |
||||
* @Version 1.0 |
||||
*/ |
||||
public class Access { |
||||
|
||||
/** |
||||
* EventID : 1 |
||||
* EventTime : 2018-08-27 10:49:00 |
||||
* EmployeeID : 1 |
||||
* EmpID : 9dcbaa15-4143-41ac-828b-4c0114e68157 |
||||
* CardNo : 10745774 |
||||
* EventType : 1 |
||||
* EmployeeName : 测试人员1 |
||||
* Sex : 1 |
||||
* PersonCode : |
||||
* ControlName : 控制器A |
||||
* QRCode : |
||||
* DeptName : 市场部 |
||||
* JobName : 经理 |
||||
* EmployeeCode : 9527 |
||||
* DoorName : 东1门 |
||||
*/ |
||||
/** |
||||
* 事件主键 |
||||
*/ |
||||
private int EventID; |
||||
/** |
||||
* 刷卡事件 |
||||
*/ |
||||
private String EventTime; |
||||
/** |
||||
* 人员id |
||||
*/ |
||||
private int EmployeeID; |
||||
/** |
||||
* 人员GUID |
||||
*/ |
||||
private String EmpID; |
||||
/** |
||||
* 卡号 |
||||
*/ |
||||
private String CardNo; |
||||
/** |
||||
* 事件类型 1进 0出 |
||||
*/ |
||||
private int EventType; |
||||
/** |
||||
* 人员名称 |
||||
*/ |
||||
private String EmployeeName; |
||||
private String Sex; |
||||
/** |
||||
* 身份证号 |
||||
*/ |
||||
private String PersonCode; |
||||
/** |
||||
* 控制器名称 |
||||
*/ |
||||
private String ControlName; |
||||
/** |
||||
* 二维码 |
||||
*/ |
||||
private String QRCode; |
||||
/** |
||||
* 部门名称 |
||||
*/ |
||||
private String DeptName; |
||||
/** |
||||
* 职务名称 |
||||
*/ |
||||
private String JobName; |
||||
/** |
||||
* 人员工号 |
||||
*/ |
||||
private String EmployeeCode; |
||||
/** |
||||
* 门名称 |
||||
*/ |
||||
private String DoorName; |
||||
|
||||
public int getEventID() { |
||||
return EventID; |
||||
} |
||||
|
||||
public void setEventID(int EventID) { |
||||
this.EventID = EventID; |
||||
} |
||||
|
||||
public String getEventTime() { |
||||
return EventTime; |
||||
} |
||||
|
||||
public void setEventTime(String EventTime) { |
||||
this.EventTime = EventTime; |
||||
} |
||||
|
||||
public int getEmployeeID() { |
||||
return EmployeeID; |
||||
} |
||||
|
||||
public void setEmployeeID(int EmployeeID) { |
||||
this.EmployeeID = EmployeeID; |
||||
} |
||||
|
||||
public String getEmpID() { |
||||
return EmpID; |
||||
} |
||||
|
||||
public void setEmpID(String EmpID) { |
||||
this.EmpID = EmpID; |
||||
} |
||||
|
||||
public String getCardNo() { |
||||
return CardNo; |
||||
} |
||||
|
||||
public void setCardNo(String CardNo) { |
||||
this.CardNo = CardNo; |
||||
} |
||||
|
||||
public int getEventType() { |
||||
return EventType; |
||||
} |
||||
|
||||
public void setEventType(int EventType) { |
||||
this.EventType = EventType; |
||||
} |
||||
|
||||
public String getEmployeeName() { |
||||
return EmployeeName; |
||||
} |
||||
|
||||
public void setEmployeeName(String EmployeeName) { |
||||
this.EmployeeName = EmployeeName; |
||||
} |
||||
|
||||
public String getSex() { |
||||
return Sex; |
||||
} |
||||
|
||||
public void setSex(String Sex) { |
||||
this.Sex = Sex; |
||||
} |
||||
|
||||
public String getPersonCode() { |
||||
return PersonCode; |
||||
} |
||||
|
||||
public void setPersonCode(String PersonCode) { |
||||
this.PersonCode = PersonCode; |
||||
} |
||||
|
||||
public String getControlName() { |
||||
return ControlName; |
||||
} |
||||
|
||||
public void setControlName(String ControlName) { |
||||
this.ControlName = ControlName; |
||||
} |
||||
|
||||
public String getQRCode() { |
||||
return QRCode; |
||||
} |
||||
|
||||
public void setQRCode(String QRCode) { |
||||
this.QRCode = QRCode; |
||||
} |
||||
|
||||
public String getDeptName() { |
||||
return DeptName; |
||||
} |
||||
|
||||
public void setDeptName(String DeptName) { |
||||
this.DeptName = DeptName; |
||||
} |
||||
|
||||
public String getJobName() { |
||||
return JobName; |
||||
} |
||||
|
||||
public void setJobName(String JobName) { |
||||
this.JobName = JobName; |
||||
} |
||||
|
||||
public String getEmployeeCode() { |
||||
return EmployeeCode; |
||||
} |
||||
|
||||
public void setEmployeeCode(String EmployeeCode) { |
||||
this.EmployeeCode = EmployeeCode; |
||||
} |
||||
|
||||
public String getDoorName() { |
||||
return DoorName; |
||||
} |
||||
|
||||
public void setDoorName(String DoorName) { |
||||
this.DoorName = DoorName; |
||||
} |
||||
} |
@ -0,0 +1,155 @@ |
||||
package com.cjy.util; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.hc.core5.net.URIBuilder; |
||||
import org.apache.http.Header; |
||||
import org.apache.http.HttpEntity; |
||||
import org.apache.http.client.config.RequestConfig; |
||||
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
import org.apache.http.client.methods.HttpGet; |
||||
import org.apache.http.client.methods.HttpRequestBase; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; |
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
import org.apache.http.util.EntityUtils; |
||||
|
||||
import java.io.InputStream; |
||||
import java.io.InputStreamReader; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 说明: |
||||
* |
||||
* @Author 清风煮酒QaQ |
||||
* @Date 2023-7-26 13:30 |
||||
* @ClassName: HttpClient |
||||
* @Version 1.0 |
||||
*/ |
||||
@Slf4j |
||||
public class HttpClientUtil { |
||||
|
||||
/** |
||||
* 配置 |
||||
*/ |
||||
private static final RequestConfig config = RequestConfig.custom() |
||||
// 连接超时时间 单位毫秒
|
||||
.setConnectTimeout(2000) |
||||
// 请求超时时间 单位毫秒
|
||||
.setConnectionRequestTimeout(2000) |
||||
//socket 读写超时时间
|
||||
.setSocketTimeout(1000) |
||||
// 是否允许重定向 默认为true
|
||||
.setRedirectsEnabled(true) |
||||
// 是否启用内容压缩 默认为true
|
||||
.setContentCompressionEnabled(true) |
||||
.build(); |
||||
/** |
||||
* 获取http客户端 |
||||
*/ |
||||
private final static CloseableHttpClient HTTP_CLIENT = HttpClientBuilder.create() |
||||
// 失败重试 默认3次
|
||||
.setRetryHandler(new DefaultHttpRequestRetryHandler()) |
||||
.build(); |
||||
|
||||
private static String execute(HttpRequestBase httpRequestBase) { |
||||
log.info("httpClientUtil execute请求地址:{},请求类型:{}", httpRequestBase.getURI(), httpRequestBase.getMethod()); |
||||
log.info("HttpClientUtil execute请求参数信息:{}", httpRequestBase.getURI().getQuery()); |
||||
StringBuffer stringBuffer = new StringBuffer(); |
||||
Header[] requestHeaders = httpRequestBase.getAllHeaders(); |
||||
for (Header header : requestHeaders) { |
||||
stringBuffer.append(header.toString()).append(","); |
||||
} |
||||
log.info("HttpClientUtil executeAsync 请求头信息: {}", stringBuffer.toString()); |
||||
String result = null; |
||||
//响应类型
|
||||
CloseableHttpResponse response = null; |
||||
try { |
||||
httpRequestBase.setConfig(config); |
||||
long startTime = System.currentTimeMillis(); |
||||
response = HTTP_CLIENT.execute(httpRequestBase); |
||||
// 从响应模型中 获取响应实体
|
||||
HttpEntity entity = response.getEntity(); |
||||
log.info("HttpClientUtil execute 响应状态码:{}", response.getStatusLine().getStatusCode()); |
||||
long endTime = System.currentTimeMillis(); |
||||
if (entity != null) { |
||||
result = EntityUtils.toString(entity, "UTF-8"); |
||||
log.info("HttpClientUtil execute 响应信息:{}", result); |
||||
} |
||||
stringBuffer = new StringBuffer(); |
||||
Header[] responseHeaders = response.getAllHeaders(); |
||||
for (Header header : responseHeaders) { |
||||
stringBuffer.append(header.toString()).append(","); |
||||
} |
||||
log.info("HttpClientUtil execute响应头信息:{}", stringBuffer.toString()); |
||||
log.info("HttpClientUtil execute执行时间:{}", endTime - startTime); |
||||
} catch (Exception e) { |
||||
log.error("HttpClientUtil execute请求失败信息:{}", e.getMessage()); |
||||
} finally { |
||||
try { |
||||
if (response != null) { |
||||
response.close(); |
||||
} |
||||
} catch (Exception e) { |
||||
log.error("HttpClientUtil execute exception result: ", e.getMessage()); |
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
public static String doGet(String url, Map<String,Object> params, Map<String,Object> headers){ |
||||
CloseableHttpResponse response = null; |
||||
try{ |
||||
//1.处理参数
|
||||
URIBuilder uriBuilder = new URIBuilder(url); |
||||
if(params !=null && !params.isEmpty()){ |
||||
for(Object param : params.keySet()){ |
||||
uriBuilder.setParameter(param+"",params.get(param)+""); |
||||
} |
||||
} |
||||
HttpGet get = new HttpGet(uriBuilder.build()); |
||||
log.info("HttpClientUtil -GET>>>>请求:{}",get); |
||||
//2. 设置请求头信息
|
||||
if(headers !=null &&!headers.isEmpty()){ |
||||
for(Object headerKey : headers.keySet()){ |
||||
get.setHeader(String.valueOf(headerKey),String.valueOf(headers.get(headerKey))); |
||||
} |
||||
} |
||||
//3. 请求数据
|
||||
response = HTTP_CLIENT.execute(get); |
||||
//4.处理响应信息
|
||||
String result = null; |
||||
if(response !=null && response.getStatusLine().getStatusCode() == 200){ |
||||
result = handleData(response.getEntity().getContent()); |
||||
} |
||||
log.info("HttpClientUtil-GET >>>>>>返回的数据:{}",result); |
||||
return result; |
||||
}catch (Exception e){ |
||||
log.info("HttpClientUtil-GET 出错了:{}",e.getMessage()); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String handleData(InputStream is) throws Exception{ |
||||
int len = 0; |
||||
//将is转换为字符流
|
||||
InputStreamReader reader = new InputStreamReader(is); |
||||
//创建StringBuffer对象
|
||||
char[] buf = new char[1024]; |
||||
StringBuffer result = new StringBuffer(); |
||||
while((len = reader.read(buf)) !=-1){ |
||||
result.append(String.valueOf(buf,0,len)); |
||||
} |
||||
reader.close(); |
||||
return String.valueOf(result); |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
String path ="http://localhost:7834/test"; |
||||
Map<String,Object> map = new HashMap<>(); |
||||
map.put("time","2023-07-26 14:43:00"); |
||||
map.put("idCard",130181199503276716L); |
||||
String s = HttpClientUtil.doGet(path,map,null); |
||||
System.out.println(s); |
||||
} |
||||
} |
@ -0,0 +1,54 @@ |
||||
package com.cjy.util; |
||||
|
||||
import org.java_websocket.client.WebSocketClient; |
||||
import org.java_websocket.handshake.ServerHandshake; |
||||
|
||||
import java.net.URI; |
||||
import java.util.Queue; |
||||
|
||||
/** |
||||
* socket推送消息(客户端) |
||||
*/ |
||||
public class WebSocket extends WebSocketClient { |
||||
|
||||
public Queue<String> queue; |
||||
public WebSocket(URI serverUri) { |
||||
super(serverUri); |
||||
} |
||||
// /**
|
||||
// * demo
|
||||
// * @param args
|
||||
// * @throws URISyntaxException
|
||||
// * @throws InterruptedException
|
||||
// */
|
||||
// public static void main(String[] args) throws URISyntaxException, InterruptedException {
|
||||
// String url = "ws://127.0.0.1:8080/socket/websocket/1233435";
|
||||
// SocketClient client = new SocketClient(new URI(url));
|
||||
// client.connect();
|
||||
// Thread.sleep(5000);
|
||||
// System.out.println(client.isOpen());
|
||||
// client.send("哈哈哈哈");
|
||||
// client.close();
|
||||
// }
|
||||
@Override |
||||
public void onOpen(ServerHandshake serverHandshake) { |
||||
// WgWebapi.logInfo("SocketClient:------打开socket--------");
|
||||
} |
||||
@Override |
||||
public void onMessage(String s) { |
||||
// WgWebapi.logInfo("SocketClient:-----接受服务端消息-----"+s);
|
||||
synchronized (queue) { |
||||
queue.offer(s); //1001数据放入缓存队列
|
||||
} |
||||
} |
||||
@Override |
||||
public void onClose(int i, String s, boolean b) { |
||||
// WgWebapi.logInfo("SocketClient:----关闭socket链接");
|
||||
} |
||||
@Override |
||||
public void onError(Exception e) { |
||||
// WgWebapi.logInfo("SocketClient|onError[error]:e={}" + e.getMessage());
|
||||
} |
||||
} |
||||
|
||||
|
@ -0,0 +1,154 @@ |
||||
package com.cjy.util;/* |
||||
版权所有(C) 2022 深圳市微耕实业有限公司. 保留所有权利. |
||||
2022-05-10 09:03:36 远程开门 上传全部权限 与1001服务器20220509对接 |
||||
JSON 采用 jackson |
||||
HTTP 采用 java.net.http.HttpClient |
||||
Websocket 采用 https://github.com/TakahikoKawasaki/nv-websocket-client
|
||||
2022-05-12 11:16:35 Websocket 改为 java.net.http.WebSocket |
||||
增加 上传全部有用时段[数据类似短报文协议] |
||||
2022-05-25 22:51:17 为了支持JDK1.8版本 而引入 |
||||
HTTP 采用 org.apache.hc.client5 |
||||
websocket 采用 org.java_websocket |
||||
*/ |
||||
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
import com.fasterxml.jackson.databind.JsonNode; |
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import org.apache.hc.client5.http.fluent.Content; |
||||
import org.apache.hc.client5.http.fluent.Request; |
||||
import org.apache.hc.core5.http.ContentType; |
||||
import org.apache.hc.core5.util.Timeout; |
||||
|
||||
import java.io.BufferedWriter; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.util.Calendar; |
||||
|
||||
public class WgWebapi { |
||||
//wg提供的外网服务器及控制器 可切换为本地的
|
||||
// public static String testUrl = "http://webapi.wiegand.com.cn:61080";;
|
||||
// public static int testControllerSN = 253118302; //253118302
|
||||
|
||||
//本地电脑
|
||||
public static String testUrl = "http://localhost:61080/"; |
||||
public static int testControllerSN = 153217233; //451000003;
|
||||
|
||||
|
||||
//url: 1001服务器域名IP及端口
|
||||
//cmd: 发送信息
|
||||
//timeoutMs: 接收超时 默认是3000毫秒
|
||||
public String PushToWebWithjson(String url , String cmdBody, int timeoutMs) //默认超时3000ms
|
||||
{ |
||||
String retInfo = ""; |
||||
int timeout = 3000; |
||||
try { |
||||
if (timeoutMs > timeout) { |
||||
timeout = timeoutMs; |
||||
} |
||||
Content content2 = Request.post(url) |
||||
.connectTimeout(Timeout.ofMilliseconds(5000)) |
||||
.responseTimeout(Timeout.ofMilliseconds(timeout)) |
||||
.setHeader("Accept", "application/json") |
||||
.setHeader("Content-Type", "application/json")//设置header信息
|
||||
.bodyString(cmdBody, ContentType.APPLICATION_JSON) |
||||
.execute() |
||||
.returnContent(); |
||||
retInfo = content2.asString(); |
||||
} |
||||
catch (IOException ioException) |
||||
{ |
||||
|
||||
} |
||||
return retInfo; |
||||
} |
||||
|
||||
public String PushToWebWithjson( String url , String body) |
||||
{ |
||||
return PushToWebWithjson(url,body,3000); |
||||
} |
||||
|
||||
public boolean successIsTrue(String strResult) |
||||
{ |
||||
boolean bRet = false; |
||||
try { |
||||
ObjectMapper objectMapper = new ObjectMapper(); |
||||
JsonNode jsonNode = objectMapper.readTree(strResult); |
||||
|
||||
JsonNode result = jsonNode.get("result"); |
||||
if (result != null) |
||||
{ |
||||
JsonNode success = result.get("success"); |
||||
if (success != null) |
||||
{ |
||||
if (success.asBoolean()) |
||||
{ |
||||
bRet = true; |
||||
} |
||||
} |
||||
} |
||||
} catch (JsonProcessingException e) { |
||||
//throw new RuntimeException(e);
|
||||
} |
||||
return bRet; |
||||
} |
||||
//bShowTime 表示是否显示当前时间
|
||||
public static void logInfoWithTime(String info, boolean bShowTime) //日志信息
|
||||
{ |
||||
Calendar cal = null; |
||||
if (bShowTime) { |
||||
cal = (Calendar.getInstance()); |
||||
System.out.print(cal.getTime().toString()); |
||||
System.out.print(" "); |
||||
} |
||||
System.out.println(info); |
||||
String filepath = "wglog.log"; |
||||
try { |
||||
BufferedWriter out = new BufferedWriter(new FileWriter(filepath,true)); |
||||
if (bShowTime) { |
||||
out.write(cal.getTime().toString()); |
||||
out.write(" "); |
||||
} |
||||
out.write(info); |
||||
out.newLine(); |
||||
out.close(); |
||||
} |
||||
catch (IOException e) { |
||||
|
||||
} |
||||
} |
||||
|
||||
public static void logInfo(String info) //日志信息
|
||||
{ |
||||
Calendar cal = (Calendar.getInstance()); |
||||
System.out.print(cal.getTime().toString()); |
||||
System.out.print(" "); |
||||
System.out.println(info); |
||||
String filepath = "wglog.log"; |
||||
try { |
||||
BufferedWriter out = new BufferedWriter(new FileWriter(filepath,true)); |
||||
out.write(cal.getTime().toString()); |
||||
out.write(" "); |
||||
out.write(info); |
||||
out.newLine(); |
||||
out.close(); |
||||
} |
||||
catch (IOException e) { |
||||
|
||||
} |
||||
} |
||||
public static void logInfoInJson(String info) //日志信息
|
||||
{ |
||||
try { |
||||
logInfo((new ObjectMapper()).readTree(info).toPrettyString()); |
||||
} catch (JsonProcessingException e) { |
||||
//throw new RuntimeException(e);
|
||||
} |
||||
} |
||||
public static void logInfoInJsonWithoutTime(String info) //日志信息
|
||||
{ |
||||
try { |
||||
logInfoWithTime((new ObjectMapper()).readTree(info).toPrettyString(),false); |
||||
} catch (JsonProcessingException e) { |
||||
//throw new RuntimeException(e);
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,9 @@ |
||||
server: |
||||
port: 33307 |
||||
servlet: |
||||
context-path: / |
||||
controller: |
||||
server: ws://192.168.130.155:61081 |
||||
|
||||
push: |
||||
url: http://localhost:7834/test |
Loading…
Reference in new issue