zhubaomin
7 天以前 00d54db361d697cf8fdf2f15940f4650b21b3185
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
package com.dy.rtuMw.server.tasks;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.dy.common.mw.core.CoreTask;
import com.dy.common.mw.protocol.MidResult;
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol.Driver;
import com.dy.common.mw.protocol.ProtocolCache;
import com.dy.rtuMw.server.ServerProperties;
import com.dy.rtuMw.server.forTcp.TcpSessionCache;
 
/**
 * 从web业务系统发向RTU的命令任务
 * @author Administrator
 *
 */
public class WebDownComTask extends CoreTask {
    
    private static Logger log = LogManager.getLogger(WebDownComTask.class.getName());
 
    @Override
    public Integer execute() {
        Command com = (Command)this.data ;
        try {
            log.info("下发远程命令" + com.getCode() + "的核心任务开始执行");
            this.deal(com);
        } catch (Exception e) {
            log.error("处理下行命令出错" + (e.getMessage()==null?"!":("," + e.getMessage())) ,e);
        }
        return null ;
    }
    
    /**
     * 处理命令
     * @param com 命令
     * @throws Exception
     */
    private void deal(Command com) throws Exception{
        String rtuAddr = com.getRtuAddr() ;
        //前面已经判断rtuAddr为空情况,至此其不为空
        Driver dri = null ;
        Object[] objs =  TcpSessionCache.getTcpProtocolNameVersion(rtuAddr) ;
        if(objs == null || objs.length == 0 || objs[0] == null){
            //RTU未曾上线
            int count = ProtocolCache.driverCount() ;
            if(count == 1){
                //只有一个协议
                dri = ProtocolCache.getFirstDriver() ;
            }
        }else{
            String protocolName = (String)objs[0];
            com.protocolVersion = (Short)objs[1];
            dri = ProtocolCache.getDriver(protocolName, com.protocolVersion) ;
        }
        if(dri == null){
            log.error("严重错误,未能得到RTU(" + rtuAddr + ")驱动类实例!");
        }else{
            MidResult[] actions = dri.createCommand(ServerProperties.isLowPower, com)  ;
            log.info("下发远程命令" + com.getCode() + "由协议驱动构造完成");
            if(actions != null){
                for(MidResult act : actions){
                    act.action();
                }
            }
        }
    }
 
}