主干合并 #1

Merged
jianguo merged 6 commits from dev into master 2024-06-03 14:01:09 +08:00
6 changed files with 143 additions and 7 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

View File

@ -6,7 +6,7 @@
<groupId>org.aohe</groupId>
<artifactId>sane-service</artifactId>
<version>0.2.6</version>
<version>0.3.1</version>
<name>aohe-sane-service</name>
@ -145,6 +145,8 @@
<generateInstaller>true</generateInstaller>
<administratorRequired>false</administratorRequired>
<platform>linux</platform>
<displayName>奥诃影像扫描控件</displayName>
<description>奥诃公司为影像 WEB 应用开发的 USB 扫描仪连接驱动,请搭配 WEB 产品使用。</description>
<linuxConfig>
<generateAppImage>true</generateAppImage>
<generateDeb>true</generateDeb>

View File

@ -1,12 +1,12 @@
package org.aohe;
import com.formdev.flatlaf.FlatDarculaLaf;
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.*;
@ -18,13 +18,13 @@ public class Main {
try {
//设置窗口主题
//FlatLightLaf.setup();
UIManager.setLookAndFeel(new FlatLightLaf());
UIManager.setLookAndFeel(new FlatDarculaLaf());
} catch (UnsupportedLookAndFeelException e) {
throw new RuntimeException(e);
}
//初始化其他资源
init();
SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true));
SwingUtilities.invokeLater(() -> new NewWindow().setVisible(true));
}
@ -47,4 +47,6 @@ public class Main {
log.info("socket 服务已关闭 ");
}));
}
}

View File

@ -0,0 +1,127 @@
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 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

@ -64,6 +64,7 @@ public class CommandUtils {
public static void killUsedPort(String port){
List<LsofInfo> lsofInfos = findPort(port);
for(LsofInfo lsofInfo : lsofInfos){
log.info(" pro kill begin {}", lsofInfo);
destroy(lsofInfo.getPid());
}
}
@ -71,12 +72,14 @@ public class CommandUtils {
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);
execForStrUtf8Sudo(exec);
String s = execForStrUtf8Sudo(exec);
log.info(s);
}
/**

View File

@ -3,6 +3,7 @@ package org.aohe.core.web;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.net.BindException;
import java.net.UnknownHostException;
/**
@ -31,6 +32,7 @@ public class SocketFactory {
private static SocketServer getSocket() throws UnknownHostException {
log.info("Create Socket ...");
socketServer = new SocketServer(8997);
socketServer.setReuseAddr(true);
return socketServer;
}
@ -40,7 +42,7 @@ public class SocketFactory {
public static boolean start() {
try {
getSocket().start();
} catch (UnknownHostException e) {
} catch (Exception e){
return false;
}
log.info("socket 已启动");