| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | import cn.hutool.core.lang.Snowflake; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.aliyuncs.iot.model.v20180120.BatchGetDeviceStateResponse; |
| | | import com.aliyuncs.iot.model.v20180120.QueryDeviceDetailResponse; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.ximon.admin.manager.iot.frame.A1Frame; |
| | | import com.sandu.ximon.admin.manager.iot.frame.FrameBuilder; |
| | | import com.sandu.ximon.admin.manager.iot.frame.IRequestFrame; |
| | | import com.sandu.ximon.admin.manager.iot.frame.inner.request.A1TernaryCodeReqInnerFrame; |
| | | import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightResetReqInnerFrame; |
| | | import com.sandu.ximon.admin.manager.iot.frame.inner.request.EmptyRequestInnerFrame; |
| | | import com.sandu.ximon.admin.manager.iot.frame.inner.response.A1DeviceMacRespInnerFrame; |
| | | import com.sandu.ximon.admin.manager.iot.frame.inner.response.A1TernaryCodeRespInnerFrame; |
| | | import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonFrame; |
| | | import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame; |
| | | import com.sandu.ximon.admin.manager.iot.rrpc.enums.A1OrderEnum; |
| | | import com.sandu.ximon.admin.manager.iot.rrpc.enums.A2OrderEnum; |
| | | 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.LampPostParam; |
| | | import com.sandu.ximon.admin.security.CountSet; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.dao.domain.Client; |
| | | import com.sandu.ximon.dao.domain.LampCount; |
| | | import com.sandu.ximon.dao.domain.LampPost; |
| | | import com.sandu.ximon.dao.enums.AdministratorEnums; |
| | | import com.sandu.ximon.dao.enums.LampPostTypeEnums; |
| | | import com.sandu.ximon.dao.mapper.ClientMapper; |
| | | import com.sandu.ximon.dao.mapper.LampPostMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 已弃用 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @AllArgsConstructor |
| | | public class LampPostService extends BaseServiceImpl<LampPostMapper, LampPost> { |
| | | private final CountSet countSet; |
| | |
| | | LampPost lampPost = new LampPost(); |
| | | // lampPost.setId(snowflake.nextId()); |
| | | Long userId = SecurityUtils.getUserId(); |
| | | lampPost.setUserId(userId); |
| | | if(AdministratorEnums.CUSTOMER.getCode().equals(SecurityUtils.getClientId())){ |
| | | lampPost.setUserId(userId); |
| | | } |
| | | boolean save=true; |
| | | lampPost.setLampId(countSet.aVoid()); |
| | | lampPost.setName(lampPostParam.getName()); |
| | | lampPost.setAddress(lampPostParam.getAddress()); |
| | |
| | | lampPost.setBelongsClient(lampPostParam.getBelongsClient()); |
| | | lampPost.setClientId(clientId); |
| | | } |
| | | if (lampPostParam.getMacCode() == null) { |
| | | lampPost.setState(1); |
| | | /* if (lampPostParam.getMacCode() == null) { |
| | | lampPost.setMacCode("虚拟灯杆"); |
| | | } else { |
| | | lampPost.setMacCode(lampPostParam.getMacCode()); |
| | | } |
| | | lampPost.setState(1); |
| | | boolean save = save(lampPost); |
| | | }*/ |
| | | |
| | | if(LampPostTypeEnums.ENTITY_LIGHT_POLE.getCode().equals(lampPostParam.getDeviceType())){ |
| | | String baseMac = "baseDevice"; |
| | | A1Frame a1Frame = new A1Frame(A1OrderEnum.REQUEST_READ_DEVICE_UNIQUE_MAC.getCode(), new EmptyRequestInnerFrame()); |
| | | CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(baseMac, a1Frame); |
| | | if(commonFrame == null){ |
| | | throw new BusinessException("读取设备唯一ID失败"); |
| | | } |
| | | A1DeviceMacRespInnerFrame a1DeviceMacRespInnerFrame = new A1DeviceMacRespInnerFrame().transformFrame(commonFrame.getPayload()); |
| | | log.info(commonFrame.toString()); |
| | | String uniqueMac = a1DeviceMacRespInnerFrame.getMac(); |
| | | uniqueMac = uniqueMac.toLowerCase(); |
| | | log.info("唯一码{}",uniqueMac); |
| | | |
| | | // 2 从阿里注册 |
| | | MainBoardInvokeSyncService.getInstance().registerDevice(uniqueMac); |
| | | |
| | | // 3 获取设备详情 |
| | | QueryDeviceDetailResponse.Data deviceDetail = MainBoardInvokeSyncService.getInstance().queryDeviceDetail(uniqueMac, null); |
| | | if(deviceDetail == null){ |
| | | throw new BusinessException("注册失败"); |
| | | } |
| | | log.info(deviceDetail.toString()); |
| | | |
| | | // 4 配置Mac① |
| | | WrapResponseCommonFrame<A1TernaryCodeRespInnerFrame> productKeyResp = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(baseMac, |
| | | FrameBuilder.builderA1().innerFrame(new A1TernaryCodeReqInnerFrame(MainBoardInvokeSyncService.getInstance().getProductKey())) |
| | | .orderType(A1OrderEnum.REQUEST_SET_PRODUCT_KEY.getCode()).build(), |
| | | A1TernaryCodeRespInnerFrame.class); |
| | | A1TernaryCodeRespInnerFrame responseInnerFrame = productKeyResp.getResponseInnerFrame(); |
| | | if(!MainBoardInvokeSyncService.getInstance().getProductKey().equals(responseInnerFrame.getTernaryCode())){ |
| | | throw new BusinessException("设备配置ProductKey失败"); |
| | | } |
| | | |
| | | // 5 配置Mac② |
| | | WrapResponseCommonFrame<A1TernaryCodeRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(baseMac, |
| | | FrameBuilder.builderA1().innerFrame(new A1TernaryCodeReqInnerFrame(uniqueMac)) |
| | | .orderType(A1OrderEnum.REQUEST_SET_DEVICE_NAME.getCode()).build(), |
| | | A1TernaryCodeRespInnerFrame.class); |
| | | A1TernaryCodeRespInnerFrame deviceNameFrame = responseCommonFrame.getResponseInnerFrame(); |
| | | if(!uniqueMac.equals(deviceNameFrame.getTernaryCode())){ |
| | | throw new BusinessException("设备配置名失败"); |
| | | } |
| | | |
| | | // 6 配置Mac③ |
| | | WrapResponseCommonFrame<A1TernaryCodeRespInnerFrame> wrapResponseCommonFrame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(baseMac, |
| | | FrameBuilder.builderA1().innerFrame(new A1TernaryCodeReqInnerFrame(deviceDetail.getDeviceSecret())) |
| | | .orderType(A1OrderEnum.REQUEST_SET_DEVICE_SECRET.getCode()).build(), |
| | | A1TernaryCodeRespInnerFrame.class); |
| | | A1TernaryCodeRespInnerFrame deviceSecretFrame = wrapResponseCommonFrame.getResponseInnerFrame(); |
| | | if (!deviceDetail.getDeviceSecret().equals(deviceSecretFrame.getTernaryCode())) { |
| | | throw new BusinessException("设备密钥失败"); |
| | | } |
| | | |
| | | |
| | | // 7 重启设备 并使用新的Mac |
| | | CommonFrame rebootFrame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(baseMac, |
| | | FrameBuilder.builderA2().innerFrame(new EmptyRequestInnerFrame()) |
| | | .orderType(A2OrderEnum.REQUEST_MAIN_BOARD_RESET.getCode()).build()); |
| | | if ("00".equals(rebootFrame.getPayload())) { |
| | | lampPost.setMacCode(uniqueMac); |
| | | |
| | | save= save(lampPost); |
| | | |
| | | System.out.println("重启成功"); |
| | | } |
| | | }/*else if(LampPostTypeEnums.VIRTUAL_LIGHT_POLE.getCode().equals(lampPostParam.getDeviceType())){lampPost.setMacCode(uniqueMac); |
| | | |
| | | }*/ |
| | | |
| | | save= save(lampPost); |
| | | |
| | | |
| | | //boolean save = save(lampPost); |
| | | |
| | | if (!save) { |
| | | LampCount lampCount = lampCountService.list().get(0); |
| | | lampCount.setCount(lampCount.getCount() - 1); |
| | |
| | | lampPost.setBelongsClient(lampPostParam.getBelongsClient()); |
| | | lampPost.setClientId(clientId); |
| | | } |
| | | if (lampPostParam.getMacCode() == null) { |
| | | /* if (lampPostParam.getMacCode() == null) { |
| | | lampPost.setMacCode("虚拟灯杆"); |
| | | } else { |
| | | lampPost.setMacCode(lampPostParam.getMacCode()); |
| | | } |
| | | }*/ |
| | | return updateById(lampPost); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 删除灯杆 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | public boolean deleteLampPost(Long id){ |
| | | LampPost byId = getById(id); |
| | | if (byId == null) { |
| | | throw new BusinessException("未找到该灯杆"); |
| | | } |
| | | return removeById(id); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param group |
| | | * @param Id 编辑分组--选择灯杆 |
| | | * @param Id 编辑分组--选择灯杆 |
| | | */ |
| | | |
| | | public void setGroup(String group, List<Long> Id) { |
| | |
| | | for (Long id : Id |
| | | ) { |
| | | lampPostMapper.serGroup(group, id); |
| | | |
| | | } |
| | | |
| | | // return updateBatchById(update); |
| | |
| | | |
| | | public List<LampPost> listLamp() { |
| | | List<LampPost> list = lampPostMapper.listLamp(); |
| | | // List<LampPost> list = list(Wrappers.lambdaQuery(LampPost.class)); |
| | | // List<LampPost> list = list(Wrappers.lambdaQuery(LampPost.class)); |
| | | return list; |
| | | |
| | | } |
| | | |
| | | public List<LampPost> queryStates() { |
| | | // List<LampPost> list = list(Wrappers.lambdaQuery(LampPost.class).eq(LampPost::getClientId, SecurityUtils.getUserId())); |
| | | List<LampPost> list = list(Wrappers.lambdaQuery(LampPost.class)); |
| | | List<String> MacCodes = new ArrayList<>(); |
| | | |
| | | for (LampPost post : list) { |
| | | MacCodes.add(post.getMacCode()); |
| | | } |
| | | List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = MainBoardInvokeSyncService.getInstance().batchGetDeviceState(MacCodes); |
| | | for (LampPost post : list) { |
| | | for (BatchGetDeviceStateResponse.DeviceStatus deviceStatus : deviceStatuses) { |
| | | if(post.getMacCode().equals(deviceStatus.getDeviceName())){ |
| | | post.setOnLineState(deviceStatus.getStatus()); |
| | | } |
| | | } |
| | | // MacCodes.add(post.getMacCode()); |
| | | } |
| | | |
| | | |
| | | return list; |
| | | } |
| | | |
| | | public boolean LightReset(Long id){ |
| | | LampPost byId = getById(id); |
| | | String deviceName = byId.getMacCode(); |
| | | if (byId == null) { |
| | | throw new BusinessException("未找到该灯杆"); |
| | | } |
| | | if(byId.getMacCode().isEmpty()){ |
| | | throw new BusinessException("该灯杆Mca为空"); |
| | | } |
| | | IRequestFrame build = FrameBuilder.builderA5() |
| | | .orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()) |
| | | .innerFrame(new A5LightResetReqInnerFrame()) |
| | | .build(); |
| | | CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(deviceName, build); |
| | | System.out.println(commonFrame.toString()); |
| | | CommonFrame rebootFrame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(deviceName, |
| | | FrameBuilder.builderA2().innerFrame(new EmptyRequestInnerFrame()) |
| | | .orderType(A2OrderEnum.REQUEST_MAIN_BOARD_RESET.getCode()).build()); |
| | | if ("00".equals(rebootFrame.getPayload())) { |
| | | System.out.println("重启成功"); |
| | | } |
| | | |
| | | CommonFrame result = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(deviceName, |
| | | FrameBuilder.builderA2().innerFrame(new EmptyRequestInnerFrame()) |
| | | .orderType(A2OrderEnum.REQUEST_MAIN_BOARD_RESET.getCode()).build()); |
| | | |
| | | boolean b=false; |
| | | if ("00".equals(result.getPayload())) { |
| | | byId.setMacCode(null); |
| | | b = updateById(byId); |
| | | System.out.println("重启成功"); |
| | | } |
| | | return b; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |