| | |
| | | import org.springframework.context.event.EventListener; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | @Resource |
| | | protected Validator validator; |
| | | |
| | | @Resource(name = "globalExecutor") |
| | | private ThreadPoolTaskExecutor executorService; |
| | | |
| | | @Override |
| | | public void run(ApplicationArguments args) { |
| | |
| | | |
| | | public void initCamera() { |
| | | //清空相机缓存 |
| | | redisService.deleteObject(redisService.keys(getCacheKey("*"))); |
| | | redisService.deleteObject(CacheConstants.CAMERA_LIST); |
| | | //重新加载相机到缓存 |
| | | ardCameraMapper.selectArdCameraList(new ArdCamera()).forEach(ardCamera -> { |
| | | ardCameraMapper.resetCameraLoginId(ardCamera); |
| | | redisService.setCacheObject(getCacheKey(ardCamera.getId()), ardCamera); |
| | | cameraSDKService.login(ardCamera); |
| | | redisService.setCacheMapValue(CacheConstants.CAMERA_LIST, ardCamera.getId(), ardCamera); |
| | | // 提交到线程池异步执行 |
| | | executorService.submit(() -> { |
| | | cameraSDKService.login(ardCamera); |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 设置cache key |
| | | * |
| | | * @param configKey 参数键 |
| | | * @return 缓存键key |
| | | */ |
| | | private String getCacheKey(String configKey) { |
| | | return CacheConstants.CAMERA_LIST_KEY + configKey; |
| | | } |
| | | |
| | | |
| | |
| | | ardCamera.setUserId(SecurityUtils.getUserId()); |
| | | int res = ardCameraMapper.insertArdCamera(ardCamera); |
| | | if (res > 0) { |
| | | redisService.setCacheMapValue(CacheConstants.CAMERA_LIST, ardCamera.getId(), ardCamera); |
| | | cameraSDKService.login(ardCamera);//sdk登录获取通道信息 |
| | | } |
| | | return res; |
| | |
| | | ardCamera.setUpdateBy(SecurityUtils.getUsername()); |
| | | int res = ardCameraMapper.updateArdCamera(ardCamera); |
| | | if (res > 0) { |
| | | redisService.setCacheMapValue(CacheConstants.CAMERA_LIST, ardCamera.getId(), ardCamera); |
| | | cameraSDKService.logout(ardCamera);//sdk注销 |
| | | cameraSDKService.login(ardCamera);//sdk登录获取通道信息 |
| | | } |
| | |
| | | ArdCamera camera = ardCameraMapper.selectArdCameraById(id); |
| | | if (camera != null) { |
| | | //redis中删除 |
| | | redisService.deleteObject(getCacheKey(id)); |
| | | redisService.deleteCacheMapValue(CacheConstants.CAMERA_LIST, id); |
| | | //删除当前相机的所有通道 |
| | | ardChannelService.deleteArdChannelByDeviceId(id); |
| | | //sdk注销 |
| | |
| | | int i = ardCameraMapper.deleteArdCameraById(id); |
| | | if (i > 0) { |
| | | //redis中删除 |
| | | redisService.deleteObject(getCacheKey(id)); |
| | | redisService.deleteCacheMapValue(CacheConstants.CAMERA_LIST, id); |
| | | //删除当前相机的所有通道 |
| | | ardChannelService.deleteArdChannelByDeviceId(id); |
| | | //sdk注销 |
| | |
| | | @Override |
| | | public ArdCamera checkAirportHasCamera(ArdCamera camera) { |
| | | ArdCamera info = ardCameraMapper.checkAirportHasCamera(camera); |
| | | if (StringUtils.isNotNull(info)&& !info.getId().equals(camera.getId())) { |
| | | if (StringUtils.isNotNull(info) && !info.getId().equals(camera.getId())) { |
| | | return info; |
| | | } |
| | | return null; |
| | |
| | | public void LoginEventListener(LoginEvent loginEvent) { |
| | | ArdCamera camera = loginEvent.getCamera(); |
| | | ardCameraMapper.updateArdCamera(camera); |
| | | // SDK 登录成功后写入 Redis |
| | | redisService.setCacheObject(getCacheKey(camera.getId()), camera); |
| | | } |
| | | |
| | | /** |
| | | * 同步设备通道与本地通道库信息 |
| | | * |
| | | * @author 刘苏义 |
| | | * @date 2024/8/22 14:37 |
| | | */ |
| | | @Scheduled(fixedDelay = 5000) |
| | | public void syncChannel() { |
| | | try { |
| | | log.debug("定时执行同步通道任务"); |
| | | List<ArdCamera> ardCameraList = this.selectArdCameraList(new ArdCamera()); |
| | | if (!ardCameraList.isEmpty()) { |
| | | ardCameraList |
| | | .forEach(ardCamera -> { |
| | | //SDK获取实际通道 |
| | | List<ArdChannel> ardChannelListSDK = cameraSDKService.getChannels(ardCamera); |
| | | //数据库获取通道 |
| | | ArdChannel ardChannel = new ArdChannel(); |
| | | ardChannel.setDeviceId(ardCamera.getId()); |
| | | List<ArdChannel> ardChannelListDb = ardChannelService.selectArdChannelList(ardChannel); |
| | | //同步通道 |
| | | asyncChannel(ardChannelListDb, ardChannelListSDK); |
| | | }); |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error("定时执行同步通道任务异常:{}", ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 定时执行登录失败相机重新登录 |
| | | * |
| | | * @author 刘苏义 |
| | | * @date 2024/8/22 14:37 |
| | | */ |
| | | @Scheduled(fixedDelay = 10000) |
| | | @Scheduled(cron = "0/10 * * * * ?") |
| | | public void failedReconnect() { |
| | | try { |
| | | log.debug("定时执行失败相机重连任务"); |
| | | // 从数据库取全部设备 |
| | | List<ArdCamera> cameraList = selectArdCameraList(new ArdCamera()); |
| | | ArdCamera ardCamera = new ArdCamera(); |
| | | ardCamera.setUseDataScope(false); |
| | | List<ArdCamera> cameraList = this.selectArdCameraList(ardCamera); |
| | | cameraList.stream() |
| | | // 只处理登录失败或离线的设备 |
| | | .filter(camera -> camera.getLoginId() == null || camera.getLoginId() < 0 || "0".equals(camera.getState())) |
| | |
| | | |
| | | /** |
| | | * 同步相机在线状态 |
| | | * 刘苏义 |
| | | * 2023/10/8 14:54:41 |
| | | */ |
| | | @Scheduled(fixedDelay = 5000) |
| | | public void cameraState() { |
| | | @Scheduled(cron = "0/10 * * * * ?") |
| | | public void syncCameraState() { |
| | | try { |
| | | log.debug("定时执行同步相机在线状态任务"); |
| | | //调用查询方法 |
| | | List<ArdCamera> ardCameraList = selectArdCameraList(new ArdCamera()); |
| | | ArdCamera ardCamera = new ArdCamera(); |
| | | ardCamera.setUseDataScope(false); |
| | | List<ArdCamera> ardCameraList = this.selectArdCameraList(ardCamera); |
| | | ardCameraList |
| | | .forEach(camera -> { |
| | | boolean onLineNew = cameraSDKService.isOnLine(camera); |
| | |
| | | log.error("同步相机在线状态异常:{}", ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 同步相机通道 |
| | | */ |
| | | @Scheduled(cron = "0/10 * * * * ?") |
| | | public void syncChannel() { |
| | | try { |
| | | log.debug("定时执行同步通道任务"); |
| | | ArdCamera camera = new ArdCamera(); |
| | | camera.setUseDataScope(false); |
| | | List<ArdCamera> ardCameraList = this.selectArdCameraList(camera); |
| | | if (!ardCameraList.isEmpty()) { |
| | | ardCameraList |
| | | .forEach(ardCamera -> { |
| | | //SDK获取实际通道 |
| | | List<ArdChannel> ardChannelListSDK = cameraSDKService.getChannels(ardCamera); |
| | | //数据库获取通道 |
| | | ArdChannel ardChannel = new ArdChannel(); |
| | | ardChannel.setUseDataScope(false); |
| | | ardChannel.setDeviceId(ardCamera.getId()); |
| | | List<ArdChannel> ardChannelListDb = ardChannelService.selectArdChannelList(ardChannel); |
| | | //同步通道 |
| | | asyncChannel(ardChannelListDb, ardChannelListSDK); |
| | | }); |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error("定时执行同步通道任务异常:{}", ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |