Compare commits

...

16 Commits

Author SHA1 Message Date
JianGuo 38ee383033 1111 2024-07-16 16:19:41 +08:00
jianguo afea000336 webSocket版本升级
1. webSocket 版本升级
2. 端口bind error错误捕获
2024-06-11 14:13:05 +08:00
jianguo 84fe5afc62 Merge remote-tracking branch 'origin/master' 2024-06-04 16:31:47 +08:00
jianguo 23063b0d9c 1. 0.3.3 bug fix 2024-06-04 16:31:39 +08:00
jianguo b29961b0bf 1. 0.3.3 bug fix 2024-06-04 16:09:07 +08:00
jianguo b0701543b1 1. 0.3.3 bug fix 2024-06-04 15:51:16 +08:00
jianguo c192fcf954 Merge pull request '1. 0.3.2 启动锁' (#2) from dev into master
Reviewed-on: http://150.158.108.156:32012/jianguo/sane-service/pulls/2
2024-06-04 15:14:02 +08:00
jianguo bd95bbb11b 1. 0.3.2 启动锁 2024-06-04 14:58:49 +08:00
jianguo 9b1138d62a Merge pull request '主干合并' (#1) from dev into master
Reviewed-on: http://150.158.108.156:32012/jianguo/sane-service/pulls/1
2024-06-03 14:01:08 +08:00
jianguo 8f0175641f 1. 0.3.1 连接修复 2024-06-03 13:59:41 +08:00
jianguo 79da72c6ea Merge remote-tracking branch 'origin/dev' into dev 2024-05-31 16:24:33 +08:00
jianguo e9a6312913 1. 0.3.0 大版本更新 2024-05-31 16:24:28 +08:00
jianguo f6a4717dac 1. 0.3.0 大版本更新 2024-05-31 16:08:18 +08:00
jianguo 0091075137 1. youhua 2024-05-29 14:41:24 +08:00
jianguo 40e1932f0c 1. 先行预览版 2024-05-29 14:21:23 +08:00
jianguo 3bec2895b1 kill 上线 2024-05-28 17:36:27 +08:00
11 changed files with 244 additions and 24 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

14
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>org.aohe</groupId>
<artifactId>sane-service</artifactId>
<version>0.2.6</version>
<version>0.3.4</version>
<name>aohe-sane-service</name>
@ -63,7 +63,7 @@
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.3</version>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>com.formdev</groupId>
@ -71,6 +71,12 @@
<version>3.4.1</version>
<classifier>no-natives</classifier>
</dependency>
<dependency>
<groupId>io.github.sanyarnd</groupId>
<artifactId>app-locker</artifactId>
<version>1.2.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.formdev</groupId>-->
<!-- <artifactId>flatlaf</artifactId>-->
@ -145,8 +151,10 @@
<generateInstaller>true</generateInstaller>
<administratorRequired>false</administratorRequired>
<platform>linux</platform>
<displayName>奥诃影像扫描控件</displayName>
<description>奥诃公司为影像 WEB 应用开发的 USB 扫描仪连接驱动,请搭配 WEB 产品使用。</description>
<linuxConfig>
<generateAppImage>true</generateAppImage>
<!-- <generateAppImage>true</generateAppImage>-->
<generateDeb>true</generateDeb>
<generateRpm>true</generateRpm>
<wrapJar>true</wrapJar>

View File

@ -1,30 +1,43 @@
package org.aohe;
import com.formdev.flatlaf.FlatDarculaLaf;
import io.github.sanyarnd.applocker.AppLocker;
import lombok.extern.slf4j.Slf4j;
import org.aohe.core.sane.utils.SaneSessionUtils;
import org.aohe.core.swing.MainWindow;
import org.aohe.core.swing.NewWindow;
import org.aohe.core.utils.CommandUtils;
import org.aohe.core.utils.SystemUtils;
import org.aohe.core.web.SocketFactory;
import com.formdev.flatlaf.FlatLightLaf;
import javax.swing.*;
import java.nio.file.Path;
@Slf4j
public class Main {
private static final String JAVA_IO_TMPDIR = System.getProperty("java.io.tmpdir");
public static void main(String[] args) {
log.info(" 当前系统类型: {}", SystemUtils.getOsName());
AppLocker locker = AppLocker.create("aoheSaneServiceLock").setPath( Path.of(JAVA_IO_TMPDIR) ).build();
try {
//启动锁只允许启动一个
locker.lock();
//设置窗口主题
//FlatLightLaf.setup();
UIManager.setLookAndFeel(new FlatLightLaf());
UIManager.setLookAndFeel(new FlatDarculaLaf());
} catch (UnsupportedLookAndFeelException e) {
throw new RuntimeException(e);
} catch (Exception ex) {
log.error("应用已经启动,当前进程退出。",ex);
System.exit(0);
}
//初始化其他资源
init();
SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true));
SwingUtilities.invokeLater(() -> new NewWindow().setVisible(true));
}
@ -47,4 +60,6 @@ public class Main {
log.info("socket 服务已关闭 ");
}));
}
}

View File

@ -142,13 +142,13 @@ public class SaneSessionUtils {
try {
saneDevice.open();
BufferedImage bufferedImage = saneDevice.acquireImage();
File file = FileUtil.createTempFile("sane","png",true);
File file = FileUtil.createTempFile("sane-",".png",true);
ImageIO.write(bufferedImage, "png", file);
return file;
} catch (IOException | SaneException e) {
throw new RuntimeException(e);
}finally {
if(saneDevice != null){
if(saneDevice != null && saneDevice.isOpen()){
saneDevice.close();
}
}

View File

@ -1,10 +1,12 @@
package org.aohe.core.swing;
import lombok.extern.slf4j.Slf4j;
import org.aohe.core.utils.CommandUtils;
import org.aohe.core.web.SocketFactory;
import javax.swing.*;
import java.awt.*;
import java.util.List;
@Slf4j
public class MainWindow extends JFrame {
@ -55,6 +57,7 @@ public class MainWindow extends JFrame {
startButton.addActionListener(e -> {
startButton.setEnabled(false);
stopButton.setEnabled(true);
CommandUtils.killUsedPort("8997");
initSocket();
setView("服务已打开...");
serviceStatus = true;
@ -125,6 +128,11 @@ public class MainWindow extends JFrame {
}
}
public boolean checkPort(String port){
List<CommandUtils.LsofInfo> lsofInfos = CommandUtils.findPort(port);
return !lsofInfos.isEmpty();
}
public static void setView(String msg){
if(logTextArea != null){
logTextArea.append(msg +"\n");

View File

@ -0,0 +1,133 @@
package org.aohe.core.swing;
import lombok.extern.slf4j.Slf4j;
import org.aohe.core.web.SocketFactory;
import javax.swing.*;
import java.awt.*;
@Slf4j
public class NewWindow extends JFrame {
private static JTextArea logTextArea;
private static final JLabel textField = new JLabel();
public NewWindow() {
// 设置窗口标题
setTitle("扫描前端");
// 设置默认的关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口大小
setSize(400, 300);
// 居中显示窗口
setLocationRelativeTo(null);
// 初始化UI组件
initializeComponents();
}
private void initializeComponents() {
textField.setForeground(Color.GRAY);
textField.setText("服务状态:启动中...");
//初始化连接服务
initSocket();
// 示例添加一个按钮点击时向文本区域添加日志信息
JButton stopButton = new JButton("退出");
stopButton.setEnabled(true);
stopButton.addActionListener(e -> {
stopButton.setEnabled(false);
logTextArea.append("关闭服务中...\n");
//开个新线程关闭防止主UI线程卡死
new Thread( () ->{
stopSocket();
//关闭任务
sleep(1000);
textField.setForeground(Color.RED);
textField.setText("服务状态:关闭");
sleep(100);
System.exit(0);
} ).start();
});
getContentPane().add(textField, BorderLayout.NORTH);
getContentPane().add(stopButton, BorderLayout.SOUTH);
}
public void setView(String view){
getLogTextArea().append(view + "\n");
}
public static void setStaticView(String view){
if(logTextArea != null){
logTextArea.append(view + "\n");
}
}
public void initSocket() {
if(SocketFactory.start()){
setView("socket 服务启动成功");
textField.setForeground(Color.GREEN);
textField.setText("服务状态:启动");
}else{
setView("socket 服务启动失败");
setView("请检查8997端口是否被占用,或是否重复打开此程序");
setView("应用退出中...");
new Thread( () -> {
sleep(5000);
System.exit(0);
} ).start();
}
}
public void stopSocket() {
try {
if(SocketFactory.stop()){
setView("服务停止成功");
}else{
setView("服务停止失败");
}
Thread.sleep(1000);
log.info("服务关闭");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public JTextArea getLogTextArea(){
if(logTextArea == null){
initLogTextArea();
}
return logTextArea;
}
public void initLogTextArea(){
// 在这里添加你的组件初始化代码
// 创建一个JTextArea用于显示日志
logTextArea = new JTextArea();
logTextArea.setEditable(false); // 设置为不可编辑
logTextArea.setEnabled(false);
// 将JTextArea放入JScrollPane中以支持滚动
JScrollPane scrollPane = new JScrollPane(logTextArea);
// 将JScrollPane添加到窗口中
getContentPane().add(scrollPane, BorderLayout.CENTER);
}
public static void sleep(long m){
try {
Thread.sleep(m);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -2,6 +2,8 @@ package org.aohe.core.utils;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
@ -59,4 +61,60 @@ public class CommandUtils {
return result;
}
public static void killUsedPort(String port){
List<LsofInfo> lsofInfos = findPort(port);
for(LsofInfo lsofInfo : lsofInfos){
log.info(" pro kill begin {}", lsofInfo);
destroy(lsofInfo.getPid());
}
}
public static List<LsofInfo> findPort(String port){
String exec = StrUtil.format("lsof -i:{}",port);
String s = execForStrUtf8Sudo(exec);
log.info(s);
return parseCommandOutput(s);
}
public static void destroy(String pid){
String exec = StrUtil.format("kill -9 {}",pid);
String s = execForStrUtf8Sudo(exec);
log.info(s);
}
/**
* lsof 格式化
* @param output s
* @return list
*/
public static List<LsofInfo> parseCommandOutput(String output) {
List<LsofInfo> processInfos = new ArrayList<>();
String[] lines = output.split("\n");
if(lines.length <= 1){
return new ArrayList<>();
}
for (int i = 1 ; i < lines.length ; i ++) {
//从第一行开始
String[] parts = lines[i].trim().split("\\s+");
if (parts.length < 6) {
return new ArrayList<>();
}
LsofInfo processInfo = new LsofInfo();
processInfo.setPid(parts[1]);
processInfo.setType(parts[4]);
processInfos.add(processInfo);
}
return processInfos;
}
@Data
@ToString
public static class LsofInfo {
private String pid;
private String type;
}
}

View File

@ -1,18 +1,11 @@
package org.aohe.core.utils;
import cn.hutool.core.net.Ipv4Util;
import cn.hutool.core.net.NetUtil;
import org.aohe.core.sane.utils.SaneSessionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.*;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class SystemUtils {

View File

@ -11,12 +11,6 @@ import java.net.UnknownHostException;
@Slf4j
public class SocketFactory {
/**
* -- GETTER --
* get 方法
*
* @return
*/
@Getter
private static SocketServer socketServer;
@ -31,6 +25,7 @@ public class SocketFactory {
private static SocketServer getSocket() throws UnknownHostException {
log.info("Create Socket ...");
socketServer = new SocketServer(8997);
socketServer.setReuseAddr(true);
return socketServer;
}
@ -40,9 +35,10 @@ public class SocketFactory {
public static boolean start() {
try {
getSocket().start();
} catch (UnknownHostException e) {
} catch (Exception e){
return false;
}
log.info("socket ReuseAddr status {}", socketServer.isReuseAddr());
log.info("socket 已启动");
return true;
}

View File

@ -8,6 +8,7 @@ import org.aohe.core.result.R;
import org.aohe.core.sane.SaneOperational;
import org.aohe.core.sane.utils.SaneSessionUtils;
import org.aohe.core.swing.NewWindow;
import org.aohe.core.utils.SystemUtils;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
@ -65,6 +66,14 @@ public class SocketServer extends WebSocketServer {
// websocket
log.error( "System error ", ex );
}
if(ex instanceof java.net.BindException){
//无法绑定接口
NewWindow.setStaticView("端口绑定失败,请检查端口占用,程序退出中...");
new Thread( ()->{
NewWindow.sleep(3000);
System.exit(0);
}).start();
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 日志存放路径 -->
<property name="log.path" value="${HOME}/Logs/ScanService"/>
<property name="log.path" value="${HOME}/logs/ScanService"/>
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>