1
This commit is contained in:
103
klp-oa/scaner/pom.xml
Normal file
103
klp-oa/scaner/pom.xml
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>jar</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>org.example.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.example.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- Maven依赖 -->
|
||||
<dependency>
|
||||
<groupId>net.java.dev.jna</groupId>
|
||||
<artifactId>jna</artifactId>
|
||||
<version>5.17.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.java.dev.jna</groupId>
|
||||
<artifactId>jna-platform</artifactId>
|
||||
<version>5.17.0</version>
|
||||
</dependency>
|
||||
<!-- MQTT dependency removed -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.klp</groupId>-->
|
||||
<!-- <artifactId>mv-code-reader-ctrl-wrapper</artifactId>-->
|
||||
<!-- <version>1.0.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20210307</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.tyrus</groupId>
|
||||
<artifactId>tyrus-server</artifactId>
|
||||
<version>1.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.tyrus</groupId>
|
||||
<artifactId>tyrus-container-grizzly-server</artifactId>
|
||||
<version>1.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.tyrus</groupId>
|
||||
<artifactId>tyrus-container-servlet</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.websocket</groupId>
|
||||
<artifactId>javax.websocket-api</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
68
klp-oa/scaner/src/main/java/org/example/HttpRequestUtil.java
Normal file
68
klp-oa/scaner/src/main/java/org/example/HttpRequestUtil.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package org.example;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class HttpRequestUtil {
|
||||
/**
|
||||
* 向指定URL发送POST请求,发送JSON数据
|
||||
* @param urlStr 目标接口地址
|
||||
* @param jsonData 发送的JSON字符串
|
||||
* @return 响应内容
|
||||
* @throws Exception 网络异常
|
||||
*/
|
||||
public static String postJson(String urlStr, String jsonData) throws Exception {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
os.write(jsonData.getBytes("UTF-8"));
|
||||
}
|
||||
int code = conn.getResponseCode();
|
||||
if (code == 200) {
|
||||
try (java.io.InputStream is = conn.getInputStream()) {
|
||||
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, len);
|
||||
}
|
||||
return new String(baos.toByteArray(), "UTF-8");
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("HTTP请求失败,状态码: " + code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定URL发送GET请求
|
||||
* @param urlStr 目标接口地址
|
||||
* @return 响应内容
|
||||
* @throws Exception 网络异常
|
||||
*/
|
||||
public static String get(String urlStr) throws Exception {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("Accept", "application/json");
|
||||
conn.setDoInput(true);
|
||||
int code = conn.getResponseCode();
|
||||
if (code == 200) {
|
||||
try (java.io.InputStream is = conn.getInputStream()) {
|
||||
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, len);
|
||||
}
|
||||
return new String(baos.toByteArray(), "UTF-8");
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("HTTP GET请求失败,状态码: " + code);
|
||||
}
|
||||
}
|
||||
}
|
||||
59
klp-oa/scaner/src/main/java/org/example/WsServer.java
Normal file
59
klp-oa/scaner/src/main/java/org/example/WsServer.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package org.example;
|
||||
|
||||
import javax.websocket.OnClose;
|
||||
import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
@ServerEndpoint("/ws")
|
||||
public class WsServer {
|
||||
private static final Set<Session> sessions = new CopyOnWriteArraySet<>();
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session) {
|
||||
sessions.add(session);
|
||||
System.out.println("WebSocket连接已建立: " + session.getId());
|
||||
// 推送所有扫码枪列表给前端(使用修改后的方法名)
|
||||
// String deviceListJson = Main.getAllDevicesJson();
|
||||
// try {
|
||||
// System.out.println("发送设备列表到前端: " + deviceListJson);
|
||||
// session.getBasicRemote().sendText(deviceListJson);
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(String message, Session session) throws IOException {
|
||||
System.out.println("收到消息: " + message);
|
||||
// 可以根据前端需求,在这里处理命令(比如刷新设备列表)
|
||||
if ("refreshDevices".equals(message)) {
|
||||
// String deviceListJson = Main.getAllDevicesJson();
|
||||
// session.getBasicRemote().sendText(deviceListJson);
|
||||
} else {
|
||||
session.getBasicRemote().sendText("服务器已收到: " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void onClose(Session session) {
|
||||
sessions.remove(session);
|
||||
System.out.println("WebSocket连接已关闭: " + session.getId());
|
||||
}
|
||||
|
||||
public static void broadcast(String message) {
|
||||
for (Session session : sessions) {
|
||||
if (session.isOpen()) {
|
||||
try {
|
||||
session.getBasicRemote().sendText(message);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user