summaryrefslogtreecommitdiff
path: root/src/net/tuschhcm/routercontrol/router/ShinyBow5544Router.java
blob: ef0e41eae634587d3535b74558bdb122d993180d (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
package net.tuschhcm.routercontrol.router;

import java.io.PrintStream;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

/**
 * Router implementation for a ShinyBow SB-5544
 * 
 */
public class ShinyBow5544Router implements Router {
	private static final int MSG_DELAY = 50;
	
    private final SerialPort mSerialPort;
    private final PrintStream mOut;

    /**
     * Create a new ShinyBow router using the given comm port.
     * 
     * @param portName Com port name
     */
    public ShinyBow5544Router(final String portName) {
        try {
            CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);
            
            // 9600 baud, 8 bit, no parity, 1 stop bit
            mSerialPort = (SerialPort) portId.open("routercontrol", 100);
            mSerialPort.setSerialPortParams(9600,
                                            SerialPort.DATABITS_8,
                                            SerialPort.STOPBITS_1,
                                            SerialPort.PARITY_NONE);

            mOut = new PrintStream(mSerialPort.getOutputStream());
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to open serial port: " + e.getMessage(), e);
        }
    }
    
    @Override
    public void switchInput(int output, int input)
            throws IllegalArgumentException {
        if (output < 1 || output > 4) {
           throw new IllegalArgumentException("Invalid Output");
        }

        if (input < 1 || input > 4) {
           throw new IllegalArgumentException("Invalid Input");
        } 


        mOut.format("SBI0%dO0%d", input, output);
        try {
        	Thread.sleep(MSG_DELAY);
        	
        } catch (InterruptedException e) {
        	// ignore
        }
    }

    @Override
    public void setPower(boolean on) {
        if (on) {
            mOut.print("SBSYSMON");

        } else {
            mOut.print("SBSYSMOF");
        }
        
        try {
        	Thread.sleep(MSG_DELAY);
        	
        } catch (InterruptedException e) {
        	// ignore
        }
    }

    @Override
    public void setLockControls(boolean enabled) {
        if (enabled) {
            mOut.print("SBSYSMLK");

        } else {
            mOut.print("SBSYSMUK");
        }
        
        try {
        	Thread.sleep(MSG_DELAY);
        	
        } catch (InterruptedException e) {
        	// ignore
        }
    }
    
    @Override
    public void close() {
        mOut.close();
        mSerialPort.close();
    }
}