summaryrefslogtreecommitdiff
path: root/src/net/tuschhcm/routercontrol/SwitcherApp.java
blob: 9a4b8ae470978f78b63659eaeca4dd16488156af (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package net.tuschhcm.routercontrol;

import java.io.File;
import java.io.IOException;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import gnu.io.CommPortIdentifier;

import net.tuschhcm.routercontrol.router.Router;
import net.tuschhcm.routercontrol.router.ShinyBow5544Router;
import net.tuschhcm.routercontrol.ui.ConsoleUI;
import net.tuschhcm.routercontrol.ui.UserInterface;
import net.tuschhcm.routercontrol.ui.UserInterface.Action;

public class SwitcherApp {
    /**
     * The router I control.
     */
    private final Router mRouter;

    /**
     * My user interface.
     */
    private final UserInterface mUI;

    /**
     * My list of presets.
     */
    private Map<Integer, Preset> mPresets;

    /**
     * Create a switcher app.
     * @param portName the port name for the router.
     */
    public SwitcherApp(final String portName) {
        mUI = new ConsoleUI();
        mRouter = new ShinyBow5544Router(portName);
        mPresets = new HashMap<Integer, Preset>();

        // Setup UI actions
        mUI.setPresetSelectionAction(new Action() {
            public void onAction() {
                handlePresetSelected();
            }
        });

        mUI.setToggleControlLockAction(new Action() {
            public void onAction() {
                handleLockToggled();
            }
        });
    }

    /**
     * Start the application.
     */
    public void run() {
        // Load the presets
        loadPresets();

        // Send the presets to the UI
        for (Map.Entry<Integer, Preset> presetEntry : mPresets.entrySet()) {
            mUI.addPreset(presetEntry.getKey(), presetEntry.getValue().getName());
        }

        mUI.run();

        // All done!
        mRouter.close();
    }

    private void loadPresets() {
        try {
            final String dir = SwitcherApp.class.getProtectionDomain().
                getCodeSource().getLocation().getPath();
            File presets = new File(dir, "presets.txt");

            if (!presets.isFile()) {
                System.err.println("Could not find presets.txt in " + dir);
                System.exit(1);
            }

            mPresets = Preset.loadPresetsFile(presets);

        } catch (Exception e) {
            System.err.println("Problem loading preset file: " + e.getMessage());
            System.exit(1);
        }
    }

    /**
     * Handles a preset selection.
     */
    private void handlePresetSelected() {
        final int selectedPreset = mUI.getSelectedPreset();
        final Preset p = mPresets.get(selectedPreset);

        if (p == null) {
            System.err.println(selectedPreset + " is not a preset.");
            return;
        }

        // Set all the outputs
        for (int output = 1; output <= p.getNumberOfOutputs(); output++) {
            mRouter.switchInput(output, p.getInputForOutput(output));
        }
    }

    /**
     * Locks or unlocks the physical router controls.
     */
    private void handleLockToggled() {
        mRouter.setLockControls(mUI.getControlLockStatus());
    }

    /**
     * Entry-point for the application.
     * @param args Command line arguments
     */
    public static void main(String[] args) {
        // Parse command line arguments
        if (args.length != 1) {
            System.err.println("You must pass one these arguments:");
            System.err.println("\t[commport]      The name of the serial port to use.");
            System.err.println("\t--list-ports    List the available serial ports.");
            return;
        }

        if (args[0].equals("--list-ports")) {
            listCommPorts();
            return;
        }

        String comPort = args[0];

        SwitcherApp app = new SwitcherApp(comPort);
        app.run();
    }

    private static void listCommPorts() {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();

        if (ports.hasMoreElements()) {
            System.out.println("These ports are available on your system: ");
            while (ports.hasMoreElements()) {
                CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
                if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    System.out.println(port.getName());
                }
            }

        } else {
            System.out.println("There are no serial ports on your system.");
        }
    }
}