2021与蓝度共同重构项目,服务端
chenjiantian
2022-01-13 0c5bbd371c4c0c352d9ca53f385a7efae4b75ee8
路灯亮度控制
已修改6个文件
122 ■■■■■ 文件已修改
dao/src/main/java/com/sandu/ximon/dao/domain/Light.java 16 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
dao/src/main/java/com/sandu/ximon/dao/mapper/LightMapper.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
dao/src/main/resources/mapper/LightMapper.xml 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/src/main/java/com/sandu/ximon/admin/controller/LightController.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/amqp/processor/LightDataProcessor.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java 82 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
dao/src/main/java/com/sandu/ximon/dao/domain/Light.java
@@ -16,7 +16,7 @@
@Data
public class Light implements Serializable {
    /**
     *
     *
     */
    @TableId(type = IdType.AUTO)
    private Long lightId;
@@ -32,15 +32,25 @@
    private String remark;
    /**
     * 灯1亮度百分比,0-100
     */
    private Integer lightPercent;
    /**
     * 灯2亮度百分比,0-100
     */
    private Integer light2Percent;
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    /**
     *
     *
     */
    private LocalDateTime updateTime;
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}
}
dao/src/main/java/com/sandu/ximon/dao/mapper/LightMapper.java
@@ -3,7 +3,6 @@
import com.sandu.ximon.dao.bo.LightBo;
import com.sandu.ximon.dao.domain.Light;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sandu.ximon.dao.domain.LightReportData;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@@ -13,7 +12,6 @@
 */
@Mapper
public interface LightMapper extends BaseMapper<Light> {
    /**
     * 服务器获取路灯列表
     *
@@ -22,7 +20,6 @@
     * @return 路灯列表
     */
    List<LightBo> listLight(Long clientId, String keyword);
}
dao/src/main/resources/mapper/LightMapper.xml
@@ -5,11 +5,13 @@
<mapper namespace="com.sandu.ximon.dao.mapper.LightMapper">
    <resultMap id="BaseResultMap" type="com.sandu.ximon.dao.domain.Light">
        <id property="lightId" column="light_id" jdbcType="BIGINT"/>
        <result property="deviceCode" column="device_code" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
            <id property="lightId" column="light_id" jdbcType="BIGINT"/>
            <result property="deviceCode" column="device_code" jdbcType="VARCHAR"/>
            <result property="remark" column="remark" jdbcType="VARCHAR"/>
            <result property="lightPercent" column="light_percent" jdbcType="INTEGER"/>
            <result property="light2Percent" column="light2_percent" jdbcType="INTEGER"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>
    <resultMap id="listLight" type="com.sandu.ximon.dao.bo.LightBo" extends="BaseResultMap">
ximon-admin/src/main/java/com/sandu/ximon/admin/controller/LightController.java
@@ -16,6 +16,7 @@
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
 * 路灯相关控制类
@@ -68,8 +69,8 @@
    // 亮度控制
    @PostMapping("/control")
    public ResponseVO<Object> controlBrightness(@RequestBody @Validated LightControlParam param) {
        Integer status = lightService.controlBrightness(param);
        return ResponseUtil.success(status);
    public ResponseVO<Object> controlBrightness(@RequestBody @Validated List<LightControlParam> paramList) {
        List<Map<String, Object>> list = lightService.controlBrightness(paramList);
        return ResponseUtil.success(list);
    }
}
ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/amqp/processor/LightDataProcessor.java
@@ -39,7 +39,7 @@
            A5LightHeartbeatReportInnerFrame heartbeatReportInnerFrame = new A5LightHeartbeatReportInnerFrame().transformFrame(frame.getPayload());
            SpringContextHolder.getBean(LightReportDataService.class).saveReportData(deviceName, heartbeatReportInnerFrame.getHeartBeatDataPackage());
            SpringContextHolder.getBean(LightService.class).saveLight(deviceName);
            SpringContextHolder.getBean(LightService.class).saveLight(deviceName,heartbeatReportInnerFrame.getHeartBeatDataPackage());
        } else if (A5LightReportEnum.Time_Synchronized.getCode().equals(functionCode)) {
//            log.info("请求时间同步");
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
@@ -8,8 +8,8 @@
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.report.A5LightHeartbeatReportInnerFrame;
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;
@@ -22,12 +22,16 @@
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.enums.DeviceRespStatusEnums;
import com.sandu.ximon.dao.mapper.LightMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -45,20 +49,31 @@
    /**
     * 录入当前设备码的路灯数据
     * @param deviceName mac
     *
     * @param deviceName           mac
     * @param heartBeatDataPackage 心跳数据
     */
    public void saveLight(String deviceName) {
    public void saveLight(String deviceName, A5LightHeartbeatReportInnerFrame.HeartBeatDataPackage heartBeatDataPackage) {
        Boolean hasKey = redisService.hasKey(LightKey.REPORT_MAC.key(deviceName));
        if(!hasKey){
        if (!hasKey) {
            int count = count(Wrappers.lambdaQuery(Light.class).eq(Light::getDeviceCode, deviceName));
            log.info("redis查不到路灯数据{}={}",count,deviceName);
            log.info("redis查不到路灯数据{}={}", count, deviceName);
            // 当前路灯表没有录入设备吗
            if(count == 0){
            if (count == 0) {
                Light light = new Light();
                light.setDeviceCode(deviceName);
                light.setLightPercent(heartBeatDataPackage.getLightPercent());
                light.setLight2Percent(heartBeatDataPackage.getLight2Percent());
                save(light);
            }
            redisService.set(LightKey.REPORT_MAC.key(deviceName),1,LightKey.REPORT_MAC.expireSeconds());
            redisService.set(LightKey.REPORT_MAC.key(deviceName), 1, LightKey.REPORT_MAC.expireSeconds());
            log.info("新增路灯");
        } else {
            Light light = new Light();
            light.setLightPercent(heartBeatDataPackage.getLightPercent());
            light.setLight2Percent(heartBeatDataPackage.getLight2Percent());
            update(light, Wrappers.lambdaUpdate(Light.class).eq(Light::getDeviceCode, deviceName));
            log.info("更新路灯亮度");
        }
    }
@@ -72,15 +87,15 @@
        PageHelper.startPage(pageNo, pageSize);
        List<LightBo> listLight = baseMapper.listLight(clientId,keyword);
        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);
        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())){
                    if (StrUtil.equals(lightBo.getDeviceCode(), lightReportData.getDeviceCode())) {
                        lightBo.setReportTime(lightReportData.getCreateTime());
                        break;
                    }
@@ -93,7 +108,7 @@
    public boolean addRemark(LightRemarkParam param) {
        Light light = getById(param.getLightId());
        if(light == null){
        if (light == null) {
            throw new BusinessException("找不到路灯");
        }
        Light update = new Light();
@@ -104,19 +119,40 @@
    /**
     * 单灯亮度控制
     * @param param
     *
     * @return com.sandu.ximon.dao.enums.DeviceRespStatusEnums
     */
    public Integer controlBrightness(LightControlParam param) {
        A5LightBrightnessReqInnerFrame lightControlFrame = new A5LightBrightnessReqInnerFrame(param.getBrightness());
        A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), lightControlFrame);
        WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame = MainBoardInvokeSyncService.getInstance()
                .sendRRPC(param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
        if(frame == null){
            throw new BusinessException("发送请求失败");
    public List<Map<String, Object>> controlBrightness(List<LightControlParam> paramList) {
        if (CollectionUtil.isEmpty(paramList)) {
            throw new BusinessException("参数不能为空");
        }
        String responseStatus = frame.getResponseInnerFrame().getResponseStatus();
        return HexUtil.hexToInt(responseStatus);
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (LightControlParam param : paramList) {
            A5LightBrightnessReqInnerFrame lightControlFrame = new A5LightBrightnessReqInnerFrame(param.getBrightness());
            A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), lightControlFrame);
            WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame = MainBoardInvokeSyncService.getInstance()
                    .sendRRPC(param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
            Map<String, Object> map = new HashMap<>();
            map.put("deviceCode", param.getDeviceCode());
            if (frame == null) {
                map.put("status", DeviceRespStatusEnums.OTHER_ERROR.getCode());
                resultList.add(map);
                continue;
            }
            String responseStatus = frame.getResponseInnerFrame().getResponseStatus();
            int status = HexUtil.hexToInt(responseStatus);
            map.put("status", status);
            resultList.add(map);
            // 更新亮度成功,修改到数据库
            if (DeviceRespStatusEnums.SUCCESS.getCode().equals(status)) {
                Light light = new Light();
                light.setLightPercent(param.getBrightness());
                update(light, Wrappers.lambdaUpdate(Light.class).eq(Light::getDeviceCode, param.getDeviceCode()));
            }
        }
        return resultList;
    }
}