| package com.dy.pipIrrRemote.video; | 
|   | 
|   | 
| import com.dy.common.multiDataSource.DataSourceContext; | 
| import com.dy.pipIrrGlobal.pojoVi.ViYsApp; | 
| import com.dy.pipIrrGlobal.util.Org; | 
| import com.dy.pipIrrGlobal.util.OrgListenerSupport; | 
| import com.dy.pipIrrRemote.video.ys.YsAppClient; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.boot.context.event.ApplicationReadyEvent; | 
| import org.springframework.context.ApplicationListener; | 
| import org.springframework.core.io.ResourceLoader; | 
| import org.springframework.lang.NonNull; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.util.*; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2025/6/6 17:19 | 
|  * @Description 视频监控 | 
|  * 监听器,实现功能:启动日统计定时任务 | 
|  * 本监听器不能采用ServletContextListener方式,因为Servlet上下文Context创建后 | 
|  * Spring容器并没有创建完,而本类中用了Spring容器中的Bean,即*Dao 。 | 
|  * 所以采用了Spring事件监听器来实现 | 
|  */ | 
| @Slf4j | 
| @Component | 
| public class YsAppListener extends OrgListenerSupport implements ApplicationListener<ApplicationReadyEvent> { | 
|   | 
|     @Autowired | 
|     protected ResourceLoader resourceLoader ; | 
|   | 
|     @Autowired | 
|     protected YsAppCtrl ysAppCtrl; | 
|   | 
|     @Autowired | 
|     protected YsAppClient ysAppClient; | 
|   | 
|     /** | 
|      * SpringBoot容器已经准备好了,执行下面方法 | 
|      * @param event 事件 | 
|      */ | 
|     @Override | 
|     public void onApplicationEvent(@NonNull ApplicationReadyEvent event) { | 
|         try { | 
|             //等1秒,等待com.alibaba.druid.pool.DruidDataSource初始化完成 | 
|             Thread.sleep(1000L); | 
|         } catch (InterruptedException e) { | 
|             e.printStackTrace(); | 
|         }finally { | 
|             try{ | 
|                 super.init(resourceLoader); | 
|                 this.atSysStart(event); | 
|             }catch (Exception e){ | 
|                 log.error("注册萤石云定时任务出错", e); | 
|             } | 
|         } | 
|     } | 
|     /** | 
|      * 初始化 | 
|      */ | 
|     private void atSysStart(ApplicationReadyEvent event) throws Exception{ | 
|         //所有Org,共用一个萤石应用的AccessToken | 
|         boolean isNowGetNewAccessToken = false ;//当下是否获得AccessToken | 
|         List<Org.OrgVo> orgList = Org.OrgList ; | 
|         Map<String, ViYsApp> viYsAppMap = new HashMap<>() ; | 
|         if(orgList != null && orgList.size() >0){ | 
|             for(Org.OrgVo vo : orgList){ | 
|                 //遍历所有org,得到各自的萤石应用AccessToken | 
|                 DataSourceContext.set(vo.tag);//设置数据源 | 
|                 ViYsApp po = ysAppCtrl.selectSingletonYsApp() ; | 
|                 if(po == null){ | 
|                     isNowGetNewAccessToken = true ; | 
|                 }else{ | 
|                     viYsAppMap.put(vo.tag, po) ; | 
|                 } | 
|             } | 
|             this.dealAtSysStart(viYsAppMap, isNowGetNewAccessToken); | 
|         } | 
|     } | 
|     private void dealAtSysStart(Map<String, ViYsApp> viYsAppMap, boolean isNowGetNewAccessToken){ | 
|         if(!isNowGetNewAccessToken){ | 
|             if(viYsAppMap != null && viYsAppMap.size() >0){ | 
|                 long nowMustAtMillis = System.currentTimeMillis()  ; | 
|                 Collection<ViYsApp> col = viYsAppMap.values() ; | 
|                 Iterator<ViYsApp> it = col.iterator() ; | 
|                 ViYsApp po ; | 
|                 while (it.hasNext()){ | 
|                     po = it.next() ; | 
|                     if(po.expireTime != null && nowMustAtMillis > po.expireTime){ | 
|                         isNowGetNewAccessToken = true ; | 
|                     } | 
|                 } | 
|             }else{ | 
|                 isNowGetNewAccessToken = true ; | 
|             } | 
|         } | 
|         if(isNowGetNewAccessToken){ | 
|             this.ysAppClient.getAccessToken(); | 
|             this.ysAppClient.reSetNextGetAccessToken(null) ; | 
|         }else{ | 
|             long nextGetTokenAtMillis = 0L ; | 
|             Collection<ViYsApp> col = viYsAppMap.values() ; | 
|             Iterator<ViYsApp> it = col.iterator() ; | 
|             ViYsApp po ; | 
|             while (it.hasNext()){ | 
|                 po = it.next() ; | 
|                 if(nextGetTokenAtMillis == 0 || nextGetTokenAtMillis > po.expireTime){ | 
|                     nextGetTokenAtMillis = po.expireTime ; | 
|                 } | 
|             } | 
|             if(nextGetTokenAtMillis > 0L){ | 
|                 this.ysAppClient.reSetNextGetAccessToken(nextGetTokenAtMillis); | 
|             } | 
|         } | 
|     } | 
|   | 
| } |