001package jmri.jmris.simpleserver.parser;
002
003import jmri.InstanceManager;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007/* This class provides an interface between the JavaTree/JavaCC
008 * parser for the SimpleServer protocol and the JMRI back end.
009 * @author Paul Bender Copyright (C) 2016
010 */
011public class SimpleVisitor extends JmriServerParserDefaultVisitor {
012
013    private String outputString = null;
014
015    public SimpleVisitor(){
016        super();
017    }
018
019    public String getOutputString() {
020        return outputString;
021    }
022
023    @Override
024    public Object visit(ASTpowercmd node, Object data){
025        log.debug("Power Command Production {}",node.jjtGetValue());
026        if(node.jjtGetNumChildren()==0) {
027            // this is just a request for status
028               try{
029                  ((jmri.jmris.simpleserver.SimplePowerServer)
030                   data).sendStatus(InstanceManager.getDefault(jmri.PowerManager.class).getPower());
031               } catch(java.io.IOException je){
032                   log.error("Error sending Power status to client",je);
033               }
034        }else{
035            if (((SimpleNode)node.jjtGetChild(1)).jjtGetValue().equals("ON"))
036               ((jmri.jmris.simpleserver.SimplePowerServer)data).setOnStatus();
037            if (((SimpleNode)node.jjtGetChild(1)).jjtGetValue().equals("OFF"))
038               ((jmri.jmris.simpleserver.SimplePowerServer)data).setOffStatus();
039        }
040
041        return data;
042    }
043
044    private static final Logger log = LoggerFactory.getLogger(SimpleVisitor.class);
045
046}