liurunyu
昨天 83d9b0de6d127cf0f2822c51139fa4e15a3326e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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);
            }
        }
    }
 
}