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
009/**
010 * A simple example of a "Faceless" (no gui) application
011 *
012 * To use it, uncomment the main() method.
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// Commented out since it's not normally used.
032// It's included as an example for anyone who wants to do their own program to launch JMRI.
033/*
034    // Main entry point
035    public static void main(String args[]) {
036
037        initLog4J();
038        log.info("Startup: {}", apps.util.Log4JUtil.startupInfo(name));
039
040        new SampleMinimalProgram(args);   // start the application class itself
041
042        log.debug("main initialization done");
043
044        // You could put your own code here,
045        // for example.  The layout connection
046        // is working at this point.
047    }
048*/
049
050    /**
051     * Static method to return a first logging statement. Used for logging
052     * startup, etc.
053     *
054     * @param program the name of the program
055     * @return the logging statement including JMRI and Java versions
056     */
057    static public String startupInfo(String program) {
058        return (program + " version " + jmri.Version.name()
059                + " starts under Java " + System.getProperty("java.version", "<unknown>"));
060    }
061
062    /**
063     * Static method to get Log4J working before the rest of JMRI starts up. In
064     * a non-minimal program, invoke jmri.util.Log4JUtil.initLogging
065     */
066    static protected void initLog4J() {
067        // initialize log4j2 - from logging configuration file (lcf) only
068        // if can find it!
069        String configFile = "default_lcf.xml";
070        apps.util.Log4JUtil.initLogging(configFile);
071        // install default exception handler
072        Thread.setDefaultUncaughtExceptionHandler(new jmri.util.exceptionhandler.UncaughtExceptionHandler());
073    }
074
075    /**
076     * Constructor starts the JMRI application running, and then returns.
077     *
078     * @param args command line arguments set at application launch
079     */
080    public SampleMinimalProgram(String[] args) {
081
082        // Load from preference file, by default the DecoderPro
083        // one so you can use DecoderPro to load the preferences values.
084        //    setConfigFilename("DecoderProConfig2.xml", args);
085        //    loadFile();
086        // load directly via code
087        codeConfig(args);
088
089        // and here we're up and running!
090    }
091
092    protected void codeConfig(String[] args) {
093        jmri.jmrix.SerialPortAdapter adapter = new jmri.jmrix.lenz.li100.LI100Adapter();
094
095        String portName = "/dev/cu.Bluetooth-PDA-Sync";
096        String baudRate = "9600";
097        //String option1Setting = null;
098        //String option2Setting = null;
099
100        adapter.setPort(portName);
101        adapter.configureBaudRate(baudRate);
102        //if (option1Setting !=null) adapter.configureOption1(option1Setting);
103        //if (option2Setting !=null) adapter.configureOption2(option2Setting);
104
105        adapter.openPort(portName, "JMRI app");
106        adapter.configure();
107
108        // install a Preferences Action Factory.
109        InstanceManager.store(new AppsPreferencesActionFactory(), JmriPreferencesActionFactory.class);
110
111        ConfigureManager cm = new AppsConfigurationManager();
112
113        // not setting preference file location!
114        InstanceManager.setDefault(ConfigureManager.class, cm);
115        // needs an error handler that doesn't invoke swing; send to log4j?
116
117        // start web server
118        final int port = 12080;
119        InstanceManager.getDefault(WebServerPreferences.class).setPort(port);
120        try {
121            WebServer.getDefault().start();
122        } catch (Exception ex) {
123            log.error("Unable to start web server.", ex);
124        }
125
126        log.info("Up!");
127    }
128
129    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SampleMinimalProgram.class);
130
131}