001package apps;
002
003import jmri.ConfigureManager;
004import jmri.InstanceManager;
005import jmri.util.prefs.JmriPreferencesActionFactory;
006import jmri.web.server.WebServer;
007import jmri.web.server.WebServerPreferences;
008
009import apps.util.Log4JUtil;
010
011/**
012 * A simple example of a "Faceless" (no gui) application
013 *
014 * <hr>
015 * This file is part of JMRI.
016 * <p>
017 * JMRI is free software; you can redistribute it and/or modify it under the
018 * terms of version 2 of the GNU General Public License as published by the Free
019 * Software Foundation. See the "COPYING" file for a copy of this license.
020 * <p>
021 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
022 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
023 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
024 *
025 * @author Bob Jacobsen Copyright 2003, 2005, 2007, 2010
026 */
027public class SampleMinimalProgram {
028
029    static String name = "Faceless App";
030
031    // Main entry point
032    public static void main(String args[]) {
033
034        initLog4J();
035        log.info("Startup: {}", Log4JUtil.startupInfo(name));
036
037        new SampleMinimalProgram(args);   // start the application class itself
038
039        log.debug("main initialization done");
040
041        // You could put your own code here,
042        // for example.  The layout connection
043        // is working at this point.
044    }
045
046    /**
047     * Static method to return a first logging statement. Used for logging
048     * startup, etc.
049     *
050     * @param program the name of the program
051     * @return the logging statement including JMRI and Java versions
052     */
053    static public String startupInfo(String program) {
054        return (program + " version " + jmri.Version.name()
055                + " starts under Java " + System.getProperty("java.version", "<unknown>"));
056    }
057
058    /**
059     * Static method to get Log4J working before the rest of JMRI starts up. In
060     * a non-minimal program, invoke jmri.util.Log4JUtil.initLogging
061     */
062    static protected void initLog4J() {
063        // initialize log4j2 - from logging configuration file (lcf) only
064        // if can find it!
065        String configFile = "default_lcf.xml";
066        apps.util.Log4JUtil.initLogging(configFile);
067        // install default exception handler
068        Thread.setDefaultUncaughtExceptionHandler(new jmri.util.exceptionhandler.UncaughtExceptionHandler());
069    }
070
071    /**
072     * Constructor starts the JMRI application running, and then returns.
073     *
074     * @param args command line arguments set at application launch
075     */
076    public SampleMinimalProgram(String[] args) {
077
078        // Load from preference file, by default the DecoderPro
079        // one so you can use DecoderPro to load the preferences values.
080        //    setConfigFilename("DecoderProConfig2.xml", args);
081        //    loadFile();
082        // load directly via code
083        codeConfig(args);
084
085        // and here we're up and running!
086    }
087
088    protected void codeConfig(String[] args) {
089        jmri.jmrix.SerialPortAdapter adapter = new jmri.jmrix.lenz.li100.LI100Adapter();
090
091        String portName = "/dev/cu.Bluetooth-PDA-Sync";
092        String baudRate = "9600";
093        //String option1Setting = null;
094        //String option2Setting = null;
095
096        adapter.setPort(portName);
097        adapter.configureBaudRate(baudRate);
098        //if (option1Setting !=null) adapter.configureOption1(option1Setting);
099        //if (option2Setting !=null) adapter.configureOption2(option2Setting);
100
101        adapter.openPort(portName, "JMRI app");
102        adapter.configure();
103
104        // install a Preferences Action Factory.
105        InstanceManager.store(new AppsPreferencesActionFactory(), JmriPreferencesActionFactory.class);
106
107        ConfigureManager cm = new AppsConfigurationManager();
108
109        // not setting preference file location!
110        InstanceManager.setDefault(ConfigureManager.class, cm);
111        // needs an error handler that doesn't invoke swing; send to log4j?
112
113        // start web server
114        final int port = 12080;
115        InstanceManager.getDefault(WebServerPreferences.class).setPort(port);
116        try {
117            WebServer.getDefault().start();
118        } catch (Exception ex) {
119            log.error("Unable to start web server.", ex);
120        }
121
122        log.info("Up!");
123    }
124
125    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SampleMinimalProgram.class);
126
127}