2021与蓝度共同重构项目,服务端
chenjiantian
2022-01-13 66a1fea480a81799d289b16c461662df1ccf6188
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.redis.RedisService;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.dto.LightDataDto;
import com.sandu.ximon.admin.manager.iot.frame.A5Frame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightBrightnessReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.response.A5LightBrightnessRespInnerFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5OrderEnum;
import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
import com.sandu.ximon.admin.param.LightControlParam;
import com.sandu.ximon.admin.param.LightRemarkParam;
import com.sandu.ximon.admin.redis.LightKey;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.dao.bo.LightBo;
import com.sandu.ximon.dao.domain.Light;
import com.sandu.ximon.dao.domain.LightReportData;
import com.sandu.ximon.dao.mapper.LightMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author chenjiantian
 * @date 2021/12/13 16:00
 * 灯设备service
 */
@Service
@Slf4j
@AllArgsConstructor
public class LightService extends BaseServiceImpl<LightMapper, Light> {
 
    private final RedisService redisService;
    private final LightReportDataService lightReportDataService;
 
    /**
     * 录入当前设备码的路灯数据
     * @param deviceName mac
     */
    public void saveLight(String deviceName) {
        Boolean hasKey = redisService.hasKey(LightKey.REPORT_MAC.key(deviceName));
        if(!hasKey){
            int count = count(Wrappers.lambdaQuery(Light.class).eq(Light::getDeviceCode, deviceName));
            log.info("redis查不到路灯数据{}={}",count,deviceName);
            // 当前路灯表没有录入设备吗
            if(count == 0){
                Light light = new Light();
                light.setDeviceCode(deviceName);
                save(light);
            }
            redisService.set(LightKey.REPORT_MAC.key(deviceName),1,LightKey.REPORT_MAC.expireSeconds());
        }
    }
 
    /**
     * 获取路灯列表
     *
     * @return 返回组合数据dto
     */
    public List<LightBo> listLight(int pageNo, int pageSize, String keyword) {
        Long clientId = SecurityUtils.getClientId();
 
        PageHelper.startPage(pageNo, pageSize);
 
        List<LightBo> listLight = baseMapper.listLight(clientId,keyword);
 
        // 获取最近的上报时间
        List<String> deviceCodeList = listLight.stream().map(Light::getDeviceCode).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(deviceCodeList)){
            List<LightReportData> reportDataList =  lightReportDataService.getNewestReportByDeviceCode(deviceCodeList);
            for (LightBo lightBo : listLight) {
                for (LightReportData lightReportData : reportDataList) {
                    if(StrUtil.equals(lightBo.getDeviceCode(),lightReportData.getDeviceCode())){
                        lightBo.setReportTime(lightReportData.getCreateTime());
                        break;
                    }
                }
            }
        }
 
        return listLight;
    }
 
    public boolean addRemark(LightRemarkParam param) {
        Light light = getById(param.getLightId());
        if(light == null){
            throw new BusinessException("找不到路灯");
        }
        Light update = new Light();
        update.setLightId(param.getLightId());
        update.setRemark(param.getRemark());
        return updateById(update);
    }
 
    /**
     * 单灯亮度控制
     * @param param
     * @return com.sandu.ximon.dao.enums.DeviceRespStatusEnums
     */
    public Integer controlBrightness(LightControlParam param) {
        Light light = getById(param.getLightId());
        if(light == null){
            throw new BusinessException("找不到路灯");
        }
        A5LightBrightnessReqInnerFrame lightControlFrame = new A5LightBrightnessReqInnerFrame(param.getBrightness());
        A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), lightControlFrame);
        WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame = MainBoardInvokeSyncService.getInstance()
                .sendRRPC(light.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
        if(frame == null){
            throw new BusinessException("发送请求失败");
        }
        String responseStatus = frame.getResponseInnerFrame().getResponseStatus();
        return HexUtil.hexToInt(responseStatus);
    }
}