OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 118 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
118 ilm 1
package org.openconcerto.modules.badge;
2
 
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6
import java.io.OutputStream;
7
import java.net.ServerSocket;
8
import java.net.Socket;
9
 
10
public class BadgeListenerBluebox extends BadgeListener {
11
 
12
    private static final int SOH = 1;
13
    private static final char STX = 2;
14
    private static final char ETX = 3;
15
    private static final char EOT = 4;
16
    private static final char ENQ = 5;
17
    private static final char ACK = 0x06;
18
    private static final char NAK = 0x15;
19
    private static final char SYN = 0x16;
20
    private static final char CR = 0x0D;
21
    private static int PORT = 3000;
22
 
23
    public boolean openDoor(int seconds) {
24
        Socket socket = null;
25
        String ret = "NAK";
26
        System.out.println("OpenDoor:" + seconds + "s");
27
 
28
        try {
29
            System.out.println("BadgeListenerBluebox.openDoor() new sockect created " + getDoorIp() + " " + PORT);
30
            socket = new Socket(getDoorIp(), PORT);
31
            socket.setSoTimeout(2000);
32
        } catch (IOException e) {
33
            e.printStackTrace();
34
            return false;
35
 
36
        }
37
        System.out.println("BadgeListenerBluebox.openDoor() sending bytes");
38
        try
39
 
40
        {
41
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
42
            OutputStream output = socket.getOutputStream();
43
 
44
            byte[] message = new byte[] { SOH, 'F', 'F', STX, '3', '7', '0', '1', '0', (byte) ('0' + seconds), ETX, 0, CR };
45
            byte crc = message[0];
46
            for (int i = 1; i < message.length - 2; i++) {
47
                crc ^= message[i];
48
            }
49
            if (seconds == 4) {
50
                crc = 2;
51
            }
52
            message[message.length - 2] = crc;
53
 
54
            output.write(message);
55
            output.flush();
56
 
57
            ret = input.readLine();
58
            ret = decode(ret);
59
 
60
        } catch (IOException e) {
61
            System.out.println(e);
62
        }
63
        try {
64
            socket.close();
65
        } catch (IOException e) {
66
            System.out.println(e);
67
        }
68
        System.out.println("BadgeListenerBluebox.openDoor() " + seconds + "s sent");
69
        return !ret.contains("NAK");
70
 
71
    };
72
 
73
    private static String decode(String line) {
74
        System.out.println("BadgeListenerBluebox.decode(): " + line);
75
        final int length = line.length();
76
        final StringBuffer output = new StringBuffer(length * 3);
77
        for (int i = 0; i < length; i++) {
78
            final char c = line.charAt(i);
79
            switch (c) {
80
            case SOH:
81
                output.append("SOH ");
82
                break;
83
            case STX:
84
                output.append("STX ");
85
                break;
86
            case ETX:
87
                output.append("ETX ");
88
                break;
89
            case EOT:
90
                output.append("EOT ");
91
                break;
92
            case ENQ:
93
                output.append("ENQ ");
94
                break;
95
            case ACK:
96
                output.append("ACK ");
97
                break;
98
            case NAK:
99
                output.append("NAK ");
100
                break;
101
            case SYN:
102
                output.append("SYN ");
103
                break;
104
            case CR:
105
                output.append("CR ");
106
                break;
107
            default:
108
                if (Character.isLetterOrDigit(c)) {
109
                    output.append(c);
110
                } else
111
                    output.append(Byte.toString((byte) c));
112
                break;
113
            }
114
 
115
        }
116
        return output.toString();
117
    }
118
 
119
    @Override
120
    public void startDaemon() {
121
        Thread t = new Thread("TCP") {
122
            public void run() {
123
                System.out.println("BadgeListenerBluebox.startDaemon() started");
124
                try {
125
                    ServerSocket listener = new ServerSocket(3000);
126
 
127
                    while (true) {
128
 
129
                        final Socket server = listener.accept();
130
                        server.setSoTimeout(15000);
131
                        final Thread tReader = new Thread() {
132
                            public void run() {
133
 
134
                                System.out.println("BlueBox.run()");
135
                                try {
136
                                    // Get input from the client
137
                                    BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
138
                                    OutputStream out = server.getOutputStream();
139
 
140
                                    String line = in.readLine();
141
                                    out.close();
142
                                    server.close();
143
                                    line = decode(line);
144
                                    try {
145
                                        Thread.sleep(1000);
146
                                    } catch (InterruptedException e) {
147
                                        // TODO Auto-generated catch block
148
                                        e.printStackTrace();
149
                                    }
150
                                    cardIdReceived(getIdFromLine(line));
151
 
152
                                    System.out.println("BlueBox.run() done");
153
                                } catch (IOException ioe) {
154
                                    System.out.println("IOException on socket listen: " + ioe);
155
                                    ioe.printStackTrace();
156
                                }
157
 
158
                            };
159
                        };
160
 
161
                        tReader.start();
162
                    }
163
                } catch (IOException ioe) {
164
                    System.out.println("IOException on socket listen: " + ioe);
165
                    ioe.printStackTrace();
166
                }
167
 
168
            };
169
        };
170
        t.start();
171
 
172
    }
173
 
174
    String getIdFromLine(String line) {
175
        if (line.length() < 5)
176
            return "";
177
        int i1 = line.indexOf("ETX");
178
        if (i1 > 0) {
179
            return line.substring(4, i1);
180
        }
181
        return "";
182
    }
183
 
167 ilm 184
    public static void main(String[] args) throws Exception {
118 ilm 185
        final BadgeListener bl = new BadgeListenerBluebox();
186
        bl.readConfiguration();
187
        bl.startDaemon();
188
    }
189
}