2021与蓝度共同重构项目,服务端
Van333
2022-11-19 c88f44a7d604bf91e22a5cb076018884a01a6261
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
@@ -3,6 +3,7 @@
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.iot.model.v20180120.BatchGetDeviceStateResponse;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -49,6 +50,8 @@
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import static com.sandu.ximon.admin.localMQTT.callback.StatusMqttCallBack.localMqttConnectStatusMap;
/**
 * @author chenjiantian
@@ -143,7 +146,8 @@
        // 获取最近的上报时间
        List<String> deviceCodeList = listLight.stream().map(Light::getDeviceCode).collect(Collectors.toList());
        //拆分list
//        //拆分list
        List<List<String>> split = CollectionUtil.split(deviceCodeList, 100);
        List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = null;
@@ -201,13 +205,17 @@
     */
    public Light getLight(String deviceCode) {
        Light one = getOne(Wrappers.<Light>lambdaQuery().eq(Light::getDeviceCode, deviceCode));
        Object o = redisService.get(LightKey.REPORT_MAC.key(deviceCode));
        if (o != null) {
            one.setOnlineStatus(1);
        if (one == null) {
            return null;
        } else {
            one.setOnlineStatus(0);
            Object o = redisService.get(LightKey.REPORT_MAC.key(deviceCode));
            if (o != null) {
                one.setOnlineStatus(1);
            } else {
                one.setOnlineStatus(0);
            }
            return one;
        }
        return one;
    }
    /**
@@ -260,13 +268,17 @@
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (LightControlParam param : paramList) {
            A5LightBrightnessReqInnerFrame lightControlFrame = new A5LightBrightnessReqInnerFrame(param.getBrightness(), param.getLightAddress());
            A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), lightControlFrame);
            A5LightBrightnessReqInnerFrame
                    lightControlFrame = new A5LightBrightnessReqInnerFrame(param.getBrightness(), param.getLightAddress());
            A5Frame a5Frame = new A5Frame(
                    A5OrderEnum.REQUEST_LIGHT_DATA.getCode(),
                    lightControlFrame);
            Map<String, Object> map = new HashMap<>();
            try {
                map.put("deviceCode", param.getDeviceCode());
                WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame
                        = MainBoardInvokeSyncService.getInstance().sendRRPC(param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
                        = MainBoardInvokeSyncService.getInstance().sendRRPC
                        (param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
                //存储控制帧指令
                StoreOperationRecordsUtils.storeInnerFrameData(param.getDeviceCode(), "单灯帧-亮度控制", a5Frame, frame);
@@ -932,10 +944,15 @@
    public void setCacheData() {
        List<String> collect = list().stream().map(Light::getDeviceCode).collect(Collectors.toList());
//        collect.forEach(
//                code -> {
//                    redisUtils.delete(DeviceRedisKey.LIGHT_DEVICE + code);
//                }
//        );
        List<List<String>> split = CollectionUtil.split(collect, 100);
        List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = null;
        List<RedisDeviceStatus> statusList = null;
        List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = new ArrayList<>();
        List<RedisDeviceStatus> statusList = new ArrayList<>();
        for (List<String> splist : split) {
            deviceStatuses = MainBoardInvokeSyncService.getInstance().batchGetDeviceState(splist);
            if (deviceStatuses != null) {
@@ -946,6 +963,10 @@
                        device.setStatus(0);
                    } else if ("OFFLINE".equals(deviceStatus.getStatus())) {
                        device.setStatus(1);
                    } else if ("UNACTIVE".equals(deviceStatus.getStatus())) {
                        device.setStatus(0);
                    } else if ("DISABLE".equals(deviceStatus.getStatus())) {
                        device.setStatus(0);
                    } else {
                        device.setStatus(2);
                    }
@@ -956,11 +977,10 @@
        }
        statusList.forEach(status -> {
            redisUtils.set(DeviceRedisKey.LIGHT_DEVICE + status.getDeviceId(), status);
            redisUtils.set(DeviceRedisKey.LIGHT_DEVICE + status.getDeviceId(), JSON.toJSONString(status));
        });
    }
    /**
@@ -988,14 +1008,12 @@
        }
        A5LightSetCalendarReqInnerFrame setCalendarReqInnerFrame =
                new A5LightSetCalendarReqInnerFrame(address, year, month, day, hour, min, sec);
                new A5LightSetCalendarReqInnerFrame(address, year % 100, month + 1, day, hour, min, sec);
        A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), setCalendarReqInnerFrame);
        System.out.println(a5Frame + "            -----a5Frame");
        CommonFrame commonFrame;
        commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(one.getDeviceCode(), a5Frame);
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(one.getDeviceCode(), a5Frame);
        StoreOperationRecordsUtils.storeInnerFrameData(one.getDeviceCode(), "单灯帧-设置日历", a5Frame, commonFrame);
        System.out.println(commonFrame + "            -----commonFrame");
@@ -1014,9 +1032,10 @@
    public void timeSynchronizationInitiative(String deviceCode, String lightAddress) {
        //单灯信息
        Light light = getLight(deviceCode);
        if (light == null) {
            log.error("单灯主动同步时间请求异常,单灯信息不存在!");
        }
//        if (light == null) {
//            log.error("单灯主动同步时间请求异常,单灯信息不存在!");
//            return;
//        }
        //单灯任务信息
        LightTaskPoleRelation lightTaskPoleRelation = SpringContextHolder.getBean(LightTaskPoleRelationService.class)
                .getOne(Wrappers.lambdaQuery(LightTaskPoleRelation.class)
@@ -1075,14 +1094,15 @@
                        count++;
                    }
                    if (haveTime) {
                        try {
                            long longTime = cal.getTimeInMillis() - System.currentTimeMillis();
                            log.error("睡眠时间(毫秒):" + longTime);
                            Thread.sleep(longTime);
                            SetCalendar(light.getLightId(), lightAddress);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        log.error("存在定时任务:" + light.getLightId()+","+light.getDeviceCode());
//                        try {
//                            long longTime = cal.getTimeInMillis() - System.currentTimeMillis();
//                            log.error("睡眠时间(毫秒):" + longTime);
//                            Thread.sleep(longTime);
//                            SetCalendar(light.getLightId(), lightAddress);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
                    } else {
                        log.error("灯杆ID为:" + light.getLightId() + ",灯头地址为:" + lightAddress + "没有找到可同步的时间,请检查任务!");
                    }