liurunyu
2023-11-20 fb31efc4c1efee99164dc83f6f67f1f609d1ba40
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
package com.dy.common.mw.protocol;
 
import java.util.Collection;
import java.util.HashMap;
 
import com.dy.common.mw.UnitAdapterInterface;
import com.dy.common.mw.UnitInterface;
import com.dy.common.mw.UnitStartedCallbackInterface;
 
 
public class ProtocolUnit implements UnitInterface {
 
    private static final ProtocolUnit instance = new ProtocolUnit() ;
    
    public ProtocolUnitAdapter adapter ;
    
    private ProtocolUnit(){}
    
    public static ProtocolUnit getInstance(){
        return instance ;
    }
    
    @Override
    public void setAdapter(UnitAdapterInterface adapter) throws Exception {
        if(adapter == null){
            throw new Exception("协议模块适配器对象不能为空!") ;
        }
        this.adapter = (ProtocolUnitAdapter)adapter ;
        
        ProtocolConfigVo vo = this.adapter.getConfig() ;
        if(vo == null){
            throw new Exception("协议模块配置对象不能为空!") ;
        }
        if(vo.centerAddr < 0 ||  vo.centerAddr > 4){//云控制中心地址是0
            throw new Exception("协议模块配置对象属性中心地址只能是0、1、2、3、4 !") ;
        }
    }
    
    /**
     * 启动模块
     */
    public void start(UnitStartedCallbackInterface callback) throws Exception {
        //得到唯一实例, 并在生成唯一实例时,扫描注解类
        AnnotationScan.getIntance() ;
        
        //各个协议驱动类扫描自己的功能码注解
        HashMap<String, AnnotationDriverVo> drivers =  ProtocolCach.getDriverMap() ;
        Collection<String> colDrivers = drivers.keySet() ;
        StringBuilder totalProtocols = new StringBuilder() ;
        for(String protocolName : colDrivers){
            if(!totalProtocols.isEmpty()){
                totalProtocols.append(",") ;
            }
            totalProtocols.append(protocolName) ;
            Driver dri = ProtocolCach.getDriver(protocolName) ;
            if(dri != null){
                dri.scanAnnotationCode();
            }
        }
        if(adapter.getConfig().showStartInfo){
            System.out.println("协议模块成功启动,"
                            + "支持的协议:" + totalProtocols  );
        }
        callback.call(null);
    }
    
    /**
     * 是否只有一个协议
     * @return 结果
     */
    @SuppressWarnings("unused")
    public boolean isOnlyOneProtocol(){
        HashMap<String, AnnotationDriverVo> drivers =  ProtocolCach.getDriverMap() ;
        return drivers.size() == 1 ;
    }
 
    @Override
    public void stop(UnitStartedCallbackInterface callback) {
    }
    
    /*
    public static void main(String[] args) throws Exception{
        ProtocolUnit protoUnit = ProtocolUnit.getInstance();
        protoUnit.start(new UnitStartedCallbackInterface(){
            @Override
            public void call(Object obj) {
            }
        });
    }
    */
 
}