张世豪
2025-12-09 32524195d474b74e48916867b2a6c2f022a40d98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package homein;
 
import denglu.UserChuShiHua;
import gecaoji.Device;
import chuankou.SerialPortAutoConnector;
import chuankou.SerialPortNativeLoader;
import set.Setsys;
import udpdell.UDPServer;
import denglu.Denglu;
import java.awt.EventQueue;
import javax.swing.JOptionPane;
 
public class Homein {
    public static void main(String[] args) {
        // 检查程序是否已经运行
        if (WenJianSuo.isAlreadyRunning()) {
            JOptionPane.showMessageDialog(null, 
                "程序已经在运行中!\n不能同时打开多个实例。", 
                "警告", 
                JOptionPane.WARNING_MESSAGE);
            System.exit(0);
            return;
        }
        
        // 添加关闭钩子,确保程序退出时释放锁
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            WenJianSuo.releaseLock();
        }));
 
        try {
            SerialPortNativeLoader.ensureLibraryPresent();
            // 初始化数据
            UserChuShiHua.initialize();
 
            Setsys setsys = new Setsys();
            setsys.initializeFromProperties();
            Device.initializeActiveDevice(setsys.getMowerId());
 
            UDPServer.startAsync();//启动数据接收线程
//            SerialPortAutoConnector.initialize();//启动串口自动连接
            
            // 显示初始数据
            System.out.println("初始用户名: " + UserChuShiHua.getProperty("userName"));
            System.out.println("初始密码: " + UserChuShiHua.getProperty("password"));
            
            // 启动登录界面
            startLoginInterface();
            
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, 
                "程序启动失败: " + e.getMessage(), 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private static void startLoginInterface() {
        // 在EDT中启动登录界面
        EventQueue.invokeLater(() -> {
            try {
                new Denglu().setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, 
                    "登录界面启动失败: " + e.getMessage(), 
                    "错误", 
                    JOptionPane.ERROR_MESSAGE);
            }
        });
    }
}