2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-06-28 c90e9e6d615b94aec96969b59d1eaecffaf8d0ef
ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3ChargingService.java
@@ -1,11 +1,12 @@
package com.sandu.ximon.admin.service;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.dao.bo.C3ChargingBo;
import com.sandu.ximon.admin.config.C3mRedisConfig;
import com.sandu.ximon.admin.manager.iot.frame.A5Frame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5C3HeartbeatReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5C3OperationReportInnerFrame;
@@ -13,16 +14,30 @@
import com.sandu.ximon.admin.manager.iot.frame.inner.request.*;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5OrderEnum;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.C3mRedisConstant;
import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
import com.sandu.ximon.admin.param.C3ChargingAddParam;
import com.sandu.ximon.admin.param.C3ChargingParam;
import com.sandu.ximon.dao.domain.*;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.LogUtils;
import com.sandu.ximon.admin.utils.RedisUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.vo.EquipmentInfomation;
import com.sandu.ximon.dao.bo.C3ChargingBo;
import com.sandu.ximon.dao.domain.C3mCharging;
import com.sandu.ximon.dao.domain.C3mChargingCharge;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.mapper.C3mChargingMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
/**
 * @author ZZQ
@@ -44,17 +59,11 @@
     * @return 是否成功
     */
    public boolean updateReportState(String deviceName, String c3Mac, String statusBit, String deviceTemperature) {
        C3mCharging one = getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getPoleDevicesCode, deviceName));
        C3mCharging one = getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getC3Mac, c3Mac));
        if (one != null) {
            one.setStatusBit(Integer.valueOf(statusBit));
            one.setDeviceTemperature(deviceTemperature);
            one.setPoleDevicesCode(deviceName);
            one.setC3Mac(c3Mac);
//            List<C3mChargingCharge> list = c3mChargingChargeService.list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, one.getC3Id()));
//            if (list.size() == 0) {//费率表中没有此充电桩数据时  添加默认费率
//                c3mChargingChargeService.initCharge((one.getC3Id()).intValue());
//            }
            return updateById(one);
        } else {
            return false;
@@ -68,35 +77,94 @@
     */
    public boolean saveReporEquipment(String deviceName, String mcuUdid, String c3Mac) {
        C3mCharging one = getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, mcuUdid));
        boolean flag;
        if (one == null) {
            //将设备信息添加到充电桩设备表
            C3mCharging c3mCharging = new C3mCharging();
            c3mCharging.setMcuUdid(mcuUdid);
            c3mCharging.setPoleDevicesCode(deviceName);
            c3mCharging.setC3Mac(c3Mac);
            boolean flag = save(c3mCharging);
            //设置费率
            C3mCharging c3mCharging1 = getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, mcuUdid));
            List<C3mChargingCharge> list = c3mChargingChargeService.list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, c3mCharging1.getC3Id()));
            if (list.size() == 0) {//费率表中没有此充电桩数据时  添加默认费率
                c3mChargingChargeService.initCharge((c3mCharging1.getC3Id()).intValue());
            }
            return flag;
            flag = save(c3mCharging);
        } else {
            //将设备信息添加到充电桩设备表
            one.setPoleDevicesCode(deviceName);
            one.setC3Mac(c3Mac);
            boolean flag = updateById(one);
            //设置费率
            C3mCharging c3mCharging1 = getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, mcuUdid));
            List<C3mChargingCharge> list = c3mChargingChargeService.list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, c3mCharging1.getC3Id()));
            if (list.size() == 0) {//费率表中没有此充电桩数据时  添加默认费率
                c3mChargingChargeService.initCharge((c3mCharging1.getC3Id()).intValue());
            }
            return flag;
            flag = updateById(one);
        }
        //设置费率
        C3mCharging c3mCharging1 = getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, mcuUdid));
        List<C3mChargingCharge> list = c3mChargingChargeService.list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, c3mCharging1.getC3Id()));
        if (list.size() == 0) {//费率表中没有此充电桩数据时  添加默认费率
            c3mChargingChargeService.initCharge((c3mCharging1.getC3Id()).intValue());
        }
//        /**
//         * 添加绑定关系开始
//         */
//        Pole pole = poleService.getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getDeviceCode, deviceName));
//        if (pole == null) {
//            Pole pole1 = new Pole();
//            pole1.setDeviceCode(deviceName);
//            pole1.setPoleName(deviceName);
//            pole1.setPoleCode(poleService.generatePoleCode());
//            poleService.save(pole1);
//        }
//
//        Long poleId = poleService.getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getDeviceCode, deviceName)).getId();
//        PoleBindingParam poleBindingParam = new PoleBindingParam();
//        poleBindingParam.setDeviceType(2);
//        poleBindingParam.setDeviceCode(mcuUdid);
//        bindingService.bindPole(poleId, poleBindingParam);
        /**
         * 添加绑定关系结束
         */
        //同步时间戳
        String date = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
        String s = SetCalendar(c3Mac, Integer.parseInt(date.substring(0, 2)), Integer.parseInt(date.substring(2, 4)),
                Integer.parseInt(date.substring(4, 6)), Integer.parseInt(date.substring(6, 8)),
                Integer.parseInt(date.substring(8, 10)), Integer.parseInt(date.substring(10, 12)));
        if (!"操作成功".equals(s)) {
            LogUtils.error("发送设置C3时间同步失败!");
            return false;
        }
        /**  跳包时间同步
         * 设置时间规则:充电时为1倍,非充电状态为1.5倍时间
         */
        Set<String> keys = RedisUtils.getBean().keys(C3mRedisConstant.C3_STATUS.getCode() + "*");
        C3mRedisConfig.reFlushTime(keys.size());
        String time1 = SetHeartbeatPacketTimeCharging(c3Mac, C3mRedisConfig.parseInt(C3mRedisConfig.HEART_BEAT_TIME) * 2, false);
        LogUtils.error("C3空闲心跳包时间同步响应结果:" + time1);
        String time2 = SetHeartbeatPacketTimeCharging(c3Mac, C3mRedisConfig.parseInt(C3mRedisConfig.HEART_BEAT_TIME), true);
        LogUtils.error("C3充电时心跳包时间同步响应结果:" + time2);
        /**
         * 获取心跳包,判断在线
         */
        A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage aPackage = ReadTheHeartbeatPackage(c3Mac);
        if (aPackage == null) {
            LogUtils.error("读取心跳包为空");
            return false;
        }
        RedisUtils redisUtils = RedisUtils.getBean();
        redisUtils.set(
                C3mRedisConstant.C3_STATUS.getCode() + c3Mac,
                JSON.toJSONString(aPackage),
                C3mRedisConfig.OUTLINE_TIME
        );
        /**
         * finally、同步结束
         */
        String end = EndOfTheSynchronization(c3Mac);
        if (!"操作成功".equals(end)) {
            LogUtils.error("C3同步结束响应结果" + end);
            return false;
        }
        return flag;
    }
    /**
@@ -105,7 +173,9 @@
     * @return
     */
    public List<C3ChargingBo> getC3ChargingListByKeyword(BaseConditionVO baseConditionVO, C3ChargingParam c3ChargingParam) {
        PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
        if (baseConditionVO != null) {
            PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
        }
        if (c3ChargingParam == null) {
            c3ChargingParam = new C3ChargingParam();
        }
@@ -115,10 +185,102 @@
            c3ChargingParam.setKeyword(c3ChargingParam.getKeyword().trim());
        }
        List<C3ChargingBo> c3ChargingBoList
                = c3mChargingMapper.listC3mChargingDto(c3ChargingParam.getKeyword());
        List<C3ChargingBo> c3ChargingBoList;
        if (SecurityUtils.getClientId() == null) {
            c3ChargingBoList = c3mChargingMapper.listC3mChargingDto(c3ChargingParam.getKeyword(), null);
        } else {
            c3ChargingBoList = c3mChargingMapper.listC3mChargingDto(c3ChargingParam.getKeyword(), SecurityUtils.getUserId());
        }
        for (C3ChargingBo one : c3ChargingBoList) {
            String s = RedisUtils.getBean().get(C3mRedisConstant.C3_STATUS.getCode() + one.getC3Mac());
            if (s != null) {
                try {
                    A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage beatDataPackage = JSON.parseObject(s, A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage.class);
                    one.setStatusBit((Integer.valueOf(beatDataPackage.getStatusBit())));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                one.setStatusBit(0);
            }
        }
        return c3ChargingBoList;
    }
    /**
     * 查询充电桩
     *
     * @return
     */
    public C3mCharging getByC3Mac(String udid) {
        C3mCharging one = getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, udid));
        String s = RedisUtils.getBean().get(C3mRedisConstant.C3_STATUS.getCode() + one.getC3Mac());
        if (s != null) {
            try {
                A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage beatDataPackage = JSON.parseObject(s, A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage.class);
                one.setStatusBit((Integer.valueOf(beatDataPackage.getStatusBit())));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            one.setStatusBit(0);
        }
        return one;
    }
    /**
     * 首页灯杆绑定信息
     *
     * @return
     */
    public EquipmentInfomation getByC3MacInfo(String udid) {
        EquipmentInfomation equipmentInfo = new EquipmentInfomation();
        equipmentInfo.setEquipmentType("C3充电桩");
        if (udid == null || udid.trim().length() == 0) {
            return equipmentInfo;
        }
        C3mCharging one = getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, udid));
        if (one != null) {
            String s = RedisUtils.getBean().get(C3mRedisConstant.C3_STATUS.getCode() + one.getC3Mac());
            if (s != null) {
                try {
                    A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage beatDataPackage = JSON.parseObject(s, A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage.class);
                    switch (beatDataPackage.getStatusBit()) {
                        case "1":
                            equipmentInfo.setEquipmentState("空闲");
                            break;
                        case "2":
                            equipmentInfo.setEquipmentState("充电中");
                            break;
                        case "3":
                            equipmentInfo.setEquipmentState("充电中断,等待服务器确认");
                            break;
                        case "4":
                            equipmentInfo.setEquipmentState("充电结束,等待服务器确认");
                            break;
                        case "5":
                            equipmentInfo.setEquipmentState("有故障");
                            break;
                        case "6":
                            equipmentInfo.setEquipmentState("与充电桩对接中");
                            break;
                        default:
                            equipmentInfo.setEquipmentState("未知");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                equipmentInfo.setEquipmentState("离线");
            }
            equipmentInfo.setEquipmentCreateTime(one.getUpdateTime());
            equipmentInfo.setEquipmentMac(one.getMcuUdid());
            equipmentInfo.setEquipmentName(one.getC3Name());
        }
        return equipmentInfo;
    }
    /**
     * 开始充电
@@ -139,6 +301,7 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-开始充电", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -172,7 +335,7 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-结束充电", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
        A5C3OperationReportInnerFrame operationReportInnerFrame = new A5C3OperationReportInnerFrame().transformFrame(commonFrame.getPayload());
@@ -202,6 +365,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-同步结束", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -230,6 +395,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-二维码", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -259,6 +426,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-版本查询", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -288,6 +457,7 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-查询心跳包间隔时间", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -317,6 +487,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-查询电压/电流常数", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -346,6 +518,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-查询地址", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -375,6 +549,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-查询故障码", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -404,6 +580,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-读取心跳包", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -437,6 +615,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-设置心跳包间隔时间", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -468,6 +648,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-设置电压/电流常数", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -508,6 +690,8 @@
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-设置地址", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -545,6 +729,7 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-设置日历", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -576,6 +761,7 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-清除故障码上报", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -607,6 +793,7 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-恢复出厂设置", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -635,7 +822,8 @@
            throw new BusinessException("C3充电桩硬件mcu_udid不存在");
        }
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(c3mCharging.getPoleDevicesCode(), a5Frame);
//        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC("32313243305008ff1a06ffff", a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(c3Mac, "C3帧-软重启", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -663,7 +851,23 @@
            c3mCharging.setC3Name(c3ChargingAddParam.getC3Name());
        }
        c3mCharging.setC3Mac(c3ChargingAddParam.getC3Mac());
        return save(c3mCharging);
        /**
         * 添加充电桩 日志记录开始
         */
        List<String> listCode = new ArrayList<>(1);
        listCode.add(c3mCharging.getMcuUdid());
        String content = "{设备code:" + c3mCharging.getMcuUdid() + "设备名:" + c3mCharging.getC3Name() +
                "}";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "添加充电桩", content);
        /**
         * 添加充电桩 日志记录结束
         */
        return
                save(c3mCharging);
    }
    public boolean updateC3ChargingName(C3ChargingAddParam c3ChargingAddParam) {
@@ -678,6 +882,21 @@
        C3mCharging c3mCharging = new C3mCharging();
        c3mCharging.setC3Id(c3ChargingAddParam.getC3Id());
        c3mCharging.setC3Name(c3ChargingAddParam.getC3Name());
        /**
         * 修改充电桩 日志记录开始
         */
        List<String> listCode = new ArrayList<>(1);
        listCode.add(c3mCharging.getMcuUdid());
        String content = "{设备code:" + c3mCharging.getMcuUdid() + "设备原名:" + one.getC3Name() + "更改后的设备名:" + c3mCharging.getC3Name() +
                "}";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "编辑充电桩", content);
        /**
         * 修改充电桩 日志记录结束
         */
        return updateById(c3mCharging);
    }
@@ -686,6 +905,8 @@
        if (one == null) {
            throw new BusinessException("未找到绑定关系");
        }
        return getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getC3Mac, one.getDeviceCode()));
        return getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, one.getDeviceCode()));
    }
}