فهرست منبع

[功能修复]优化空闲状态上报 下单5分钟内不上报空闲

liuf 1 سال پیش
والد
کامیت
ed988272e1

+ 2 - 0
src/main/java/com/tmzn/devicelinkykc/constant/RedisConstant.java

@@ -32,6 +32,8 @@ public class RedisConstant {
     //云快充启充但是没插枪
     public static final String NO_INSERT_GUN_YKC="noInsertGunYKC";
 
+    public static final String PILE_GUN_PAY_TIME = "pileGunPayTime";
+
     /**
      *订单未校验识别
      */

+ 57 - 0
src/main/java/com/tmzn/devicelinkykc/controller/TestController.java

@@ -4,12 +4,17 @@ import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.tmzn.devicelinkykc.constant.RedisConstant;
 import com.tmzn.devicelinkykc.entity.BillingModel;
 import com.tmzn.devicelinkykc.entity.param.dto.BillingModelDTO;
 import com.tmzn.devicelinkykc.mapstruct.BillingModelMapping;
+import com.tmzn.devicelinkykc.message.DeviceMsgHandle;
+import com.tmzn.devicelinkykc.redis.RedisCache;
 import com.tmzn.devicelinkykc.service.BillingModelService;
 import com.tmzn.devicelinkykc.service.DeviceControlerService;
 import com.tmzn.devicelinkykc.transdata.entity.DeviceParam;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Controller;
@@ -18,6 +23,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 
 import java.util.HashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author xp
@@ -36,6 +44,9 @@ public class TestController {
     @Value("${billingInterface}")
     private String url;
 
+    @Autowired
+    private RedisCache redisCache;
+
     @GetMapping("/test")
     public String test(@RequestParam("method") String method, @RequestParam("imei") String imei) {
 
@@ -59,6 +70,52 @@ public class TestController {
         }
         return "ok";
     }
+    private static final Logger logger = LoggerFactory.getLogger(DeviceMsgHandle.class);
+
+    @GetMapping("/test2")
+    public String test2(@RequestParam("pileCode") String pileCode) {
+        //调用重启
+
+      //  redisCache.setCacheMapValue(RedisConstant.PILE_GUN_PAY_TIME, pileCode+"_"+1,System.currentTimeMillis());
+
+//        return "ok";
+//        //标记最后订单时间 5分钟内不上报空闲状态 避免刚下单状态还没更新就被freeTask任务给顶掉了
+
+
+        new Thread(() -> {
+            try {
+                Thread.sleep(5000); // 延迟 5 秒
+                DeviceParam dataParam = new DeviceParam();
+                dataParam.setDeviceId("123456");
+                dataParam.setCcid("123456");
+//                deviceControlerService.sendPortDetailCmd(dataParam);
+                logger.info(":主动下发103消息");
+            } catch (Exception e) {
+                logger.info(":主动下发103消息异常" + e.getMessage());
+            }
+        }).start();
+
+
+
+
+//        try{
+//            long ts = redisCache.getCacheMapValue(RedisConstant.PILE_GUN_PAY_TIME, pileCode+"_1");
+//            if(ts!=0){
+//                long ts1 = System.currentTimeMillis();
+//                if((ts1-ts)<300*1000){
+//                    return "ok";
+//                }
+//            }
+//        }catch (Exception e){
+//            return "error";
+//        }
+
+        return "ok";
+    }
+
+
+
+
 
     @GetMapping("/test1")
     public String test1(@RequestParam("pileCode") String pileCode) {

+ 40 - 11
src/main/java/com/tmzn/devicelinkykc/message/DeviceMsgHandle.java

@@ -34,6 +34,8 @@ import org.springframework.stereotype.Component;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
@@ -312,17 +314,31 @@ public class DeviceMsgHandle {
                 }
             }
 
-            //发送一条103消息 等待状态上报
-            try{
-                DeviceParam dataParam = new DeviceParam();
-                dataParam.setDeviceId(imei);
-                dataParam.setCcid(imei);
-                deviceControlerService.sendPortDetailCmd(dataParam);
-                logger.info(statusServiceOne.getPileCode() + ":主动下发103消息");
-            }catch (Exception e){
-                logger.info(statusServiceOne.getPileCode() + ":主动下发103消息异常"+e.getMessage());
-            }
-            return true;
+            new Thread(() -> {
+                try {
+                    Thread.sleep(5000); // 延迟 5 秒
+                    DeviceParam dataParam = new DeviceParam();
+                    dataParam.setDeviceId(imei);
+                    dataParam.setCcid(imei);
+                    deviceControlerService.sendPortDetailCmd(dataParam);
+                    logger.info(statusServiceOne.getPileCode() + ":延迟主动下发103消息"+imei);
+                } catch (Exception e) {
+                    logger.info(statusServiceOne.getPileCode() + ":延迟主动下发103消息异常" + e.getMessage()+imei);
+                }
+            }).start();
+
+
+//            //发送一条103消息 等待状态上报
+//            try{
+//                DeviceParam dataParam = new DeviceParam();
+//                dataParam.setDeviceId(imei);
+//                dataParam.setCcid(imei);
+//                deviceControlerService.sendPortDetailCmd(dataParam);
+//                logger.info(statusServiceOne.getPileCode() + ":主动下发103消息");
+//            }catch (Exception e){
+//                logger.info(statusServiceOne.getPileCode() + ":主动下发103消息异常"+e.getMessage());
+//            }
+//            return true;
         }
 
         if (NormalChargeConstant.KEY_END_NOTICE.equals(type)) {
@@ -577,8 +593,21 @@ public class DeviceMsgHandle {
             if((System.currentTimeMillis()- one.getCreateTime())<60*1000
                     &&one.getReasonStopCharging()!=TransConstant.APP_REMOTE_STOP){
                 logger.info("启动状态还没变化--->当前7状态不处理");
+
+//                //发送一条103消息
+//                try{
+//                    DeviceParam dataParam = new DeviceParam();
+//                    dataParam.setDeviceId(imei);
+//                    dataParam.setCcid(imei);
+//                    deviceControlerService.sendPortDetailCmd(dataParam);
+//                    logger.info(imei+ ":7状态主动下发103消息");
+//                }catch (Exception e){
+//                    logger.info(imei+ ":7状态主动下发103消息异常"+e.getMessage());
+//                }
+
                 return true;
             }
+
             //7状态:充电完成未拔枪会一直上报,只有当订单完成并回复解析订单成功,7状态当空闲状态上报
             if (one.getTransactionOrderReportingActionStatus()==StatusConstant.TRANSACTION_ORDER_REPORTING_ACTION_STATUS_OK
                     &&one.getTransactionOrderReplyStatus()==StatusConstant.TRANSACTION_ORDER_REPLY_STATUS_SUCC){

+ 5 - 0
src/main/java/com/tmzn/devicelinkykc/message/YkcMsgHandle.java

@@ -369,6 +369,11 @@ public class YkcMsgHandle {
                 deviceControlerService.startCharge(deviceStatus.getDeviceImei(), deviceStatus.getDeviceImei(), (int) guns[0],0);
             }
 
+            String gunCode = String.valueOf(guns[0]);
+            //标记最后订单时间 5分钟内不上报空闲状态 避免刚下单状态还没更新就被freeTask任务给顶掉了
+            redisCache.setCacheMapValue(RedisConstant.PILE_GUN_PAY_TIME, orderStatus.getPileCode()+"_"+gunCode,System.currentTimeMillis());
+
+
 
             //TODO:在处理设备消息的地方需要处理设备启动充电是否成功
             //TODO:保存交易流水时保存一个交易订单

+ 35 - 1
src/main/java/com/tmzn/devicelinkykc/taskQueue/queue/TaskRunner.java

@@ -172,7 +172,7 @@ public class TaskRunner {
         }
 
         QueryWrapper<OrderStatus> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("now_order_status", 0); //充电完成
+        queryWrapper.eq("now_order_status", 0); //充电中状态上报
 
 
         //查询所有充电中设备的最新的订单记录,来上报设备状态消息.........?????????????????
@@ -440,12 +440,30 @@ public class TaskRunner {
         log.info("free.deviceConnectionSize>>"+devicePileCodes.size());
         devicePileCodes.stream().forEach(devicePileCode -> {
             try{
+
+
                 if (redisCache.hasKey(RedisConstant.ONLINE_DEVICE_ONE)) {
                     DeviceStatus deviceStatus = redisCache.getCacheMapValue(RedisConstant.ONLINE_DEVICE_ONE, devicePileCode);
                     if (deviceStatus != null) {
                         if (deviceStatus.getGunStatus() == StatusConstant.CHARGING) {
                             log.info("freeMsg>>>deviceguns status>>"+deviceStatus.getGunStatus());
                         }
+
+                        //标记最后订单时间 5分钟内不上报空闲状态 避免刚下单状态还没更新就被freeTask任务给顶掉了
+                        try{
+                            long ts = redisCache.getCacheMapValue(RedisConstant.PILE_GUN_PAY_TIME, devicePileCode+"_1");
+                            if(ts!=0){
+                                long ts1 = System.currentTimeMillis();
+                                if((ts1-ts)<300*1000){
+                                    log.info("{}下单时间未超过5分钟不上报空闲状态",devicePileCode+"_1");
+                                    return;
+                                }
+                            }
+                        }catch (Exception e){
+
+                        }
+
+
                         //存在设备在线状态的上送空闲状态
                         DeviceConnectionMsg deviceConnectionMsg = map.get(devicePileCode);
                         if (deviceConnectionMsg.getLoginStatus()== Constant.DEVICE_NOT_LOGIN_STATUS){
@@ -475,7 +493,23 @@ public class TaskRunner {
                 }
                 if (redisCache.hasKey(RedisConstant.ONLINE_DEVICE_TWO)) {
                     DeviceStatus deviceStatus = redisCache.getCacheMapValue(RedisConstant.ONLINE_DEVICE_ONE, devicePileCode);
+
                     if (deviceStatus != null) {
+
+                        try{
+                            long ts = redisCache.getCacheMapValue(RedisConstant.PILE_GUN_PAY_TIME, devicePileCode+"_2");
+                            if(ts!=0){
+                                long ts1 = System.currentTimeMillis();
+                                if((ts1-ts)<300*1000){
+                                    log.info("{}下单时间未超过5分钟不主动上报空闲状态",devicePileCode+"_2");
+                                    return;
+                                }
+                            }
+                        }catch (Exception e){
+
+                        }
+
+                        //标记最后订单时间 5分钟内不上报空闲状态 避免刚下单状态还没更新就被freeTask任务给顶掉了
                         //存在设备在线状态的上送空闲状态
                         DeviceConnectionMsg deviceConnectionMsg = map.get(devicePileCode);
                         if (deviceConnectionMsg.getLoginStatus()== Constant.DEVICE_NOT_LOGIN_STATUS){