OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 118 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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