From 881ec010f93179f640dc7ad92bde0a8a4e32c775 Mon Sep 17 00:00:00 2001 From: 18650502300 <18650502300@163.com> Date: Sat, 3 Aug 2024 14:16:07 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=96=B0=E5=A2=9E=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E6=97=B6=EF=BC=8C=E4=BF=9D=E5=AD=98msg=202.?= =?UTF-8?q?=20=E6=97=A0=E8=AE=A2=E5=8D=95=E5=85=B3=E9=94=81=E5=8F=AA?= =?UTF-8?q?=E5=8F=91=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/receive/ReceiveController.java | 12 +++- .../com/ruoyi/system/domain/EtCommandLog.java | 4 ++ .../system/service/IAsDeviceService.java | 5 ++ .../service/impl/AsDeviceServiceImpl.java | 57 +++++++++++++++++++ .../mapper/system/EtCommandLogMapper.xml | 6 +- 5 files changed, 81 insertions(+), 3 deletions(-) diff --git a/electripper-admin/src/main/java/com/ruoyi/web/controller/iot/receive/ReceiveController.java b/electripper-admin/src/main/java/com/ruoyi/web/controller/iot/receive/ReceiveController.java index 21d8546..460e38f 100644 --- a/electripper-admin/src/main/java/com/ruoyi/web/controller/iot/receive/ReceiveController.java +++ b/electripper-admin/src/main/java/com/ruoyi/web/controller/iot/receive/ReceiveController.java @@ -79,6 +79,8 @@ public class ReceiveController { private final Object lock = new Object(); + private boolean lockCommandSent = false; // 添加标志变量 + /** * 功能描述:第三方平台数据接收。

@@ -296,11 +298,17 @@ public class ReceiveController { // 优化轨迹,如果获取到的定位与最后一个定位相同,则不添加 optimizeRoute(jsonArray, newPoint,lon,lat,etOrder); + + // 重置关锁命令发送标志 + lockCommandSent = false; }else{ //当前无订单,并且不是管理员开锁的情况下,直接关锁 if(!isAdminUnlocking.equals("1")){ - log.info("当前无订单,并且不是管理员开锁的情况下,直接关锁--------" +logEntry.getDevName()); - asDeviceService.sendCommand(asDevice.getMac(), Token.getToken(), IotConstants.COMMAND_CLOSE+IotConstants.COMMAND_FREQUENCY_3600,"无订单关锁",null,null); + if (!lockCommandSent) { // 如果未发送过关锁命令 + log.info("当前无订单,并且不是管理员开锁的情况下,直接关锁--------" + logEntry.getDevName()); + asDeviceService.sendCommand(asDevice.getMac(), Token.getToken(), IotConstants.COMMAND_CLOSE + IotConstants.COMMAND_FREQUENCY_3600, "无订单关锁", null, null, msg); + lockCommandSent = true; // 设置标志为已发送 + } } } } diff --git a/electripper-system/src/main/java/com/ruoyi/system/domain/EtCommandLog.java b/electripper-system/src/main/java/com/ruoyi/system/domain/EtCommandLog.java index 25b6649..e045ed2 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/domain/EtCommandLog.java +++ b/electripper-system/src/main/java/com/ruoyi/system/domain/EtCommandLog.java @@ -58,4 +58,8 @@ public class EtCommandLog extends BaseEntity @Excel(name = "订单号") private String orderNo; + /** onenet上报的消息 */ + @Excel(name = "onenet上报的消息") + private String msg; + } diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java b/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java index eaabe6e..3f9ae71 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/IAsDeviceService.java @@ -177,6 +177,11 @@ public interface IAsDeviceService extends IService */ public void sendCommand(String mac, String token,String command,String type,String orderNo,String userName); + /** + * 发送命令 + */ + public void sendCommand(String mac, String token,String command,String type,String orderNo,String userName,String msg); + /** * 发送命令(带响应) */ diff --git a/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java b/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java index 20882d7..c8e0297 100644 --- a/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java +++ b/electripper-system/src/main/java/com/ruoyi/system/service/impl/AsDeviceServiceImpl.java @@ -968,6 +968,35 @@ public class AsDeviceServiceImpl extends ServiceImpl i } } + @Transactional + @Override + /** 发送命令*/ + public void sendCommand(String mac, String token,String command,String type,String orderNo,String userName,String msg) { + String param = "device_name=" + mac + "&product_id=" + productId +"&timeout=" + timeout; + String sendUrl = iotUrl+ IotConstants.ADDS_COMMAND + "?"+param; + String result = HttpUtils.sendPostWithToken(sendUrl, command, token); + log.info("【"+type+"】===>IOT请求调用结果:【{}】",result); + JSONObject paramsObj = JSON.parseObject(result); + String code = paramsObj.getString("code"); + + if ("10500".equals(code)) { // 超时 + log.info("第一次请求超时,进行第二次请求..."); + result = HttpUtils.sendPostWithToken(sendUrl, command, token); + log.info("【" + type + "】===>IOT第二次请求调用结果:【{}】", result); + paramsObj = JSON.parseObject(result); + code = paramsObj.getString("code"); + } + + asynchronousSaveLog(sendUrl,command,mac,result,type,orderNo,userName,msg); + //记录命令 + if (!HttpStatus.IOT_SUCCESS.equals(code)) + { + // 异步更新在线状态 + asynchronousUpdateOnlineStatus(mac); + throw new ServiceException(code+"-----"+ IotUtil.formatMsg(code)); + } + } + private void asynchronousUpdateOnlineStatus(String mac) { AsDevice device = asDeviceMapper.selectAsDeviceByMac(mac); //异步更新在线状态 @@ -1021,6 +1050,34 @@ public class AsDeviceServiceImpl extends ServiceImpl i }, 0, TimeUnit.SECONDS); } + /* 异步保存发送命令日志*/ + private void asynchronousSaveLog(String url,String command,String mac,String result,String type,String orderNo,String userName,String msg) { + //异步保存发送命令日志 + scheduledExecutorService.schedule(() -> { + EtCommandLog etCommandLog = new EtCommandLog(); + etCommandLog.setUrl(url); + etCommandLog.setCommand(command); + etCommandLog.setType(type); + etCommandLog.setMac(mac); + AsDevice device = asDeviceMapper.selectAsDeviceByMac(mac); + etCommandLog.setSn(device.getSn()); + etCommandLog.setResult(result); + etCommandLog.setMsg(msg); + etCommandLog.setLongitude(device.getLongitude()); + etCommandLog.setLatitude(device.getLatitude()); + etCommandLog.setCreateTime(DateUtils.getNowDate()); + JSONObject paramsObj = JSON.parseObject(result); + String code = paramsObj.getString("code"); + etCommandLog.setCallStatus(code); + etCommandLog.setOrderNo(orderNo); + etCommandLog.setCreateBy(userName); + int i = etCommandLogMapper.insertEtCommandLog(etCommandLog); + if(i>0){ + log.info("【发送命令】异步保存发送命令日志"); + } + }, 0, TimeUnit.SECONDS); + } + @Override /** 发送命令*/ public ResponseVo sendCommandWithResp(String mac, String token,String command,String type,String orderNo) { diff --git a/electripper-system/src/main/resources/mapper/system/EtCommandLogMapper.xml b/electripper-system/src/main/resources/mapper/system/EtCommandLogMapper.xml index 0782dd0..5533ae9 100644 --- a/electripper-system/src/main/resources/mapper/system/EtCommandLogMapper.xml +++ b/electripper-system/src/main/resources/mapper/system/EtCommandLogMapper.xml @@ -18,10 +18,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + - select id, url, command, type, mac, sn, result, longitude, latitude, call_status, create_by, create_time, order_no from et_command_log + select id, url, command, type, mac, sn, result, longitude, latitude, call_status, create_by, create_time, order_no, msg from et_command_log