2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-03-01 da6ea6d775461e6d56eb50cbdd79e6ba3b8f41e4
ximon-admin/src/main/java/com/sandu/ximon/admin/service/PoleService.java
@@ -35,11 +35,13 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -109,10 +111,10 @@
     *
     * @return
     */
    public List<Pole> queryStatesAndList(Integer pageNo,Integer pageSize) {
    public List<Pole> queryStatesAndList(Integer pageNo, Integer pageSize) {
        // List<LampPost> list = list(Wrappers.lambdaQuery(LampPost.class).eq(LampPost::getClientId, SecurityUtils.getUserId()));
        //List<Pole> list = list(Wrappers.lambdaQuery(Pole.class));
        PageHelper.startPage(pageNo,pageSize);
        PageHelper.startPage(pageNo, pageSize);
        List<Pole> list = new ArrayList<>();
        if (SecurityUtils.getClientId() == null) {
            list = list(Wrappers.lambdaQuery(Pole.class));
@@ -153,6 +155,15 @@
            throw new BusinessException("未找到该灯杆");
        }
        return poleBindingService.bindPole(poleId, param);
    }
    /**
     * 灯杆解绑绑定设备
     *
     * @return 是否成功
     */
    public boolean unBindPole(String deviceCode) {
        return poleBindingService.unBindPole(deviceCode);
    }
@@ -204,16 +215,15 @@
    /**
     * 给灯杆注册三元码
     */
    public boolean setMac(Long poleId) {
        Pole pole = getById(poleId);
        if (pole == null) {
    public boolean setMac(String baseMac) {
        // public boolean setMac() {
        //Pole pole = getById(poleId);
       /* if (pole == null) {
            throw new BusinessException("未找到该灯杆");
        }
        }*/
        boolean setMac = false;
        String baseMac = "baseDevice";
        //String baseMac = "baseDevice";
        A1Frame a1Frame = new A1Frame(A1OrderEnum.REQUEST_READ_DEVICE_UNIQUE_MAC.getCode(), new EmptyRequestInnerFrame());
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance()
                .sendRRPC(baseMac, a1Frame);
@@ -224,6 +234,10 @@
        log.info(commonFrame.toString());
        String uniqueMac = a1DeviceMacRespInnerFrame.getMac();
        uniqueMac = uniqueMac.toLowerCase();
        if (uniqueMac.isEmpty()) {
            throw new BusinessException("读取设备唯一ID失败!");
        }
        log.info("唯一码{}", uniqueMac);
        // 2  从阿里注册
@@ -276,25 +290,38 @@
                        FrameBuilder.builderA2().innerFrame(new EmptyRequestInnerFrame())
                                .orderType(A2OrderEnum.REQUEST_MAIN_BOARD_RESET.getCode()).build());
        if ("00".equals(rebootFrame.getPayload())) {
            pole.setDeviceCode(uniqueMac);
            //  pole.setDeviceCode(uniqueMac);
            setMac = updateById(pole);
            // setMac = updateById(pole);
            System.out.println("重启成功");
        }
        Pole pole = new Pole();
        String strh = uniqueMac.substring(uniqueMac.length() - 2, uniqueMac.length());
        pole.setDeviceCode(strh);
        pole.setPoleName(strh);
        String strm = uniqueMac.substring(0, uniqueMac.length() - 2);
        int i = Integer.parseInt(strm);
        pole.setPoleCode(generatePoleCode());
        pole.setDeviceType(i);
        setMac = save(pole);
        return setMac;
    }
    public List<String> listDeviceCodeByIds(List<Long> poleIdList) {
        if(CollectionUtil.isEmpty(poleIdList)){
        if (CollectionUtil.isEmpty(poleIdList)) {
            return null;
        }
        return list(Wrappers.lambdaQuery(Pole.class).in(Pole::getId, poleIdList).select(Pole::getDeviceCode))
                .stream().map(Pole::getDeviceCode).filter(StrUtil::isNotEmpty).collect(Collectors.toList());
        List<Pole> list = list(Wrappers.lambdaQuery(Pole.class).in(Pole::getId, poleIdList).select(Pole::getDeviceCode));
        return list
                .stream().filter(Objects::nonNull).map(Pole::getDeviceCode).filter(StrUtil::isNotEmpty).collect(Collectors.toList());
    }
    /**
     * 批量获取阿里云设备的状态
     *
     * @param deviceCodeList 阿里云设备码
     * @return 设备状态列表
     */
@@ -304,7 +331,7 @@
        List<DeviceStatus> statusList = new ArrayList<>();
        for (List<String> list : split) {
            List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = MainBoardInvokeSyncService.getInstance().batchGetDeviceState(list);
            if(CollectionUtil.isNotEmpty(deviceStatuses)){
            if (CollectionUtil.isNotEmpty(deviceStatuses)) {
                for (BatchGetDeviceStateResponse.DeviceStatus d : deviceStatuses) {
                    DeviceStatus deviceStatus = new DeviceStatus();
                    deviceStatus.setDeviceCode(d.getDeviceName());
@@ -315,4 +342,29 @@
        }
        return statusList;
    }
    /**
     * 用户绑定灯杆
     *
     * @param clientId 用户ID
     * @param poleIds  灯杆ID
     * @return
     */
    public boolean ClientBindingPole(long clientId, int[] poleIds) {
        boolean r = false;
        for (int poleId : poleIds) {
            Pole pole = getById(poleId);
            if (pole == null) {
                throw new BusinessException("灯杆不存在");
            }
            pole.setClientId(clientId);
            r = updateById(pole);
            if (!r) {
                throw new BusinessException("灯杆ID为" + poleId + "设置失败,自动停止");
            }
        }
        return r;
    }
}