summaryrefslogtreecommitdiffstats
path: root/gui/LocalizationNew.java
diff options
context:
space:
mode:
authorRichard Zahoransky2011-11-07 16:29:56 +0100
committerRichard Zahoransky2011-11-07 16:29:56 +0100
commit08d5f7b0a0b24c042aa5976f66bf3a1b5b754478 (patch)
treeba5388774100c1b218cb264927c3bb3669fd7e06 /gui/LocalizationNew.java
parentinit (diff)
downloadlocalization-08d5f7b0a0b24c042aa5976f66bf3a1b5b754478.tar.gz
localization-08d5f7b0a0b24c042aa5976f66bf3a1b5b754478.tar.xz
localization-08d5f7b0a0b24c042aa5976f66bf3a1b5b754478.zip
Localization Code. How-To will follow...
Diffstat (limited to 'gui/LocalizationNew.java')
-rw-r--r--gui/LocalizationNew.java773
1 files changed, 773 insertions, 0 deletions
diff --git a/gui/LocalizationNew.java b/gui/LocalizationNew.java
new file mode 100644
index 0000000..911dc9e
--- /dev/null
+++ b/gui/LocalizationNew.java
@@ -0,0 +1,773 @@
+package gui;
+
+import helper.ListBTS;
+
+import java.awt.BorderLayout;
+import java.awt.TextField;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Date;
+
+import javax.swing.DefaultListModel;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.JScrollPane;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+
+import org.jdesktop.swingx.JXMapKit;
+import org.jdesktop.swingx.JXMapKit.DefaultProviders;
+import org.jdesktop.swingx.JXMapViewer;
+import org.jdesktop.swingx.mapviewer.DefaultTileFactory;
+import org.jdesktop.swingx.mapviewer.GeoPosition;
+import org.jdesktop.swingx.mapviewer.TileFactoryInfo;
+import org.jdesktop.swingx.painter.Painter;
+
+import DataStructure.BayesAll;
+import DataStructure.GSMMap;
+import DataStructure.Interpolator;
+import DataStructure.MobilePhone;
+import DataStructure.SingleBTS;
+import Parse.SqlPollerDate;
+
+public class LocalizationNew {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ UserInterface.showFrames();
+ UserInterface.setLive(false);
+ UserInterface.setIMSIorRef(153227);
+ try {
+ SqlPollerDate sql = new SqlPollerDate();
+ BayesAll bayes = new BayesAll(UserInterface.getGsmMap());
+ MobilePhone phone = new MobilePhone(bayes);
+
+ while (true) {
+ try {
+ if (!UserInterface.getLive()
+ && UserInterface.getIMSI() != 0) {
+ phone = getPhoneRefID(UserInterface.getIMSI(), bayes,
+ sql);
+
+ if (!UserInterface.getStep()) {
+ // Locate based on average of all MRs
+ phone.locate(UserInterface.getConfidence());
+ UserInterface.setMapPainter(phone);
+ } else {
+ long time = phone.getMinTime();
+ long maxtime = phone.getMaxTime();
+ while (time < maxtime && UserInterface.getStep()) {
+ phone.locateTimeSensitiv(
+ UserInterface.getConfidence(), time);
+ time += 2000;
+ UserInterface.setMapPainter(phone);
+ }
+ }
+
+ } else if (UserInterface.getLive()
+ && UserInterface.getIMSI() != 0) {
+ phone = getPhoneLastSeen(UserInterface.getIMSI(),
+ bayes, sql);
+ phone.locate(UserInterface.getConfidence());
+ UserInterface.setMapPainter(phone);
+ }
+ System.gc();
+ Thread.sleep(500);
+ } catch (Exception e) {
+ System.out.println("Error. Cannot Localize.");
+ // e.printStackTrace();
+ }
+ }
+
+ } catch (ClassNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ System.out.println("No Internet Connection?");
+ }
+
+ }
+
+ private static MobilePhone getPhoneLastSeen(long imsi, BayesAll bayes,
+ SqlPollerDate sql) throws SQLException {
+ // TODO Auto-generated method stub
+ MobilePhone phone = new MobilePhone();
+ phone = sql.getLastMRs(imsi);
+ phone.bayes = bayes;
+ if (UserInterface.getFilter())
+ BTSFilter.filterMR(phone);
+ return phone;
+ }
+
+ private static MobilePhone getPhoneRefID(long IMSI, BayesAll bayes,
+ SqlPollerDate sql) throws SQLException {
+ MobilePhone phone = sql.getActionRefID(UserInterface.getIMSI());
+ phone.bayes = bayes;
+ if (UserInterface.getFilter())
+ BTSFilter.filterMR(phone);
+ return phone;
+ }
+}
+
+/**
+ * Creates user Interface for Localization
+ *
+ * @author richy
+ *
+ */
+class UserInterface {
+ private static TextField input;
+ private static JCheckBox live;
+ private static JCheckBox history;
+ private static TextField confidence;
+ private static JXMapKit mapViewer;
+ private static GSMMap map;
+ private static JCheckBox filter;
+
+ /**
+ * Show UserInterface
+ */
+ public static void showFrames() {
+ JFrame map = new JFrame();
+ map.addWindowListener(new quit());
+ map.setSize(600, 450);
+ mapViewer = getMapViewer();
+ map.add(mapViewer);
+ map.setVisible(true);
+ JFrame control = new JFrame();
+ control.addWindowListener(new quit());
+ control.setSize(400, 300);
+ control.setLayout(null);
+ // JCheckBox imsiOrActionRef = new JCheckBox("IMSI");
+ input = new TextField("187970");
+ input.setLocation(20, 20);
+ input.setSize(200, 20);
+ final JLabel imsiLabel = new JLabel("IMSI");
+ imsiLabel.setSize(200, 20);
+ imsiLabel.setLocation(20, 0);
+
+ live = new JCheckBox("Last known position");
+ live.setSelected(true);
+ live.setLocation(20, 40);
+ live.setSize(200, 20);
+ live.addChangeListener(new ChangeListener() {
+ @Override
+ public void stateChanged(ChangeEvent e) {
+
+ if (live.isSelected()) {
+ imsiLabel.setText("IMSI");
+ // System.out.println("Enabled");
+ } else {
+ imsiLabel.setText("ActionRefID");
+ // System.out.println("Disabled");
+ }
+ }
+ });
+ JLabel confidenceLabel = new JLabel("Confidence");
+ confidenceLabel.setLocation(230, 0);
+ confidenceLabel.setSize(100, 20);
+
+ confidence = new TextField("0.75");
+ confidence.setLocation(230, 20);
+ confidence.setSize(60, 20);
+
+ history = new JCheckBox("Step by Step");
+ history.setLocation(20, 60);
+ history.setSize(200, 20);
+
+ filter = new JCheckBox("filter BTS");
+ filter.setLocation(20, 80);
+ filter.setSize(200, 20);
+ filter.addActionListener(new ActionListener() {
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ if (filter.isSelected()) {
+ BTSFilter.showFilter(getGsmMap());
+ } else {
+ BTSFilter.hideFilter();
+ }
+
+ }
+ });
+
+ JButton getFromName = new JButton("Get IMSI");
+ getFromName.setLocation(20, 120);
+ getFromName.setSize(80, 20);
+ getFromName.addActionListener(new ImsiFromName());
+
+ JButton getActions = new JButton("Get Actions");
+ getActions.setLocation(120, 120);
+ getActions.setSize(120, 20);
+ getActions.addActionListener(new ActionFromIMSI());
+
+ control.add(input);
+ control.add(imsiLabel);
+ control.add(live);
+ control.add(confidenceLabel);
+ control.add(confidence);
+ control.add(history);
+ control.add(getFromName);
+ control.add(getActions);
+ control.add(filter);
+ control.setVisible(true);
+ }
+
+ private static JXMapKit getMapViewer() {
+ JXMapKit mapViewer = new JXMapKit();
+ // JXMapViewer
+ // Painter
+ // mapViewer.setDefaultProvider(DefaultProviders.OpenStreetMaps);
+ mapViewer.setDefaultProvider(DefaultProviders.Custom);
+
+ TileFactoryInfo ownTileFactory = new TileFactoryInfo(0, 18, 18, 256,
+ false, false, "http://c.tile.openstreetmap.org", "/", "/", "/") {
+ public String getTileUrl(int x, int y, int zoom) {
+ return this.baseURL + "/" + (18 - zoom) + "/" + x + "/" + y
+ + ".png";
+ }
+ };
+ mapViewer.setTileFactory(new DefaultTileFactory(ownTileFactory));
+
+ mapViewer.setDataProviderCreditShown(true);
+
+ try {
+ map = readGSM("parallel-strictdB-BER.obj");
+ mapViewer.setAddressLocation(new GeoPosition(
+ (map.minY + map.maxY) / 2, (map.minX + map.maxX) / 2));
+ mapViewer.setAddressLocationShown(true);
+ mapViewer.setZoom(1);
+ mapViewer.setCenterPosition(new GeoPosition(
+ (map.minY + map.maxY) / 2, (map.minX + map.maxX) / 2));
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ System.out.println("GSM-map not readable");
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ System.out.println("GSM-map not readable");
+ } catch (ClassNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return mapViewer;
+ }
+
+ public static void setMapPainter(Painter<JXMapViewer> painter) {
+ mapViewer.getMainMap().setOverlayPainter(painter);
+ }
+
+ public static GSMMap getGsmMap() {
+ return map;
+ }
+
+ static private Interpolator readGSM(String filename)
+ throws FileNotFoundException, IOException, ClassNotFoundException {
+ System.out.println("trying to read GSM-map...");
+ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
+ filename));
+ Interpolator map = (Interpolator) ois.readObject();
+ System.out.println("GSMMap read successful");
+ return map;
+ }
+
+ public static long getIMSI() {
+ try {
+ return Long.parseLong(input.getText().trim());
+ } catch (Exception e) {
+ return 0;
+ }
+ }
+
+ public static void setIMSIorRef(long IMSI) {
+ try {
+ input.setText(Long.toString(IMSI));
+ } catch (NullPointerException e) {
+ setIMSIorRef(0);
+ }
+ }
+
+ public static void setLive(boolean value) {
+ live.setSelected(value);
+ }
+
+ public static boolean getLive() {
+ return live.isSelected();
+ }
+
+ public static boolean getStep() {
+ return history.isSelected();
+ }
+
+ public static boolean getFilter() {
+ return filter.isSelected();
+ }
+
+ public static void setFilter(boolean value) {
+ filter.setSelected(value);
+ }
+
+ public static double getConfidence() {
+ try {
+ return Double.parseDouble(confidence.getText().trim());
+ } catch (NumberFormatException e) {
+ return 0.75;
+ }
+ }
+}
+
+class ImsiFromName implements ActionListener {
+ // private name;
+ // JFrame result;
+ DefaultListModel listModel;
+ long[] IMSIs;
+
+ private void createFrame() {
+ final JFrame result = new JFrame("Search for IMSIs from Name");
+ result.setSize(400, 400);
+ JButton search = new JButton("search");
+
+ JButton ok = new JButton("OK");
+
+ final TextField name = new TextField();
+ listModel = new DefaultListModel();
+ final JList list = new JList(listModel);
+ list.setSelectionMode(0);
+ JScrollPane listScroller = new JScrollPane(list);
+ result.add(listScroller, BorderLayout.CENTER);
+ result.add(name, BorderLayout.NORTH);
+ result.add(ok, BorderLayout.SOUTH);
+ result.add(search, BorderLayout.EAST);
+ result.addWindowListener(new close());
+ result.setVisible(true);
+
+ search.addActionListener(new ActionListener() {
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ try {
+ IMSIs = searchForIMSI(name.getText().trim());
+ } catch (ClassNotFoundException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (SQLException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
+ }
+ });
+
+ ok.addActionListener(new ActionListener() {
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ // get choosen index within list
+ int i = list.getSelectedIndex();
+ try {
+ UserInterface.setIMSIorRef(IMSIs[i]);
+ UserInterface.setLive(true);
+ } catch (ArrayIndexOutOfBoundsException e1) {
+
+ } finally {
+ // result.dispose();
+ }
+
+ }
+ });
+
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ createFrame();
+
+ // result.dispose();
+
+ }
+
+ private long[] searchForIMSI(String name) throws ClassNotFoundException,
+ SQLException {
+ // clear the list-window
+ listModel.clear();
+
+ // get IMSIs for given name
+ name = name.trim();
+ Connection cn = getConnection();
+ // Connection is ready
+ // get IMSIs now...
+ Statement st = cn.createStatement();
+ st.setQueryTimeout(3);
+ ResultSet rs = st.executeQuery(query(name));
+ // IMSIs are now stored in rs.
+ ArrayList<Long> IMSIs = new ArrayList<Long>();
+ while (rs.next()) {
+ // add to result
+ IMSIs.add(rs.getLong("imsi"));
+ // add to list
+ listModel.addElement(rs.getLong("imsi") + " ("
+ + rs.getString("extension") + ")");
+ }
+ long[] result = new long[IMSIs.size()];
+ for (int i = 0; i < result.length; i++) {
+ result[i] = IMSIs.get(i);
+ }
+ rs.close();
+ st.close();
+ cn.close();
+ return result;
+ }
+
+ private Connection getConnection() throws ClassNotFoundException,
+ SQLException {
+ Class.forName("com.mysql.jdbc.Driver");
+ Connection cn = DriverManager
+ .getConnection("jdbc:mysql://132.230.4.13:3306/hlr", "richard",
+ "uh237Aug.ad7");
+ cn.setReadOnly(true);
+ return cn;
+ }
+
+ private String query(String name) {
+
+ String query = "select imsi, extension from User JOIN UserWatch ON"
+ + " surname = '" + name + "' AND user_id = User.id "
+ + "JOIN Subscriber ON Subscriber.id = subscriber_id";
+
+ return query;
+ }
+
+}
+
+class ActionFromIMSI implements ActionListener {
+ DefaultListModel listModel;
+ long[] Actions;
+
+ private void createFrame() {
+ final JFrame result = new JFrame("Search for Actions from IMSI");
+ result.setSize(400, 400);
+ JButton search = new JButton("search");
+
+ JButton ok = new JButton("OK");
+
+ final TextField imsi = new TextField(Long.toString(UserInterface
+ .getIMSI()));
+ listModel = new DefaultListModel();
+ final JList list = new JList(listModel);
+ list.setSelectionMode(0);
+ JScrollPane listScroller = new JScrollPane(list);
+ result.add(listScroller, BorderLayout.CENTER);
+ result.add(imsi, BorderLayout.NORTH);
+ result.add(ok, BorderLayout.SOUTH);
+ result.add(search, BorderLayout.EAST);
+ result.addWindowListener(new close());
+ result.setVisible(true);
+
+ search.addActionListener(new ActionListener() {
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ try {
+ Actions = searchForAction(imsi.getText().trim());
+
+ } catch (ClassNotFoundException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (SQLException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
+ }
+
+ private long[] searchForAction(String imsi)
+ throws ClassNotFoundException, SQLException {
+ imsi = imsi.trim();
+ Connection cn = getConnection();
+ Statement st = cn.createStatement();
+ st.setQueryTimeout(3);
+ ResultSet rs = st.executeQuery(query(imsi));
+ ArrayList<Long> IDs = new ArrayList<Long>(500);
+ listModel.clear();
+ while (rs.next()) {
+ Date time = rs.getTimestamp("cur_timestamp");
+ String reason = rs.getString("name");
+ String actionID = rs.getString("id");
+ actionID current = new actionID(time, reason, actionID);
+ listModel.addElement(current);
+ IDs.add(Long.parseLong(actionID));
+ }
+
+ long[] result = new long[IDs.size()];
+ for (int i = 0; i < result.length; i++) {
+ result[i] = IDs.get(i);
+ }
+ rs.close();
+ st.close();
+ cn.close();
+ return result;
+ }
+ });
+
+ ok.addActionListener(new ActionListener() {
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ // get choosen index within list
+ int i = list.getSelectedIndex();
+ try {
+ UserInterface.setIMSIorRef(Actions[i]);
+ UserInterface.setLive(false);
+ } catch (ArrayIndexOutOfBoundsException e1) {
+
+ } finally {
+ // result.dispose();
+ }
+
+ }
+ });
+
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent arg0) {
+ createFrame();
+ }
+
+ private Connection getConnection() throws ClassNotFoundException,
+ SQLException {
+ Class.forName("com.mysql.jdbc.Driver");
+ Connection cn = DriverManager.getConnection(
+ "jdbc:mysql://132.230.4.13:3306/logging", "richard",
+ "uh237Aug.ad7");
+ cn.setReadOnly(true);
+ return cn;
+ }
+
+ private String query(String imsi) {
+
+ String query = "Select action.cur_timestamp, action.id, "
+ + "Name from action JOIN actionIdentifier ON "
+ + "actionID = actionIdentifier.id WHERE " + "IMSI = '" + imsi
+ + "' ORDER BY action.id desc LIMIT 0,500";
+
+ return query;
+ }
+}
+
+class close implements WindowListener {
+
+ @Override
+ public void windowOpened(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowIconified(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowDeiconified(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowDeactivated(WindowEvent e) {
+ // ((JFrame) e.getSource()).dispose();
+
+ }
+
+ @Override
+ public void windowClosing(WindowEvent e) {
+ ((JFrame) e.getSource()).dispose();
+ }
+
+ @Override
+ public void windowClosed(WindowEvent e) {
+ // TODO Auto-generated method stub
+ // result.dispose();
+ }
+
+ @Override
+ public void windowActivated(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
+
+class quit implements WindowListener {
+
+ @Override
+ public void windowActivated(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowClosed(WindowEvent e) {
+ System.exit(0);
+
+ }
+
+ @Override
+ public void windowClosing(WindowEvent e) {
+ System.exit(0);
+
+ }
+
+ @Override
+ public void windowDeactivated(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowDeiconified(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowIconified(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowOpened(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
+
+class actionID {
+ Date time;
+ String reason;
+ String actionID;
+
+ public actionID(Date time, String reason, String actionID) {
+ this.time = time;
+ this.reason = reason;
+ this.actionID = actionID;
+ }
+
+ public String toString() {
+ return actionID + ": " + time + " (" + reason + ")";
+ }
+}
+
+class BTSFilter {
+ private static JCheckBox[] btsFilter;
+ private static JFrame filter;
+ private static GSMMap map;
+
+ static void showFilter(GSMMap GSMmap) {
+ map = GSMmap;
+ filter = new JFrame("filter BTS");
+ filter.addWindowListener(new WindowListener() {
+
+ @Override
+ public void windowOpened(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowIconified(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowDeiconified(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowDeactivated(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void windowClosing(WindowEvent e) {
+ UserInterface.setFilter(false);
+
+ }
+
+ @Override
+ public void windowClosed(WindowEvent e) {
+ UserInterface.setFilter(false);
+
+ }
+
+ @Override
+ public void windowActivated(WindowEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+ });
+ filter.setSize(70, 250);
+ SingleBTS[] content = map.content();
+ btsFilter = new JCheckBox[content.length];
+ int y = 5;
+ filter.setLayout(null);
+ for (int i = 0; i < content.length; i++) {
+ btsFilter[i] = new JCheckBox(Integer.toString(content[i].ARFCN),
+ true);
+ btsFilter[i].setLocation(10, y);
+ btsFilter[i].setSize(70, 25);
+ y += 20;
+ filter.add(btsFilter[i]);
+ }
+ filter.validate();
+ filter.setVisible(true);
+ }
+
+ static void hideFilter() {
+ filter.setVisible(false);
+ }
+
+ static void filterMR(MobilePhone phone) {
+ SingleBTS[] content = map.content();
+ ArrayList<SingleBTS> result = new ArrayList<SingleBTS>();
+ for (int i = 0; i < content.length; i++) {
+ if (btsFilter[i].isSelected()) {
+ // add this BTS
+ ArrayList<SingleBTS> elementsToAdd = ListBTS.getAllARFCN(
+ phone.MR, content[i].ARFCN);
+ if (elementsToAdd != null)
+ result.addAll(elementsToAdd);
+ }
+ }
+ phone.MR = result;
+ }
+} \ No newline at end of file