001package jmri.jmrix.srcp.parser;
002
003import jmri.jmrix.srcp.SRCPBusConnectionMemo;
004import jmri.jmrix.srcp.SRCPSystemConnectionMemo;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/* This class provides an interface between the JavaTree/JavaCC
009 * parser for the SRCP protocol and the JMRI front end.
010 *
011 * @author Paul Bender Copyright (C) 2011
012 */
013public class SRCPClientVisitor extends SRCPClientParserDefaultVisitor {
014
015    @Override
016    public Object visit(ASTinfo node, Object data) {
017        log.debug("Info Response {}", node.jjtGetValue());
018        int bus = Integer.parseInt(((String) ((SimpleNode) node.jjtGetChild(0)).jjtGetValue()));
019        SRCPBusConnectionMemo busMemo = ((SRCPSystemConnectionMemo) data).getMemo(bus);
020
021        SimpleNode group = (SimpleNode) node.jjtGetChild(1);
022
023        log.debug("Info Response Group: {}", group.jjtGetValue());
024
025        if (group instanceof ASTfb) {
026            if (busMemo.provides(jmri.SensorManager.class)) {
027                int address = Integer.parseInt((String) (((SimpleNode) group.jjtGetChild(0)).jjtGetValue()));
028                ((jmri.jmrix.srcp.SRCPSensor) ((jmri.jmrix.srcp.SRCPSensorManager) busMemo.getSensorManager()).provideSensor("" + address)).reply(node);
029            }
030        } else if (group instanceof ASTga) {
031            if (busMemo.provides(jmri.TurnoutManager.class)) {
032                if (group.jjtGetNumChildren() >= 2) {
033                    //int address = Integer.parseInt((String)(((SimpleNode)group.jjtGetChild(0)).jjtGetValue()));
034                    //boolean thrown = ((String)((SimpleNode)group.jjtGetChild(1)).jjtGetValue()).equals("1");
035                } else {
036                    // just returning the protocol.
037                }
038            }
039        } else if (group instanceof ASTgl) {
040            if (busMemo.provides(jmri.ThrottleManager.class)) {
041                //int address = Integer.parseInt((String)(((SimpleNode)group.jjtGetChild(0)).jjtGetValue()));
042            }
043        } else if (group instanceof ASTsm) {
044            if (busMemo.provides(jmri.GlobalProgrammerManager.class)) {
045                jmri.jmrix.srcp.SRCPProgrammer programmer = (jmri.jmrix.srcp.SRCPProgrammer) (busMemo.getProgrammerManager().getGlobalProgrammer());
046                if (programmer != null) {
047                    programmer.reply(node);
048                }
049            }
050        } else if (group instanceof ASTpower) {
051            if (busMemo.provides(jmri.PowerManager.class)) {
052                //String state = (String)((SimpleNode)group.jjtGetChild(1)).jjtGetValue();
053                //busMemo.getPowerManager().setPower(state.equals("ON")?jmri.PowerManager.ON:jmri.PowerManager.OFF);
054                ((jmri.jmrix.srcp.SRCPPowerManager) busMemo.getPowerManager()).reply(node);
055            }
056        } else if (group instanceof ASTtime) {
057            log.debug("INFO Response for TIME group with bus {}", bus);
058        } else if (group instanceof ASTsession) {
059            log.debug("INFO Response for SESSION group with bus {}", bus);
060        } else if (group instanceof ASTserver) {
061            log.debug("INFO Response for SERVER group with bus {}", bus);
062        } else if (group instanceof ASTdescription) {
063            log.debug("INFO Response for DESCRIPTION group with bus {}", bus);
064        } else if (group instanceof ASTlock) {
065            log.debug("INFO Response for LOCK group with bus {}", bus);
066        }
067        return data;
068    }
069
070    @Override
071    public Object visit(ASTok node, Object data) {
072        log.debug("Ok Response {}", node.jjtGetValue());
073        SRCPSystemConnectionMemo memo = (SRCPSystemConnectionMemo) data;
074        if (((String) ((SimpleNode) node).jjtGetValue()).contains("GO")) {
075            memo.setMode(jmri.jmrix.srcp.SRCPTrafficController.RUNMODE);
076            return data;
077        }
078        return node.childrenAccept(this, data);
079    }
080
081    private final static Logger log = LoggerFactory.getLogger(SRCPClientVisitor.class);
082
083}