‘liusuyi’
2023-10-25 c8b6f74ecc29022a7f52a2ee35aa25eef3312f39
src/main/java/com/ard/alarm/camera/service/impl/ArdCamerasServiceImpl.java
@@ -1,56 +1,132 @@
package com.ard.alarm.camera.service.impl;
import java.util.*;
import com.ard.alarm.camera.domain.ArdCameras;
import com.ard.alarm.camera.mapper.ArdCamerasMapper;
import com.ard.alarm.camera.service.IArdCamerasService;
import com.ard.utils.hiksdk.service.impl.HikClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
 * 相机设备Service业务层处理
 *
 * @author 刘苏义
 * @date 2023-02-11
 */
@Service
@Slf4j(topic = "camera")
public class ArdCamerasServiceImpl implements IArdCamerasService {
    @Resource
    private ArdCamerasMapper ardCamerasMapper;
    @PostConstruct
    public void init()
    {
        List<ArdCameras> ardCameras = ardCamerasMapper.selectArdCamerasList(new ArdCameras());
        HikClientUtil.loadHCNetSDKLib();
        HikClientUtil.loginAll(ardCameras);
    }
    /**
     * 查询相机设备
     *
     * @param id 相机设备主键
     * @return 相机设备
     */
    @Override
    public ArdCameras selectArdCamerasById(String id) {
        return ardCamerasMapper.selectArdCamerasById(id);
    }
    /**
     * 查询相机设备列表
     *
     * @param ardCameras 相机设备
     * @return 相机设备
     */
    @Override
    public List<ArdCameras> selectArdCamerasList(ArdCameras ardCameras) {
        return ardCamerasMapper.selectArdCamerasList(ardCameras);
    }
}
package com.ard.alarm.camera.service.impl;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import com.ard.alarm.camera.domain.ArdCameras;
import com.ard.alarm.camera.mapper.ArdCamerasMapper;
import com.ard.alarm.camera.service.IArdCamerasService;
import com.ard.alarm.external.domain.ArdEquipExternal;
import com.ard.alarm.external.mapper.ArdEquipExternalMapper;
import com.ard.utils.hiksdk.service.impl.HikClientUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
 * 相机设备Service业务层处理
 *
 * @author 刘苏义
 * @date 2023-02-11
 */
@Service
@Slf4j(topic = "camera")
@Order(4)
public class ArdCamerasServiceImpl implements IArdCamerasService, ApplicationRunner {
    private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    public static List<ArdCameras> ardCameraList = new ArrayList<>();
    @Resource
    private ArdCamerasMapper ardCamerasMapper;
    //初始化执行
    @Override
    public void run(ApplicationArguments args) {
        //获取小光电尝试登录
        ardCameraList = selectArdCamerasList(new ArdCameras());
        HikClientUtil.loginAllCamera(ardCameraList);
        syncCameraListTask();
    }
    /**
     * 同步相机列表任务
     * 实时获取在线的小光电
     * 刘苏义
     * 2023/8/11 9:09:27
     */
    private void syncCameraListTask() {
        scheduler.scheduleAtFixedRate(() -> {
            try {
                //region 定时同步小光电
                List<ArdCameras> newArdCameraList = selectArdCamerasList(new ArdCameras());
                //需要更新的数据
                List<ArdCameras> updateList = sameListWithDifferent(ardCameraList, newArdCameraList);
                if (updateList.size() > 0) {
                    HikClientUtil.logoutAllCamera(updateList);
                    HikClientUtil.loginAllCamera(updateList);
                    ardCameraList.clear();
                    ardCameraList.addAll(newArdCameraList);
                }
                //需要删除的数据
                List<ArdCameras> delList = diffList(ardCameraList, newArdCameraList);
                if (delList.size() > 0) {
                    HikClientUtil.logoutAllCamera(delList);
                    for (ArdCameras ardCameras : delList) {
                        ardCameraList.remove(ardCameras);
                    }
                }
                //需要新增的数据
                List<ArdCameras> inserList = diffList(newArdCameraList, ardCameraList);
                if (inserList.size() > 0) {
                    HikClientUtil.loginAllCamera(inserList);
                    for (ArdCameras ardCameras : inserList) {
                        ardCameraList.add(ardCameras);
                    }
                }
                //endregion
            } catch (Exception e) {
                log.error("同步相机任务执行出错" + e.getMessage());
            }
        }, 10, 10, TimeUnit.SECONDS);
    }
    /**
     * 查询相机设备列表
     *
     * @param ardCamera 相机设备
     * @return 相机设备
     */
    @Override
    public List<ArdCameras> selectArdCamerasList(ArdCameras ardCamera) {
        ardCamera.setGdType("0");
        ardCamera.setFactory("1");
        Wrapper<ArdCameras> queryWrapper=new QueryWrapper<>(ardCamera);
        return ardCamerasMapper.selectList(queryWrapper);
    }
    //  求两个对象List的差集
    private List<ArdCameras> diffList(List<ArdCameras> oldArrayList, List<ArdCameras> newArrayList) {
        List<ArdCameras> resultList = oldArrayList.stream()
                .filter(item -> !newArrayList.stream().map(e -> e.getId()).collect(Collectors.toList()).contains(item.getId()))
                .collect(Collectors.toList());
        return resultList;
    }
    //求两个对象List的交集字段不同Id相同
    private List<ArdCameras> sameListWithDifferent(List<ArdCameras> oldList, List<ArdCameras> newList) {
        List<ArdCameras> differentFieldsList = newList.stream()
                .filter(newItem -> {
                    ArdCameras oldItem = oldList.stream()
                            .filter(item -> item.getId().equals(newItem.getId()))
                            .findFirst()
                            .orElse(null);
                    return oldItem == null || !Objects.equals(oldItem.getUpdateTime(), newItem.getUpdateTime());
                })
                .collect(Collectors.toList());
        return differentFieldsList;
    }
}