dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #00746
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 195: smslistener: Added inf file for storing port number. Moved gateway setup to Start Listener. Added...
------------------------------------------------------------
revno: 195
author: Saptarshi
committer: sunbiz <sunbiz@hispindia>
branch nick: trunk
timestamp: Mon 2009-04-20 01:41:50 +0530
message:
smslistener: Added inf file for storing port number. Moved gateway setup to Start Listener. Added SMS decompression at gateway
modified:
local/in/SMSListener/src/org/hispindia/smslistener/SMSListener.java
local/in/SMSListener/src/org/hispindia/smslistener/SettingsWindow.form
local/in/SMSListener/src/org/hispindia/smslistener/SettingsWindow.java
=== modified file 'local/in/SMSListener/src/org/hispindia/smslistener/SMSListener.java'
--- local/in/SMSListener/src/org/hispindia/smslistener/SMSListener.java 2009-04-10 16:30:05 +0000
+++ local/in/SMSListener/src/org/hispindia/smslistener/SMSListener.java 2009-04-19 20:11:50 +0000
@@ -8,8 +8,14 @@
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
+import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
@@ -17,6 +23,7 @@
import org.smslib.AGateway.Protocols;
import org.smslib.GatewayException;
import org.smslib.IInboundMessageNotification;
+import org.smslib.InboundBinaryMessage;
import org.smslib.InboundMessage;
import org.smslib.Message.MessageTypes;
import org.smslib.SMSLibException;
@@ -28,22 +35,10 @@
private Service service;
private InboundNotification inboundNotification;
+ private int PORT = 8; //default port
+ private File infFile = new File(System.getProperty("user.home") + "/.smslistener", "SMSListener.inf");
public SMSListener() {
- showTrayIcon();
-
- service = new Service();
- System.out.println("#######Service Created Successfully");
- System.out.println("Listening on port: COM" + SettingsWindow.portNumber);
- inboundNotification = new InboundNotification();
- SerialModemGateway gateway = new SerialModemGateway("modem.com" + SettingsWindow.portNumber, "COM" + SettingsWindow.portNumber, 115200, "Generic USB", "generic-usb-modem");
- System.out.println("#######Gateway Created Successfully");
- gateway.setProtocol(Protocols.PDU);
- gateway.setInbound(true);
- gateway.setOutbound(false);
- service.setInboundNotification(inboundNotification);
- service.addGateway(gateway);
- System.out.println("#######Gateway Added to Service");
}
public class InboundNotification implements IInboundMessageNotification {
@@ -73,7 +68,6 @@
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
- System.out.println("#######Exit Item pressed");
System.exit(0);
}
});
@@ -82,11 +76,9 @@
public void actionPerformed(ActionEvent evt) {
if (serviceItem.getLabel().equals("Start SMS Listening")) {
- System.out.println("#######Start Service Pressed");
startSMSListener();
serviceItem.setLabel("Stop SMS Listening");
} else {
- System.out.println("#######Stop Service Pressed");
stopSMSListener();
serviceItem.setLabel("Start SMS Listening");
}
@@ -123,15 +115,23 @@
//<editor-fold defaultstate="collapsed" desc=" Start Service ">
private void startSMSListener() {
+ service = new Service();
+ inboundNotification = new InboundNotification();
+ SerialModemGateway gateway = new SerialModemGateway("modem.com" + this.PORT, "COM" + this.PORT, 115200, "Generic USB", "generic-usb-modem");
+ gateway.setProtocol(Protocols.PDU);
+ gateway.setInbound(true);
+ gateway.setOutbound(false);
+ service.setInboundNotification(inboundNotification);
+ service.addGateway(gateway);
try {
- System.out.println("Starting Service on Com:"+SettingsWindow.portNumber);
+ System.out.println("Starting Service on Com:" + this.PORT);
service.startService();
} catch (SMSLibException ex) {
- Logger.getLogger(SMSListener.class.getName()).log(Level.SEVERE, null, ex);
+ JOptionPane.showMessageDialog(null, ex.getMessage());
} catch (IOException ex) {
- Logger.getLogger(SMSListener.class.getName()).log(Level.SEVERE, null, ex);
+ JOptionPane.showMessageDialog(null, ex.getMessage());
} catch (InterruptedException ex) {
- Logger.getLogger(SMSListener.class.getName()).log(Level.SEVERE, null, ex);
+ JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
//</editor-fold>
@@ -139,7 +139,7 @@
//<editor-fold defaultstate="collapsed" desc=" Stop Service ">
private void stopSMSListener() {
try {
- System.out.println("Stopping Service on Com:"+SettingsWindow.portNumber);
+ System.out.println("Stopping Service on Com:" + this.PORT);
service.stopService();
} catch (TimeoutException ex) {
Logger.getLogger(SMSListener.class.getName()).log(Level.SEVERE, null, ex);
@@ -154,20 +154,79 @@
//</editor-fold>
private void processMessage(InboundMessage message) {
- //System.out.println("PDU HEX String:" + message.getPduUserData());
- //System.out.println("PDU HEX String#:" + message.getPduUserData().length());
- InboundMessage textMsg = (InboundMessage) message;
- String compressedText = textMsg.getText();
- System.out.println("Compressed text: " + compressedText);
-
- //byte[] compressedData = compressedText.getBytes("UTF-8");
- //String unCompressedText = new String(Compressor.decompress(compressedData), "UTF-8");
- System.out.println("Uncompressed text: " + compressedText);
- XMLCreator xmlcreator = new XMLCreator();
- xmlcreator.writeXML(message.getOriginator(), new Timestamp(message.getDate().getTime()).toString(), compressedText);
+ InboundBinaryMessage binaryMsg = (InboundBinaryMessage) message;
+ try {
+ byte[] compressedData = binaryMsg.getDataBytes();
+ String unCompressedText = new String(Compressor.decompress(compressedData), "UTF-8");
+ XMLCreator xmlcreator = new XMLCreator();
+ xmlcreator.writeXML(message.getOriginator(), new Timestamp(message.getDate().getTime()).toString(), unCompressedText);
+ } catch (UnsupportedEncodingException uneex) {
+ JOptionPane.showMessageDialog(null, "Message Decryption Error: " + uneex.getMessage());
+ }
+ }
+
+ public void setPort(int portNumber) {
+ try {
+ Properties prop = new Properties();
+ if (infFile.exists()) {
+ FileOutputStream fos = new FileOutputStream(infFile);
+ prop.setProperty("com.port", Integer.toString(portNumber));
+ prop.store(fos, "COM Port Property");
+ fos.close();
+ } else {
+ JOptionPane.showMessageDialog(null, "Properties File Not Found at: " + infFile.getAbsolutePath());
+ }
+ } catch (FileNotFoundException fnfex) {
+ JOptionPane.showMessageDialog(null, "Properties File Exception: " + fnfex.getMessage());
+ } catch (IOException ioex) {
+ JOptionPane.showMessageDialog(null, "Properties I/O Exception: " + ioex.getMessage());
+ } catch (NumberFormatException nfex) {
+ JOptionPane.showMessageDialog(null, "Properties Port Number Exception: " + nfex.getMessage());
+ } catch (SecurityException nfex) {
+ JOptionPane.showMessageDialog(null, "Properties File Exception: " + nfex.getMessage());
+ }
+ }
+
+ public int getPort() {
+ try {
+ Properties prop = new Properties();
+ if (infFile.exists()) {
+ FileInputStream fis = new FileInputStream(infFile);
+ prop.load(fis);
+ this.PORT = Integer.parseInt(prop.getProperty("com.port"));
+ fis.close();
+ } else {
+ JOptionPane.showMessageDialog(null, "Properties File Not Found at: " + infFile.getAbsolutePath());
+ }
+ } catch (FileNotFoundException fnfex) {
+ JOptionPane.showMessageDialog(null, "Properties File Exception: " + fnfex.getMessage());
+ } catch (IOException ioex) {
+ JOptionPane.showMessageDialog(null, "Properties I/O Exception: " + ioex.getMessage());
+ } catch (NumberFormatException nfex) {
+ JOptionPane.showMessageDialog(null, "Properties Port Number Exception: " + nfex.getMessage());
+ } catch (SecurityException nfex) {
+ JOptionPane.showMessageDialog(null, "Properties File Exception: " + nfex.getMessage());
+ }
+ return this.PORT;
}
public static void main(String args[]) {
- SMSListener obj = new SMSListener();
+ SMSListener app = new SMSListener();
+ app.showTrayIcon();
+
+ if (app.infFile.exists()) {
+ app.getPort();
+ } else {
+ try {
+ if (!app.infFile.getParentFile().exists()) {
+ app.infFile.getParentFile().mkdir();
+ }
+ app.infFile.createNewFile();
+ app.setPort(app.PORT);
+ } catch (IOException ioex) {
+ JOptionPane.showMessageDialog(null, "Properties I/O Exception: " + ioex.getMessage());
+ System.exit(1);
+ }
+ }
}
}
\ No newline at end of file
=== modified file 'local/in/SMSListener/src/org/hispindia/smslistener/SettingsWindow.form'
--- local/in/SMSListener/src/org/hispindia/smslistener/SettingsWindow.form 2009-03-09 05:48:42 +0000
+++ local/in/SMSListener/src/org/hispindia/smslistener/SettingsWindow.form 2009-04-19 20:11:50 +0000
@@ -33,7 +33,7 @@
<EmptySpace min="-2" pref="98" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="1" attributes="0">
- <EmptySpace pref="137" max="32767" attributes="0"/>
+ <EmptySpace pref="153" max="32767" attributes="0"/>
<Component id="saveCmd" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
=== modified file 'local/in/SMSListener/src/org/hispindia/smslistener/SettingsWindow.java'
--- local/in/SMSListener/src/org/hispindia/smslistener/SettingsWindow.java 2009-03-09 05:48:42 +0000
+++ local/in/SMSListener/src/org/hispindia/smslistener/SettingsWindow.java 2009-04-19 20:11:50 +0000
@@ -1,26 +1,13 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-
-/*
- * SettingsWindow.java
- *
- * Created on Feb 6, 2009, 3:45:50 PM
- */
package org.hispindia.smslistener;
-/**
- *
- * @author Administrator
- */
public class SettingsWindow extends javax.swing.JFrame {
- public static int portNumber = 8; //default set to 8
+ private SMSListener app = new SMSListener();
/** Creates new form SettingsWindow */
public SettingsWindow() {
initComponents();
+ portNumCombo.setSelectedIndex(app.getPort() - 1);
}
/** This method is called from within the constructor to
@@ -68,7 +55,7 @@
.addComponent(portNumCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(98, 98, 98))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(137, Short.MAX_VALUE)
+ .addContainerGap(153, Short.MAX_VALUE)
.addComponent(saveCmd)
.addContainerGap())
);
@@ -88,7 +75,7 @@
}// </editor-fold>//GEN-END:initComponents
private void portNumComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_portNumComboActionPerformed
- SettingsWindow.portNumber = portNumCombo.getSelectedIndex() + 1;
+ app.setPort(portNumCombo.getSelectedIndex() + 1);
}//GEN-LAST:event_portNumComboActionPerformed
private void saveCmdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveCmdActionPerformed
@@ -106,7 +93,6 @@
}
});
}
-
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox portNumCombo;
private javax.swing.JLabel portNumLabel;
--
Trunk
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription.