Преглед изворни кода

[功能修复]实时状态查询

liuf пре 9 месеци
родитељ
комит
c1d9003036

+ 61 - 0
src/main/java/com/tmzn/devicelinkykc/frameMsg/frameType/RealTimeStatusPushFrame.java

@@ -31,6 +31,67 @@ public class RealTimeStatusPushFrame {
     @Autowired
     private RedisCache redisCache;
 
+
+    public void deviceStatusPush(byte[] responeMsg,DeviceConnectionMsg deviceConnectionMsg, byte[] trans, String pileCode, byte guns, byte gunsStatus, byte gunsInsert, double voltage, double power, BigDecimal elec, BigDecimal money, int time) {
+        byte[] encrypt = new byte[0];
+
+
+        int encFlag = DeviceSendYkc.UPLOAD_DEVICE_STATUS_RESPONSE.getEncryptFlag();
+        if(deviceConnectionMsg.getIs18()){
+            try {
+                byte[] send = upStatusDevice(deviceConnectionMsg,trans, pileCode, guns, gunsStatus, gunsInsert, voltage, power, elec, money, time);
+                log.info("deviceStatusPush>{}reporting>>>"+DataConversion.bytesToHexString(send),pileCode);
+//                String key = redisCache.getCacheObject(RedisConstant.KEYS+deviceConnectionMsg.getDeviceId());
+                String key = redisCache.getCacheMapValue(RedisConstant.YKC_KEY_MAP,deviceConnectionMsg.getDeviceId());
+                // log.info("deviceStatusPush>设备实时状态key>>>"+key);
+                encrypt = Encrytion.aesEncrypt(send, key.getBytes());
+//                log.info("deviceStatusPush>设备实时状态encrypt msg::>>>"+DataConversion.bytesToHexString(encrypt));
+            } catch (Exception e) {
+                log.info("pileCode:" + pileCode + " deviceStatusPush params Exception");
+                e.printStackTrace();
+            }
+        }else{
+            try {
+                byte[] send = upStatusDevice(deviceConnectionMsg,trans, pileCode, guns, gunsStatus, gunsInsert, voltage, power, elec, money, time);
+                log.info("deviceStatusPush>reporting-save>>>"+DataConversion.bytesToHexString(send));
+                // log.info("deviceStatusPush>设备实时状态key>>>"+key);
+                encrypt = send;
+                encFlag = 0;
+            } catch (Exception e) {
+                log.info("pileCode:" + pileCode + " deviceStatusPush params Exception");
+                e.printStackTrace();
+            }
+        }
+
+        if (encrypt.length > 0) {
+            byte[] bytes = FrameDataSplicing.spliceing(responeMsg, DeviceSendYkc.UPLOAD_DEVICE_STATUS_RESPONSE.getFrameType() ,encFlag, encrypt, encrypt.length, deviceConnectionMsg.getIs18());
+            try {
+                deviceConnectionMsg.getOutputStream().write(bytes,0,bytes.length);
+                deviceConnectionMsg.getOutputStream().flush();
+            } catch (IOException e) {
+                log.info("pileCode:" + pileCode + " ,realTimeStatus push frame Exception");
+
+                //调用重启
+                try{
+                    String url = "http://127.0.0.1:8063/api/device/restLogin/"+pileCode;
+                    HttpResponse execute = HttpRequest.post(url)
+                            .timeout(3000)
+                            .execute();
+                    String body = execute.body();
+                    System.out.println("reset login body:>>>"+body);
+                } catch (Exception ex) {
+                    System.out.println("reset error :>>>"+ex.toString());
+                }
+
+                e.printStackTrace();
+            }
+            deviceConnectionMsg.incrementMessageCount();
+        } else {
+            log.info("pileCode:" + pileCode + " deviceStatusPush params null");
+        }
+
+    }
+
     public void deviceStatusPush(DeviceConnectionMsg deviceConnectionMsg, byte[] trans, String pileCode, byte guns, byte gunsStatus, byte gunsInsert, double voltage, double power, BigDecimal elec, BigDecimal money, int time) {
         byte[] encrypt = new byte[0];
 

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

@@ -12,6 +12,7 @@ import com.tmzn.devicelinkykc.constant.ykc.StatusConstant;
 import com.tmzn.devicelinkykc.constant.ykc.TransConstant;
 import com.tmzn.devicelinkykc.entity.*;
 import com.tmzn.devicelinkykc.frameMsg.DataConversion;
+import com.tmzn.devicelinkykc.frameMsg.FrameDataSplicing;
 import com.tmzn.devicelinkykc.frameMsg.TransMoney;
 import com.tmzn.devicelinkykc.frameMsg.frameType.*;
 import com.tmzn.devicelinkykc.mapstruct.TransMapping;
@@ -207,6 +208,9 @@ public class YkcMsgHandle {
 
                 } else if (framType == YkcSendDevice.DEVICE_STATUS_REQUEST.getFrameType()) {
                     logger.info("↓↓↓↓↓{}读取实时监测数据",deviceConnectionMsg.getDeviceId());
+                    handleRealTimeReport(deviceConnectionMsg,respone_msg,fromMsgSeq);
+
+
 
                 } else if (framType == YkcSendDevice.START_CHARNGING_REQUEST.getFrameType() || framType == YkcSendDevice.START_CHARNGING_REQUEST_16.getFrameType()) {
                     logger.info("↓↓↓↓↓{}运营平台远程控制启机" + framType,deviceConnectionMsg.getDeviceId());
@@ -292,6 +296,108 @@ public class YkcMsgHandle {
             //登录失败等心跳包10秒后进行重试
             return false;
         }
+    }
+
+    //读取时实数据
+    private void handleRealTimeReport(DeviceConnectionMsg deviceConnectionMsg, byte[] respone_msg,byte[] fromMsgSeq){
+        String pileCode = deviceConnectionMsg.getDeviceId();
+        int port = respone_msg[7] & 0xFF; //0正常 1禁用
+        //请求获取实时数据
+        //获取枪状态
+        QueryWrapper<DeviceStatus> deviceStatusQueryWrapper = new QueryWrapper<>();
+        deviceStatusQueryWrapper.eq("pile_code", deviceConnectionMsg.getDeviceId());
+        deviceStatusQueryWrapper.eq("gun_port", port);
+        DeviceStatus deviceStatus = deviceStatusService.getOne(deviceStatusQueryWrapper);
+        if(deviceStatus==null){
+            logger.info("实时无端口信息");
+            return;
+        }
+        //不是充电中 就上报空闲
+        if(deviceStatus.getGunStatus()!=StatusConstant.CHARGING){
+            realTimeStatusPushFrame.deviceStatusPush(fromMsgSeq,deviceConnectionMsg, FrameDataSplicing.
+                    transactionNum(deviceStatus.getPileCode(), deviceConnectionMsg.getMessageCount()) , deviceStatus.getPileCode(), deviceStatus.getGunPort(), deviceStatus.getGunStatus(), deviceStatus.getInsertGunStatus(), 0, 0, zero, zero, 0);
+            return;
+        }
+
+        //上报充电中的实时状态
+        //上送充电状态
+        JSONObject statusJSON = redisCache.getCacheObject(RedisConstant.DEVICE_PORT_STATUS + deviceStatus.getDeviceImei());
+        if(statusJSON==null){
+            logger.info("实时无最近103数据{}",pileCode);
+            return;
+        }
+        //拿当前的订单去更新余额
+        QueryWrapper<OrderStatus> orderStatusQueryWrapper = new QueryWrapper<>();
+        orderStatusQueryWrapper.eq("pile_code", pileCode).eq("guns_code", port).orderByDesc("create_time").last("limit 1");
+        OrderStatus orderStatus = orderStatusService.getOne(orderStatusQueryWrapper);
+        
+        if(orderStatus==null){
+            logger.info("实时无订单{}",pileCode);
+            return;
+        }
+
+        Integer voltage;
+        Integer power;
+
+        if(port==1){
+            voltage = statusJSON.getInteger("voltage");
+        }else{
+            voltage = statusJSON.getInteger("voltage_1");
+            if(voltage==null){
+                voltage = statusJSON.getInteger("voltage");
+            }
+        }
+
+        if(port == 1){
+            power = statusJSON.getInteger("power");
+        }else{
+            power = statusJSON.getInteger("power_1");
+        }
+
+        if(power==null){
+            power = 0;
+        }
+        if(voltage==null){
+            voltage = 0;
+        }
+
+        //计算实时数据
+        long endTime = System.currentTimeMillis();
+        //算电量
+        Map<String, BigDecimal> start = transMoney.getTransData();
+
+        String transCacheKey = "ChargingTrans:"+orderStatus.getId();
+        if(redisCache.hasKey(transCacheKey)){
+            start = redisCache.getCacheObject(transCacheKey);
+        }else{
+            //缓存120秒避免重复计算电量
+            try {
+                QueryWrapper<BillingModel> billWapper = new QueryWrapper<>();
+                billWapper.in("pile_code", pileCode);
+                BillingModel billingModel = billingModelService.getOne(billWapper);
+                start = transMoney.compute(port, billingModel, orderStatus.getCreateTime(), endTime);
+            } catch (Exception e) {
+                e.printStackTrace();
+                logger.error("计算订单异常{},{}",orderStatus.getPileCode(),e.getMessage());
+                return;
+            }
+            redisCache.setCacheObject(transCacheKey, start, 90, TimeUnit.SECONDS);
+        }
+
+        BigDecimal elec = start.get("elec");
+        BigDecimal money = start.get("money");
+        if (elec.equals(new BigDecimal("0.0000"))) {
+            elec = elec.add(new BigDecimal("0.0001").setScale(4, BigDecimal.ROUND_DOWN));
+        }
+        logger.info("{}-{}-{}充电中=>实时elec:{}>>>实时金额>>>{}=>p:{},v:{}" ,orderStatus.getPileCode(),port,elec,orderStatus.getGunsCode(),money,power,voltage);
+
+        int mi = (int) (System.currentTimeMillis() - orderStatus.getCreateTime()) / 1000 / 60;
+        realTimeStatusPushFrame.deviceStatusPush(fromMsgSeq,deviceConnectionMsg, orderStatus.getTransOrder(), deviceStatus.getPileCode(), deviceStatus.getGunPort(), deviceStatus.getGunStatus(), deviceStatus.getInsertGunStatus(), voltage, power, elec, money, mi);
+        logger.info("↑↑↑{},{},{}充电中实时状态上报 ",orderStatus.getPileCode(),orderStatus.getGunsCode(),orderStatus.getId());
+
+
+
+
     }
 
     /**

+ 1 - 1
src/main/java/com/tmzn/devicelinkykc/taskQueue/runner/MsgHeartRunner.java

@@ -90,7 +90,7 @@ public class MsgHeartRunner {
 
         DeviceStatus oneDs = redisCache.getCacheMapValue(portStatusCacheKey, devicePileCode);
         if(oneDs==null){
-            log.info("not found ones:{}",devicePileCode);
+           // log.info("not found ones:{}",devicePileCode);
             return;
         }
         boolean res = heartFrameSend.heartSend(deviceConnectionMsg, oneDs);