| New file | 
|  |  |  | 
|---|
|  |  |  | 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.UnitCallbackInterface; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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(UnitCallbackInterface callback) throws Exception { | 
|---|
|  |  |  | //得到唯一实例, 并在生成唯一实例时,扫描注解类 | 
|---|
|  |  |  | AnnotationScan.getIntance() ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //各个协议驱动类扫描自己的功能码注解 | 
|---|
|  |  |  | HashMap<String, AnnotationDriverVo> drivers =  ProtocolCache.getDriverMap() ; | 
|---|
|  |  |  | Collection<String> keys = drivers.keySet() ; | 
|---|
|  |  |  | StringBuilder totalProtocols = new StringBuilder() ; | 
|---|
|  |  |  | for(String protocolNameVersion : keys){ | 
|---|
|  |  |  | if(!totalProtocols.isEmpty()){ | 
|---|
|  |  |  | totalProtocols.append(",") ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | totalProtocols.append(protocolNameVersion) ; | 
|---|
|  |  |  | Driver dri = ProtocolCache.getDriver(protocolNameVersion) ; | 
|---|
|  |  |  | if(dri != null){ | 
|---|
|  |  |  | dri.scanAnnotationCode(); | 
|---|
|  |  |  | dri.setNotify(adapter.getSingleRtuInfoNotify(protocolNameVersion)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(adapter.getConfig().showStartInfo){ | 
|---|
|  |  |  | System.out.println("协议模块成功启动," | 
|---|
|  |  |  | + "支持的协议:" + totalProtocols  ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | callback.call(null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 是否只有一个协议 | 
|---|
|  |  |  | * @return 结果 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @SuppressWarnings("unused") | 
|---|
|  |  |  | public boolean isOnlyOneProtocol(){ | 
|---|
|  |  |  | HashMap<String, AnnotationDriverVo> drivers =  ProtocolCache.getDriverMap() ; | 
|---|
|  |  |  | return drivers.size() == 1 ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void stop(UnitCallbackInterface callback) { | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /* | 
|---|
|  |  |  | public static void main(String[] args) throws Exception{ | 
|---|
|  |  |  | ProtocolUnit protoUnit = ProtocolUnit.getInstance(); | 
|---|
|  |  |  | protoUnit.start(new UnitStartedCallbackInterface(){ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void call(Object obj) { | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|