AptivApp.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package co.dc.aptiv;
  2. import co.dc.aptiv.mina.CollectorMina;
  3. import co.dc.aptiv.mina.ComMinaA;
  4. import co.dc.aptiv.mina.PLCMina;
  5. import co.dc.aptiv.mina.TestMachineCollectorMina;
  6. import co.dc.aptiv.pojo.BillPojo;
  7. import co.dc.aptiv.pojo.ConfigPojo;
  8. import co.dc.aptiv.pojo.MaterielPojo;
  9. import co.dc.aptiv.pojo.UserPojo;
  10. import co.dc.aptiv.service.*;
  11. import co.dc.aptiv.utils.CharacterConversionUtil;
  12. import co.dc.aptiv.utils.KeyboardUtil;
  13. import co.dc.aptiv.utils.ResetJButton;
  14. import co.dc.aptiv.utils.WindowUtil;
  15. import org.apache.commons.configuration.PropertiesConfiguration;
  16. import org.apache.commons.dbutils.QueryRunner;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.springframework.context.ApplicationContext;
  21. import org.springframework.context.support.ClassPathXmlApplicationContext;
  22. import javax.swing.*;
  23. import javax.swing.border.EmptyBorder;
  24. import javax.swing.plaf.FontUIResource;
  25. import java.awt.*;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.awt.event.KeyEvent;
  29. import java.awt.event.KeyListener;
  30. import java.io.BufferedReader;
  31. import java.io.File;
  32. import java.io.InputStreamReader;
  33. import java.io.RandomAccessFile;
  34. import java.net.UnknownHostException;
  35. import java.nio.channels.FileChannel;
  36. import java.nio.channels.FileLock;
  37. import java.util.Arrays;
  38. import java.util.Enumeration;
  39. /**
  40. * @describe: 主程序
  41. * @author: liuxinglong
  42. * @version: 2018/4/17 15:21
  43. */
  44. public class AptivApp {
  45. public static UserPojo session = null;
  46. private static final Log log = LogFactory.getLog (AptivApp.class);
  47. private JFrame frmv;
  48. private JPanel contentPane;
  49. private JTextField username;
  50. private JTextField password;
  51. private JLabel lblNewLabel;
  52. public static QueryRunner queryRunner;
  53. public static ConfigService configService;
  54. public static UserService userService;
  55. public static BillService billService;
  56. public static BillDetailService billDetailService;
  57. public static PropertiesConfiguration configuration;
  58. public static MaterielService materielService;
  59. public static QrCodeService qrcodeService;
  60. public static WindowUtil windowUtil;
  61. /**
  62. * plc
  63. */
  64. public static PLCMina plcMina;
  65. /**
  66. * 采集器
  67. */
  68. public static CollectorMina collectorMina;
  69. /**
  70. * 激光机
  71. */
  72. public static ComMinaA printMina;
  73. /**
  74. * 测试机
  75. */
  76. public static TestMachineCollectorMina testMachineCollectorMina;
  77. public static String loginToken = null;
  78. /**
  79. * 物料信息
  80. */
  81. public static MaterielPojo nowMaterielPojo;
  82. /**
  83. * 当前打印状态 false 停止 true 开始
  84. */
  85. public static boolean flag = false;
  86. /**
  87. * 单据Id
  88. */
  89. public static BillPojo billPojo;
  90. /**
  91. * 生成的二维码
  92. */
  93. public static String qrCode;
  94. /**
  95. * 激光机是否可打印
  96. */
  97. // public static boolean printStatus = true;
  98. /**
  99. * 采集器是否可采集
  100. */
  101. // public static boolean scanStatus = false;
  102. /**
  103. * 字符转换工具类
  104. */
  105. public static CharacterConversionUtil ccu = new CharacterConversionUtil ();
  106. /**
  107. * 程序入口
  108. *
  109. * @param args
  110. */
  111. public static void main (String[] args) {
  112. //检查文件锁--用于限制程序多开
  113. try {
  114. boolean runFlag = isRunning ("德尔福激光工控V1.2.exe");
  115. log.info ("is running " + runFlag);
  116. if (runFlag) {
  117. JOptionPane.showMessageDialog (null, "程序已打开,不可多开。", "提示", JOptionPane.ERROR_MESSAGE);
  118. //退出程序
  119. System.exit (0);
  120. }
  121. } catch (Exception e) {
  122. log.error ("检查文件锁异常。", e);
  123. }
  124. //新建进度条对象
  125. JWindowLoding jWindowLoding = new JWindowLoding (500, 300, Color.white, Color.green, 100, 200, 300, 15);
  126. jWindowLoding.start ();
  127. //操作配置文件
  128. String exePath = System.getProperty ("exe.path");
  129. if (StringUtils.isEmpty (exePath)) {
  130. String path = AptivApp.class.getResource ("/").getPath ();
  131. exePath = path;
  132. }
  133. System.setProperty ("exe.path", exePath + "config.properties");
  134. //进度条进度设定
  135. jWindowLoding.setProgress ("正在初始化系统配置", 5);
  136. //加载各种bean
  137. ApplicationContext cxt = new ClassPathXmlApplicationContext ("applicationContent.xml");
  138. //数据库管理
  139. queryRunner = (QueryRunner) cxt.getBean ("queryRunner");
  140. //用户管理
  141. userService = (UserService) cxt.getBean ("userService");
  142. //单据管理
  143. billService = (BillService) cxt.getBean ("billService");
  144. //单据详情管理
  145. billDetailService = (BillDetailService) cxt.getBean ("billDetailService");
  146. //PLC
  147. plcMina = (PLCMina) cxt.getBean ("plcMina");
  148. //采集器
  149. collectorMina = (CollectorMina) cxt.getBean ("collectorMina");
  150. //测试机设备管理
  151. testMachineCollectorMina = (TestMachineCollectorMina) cxt.getBean ("testMachineCollectorMina");
  152. //配置管理
  153. configuration = (PropertiesConfiguration) cxt.getBean ("configuration");
  154. //产品管理
  155. materielService = (MaterielService) cxt.getBean ("materielService");
  156. //二维码
  157. qrcodeService = (QrCodeService) cxt.getBean ("qrCodesService");
  158. windowUtil = (WindowUtil) cxt.getBean ("windowUtil");
  159. jWindowLoding.setProgress ("正在连接系统数据库", 10);
  160. //检查数据库连接
  161. boolean dbFlag = userService.checkDb ();
  162. if (dbFlag == false) {
  163. jWindowLoding.setProgress ("系统数据库连接失败", 30);
  164. JOptionPane.showMessageDialog (null, "系统数据库连接失败.", "提示", JOptionPane.ERROR_MESSAGE);
  165. System.exit (0);
  166. }
  167. //检查PLC
  168. jWindowLoding.setProgress ("正在连接PLC", 50);
  169. //tcp配置--检查设备
  170. plcMina.startConnection ();
  171. if (plcMina.isConnect () == false) {
  172. plcMina.closeConnection ();
  173. jWindowLoding.setProgress ("PLC连接失败", 90);
  174. JOptionPane.showMessageDialog (null, "PLC连接失败.", "提示", JOptionPane.ERROR_MESSAGE);
  175. System.exit (0);
  176. }
  177. //检查激光机
  178. jWindowLoding.setProgress ("正在连接激光器", 70);
  179. //tcp配置--检查设备
  180. printMina = new ComMinaA (configuration.getString ("com"), Integer.parseInt (configuration.getString ("bot")));
  181. printMina.startConnection ();
  182. if (printMina.isConnect () == false) {
  183. printMina.closeConnection ();
  184. jWindowLoding.setProgress ("激光器连接失败", 80);
  185. JOptionPane.showMessageDialog (null, "激光器连接失败.", "提示", JOptionPane.ERROR_MESSAGE);
  186. System.exit (0);
  187. }
  188. //检查采集器
  189. jWindowLoding.setProgress ("正在连接采集器", 80);
  190. //tcp配置--检查设备
  191. collectorMina.startConnection ();
  192. if (collectorMina.isConnect () == false) {
  193. collectorMina.closeConnection ();
  194. jWindowLoding.setProgress ("采集器连接失败", 90);
  195. JOptionPane.showMessageDialog (null, "采集器连接失败.", "提示", JOptionPane.ERROR_MESSAGE);
  196. System.exit (0);
  197. }
  198. jWindowLoding.setProgress ("初始化完成正在启动控制界面", 100);
  199. jWindowLoding.finish ();
  200. //加载页面
  201. EventQueue.invokeLater (new Runnable () {
  202. public void run () {
  203. try {
  204. AptivApp window = new AptivApp ();
  205. window.frmv.setVisible (true);
  206. } catch (Exception e) {
  207. log.error ("系统启动界面异常:", e);
  208. }
  209. }
  210. });
  211. }
  212. /**
  213. * Create the application.
  214. */
  215. public AptivApp () {
  216. initialize ();
  217. }
  218. /**
  219. * Initialize the contents of the frame.
  220. */
  221. public void initialize () {
  222. //禁止使用本地默认窗口样式,便于下方设置圆角窗口
  223. //JFrame.setDefaultLookAndFeelDecorated(true);
  224. frmv = new JFrame ();
  225. frmv.setUndecorated (true);
  226. frmv.setResizable (false);
  227. frmv.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  228. //程序置顶
  229. // frmv.setAlwaysOnTop(!frmv.isAlwaysOnTop());
  230. /*
  231. * 统一所有子窗口设置字体样式
  232. */
  233. FontUIResource fontRes = new FontUIResource (new Font ("微软雅黑", Font.PLAIN, 16));
  234. for (Enumeration<Object> keys = UIManager.getDefaults ().keys (); keys.hasMoreElements (); ) {
  235. Object key = keys.nextElement ();
  236. Object value = UIManager.get (key);
  237. if (value instanceof FontUIResource) {
  238. UIManager.put (key, fontRes);
  239. }
  240. }
  241. frmv.setBounds (100, 100, 420, 460);
  242. frmv.setLocationRelativeTo (null);
  243. contentPane = new JPanel () {
  244. @Override
  245. protected void paintComponent (Graphics g) {
  246. super.paintComponent (g);
  247. //登陆背景
  248. Image image = new ImageIcon (MainFrame.class.getResource ("img/loginbg.png")).getImage ();
  249. g.drawImage (image, 0, 0, this.getWidth (), this.getHeight (), this);
  250. }
  251. };
  252. contentPane.setBorder (new EmptyBorder (5, 5, 5, 5));
  253. contentPane.setBackground (Color.white);
  254. frmv.setContentPane (contentPane);
  255. contentPane.setLayout (null);
  256. /*
  257. * 关闭按钮
  258. */
  259. JButton jButton = new ResetJButton ("/co/dc/aptiv/img/close.png", "/co/dc/aptiv/img/close.png", "/co/dc/aptiv/img/close.png");
  260. jButton.addActionListener (new ActionListener () {
  261. public void actionPerformed (ActionEvent e) {
  262. System.exit (0);
  263. }
  264. });
  265. jButton.setBounds (368, 0, 52, 52);
  266. contentPane.add (jButton);
  267. /*
  268. * 用户名框
  269. */
  270. username = new JTextField ();
  271. KeyboardUtil kk2 = KeyboardUtil.getInstance ();
  272. kk2.register (username);
  273. username.setBorder (new EmptyBorder (0, 0, 0, 0));
  274. username.setBounds (120, 185, 220, 40);
  275. contentPane.add (username);
  276. username.setColumns (10);
  277. //加载最近一次登录的用户名
  278. final ConfigPojo configPojo = configService.get (1L);
  279. if (configPojo != null && StringUtils.isNotEmpty (configPojo.getLastName ())) {
  280. username.setText (configPojo.getLastName ());
  281. }
  282. /*
  283. * 密码框
  284. */
  285. password = new JPasswordField ();
  286. KeyboardUtil kk = KeyboardUtil.getInstance ();
  287. kk.register (password, true);
  288. password.setBorder (new EmptyBorder (0, 0, 0, 0));
  289. password.setColumns (10);
  290. password.setBounds (120, 240, 220, 40);
  291. contentPane.add (password);
  292. password.addKeyListener (new KeyListener () {
  293. public void keyPressed (KeyEvent e) {
  294. if (e.getKeyCode () == KeyEvent.VK_ENTER) {
  295. //执行登陆
  296. UserPojo _user = userService.checklogin (username.getText (), password.getText ());
  297. if (_user != null) {
  298. lblNewLabel.setText ("");
  299. ConfigPojo configPojo2 = new ConfigPojo ();
  300. configPojo2.setLastName (username.getText ());
  301. configService.updateConfig (configPojo2);
  302. AptivApp.session = _user;
  303. //进入下一个界面
  304. new MainFrame ().setVisible (true);
  305. frmv.dispose ();
  306. } else {
  307. //提示有误,重新登录
  308. lblNewLabel.setText ("登录名或密码有误,请重新登录.");
  309. }
  310. }
  311. }
  312. public void keyReleased (KeyEvent e) {
  313. }
  314. public void keyTyped (KeyEvent e) {
  315. }
  316. });
  317. /*
  318. * 登录按钮
  319. */
  320. JButton button = new ResetJButton ("/co/dc/aptiv/img/btn.png", "/co/dc/aptiv/img/btn-h.png", "/co/dc/aptiv/img/btn-h.png");
  321. button.addActionListener (new ActionListener () {
  322. //点击执行登陆操作
  323. public void actionPerformed (ActionEvent arg0) {
  324. //执行登陆
  325. UserPojo _user = userService.checklogin (username.getText (), password.getText ());
  326. if (_user != null) {
  327. lblNewLabel.setText ("");
  328. //保存登录名
  329. ConfigPojo configPojo = new ConfigPojo ();
  330. configPojo.setLastName (username.getText ());
  331. configService.updateConfig (configPojo);
  332. //进入下一个界面
  333. new MainFrame ().setVisible (true);
  334. AptivApp.session = _user;
  335. frmv.dispose ();
  336. } else {
  337. //提示有误,重新登录
  338. lblNewLabel.setText ("登录名或密码有误,请重新登录.");
  339. }
  340. }
  341. });
  342. button.setBounds (60, 346, 300, 50);
  343. contentPane.add (button);
  344. /*
  345. * 提示信息块
  346. */
  347. lblNewLabel = new JLabel ("");
  348. lblNewLabel.setForeground (Color.RED);
  349. lblNewLabel.setBounds (95, 320, 248, 15);
  350. contentPane.add (lblNewLabel);
  351. }
  352. /**
  353. * 检查时候有文件锁
  354. *
  355. * @param singleId
  356. * @return
  357. * @throws UnknownHostException
  358. */
  359. public static boolean isUsing (int singleId) throws UnknownHostException {
  360. try {
  361. RandomAccessFile raf = null;
  362. FileChannel channel = null;
  363. FileLock lock = null;
  364. // 在临时文件夹创建一个临时文件,锁住这个文件用来保证应用程序只有一个实例被创建.
  365. File sf = new File (System.getProperty ("java.io.tmpdir") + singleId + ".single");
  366. sf.deleteOnExit ();
  367. sf.createNewFile ();
  368. raf = new RandomAccessFile (sf, "rw");
  369. channel = raf.getChannel ();
  370. lock = channel.tryLock ();
  371. if (lock == null) {
  372. // 如果没有得到锁,则程序退出.
  373. // 没有必要手动释放锁和关闭流,当程序退出时,他们会被关闭的.
  374. return false;
  375. }
  376. return true;
  377. } catch (Exception e) {
  378. log.error ("检查时候有文件锁--异常", e);
  379. return false;
  380. }
  381. }
  382. public static boolean isRunning (String processName) {
  383. BufferedReader bufferedReader = null;
  384. try {
  385. Process proc = Runtime.getRuntime ().exec ("tasklist /FI \"IMAGENAME eq " + processName + "\"");
  386. bufferedReader = new BufferedReader (new InputStreamReader (proc.getInputStream ()));
  387. String line = null;
  388. int i = 0;
  389. while ((line = bufferedReader.readLine ()) != null) {
  390. //判断是否存在
  391. if (line.contains (processName)) {
  392. log.info ("running info " + line);
  393. i++;
  394. }
  395. }
  396. if (i > 1) {
  397. return true;
  398. }
  399. return false;
  400. } catch (Exception ex) {
  401. ex.printStackTrace ();
  402. return false;
  403. } finally {
  404. if (bufferedReader != null) {
  405. try {
  406. bufferedReader.close ();
  407. } catch (Exception ex) {
  408. }
  409. }
  410. }
  411. }
  412. public static String httpStr (String code) {
  413. if (code.indexOf ("http") != -1) {
  414. int begin = code.indexOf ("=");
  415. if (begin == -1) {
  416. begin = code.indexOf ("?");
  417. }
  418. return code.substring (begin + 1, code.length ());
  419. } else {
  420. return code;
  421. }
  422. }
  423. public void Test1 () {
  424. CharacterConversionUtil ccu = new CharacterConversionUtil ();
  425. System.out.println ("abc 转成ascii码后结果为 " + Arrays.toString (ccu.String2ASCII ("abc")));
  426. System.out.println ("abc 转成十六进制 ascii码后结果为 " + ccu.String2HexString ("abc"));
  427. System.out.println ("[97, 98, 99] 转成字符串后结果为 " + ccu.ASCII2String (new int[]{97, 98, 99}));
  428. System.out.println ("十六进制 ascii码 02 41 42 43 0D 0A 转成字符串后结果为 " + ccu.HexString2String ("02 41 42 43 0D 0A"));
  429. // System.out.println(AptivApp.qrcodeService.getQrCode(1L));
  430. //退出程序
  431. System.exit (0);
  432. }
  433. }