summaryrefslogtreecommitdiffstats
path: root/gui/Localization.java
blob: 8024c9e8301777a0014db769027e3ad173f7899d (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
package gui;

import helper.ListBTS;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Graphics2D;
import java.awt.TextField;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

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.CompoundPainter;
import org.jdesktop.swingx.painter.Painter;

import DataStructure.BayesAll;
import DataStructure.Interpolator;
import DataStructure.MobilePhone;
import DataStructure.PhoneContainer;
import DataStructure.SingleBTS;
import Parse.SqlPollerDate;
import Parse.SqlPollerUnThreaded;

public class Localization {
	static Interpolator map;

	private static ArrayList<SingleBTS> filterMR(ArrayList<SingleBTS> MR,
			JCheckBox[] btsFilter) {
		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(MR,
						content[i].ARFCN);
				if (elementsToAdd != null)
					result.addAll(elementsToAdd);
			}
		}
		return result;
	}

	/**
	 * @param args
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JFrame control = new JFrame();
		control.setSize(400, 300);
		JFrame filter = new JFrame("filter BTS");
		filter.setSize(200, 600);

		frame.setSize(400, 400);
		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);
		frame.add(mapViewer);
		System.out.println("da");

		SqlPollerUnThreaded sql = new SqlPollerUnThreaded();
		SqlPollerDate sqlDate;
		sqlDate = new SqlPollerDate(sql);
		TextField actionRefID = new TextField();
		TextField confidenceField = new TextField();
		control.add(actionRefID, BorderLayout.CENTER);
		JCheckBox useActionRef = new JCheckBox("use RefID");
		control.add(useActionRef, BorderLayout.SOUTH);
		confidenceField.setText("0.75");
		actionRefID.setText("146373");
		control.add(confidenceField, BorderLayout.NORTH);
		control.setVisible(true);
		Button next = new Button("next");
		control.add(next, BorderLayout.EAST);
		frame.setVisible(true);
		mapViewer.setZoom(18);
		try {

			map = readGSM("D:/parallel-strictdB-BER.obj");
			// map = readGSM("parallel-strictdB.obj");
			// map = readGSM("RichyUndKerstinInterpoliert.obj");
			// map = readGSM("parallel.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));

			// mapViewer.setTileFactory(new DefaultTileFactory(new
			// ownTileFactory(minimumZoomLevel, maximumZoomLevel, totalMapZoom,
			// tileSize, xr2l, yt2b, baseURL, xparam, yparam, zparam)))
			// mapViewer.getMainMap().

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		SingleBTS[] content = map.content();
		JCheckBox[] btsFilter = new JCheckBox[content.length];
		int y = 30;
		filter.setLayout(null);
		for (int i = 0; i < content.length; i++) {
			btsFilter[i] = new JCheckBox(Integer.toString(content[i].ARFCN),
					true);
			btsFilter[i].setLocation(30, y);
			btsFilter[i].setSize(70, 25);
			y += 30;
			filter.add(btsFilter[i]);
		}
		filter.validate();
		filter.setVisible(true);

		PhoneContainer container = new PhoneContainer();
		CompoundPainter cp = new CompoundPainter();
		BayesAll bayes = new BayesAll(map);

		// hack: make while (true) again!
		// long lastRefID = 0;

		while (true) {

			if (useActionRef.isSelected()) {

				try {
					double confidence = Double.parseDouble(confidenceField
							.getText());
					long refID = Long.parseLong(actionRefID.getText());

					ArrayList<SingleBTS> report = sqlDate.getActionRefID(refID).MR;

					// MobilePhone phone = sqlDate.getActionRefID(refID);
					MobilePhone phone = new MobilePhone();
					phone.bayes = bayes;
					phone.MR = filterMR(report, btsFilter);
					phone.correctCoordinate = sqlDate.getCoord(refID);
					long time = phone.getMinTime();
					// long timeMax = phone.getMaxTime();
					confidence = Double.parseDouble(confidenceField.getText());
					phone.locate(confidence);
					// phone.locateTimeSensitiv(confidence, (time + timeMax) /
					// 2);
					// phone.locateTimeSensitiv(confidence, timeMax - 3000);
					// Zeitschleife
					System.out.println(phone.localizationAccuracy());

					while (time <= phone.getMaxTime()) {
						// go out of while loop
						if (true)
							break;
						phone.MR = filterMR(report, btsFilter);
						// phone.MR = ListBTS.generateAveragedList(phone.MR);

						System.out.println("Zeit: " + new Date(time) + "("
								+ time + ")");
						confidence = Double.parseDouble(confidenceField
								.getText());
						// phone.locateTimeSensitiv(map, confidence, time);
						phone.locateTimeSensitiv(confidence, time);
						System.out.println(phone.localizationAccuracy());
						mapViewer.getMainMap().setOverlayPainter(phone);
						time += 3000;
					}
					System.out.println("Ende Zeitschleife... Neuanfang");

					// phone.locate(map, confidence, false);
					System.out.println("confidence: " + confidence);
					mapViewer.getMainMap().setOverlayPainter(phone);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					System.out.println("irgednwas anderes hat nicht geklappt");
					e.printStackTrace();
				}
			} else {
				container.removeAll();
				ArrayList<MobilePhone> phones = sql.getMRs();
				// MobilePhone hackPhone = new MobilePhone();

				// insert MR reports from phone!

				for (MobilePhone phone : phones) {
					phone.locate(map, 0.75, false);
					container.add(phone);
				}

				// make mappainter overlay

				cp.setCacheable(false);

				mapViewer.getMainMap().setOverlayPainter(container);
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	static private Interpolator readGSM(String filename)
			throws FileNotFoundException, IOException, ClassNotFoundException {

		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				filename));
		Interpolator map = (Interpolator) ois.readObject();
		System.out.println("GSMMap eingelesen");
		return map;
	}

}

class locationPainter implements Painter<JXMapViewer> {
	private ArrayList<MobilePhone> phones = new ArrayList<MobilePhone>();

	// private Interpolator map;

	public locationPainter(ArrayList<MobilePhone> phones, Interpolator map) {
		this.phones = phones;
		// this.map = map;
		for (int i = 0; i < phones.size(); i++) {
			phones.get(i).locate(map, false);
		}

	}

	@Override
	public void paint(Graphics2D g, JXMapViewer map, int w, int h) {

		for (int i = 0; i < phones.size(); i++) {

		}

	}

}

class ownTileFactory extends TileFactoryInfo {

	public ownTileFactory(int minimumZoomLevel, int maximumZoomLevel,
			int totalMapZoom, int tileSize, boolean xr2l, boolean yt2b,
			String baseURL, String xparam, String yparam, String zparam) {
		super(0, 18, 18, 256, true, true, "http://b.tile.openstreetmap.org",
				xparam, yparam, zparam);
		maximumZoomLevel = 18;
		minimumZoomLevel = 0;
		totalMapZoom = 18;

		// TODO Auto-generated constructor stub
	}

}