diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/ys/NetDemo.java b/wms-webapi/src/main/java/top/wms/admin/controller/ys/NetCommon.java similarity index 51% rename from wms-webapi/src/main/java/top/wms/admin/controller/ys/NetDemo.java rename to wms-webapi/src/main/java/top/wms/admin/controller/ys/NetCommon.java index 46ff538..be3af54 100644 --- a/wms-webapi/src/main/java/top/wms/admin/controller/ys/NetDemo.java +++ b/wms-webapi/src/main/java/top/wms/admin/controller/ys/NetCommon.java @@ -1,17 +1,57 @@ package top.wms.admin.controller.ys; +import cn.hutool.core.util.StrUtil; import com.sun.jna.Pointer; import com.sun.jna.ptr.IntByReference; +import lombok.extern.slf4j.Slf4j; import top.wms.admin.controller.ys.lib.NetDEVSDKLib; import java.io.File; +import java.util.Timer; +import java.util.TimerTask; -public class NetDemo { +@Slf4j +public class NetCommon { public static Pointer lpUserID = null; public static String strPicturePath = "." + File.separator + "Picture" + File.separator; public static NetDEVSDKLib netdevsdk = NetDEVSDKLib.NETDEVSDK_INSTANCE; public static int ChannelID = 0; + private static Timer captureTimer = null; + // 状态标志 + private static boolean sdkInitialized = false; + private static boolean deviceLoggedIn = false; + + /** + * 初始化SDK + */ + public static boolean initSDK() { + String strLogPath = "./sdklog/"; + boolean bRet = netdevsdk.NETDEV_SetLogPath(strLogPath); + if (!bRet) { + log.error("NETDEV_SetLogPath failed:{}", netdevsdk.NETDEV_GetLastError()); + return false; + } + + bRet = netdevsdk.NETDEV_Init(); + if (!bRet) { + log.error("Initialize failed:{}", netdevsdk.NETDEV_GetLastError()); + return false; + } + + // 创建图片保存目录 + File file = new File(strPicturePath); + if (!file.exists()) { + file.mkdir(); + } + + // 设置SDK初始化标志 + sdkInitialized = true; + log.info("SDK初始化成功"); + return true; + } + + /** * Launch the application. @@ -29,7 +69,7 @@ public class NetDemo { } // 抓拍图片 - captureImage(); + captureImage(null); // 登出设备 logoutDevice(); @@ -40,37 +80,13 @@ public class NetDemo { System.out.println("程序执行完成"); } - /** - * 初始化SDK - */ - private static void initSDK() { - String strLogPath = "./sdklog/"; - boolean bRet = netdevsdk.NETDEV_SetLogPath(strLogPath); - if (false == bRet) { - System.out.printf("NETDEV_SetLogPath failed:%d", netdevsdk.NETDEV_GetLastError()); - } - - bRet = netdevsdk.NETDEV_Init(); - if (false == bRet) { - System.out.printf("Initialize failed:%d", netdevsdk.NETDEV_GetLastError()); - return; - } - - // 创建图片保存目录 - File file = new File(strPicturePath); - if (!file.exists()) { - file.mkdir(); - } - - System.out.println("SDK初始化成功"); - } /** * 登录设备 * * @return 登录是否成功 */ - private static boolean loginDevice() { + public static boolean loginDevice() { // 设备信息 String strUserName = "admin"; String strPassword = "admin@123"; @@ -90,17 +106,19 @@ public class NetDemo { stDevLoginInfo.dwLoginProto = dwLoginProto; // 登录设备 - System.out.println("正在登录设备..."); + log.info("正在登录设备..."); lpUserID = netdevsdk.NETDEV_Login_V30(stDevLoginInfo, stSELogInfo); if (null != lpUserID) { - System.out.println("登录成功"); + // 设置设备登录标志 + deviceLoggedIn = true; + log.info("登录成功"); // 查询通道信息 queryChannels(); return true; } else { - System.out.printf("登录失败, 错误码:%d\n", netdevsdk.NETDEV_GetLastError()); + log.error("登录失败, 错误码:{}", netdevsdk.NETDEV_GetLastError()); return false; } } @@ -108,71 +126,134 @@ public class NetDemo { /** * 查询通道信息 */ - private static void queryChannels() { + public static void queryChannels() { int nMaxChlCount = 256; IntByReference dwChlCount = new IntByReference(nMaxChlCount); NetDEVSDKLib.NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S[] stVideoChlList = (NetDEVSDKLib.NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S[]) new NetDEVSDKLib.NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S().toArray(nMaxChlCount); boolean bRet = netdevsdk.NETDEV_QueryVideoChlDetailListEx(lpUserID, dwChlCount, stVideoChlList); if (bRet) { - System.out.println("通道查询成功,通道数量: " + dwChlCount.getValue()); + log.info("通道查询成功,通道数量: {}", dwChlCount.getValue()); for (int i = 0; i < dwChlCount.getValue(); i++) { System.out.println("通道ID: " + stVideoChlList[i].dwChannelID + ", 状态: " + (stVideoChlList[i].enStatus == 1 ? "在线" : "离线") + ", 名称: " + new String(stVideoChlList[i].szChnName).trim()); } } else { - System.out.printf("通道查询失败, 错误码:%d\n", netdevsdk.NETDEV_GetLastError()); + log.error("通道查询失败, 错误码:{}", netdevsdk.NETDEV_GetLastError()); } } /** * 抓拍图片 */ - private static void captureImage() { + public static boolean captureImage(String strPicPath) { if (null == lpUserID) { - System.out.println("请先登录设备"); - return; + log.error("请先登录设备"); + return false; } // 设置通道为1 ChannelID = 1; - String strPicPath = "D:\\test\\ys\\carousel.jpg"; - - // 创建保存目录 - File picDir = new File("D:\\test\\ys"); - if (!picDir.exists()) { - picDir.mkdir(); + if (StrUtil.isBlank(strPicPath)) { + strPicPath = "D:\\test\\ys\\carousel"; + // 创建保存目录 + File picDir = new File("D:\\test\\ys"); + if (!picDir.exists()) { + picDir.mkdir(); + } + } else { + File picDir = new File("D:\\test\\ys\\workOrder"); + if (!picDir.exists()) { + picDir.mkdir(); + } } // 使用NETDEV_CaptureNoPreview直接保存图片文件 - System.out.println("正在抓拍图片..."); boolean bRet = netdevsdk.NETDEV_CaptureNoPreview(lpUserID, ChannelID, 0, strPicPath, 1); if (bRet) { - System.out.println("抓图成功! 保存路径: " + strPicPath); + return true; } else { - System.out.printf("抓图失败, 错误码:%d\n", netdevsdk.NETDEV_GetLastError()); - System.out.println("请确认设备是否支持非预览抓图功能"); + log.error("抓图失败, 错误码:{}", netdevsdk.NETDEV_GetLastError()); + log.error("请确认设备是否支持非预览抓图功能"); + return false; + } + } + + /** + * 开始定时抓拍(每2秒一次) + */ + public static void startCaptureTimer() { + if (captureTimer != null) { + stopCaptureTimer(); + } + + captureTimer = new Timer(); + captureTimer.schedule(new TimerTask() { + @Override + public void run() { + captureImage(null); + } + }, 0, 2000); // 立即开始,每2秒执行一次 + log.info("定时抓拍已启动,每2秒一次"); + } + + /** + * 停止定时抓拍 + */ + public static void stopCaptureTimer() { + if (captureTimer != null) { + captureTimer.cancel(); + captureTimer = null; + log.info("定时抓拍已停止"); } } /** * 登出设备 */ - private static void logoutDevice() { + public static void logoutDevice() { + // 先停止定时抓拍 + stopCaptureTimer(); + if (null != lpUserID) { - System.out.println("正在登出设备..."); + log.info("正在登出设备..."); netdevsdk.NETDEV_Logout(lpUserID); lpUserID = null; - System.out.println("设备登出成功"); + // 清除设备登录标志 + deviceLoggedIn = false; + log.info("设备登出成功"); } } /** * 释放SDK */ - private static void cleanupSDK() { - System.out.println("正在释放SDK..."); + public static void cleanupSDK() { + // 先停止定时抓拍 + stopCaptureTimer(); + + log.info("正在释放SDK..."); netdevsdk.NETDEV_Cleanup(); - System.out.println("SDK释放成功"); + // 清除SDK初始化标志 + sdkInitialized = false; + // 清除设备登录标志 + deviceLoggedIn = false; + // 清除登录ID + lpUserID = null; + log.info("SDK释放成功"); } -} + + /** + * 检查SDK是否已初始化 + */ + public static boolean isSDKInitialized() { + return sdkInitialized; + } + + /** + * 检查设备是否已登录 + */ + public static boolean isDeviceLoggedIn() { + return deviceLoggedIn && lpUserID != null; + } +} \ No newline at end of file diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/ys/ysNetController.java b/wms-webapi/src/main/java/top/wms/admin/controller/ys/ysNetController.java index 5af381e..bfd23c9 100644 --- a/wms-webapi/src/main/java/top/wms/admin/controller/ys/ysNetController.java +++ b/wms-webapi/src/main/java/top/wms/admin/controller/ys/ysNetController.java @@ -1,11 +1,106 @@ package top.wms.admin.controller.ys; - -import org.springframework.web.bind.annotation.RestController; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; +import top.continew.starter.log.annotation.Log; +import top.continew.starter.web.model.R; @RestController +@RequestMapping("/api/ys") +@Slf4j +@Log(ignore = true) public class ysNetController { + /** + * 前端进入称重页面时调用 + */ + @GetMapping("/enter-weigh-page") + public R enterWeighPage() { + try { + // 检查SDK是否已初始化 + if (!NetCommon.isSDKInitialized()) { + // 初始化SDK + boolean initSuccess = NetCommon.initSDK(); + if (!initSuccess) { + log.error("初始化SDK失败"); + return R.fail("500", "初始化SDK失败"); + } + } + // 检查设备是否已登录 + if (!NetCommon.isDeviceLoggedIn()) { + // 登录设备 + boolean loginSuccess = NetCommon.loginDevice(); + if (!loginSuccess) { + return R.fail("500", "登录设备失败"); + } + } -} + // 开始定时抓拍(每2秒一次) + NetCommon.startCaptureTimer(); + + return R.ok("进入称重页面成功,开始定时抓拍"); + } catch (Exception e) { + return R.fail("500", "进入称重页面失败:" + e.getMessage()); + } + } + + /** + * 前端离开称重页面时调用 + */ + @GetMapping("/leave-weigh-page") + public R leaveWeighPage() { + try { + // 登出设备 + NetCommon.logoutDevice(); + + // 释放SDK + NetCommon.cleanupSDK(); + + return R.ok("离开称重页面成功,已释放资源"); + } catch (Exception e) { + return R.fail("500", "离开称重页面失败:" + e.getMessage()); + } + } + + /** + * 手动触发一次抓图 + */ + @GetMapping("/capture-image") + public R captureImage(@RequestParam String imgName) { + try { + // 检查设备是否已登录 + if (!NetCommon.isDeviceLoggedIn()) { + return R.fail("500", "设备未登录,请先进入称重页面"); + } + + // 执行抓图 + long l = System.currentTimeMillis(); + imgName = imgName + "_" + l; + boolean success = NetCommon.captureImage("D:\\test\\ys\\workOrder\\" + imgName); + if (success) { + return R.ok("http://localhost:6609/file/ys/workOrder/" + imgName + ".jpg"); + } else { + return R.fail("500", "抓图失败:抓图失败"); + } + } catch (Exception e) { + return R.fail("500", "抓图失败:" + e.getMessage()); + } + } + + /** + * 检查SDK状态 + */ + @GetMapping("/status") + public R checkStatus() { + try { + boolean isSDKInitialized = NetCommon.isSDKInitialized(); + boolean isDeviceLoggedIn = NetCommon.isDeviceLoggedIn(); + + return R.ok("SDK初始化状态:" + (isSDKInitialized ? "已初始化" : "未初始化") + "," + + "设备登录状态:" + (isDeviceLoggedIn ? "已登录" : "未登录")); + } catch (Exception e) { + return R.fail("500", "检查状态失败:" + e.getMessage()); + } + } +} \ No newline at end of file