Dépôt officiel du code source de l'ERP OpenConcerto
/trunk/Modules/Module Badge/src/org/openconcerto/modules/badge/BadgeListener.java |
---|
20,6 → 20,7 |
import java.util.Calendar; |
import java.util.Date; |
import java.util.List; |
import java.util.Locale; |
import java.util.Properties; |
import javax.swing.ImageIcon; |
43,16 → 44,25 |
import org.openconcerto.sql.preferences.SQLPreferences; |
import org.openconcerto.sql.preferences.UserProps; |
import org.openconcerto.sql.sqlobject.IComboSelectionItem; |
import org.openconcerto.sql.users.UserManager; |
import org.openconcerto.utils.ExceptionHandler; |
import org.openconcerto.utils.i18n.TranslationManager; |
public class BadgeListener implements Runnable { |
private static final int UDP_PORT = 1470; |
private String ip; |
private String doorIp; |
private int relai; |
protected TrayIcon trayIcon; |
public BadgeListener() { |
} |
public void init(String id) { |
TranslationManager.getInstance().addTranslationStreamFromClass(BadgeListener.class); |
TranslationManager.getInstance().setLocale(Locale.FRANCE); |
final ComptaPropsConfiguration conf = ComptaPropsConfiguration.create(true); |
if (conf == null) { |
ServerFinderPanel.main(new String[0]); |
60,6 → 70,7 |
} |
Configuration.setInstance(conf); |
UserManager.getInstance().setCurrentUser(2); |
try { |
conf.getBase(); |
80,6 → 91,14 |
} |
int selectedSociete = UserProps.getInstance().getLastSocieteID(); |
if (id != null) { |
try { |
selectedSociete = Integer.valueOf(id); |
} catch (Exception e) { |
e.printStackTrace(); |
} |
} |
if (selectedSociete < SQLRow.MIN_VALID_ID) { |
final SQLElement elem = conf.getDirectory().getElement(conf.getRoot().getTable("SOCIETE_COMMON")); |
final List<IComboSelectionItem> comboItems = elem.getComboRequest().getComboItems(); |
88,8 → 107,8 |
else |
throw new IllegalStateException("No " + elem + " found"); |
} |
System.err.println("BadgeListener.init() societe " + selectedSociete); |
conf.setUpSocieteDataBaseConnexion(selectedSociete); |
} |
private PopupMenu createTrayMenu() { |
102,14 → 121,8 |
ActionListener executeListener = new ActionListener() { |
public void actionPerformed(ActionEvent e) { |
Relai r = new Relai(ip, relai); |
try { |
r.pulse(2); |
trayIcon.displayMessage("Ouverture", "Ouverture de la porte OK", TrayIcon.MessageType.INFO); |
} catch (Throwable ex) { |
trayIcon.displayMessage("Erreur", ex.getMessage(), TrayIcon.MessageType.ERROR); |
openDoor(4); |
} |
} |
}; |
PopupMenu menu = new PopupMenu(); |
155,18 → 168,21 |
@Override |
public void run() { |
if (!SystemTray.isSupported()) { |
try { |
JOptionPane.showMessageDialog(new JFrame(), "System tray not supported on this platform"); |
System.exit(1); |
} catch (Exception e) { |
System.out.println("System tray not supported on this platform"); |
} |
} else { |
try { |
final SystemTray sysTray = SystemTray.getSystemTray(); |
trayIcon = createTrayIcon(); |
sysTray.add(trayIcon); |
trayIcon.displayMessage("Service de badge", "Ecoute sur port " + UDP_PORT, TrayIcon.MessageType.NONE); |
displayMessage("Service de badge", "Ecoute sur port " + UDP_PORT); |
} catch (AWTException e) { |
System.out.println("Unable to add icon to the system tray"); |
System.exit(1); |
} |
} |
} |
}); |
180,7 → 196,7 |
try { |
final FileInputStream inStream = new FileInputStream(file); |
props.load(inStream); |
this.ip = props.getProperty("ip"); |
this.doorIp = props.getProperty("ip").trim(); |
this.relai = Integer.parseInt(props.getProperty("relai", "1")); |
inStream.close(); |
} catch (FileNotFoundException e) { |
188,7 → 204,9 |
} catch (IOException e) { |
JOptionPane.showMessageDialog(new JFrame(), e.getMessage()); |
} |
System.err.println("BadgeListener.readConfiguration() door ip : " + doorIp + ", index :" + relai); |
init(props.getProperty("idSociete")); |
} |
@Override |
204,21 → 222,11 |
final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
serverSocket.receive(receivePacket); |
final String sentence = new String(receivePacket.getData()).trim(); |
System.out.println(sentence); |
if (isBadgeAllowed(sentence)) { |
final Relai r = new Relai(ip, relai); |
try { |
r.pulse(2); |
} catch (Throwable ex) { |
trayIcon.displayMessage("Erreur", ex.getMessage(), TrayIcon.MessageType.ERROR); |
cardIdReceived(sentence); |
} |
} else { |
trayIcon.displayMessage("Carte refusée", "Carte " + sentence + " non acceptée", TrayIcon.MessageType.INFO); |
} |
} |
} catch (Throwable e) { |
e.printStackTrace(); |
trayIcon.displayMessage("Erreur", (e == null) ? "" : e.getMessage(), TrayIcon.MessageType.ERROR); |
displayError("Erreur", (e == null) ? "" : e.getMessage()); |
} finally { |
if (serverSocket != null) { |
serverSocket.close(); |
233,6 → 241,38 |
} |
} |
public void cardIdReceived(final String sentence) { |
System.err.println("BadgeListener.cardIdReceived() " + sentence); |
if (isBadgeAllowed(sentence)) { |
boolean b = openDoor(4); |
if (b) { |
displayMessage("Ouverture", "Ouverture de la porte OK"); |
} else { |
displayError("Erreur", "Impossible d'ouvrir la porte"); |
} |
} else { |
displayMessage("Carte refusée", "Carte " + sentence + " non acceptée"); |
} |
} |
public void displayMessage(String title, String txt) { |
if (trayIcon != null) { |
trayIcon.displayMessage(title, txt, TrayIcon.MessageType.INFO); |
} else { |
System.out.println("[INFO] " + title + " : " + txt); |
} |
} |
public void displayError(String title, String txt) { |
if (trayIcon != null) { |
trayIcon.displayMessage(title, txt, TrayIcon.MessageType.ERROR); |
} else { |
System.out.println("[ERROR] " + title + " : " + txt); |
} |
} |
public boolean isBadgeAllowed(String cardNumber) { |
SQLBase base = Configuration.getInstance().getBase(); |
SQLSelect sel = new SQLSelect(base); |
355,4 → 395,17 |
return allow; |
} |
public boolean openDoor(int seconds) { |
final Relai r = new Relai(doorIp, relai); |
try { |
r.pulse(seconds); |
return true; |
} catch (Throwable ex) { |
return false; |
} |
} |
public String getDoorIp() { |
return doorIp; |
} |
} |
/trunk/Modules/Module Badge/src/org/openconcerto/modules/badge/BadgeListenerBluebox.java |
---|
New file |
0,0 → 1,189 |
package org.openconcerto.modules.badge; |
import java.io.BufferedReader; |
import java.io.IOException; |
import java.io.InputStreamReader; |
import java.io.OutputStream; |
import java.net.ServerSocket; |
import java.net.Socket; |
public class BadgeListenerBluebox extends BadgeListener { |
private static final int SOH = 1; |
private static final char STX = 2; |
private static final char ETX = 3; |
private static final char EOT = 4; |
private static final char ENQ = 5; |
private static final char ACK = 0x06; |
private static final char NAK = 0x15; |
private static final char SYN = 0x16; |
private static final char CR = 0x0D; |
private static int PORT = 3000; |
public boolean openDoor(int seconds) { |
Socket socket = null; |
String ret = "NAK"; |
System.out.println("OpenDoor:" + seconds + "s"); |
try { |
System.out.println("BadgeListenerBluebox.openDoor() new sockect created " + getDoorIp() + " " + PORT); |
socket = new Socket(getDoorIp(), PORT); |
socket.setSoTimeout(2000); |
} catch (IOException e) { |
e.printStackTrace(); |
return false; |
} |
System.out.println("BadgeListenerBluebox.openDoor() sending bytes"); |
try |
{ |
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
OutputStream output = socket.getOutputStream(); |
byte[] message = new byte[] { SOH, 'F', 'F', STX, '3', '7', '0', '1', '0', (byte) ('0' + seconds), ETX, 0, CR }; |
byte crc = message[0]; |
for (int i = 1; i < message.length - 2; i++) { |
crc ^= message[i]; |
} |
if (seconds == 4) { |
crc = 2; |
} |
message[message.length - 2] = crc; |
output.write(message); |
output.flush(); |
ret = input.readLine(); |
ret = decode(ret); |
} catch (IOException e) { |
System.out.println(e); |
} |
try { |
socket.close(); |
} catch (IOException e) { |
System.out.println(e); |
} |
System.out.println("BadgeListenerBluebox.openDoor() " + seconds + "s sent"); |
return !ret.contains("NAK"); |
}; |
private static String decode(String line) { |
System.out.println("BadgeListenerBluebox.decode(): " + line); |
final int length = line.length(); |
final StringBuffer output = new StringBuffer(length * 3); |
for (int i = 0; i < length; i++) { |
final char c = line.charAt(i); |
switch (c) { |
case SOH: |
output.append("SOH "); |
break; |
case STX: |
output.append("STX "); |
break; |
case ETX: |
output.append("ETX "); |
break; |
case EOT: |
output.append("EOT "); |
break; |
case ENQ: |
output.append("ENQ "); |
break; |
case ACK: |
output.append("ACK "); |
break; |
case NAK: |
output.append("NAK "); |
break; |
case SYN: |
output.append("SYN "); |
break; |
case CR: |
output.append("CR "); |
break; |
default: |
if (Character.isLetterOrDigit(c)) { |
output.append(c); |
} else |
output.append(Byte.toString((byte) c)); |
break; |
} |
} |
return output.toString(); |
} |
@Override |
public void startDaemon() { |
Thread t = new Thread("TCP") { |
public void run() { |
System.out.println("BadgeListenerBluebox.startDaemon() started"); |
try { |
ServerSocket listener = new ServerSocket(3000); |
while (true) { |
final Socket server = listener.accept(); |
server.setSoTimeout(15000); |
final Thread tReader = new Thread() { |
public void run() { |
System.out.println("BlueBox.run()"); |
try { |
// Get input from the client |
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream())); |
OutputStream out = server.getOutputStream(); |
String line = in.readLine(); |
out.close(); |
server.close(); |
line = decode(line); |
try { |
Thread.sleep(1000); |
} catch (InterruptedException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
cardIdReceived(getIdFromLine(line)); |
System.out.println("BlueBox.run() done"); |
} catch (IOException ioe) { |
System.out.println("IOException on socket listen: " + ioe); |
ioe.printStackTrace(); |
} |
}; |
}; |
tReader.start(); |
} |
} catch (IOException ioe) { |
System.out.println("IOException on socket listen: " + ioe); |
ioe.printStackTrace(); |
} |
}; |
}; |
t.start(); |
} |
String getIdFromLine(String line) { |
if (line.length() < 5) |
return ""; |
int i1 = line.indexOf("ETX"); |
if (i1 > 0) { |
return line.substring(4, i1); |
} |
return ""; |
} |
public static void main(String[] args) { |
final BadgeListener bl = new BadgeListenerBluebox(); |
bl.readConfiguration(); |
bl.startDaemon(); |
} |
} |
/trunk/Modules/Module Badge/src/org/openconcerto/modules/badge/PlageHoraireSQLElement.java |
---|
18,7 → 18,7 |
import org.openconcerto.sql.element.SQLComponent; |
import org.openconcerto.ui.DefaultGridBagConstraints; |
import org.openconcerto.ui.JTime; |
import org.openconcerto.utils.CollectionMap; |
import org.openconcerto.utils.ListMap; |
public class PlageHoraireSQLElement extends ComptaSQLConfElement { |
41,8 → 41,8 |
} |
@Override |
public CollectionMap<String, String> getShowAs() { |
return CollectionMap.singleton(null, getComboFields()); |
public ListMap<String, String> getShowAs() { |
return ListMap.singleton(null, getComboFields()); |
} |
@Override |
/trunk/Modules/Module Badge/src/org/openconcerto/modules/badge/AdherentSQLElement.java |
---|
24,7 → 24,7 |
import org.openconcerto.ui.JDate; |
import org.openconcerto.ui.TitledSeparator; |
import org.openconcerto.ui.component.ITextArea; |
import org.openconcerto.utils.CollectionMap; |
import org.openconcerto.utils.ListMap; |
public class AdherentSQLElement extends ComptaSQLConfElement { |
public AdherentSQLElement() { |
61,8 → 61,8 |
} |
@Override |
public CollectionMap<String, String> getShowAs() { |
return CollectionMap.singleton(null, getComboFields()); |
public ListMap<String, String> getShowAs() { |
return ListMap.singleton(null, getComboFields()); |
} |
@Override |
/trunk/Modules/Module Badge/src/org/openconcerto/modules/badge/Module.java |
---|
53,8 → 53,8 |
import org.openconcerto.sql.view.list.RowAction; |
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline; |
import org.openconcerto.ui.PanelFrame; |
import org.openconcerto.utils.CollectionMap; |
import org.openconcerto.utils.FileUtils; |
import org.openconcerto.utils.ListMap; |
import org.openconcerto.utils.PrefType; |
import org.openconcerto.utils.cc.IClosure; |
185,8 → 185,8 |
} |
@Override |
public CollectionMap<String, String> getShowAs() { |
return CollectionMap.singleton(null, getComboFields()); |
public ListMap<String, String> getShowAs() { |
return ListMap.singleton(null, getComboFields()); |
} |
@Override |
303,7 → 303,7 |
} |
public static void main(String[] args) throws IOException { |
final File propsFile = new File("gestionModule.properties"); |
final File propsFile = new File("module.properties"); |
final ModuleFactory factory = new RuntimeModuleFactory(propsFile); |
/trunk/Modules/Module Badge/badge.properties |
---|
1,2 → 1,3 |
ip=192.168.1.147 |
relai=1 |
idSociete=42 |
/trunk/Modules/Module Badge/.classpath |
---|
2,6 → 2,6 |
<classpath> |
<classpathentry kind="src" path="src"/> |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> |
<classpathentry combineaccessrules="false" kind="src" path="/OpenConcerto GoogleCode"/> |
<classpathentry combineaccessrules="false" kind="src" path="/OpenConcerto"/> |
<classpathentry kind="output" path="bin"/> |
</classpath> |