| | |
| | | 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.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; |
| | | 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.domain.Pole; |
| | | import com.sandu.ximon.dao.domain.PoleBinding; |
| | | 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; |
| | | |
| | | /** |
| | |
| | | |
| | | private final RedisService redisService; |
| | | private final LightReportDataService lightReportDataService; |
| | | private final PoleBindingService bindingService; |
| | | private final PoleService poleService; |
| | | |
| | | /** |
| | | * 录入当前设备码的路灯数据 |
| | | * @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("更新路灯亮度"); |
| | | } |
| | | } |
| | | |
| | |
| | | * @return 返回组合数据dto |
| | | */ |
| | | public List<LightBo> listLight(int pageNo, int pageSize, String keyword) { |
| | | Long clientId = SecurityUtils.getClientId(); |
| | | Long clientId=null; |
| | | if(SecurityUtils.getClientId()!=null){ |
| | | clientId = SecurityUtils.getClientId(); |
| | | } |
| | | |
| | | 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) { |
| | | deviceCodeList.forEach(code -> { |
| | | PoleBinding bind = bindingService.getPoleIdByMac(code); |
| | | if (bind != null&&lightBo.getDeviceCode().equals(bind.getDeviceCode())) { |
| | | Long poleId = bind.getPoleId(); |
| | | Pole pole = poleService.getById(poleId); |
| | | lightBo.setPoleId(pole.getId()); |
| | | lightBo.setPoleCode(pole.getDeviceCode()); |
| | | lightBo.setPoleName(pole.getPoleName()); |
| | | } |
| | | } |
| | | ); |
| | | for (LightReportData lightReportData : reportDataList) { |
| | | if(StrUtil.equals(lightBo.getDeviceCode(),lightReportData.getDeviceCode())){ |
| | | if (StrUtil.equals(lightBo.getDeviceCode(), lightReportData.getDeviceCode())) { |
| | | lightBo.setReportTime(lightReportData.getCreateTime()); |
| | | break; |
| | | } |
| | |
| | | return listLight; |
| | | } |
| | | |
| | | /** |
| | | * 获取单个路灯信息 |
| | | * @param deviceCode 设备码 |
| | | * @return |
| | | */ |
| | | 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); |
| | | }else { |
| | | one.setOnlineStatus(0); |
| | | } |
| | | return one; |
| | | } |
| | | |
| | | public boolean addRemark(LightRemarkParam param) { |
| | | Light light = getById(param.getLightId()); |
| | | if(light == null){ |
| | | if (light == null) { |
| | | throw new BusinessException("找不到路灯"); |
| | | } |
| | | Light update = new Light(); |
| | |
| | | update.setRemark(param.getRemark()); |
| | | return updateById(update); |
| | | } |
| | | |
| | | /** |
| | | * 单灯亮度控制 |
| | | * |
| | | * @return com.sandu.ximon.dao.enums.DeviceRespStatusEnums |
| | | */ |
| | | public List<Map<String, Object>> controlBrightness(List<LightControlParam> paramList) { |
| | | if (CollectionUtil.isEmpty(paramList)) { |
| | | throw new BusinessException("参数不能为空"); |
| | | } |
| | | |
| | | 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); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | try { |
| | | map.put("deviceCode", param.getDeviceCode()); |
| | | WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class); |
| | | 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())); |
| | | } |
| | | } catch (BusinessException e) { |
| | | map.put("status", DeviceRespStatusEnums.OTHER_ERROR.getCode()); |
| | | resultList.add(map); |
| | | } |
| | | } |
| | | |
| | | return resultList; |
| | | } |
| | | |
| | | /** |
| | | * 单灯节能率 |
| | | */ |
| | | public List<Map<String, Object>> controlEnergySaving() { |
| | | |
| | | List<LightBo> listLight = baseMapper.listLight(SecurityUtils.getUserId(), null); |
| | | |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 用户拥有的路灯 |
| | | */ |
| | | public List<Light> listLight() { |
| | | List<LightBo> listLight = baseMapper.listLight(SecurityUtils.getUserId(), null); |
| | | if (CollectionUtil.isEmpty(listLight)) { |
| | | throw new BusinessException("没有路灯数据"); |
| | | } |
| | | List<String> deviceCodeList = listLight.stream().map(LightBo::getDeviceCode).collect(Collectors.toList()); |
| | | List<LightReportData> reportDataList = lightReportDataService.list(Wrappers.<LightReportData>lambdaQuery().in(LightReportData::getDeviceCode, deviceCodeList)); |
| | | if (CollectionUtil.isEmpty(reportDataList)) { |
| | | throw new BusinessException("没有路灯数据"); |
| | | } |
| | | List<Light> lightList = new ArrayList<>(); |
| | | for (String deviceCode : deviceCodeList) { |
| | | Light light = getLight(deviceCode); |
| | | lightList.add(light); |
| | | } |
| | | return lightList; |
| | | } |
| | | |
| | | } |