Skip to content

Commit f478639

Browse files
alimonculerenkundakci
authored andcommitted
TCP connection for Target Location added.
1 parent 508f5f4 commit f478639

File tree

1 file changed

+85
-66
lines changed

1 file changed

+85
-66
lines changed

app/src/main/java/com/teamfire/picontroller/AutoDriveActivity.java

+85-66
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ public class AutoDriveActivity extends AppCompatActivity {
4444
public String newUrl;
4545
private boolean isClientRunning = false;
4646
public static int CMD = 99;
47+
public static String targetLocation;
4748
public static int UDP_LocationReceivingPort = 11445;
4849

4950
Button btn_manualDrive, btn_camera, btn_showCoordinates, btn_GPS;
5051
WebView wb_liveFeed;
5152
EditText ipAddress, mapLat, mapLon;
5253
MapView map = null;
5354
DatagramSocket ds = null;
55+
String gpsresponse;
5456

5557
@Override
5658
protected void onCreate(Bundle savedInstanceState) {
@@ -140,8 +142,7 @@ public void onClick(View view) {
140142
map.invalidate();
141143

142144
getIPandPort();
143-
SendTargetAsyncTask send_targetLocation = new SendTargetAsyncTask();
144-
send_targetLocation.execute();
145+
new SendTargetAsyncTask().execute();
145146
} catch (Exception e) {
146147
e.printStackTrace();
147148
}
@@ -153,7 +154,7 @@ public void onClick(View view) {
153154
public void onClick(View view) {
154155
if (!isClientRunning) {
155156
Log.e("UDP", "onClick: Reading UDP Packet...");
156-
receivePiLocation();
157+
new ReceivePiLocation().execute();
157158
} else {
158159
Log.e("UDP", "onClick: A Thread already started and not finished.");
159160
}
@@ -162,41 +163,6 @@ public void onClick(View view) {
162163

163164
}
164165

165-
public void setMapView(String result) {
166-
//parse the result values
167-
String regex = "(.)*(\\d)(.)*";
168-
Pattern pattern = Pattern.compile(regex);
169-
boolean containsNumber = pattern.matcher(result).matches();
170-
171-
double lat = 0;
172-
double lon = 0;
173-
if (containsNumber) {
174-
String[] coords = result.split(",");
175-
lat = Double.parseDouble(coords[0]);
176-
String templon = coords[1];
177-
Pattern p = Pattern.compile("\\d*\\.\\d+");
178-
Matcher m = p.matcher(templon);
179-
while (m.find()) {
180-
lon = Double.parseDouble(m.group());
181-
}
182-
183-
final IMapController mapViewController = map.getController();
184-
mapViewController.setCenter(new GeoPoint(lat, lon));
185-
mapViewController.setZoom(15.0d);
186-
187-
//add the gps location marker
188-
Marker GPSMarker = new Marker(map);
189-
final GeoPoint markerPointGPS = new GeoPoint(lat, lon);
190-
GPSMarker.setPosition(markerPointGPS);
191-
GPSMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
192-
GPSMarker.setIcon(ContextCompat.getDrawable(this, R.drawable.gps_marker));
193-
GPSMarker.setTitle("Device Location");
194-
map.getOverlays().add(GPSMarker);
195-
map.invalidate();
196-
} else {
197-
Toast.makeText(getApplicationContext(), "Can't read GPS data.", Toast.LENGTH_LONG).show();
198-
}
199-
}
200166

201167
public void getIPandPort() {
202168
String iPandPort = ipAddress.getText().toString();
@@ -230,45 +196,98 @@ protected void onStop() {
230196
editor.commit();
231197
}
232198

233-
private void receivePiLocation() {
234-
new Thread(new Runnable() {
235-
@Override
236-
public void run() {
237-
String lText;
238-
byte[] lMsg = new byte[16];
239-
DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
240-
try {
241-
isClientRunning = true;
242-
ds = new DatagramSocket(UDP_LocationReceivingPort);
243-
ds.setSoTimeout(7000);
244-
ds.setReuseAddress(true);
245-
ds.receive(dp);
246-
lText = new String(dp.getData());
247-
Log.e("UDP", "run: paket alındı!" + lText);
248-
} catch (SocketException e) {
249-
e.printStackTrace();
250-
} catch (IOException e) {
251-
e.printStackTrace();
252-
} finally {
253-
if (ds != null) {
254-
ds.close();
255-
isClientRunning = false;
256-
}
199+
public void setMapView(String result) {
200+
//parse the result values
201+
String regex = "(.)*(\\d)(.)*";
202+
Pattern pattern = Pattern.compile(regex);
203+
try {
204+
boolean containsNumber = pattern.matcher(result).matches();
205+
double lat = 0;
206+
double lon = 0;
207+
double header = 0;
208+
if (containsNumber) {
209+
String[] coords = result.split("/");
210+
lat = Double.parseDouble(coords[0]);
211+
lon = Double.parseDouble(coords[1]);
212+
String tempHeader = coords[2];
213+
Pattern p = Pattern.compile("\\d*\\.\\d+");
214+
Matcher m = p.matcher(tempHeader);
215+
while (m.find()) {
216+
header = Double.parseDouble(m.group());
257217
}
218+
219+
final IMapController mapViewController = map.getController();
220+
mapViewController.setCenter(new GeoPoint(lat, lon));
221+
mapViewController.setZoom(15.0d);
222+
223+
//add the gps location marker
224+
Marker GPSMarker = new Marker(map);
225+
final GeoPoint markerPointGPS = new GeoPoint(lat, lon);
226+
GPSMarker.setPosition(markerPointGPS);
227+
GPSMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
228+
GPSMarker.setIcon(ContextCompat.getDrawable(this, R.drawable.gps_marker));
229+
GPSMarker.setTitle("Device Location");
230+
map.getOverlays().add(GPSMarker);
231+
map.invalidate();
232+
} else {
233+
Toast.makeText(getApplicationContext(), "Can't read GPS data.", Toast.LENGTH_LONG).show();
258234
}
259-
}).start();
235+
} catch (Exception e) {
236+
e.printStackTrace();
237+
}
238+
239+
260240
}
261241

242+
public class ReceivePiLocation extends AsyncTask<Void, Void, Boolean> {
243+
244+
@Override
245+
protected void onPreExecute() {
246+
isClientRunning = true;
247+
}
248+
249+
@Override
250+
protected Boolean doInBackground(Void... voids) {
251+
byte[] lMsg = new byte[64];
252+
DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
253+
try {
254+
isClientRunning = true;
255+
ds = new DatagramSocket(UDP_LocationReceivingPort);
256+
ds.setSoTimeout(7000);
257+
ds.setReuseAddress(true);
258+
ds.receive(dp);
259+
gpsresponse = new String(dp.getData());
260+
Log.e("UDP", "run: paket alındı!" + gpsresponse);
261+
} catch (SocketException e) {
262+
e.printStackTrace();
263+
} catch (IOException e) {
264+
e.printStackTrace();
265+
} finally {
266+
if (ds != null) {
267+
ds.close();
268+
isClientRunning = false;
269+
}
270+
}
271+
return true;
272+
}
273+
274+
protected void onPostExecute(Boolean state) {
275+
if (state) {
276+
setMapView(gpsresponse);
277+
}
278+
}
279+
}
262280
public class SendTargetAsyncTask extends AsyncTask<Void, Void, Void> {
263281
Socket socket;
264282

265283
@Override
266284
protected Void doInBackground(Void... params) {
267285
try {
268-
InetAddress inetAddress = InetAddress.getByName(wifiModuleIP);
269-
socket = new java.net.Socket(inetAddress, 10200);
286+
InetAddress inetAddress = InetAddress.getByName(AutoDriveActivity.wifiModuleIP);
287+
socket = new java.net.Socket(inetAddress, 10201);
270288
PrintStream printStream = new PrintStream(socket.getOutputStream());
271-
printStream.print(CMD);
289+
targetLocation = mapLat.getText().toString() + "/" + mapLon.getText().toString();
290+
printStream.print(targetLocation);
272291
printStream.close();
273292
socket.close();
274293
} catch (UnknownHostException e) {

0 commit comments

Comments
 (0)