summaryrefslogtreecommitdiffstats
path: root/gui/LocalizationNew.java
blob: 911dc9e02cd84473e0ab434304141aff8e333a28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
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;
	}
}