Browse Source

App: Socket in Async Task

master
Baran Yasar 1 year ago
parent
commit
2ee3603229

+ 10
- 24
MobileApp/app/src/main/java/com/example/lfrmobileapp/Communication.java View File

@@ -11,53 +11,39 @@ import java.util.Iterator;
import java.util.Objects;

public class Communication {
public boolean sendData(String data, String ip, int port) {
try {
Socket socket = new Socket(ip, port);
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.writeUTF(data);
socket.close();
} catch (Exception e) {
e.printStackTrace();
return true; //ToDo: change to false to test connection - currently always true is delivered
}
return true;
}

public String telegram(int[] wheels, int strenght){
String tel = "";
tel += "0;"; //Manuel mode
tel += "0"; //Manuel mode

for(int i = 0; i < wheels.length; i++){
if(wheels[i] == 1){
tel += Float.toString(((float)strenght)/100) + ";";
tel += ";" + Float.toString(((float)strenght)/100) ;
}else if(wheels[i] == -1)
tel += Float.toString(((float)-strenght)/100) + ";";
tel += ";" + Float.toString(((float)-strenght)/100);
else{
tel += "0.00;";
tel += ";0.00";
}
}
Log.v("TAG", tel + "\n");
return tel;
}

public String telegram(boolean startStop, ArrayList<String> list){
String tel = "";
tel += "1;"; //Autonomous mode
tel += "1"; //Autonomous mode
if(startStop){
tel += "1;";
tel += ";1";
}else{
tel += "0;";
tel += ";0";
}

for (String i : list) {
if(i.equals("Links")){
tel += "0;";
tel += ";0";
}else if (i.equals("Rechts")){
tel += "1;";
tel += ";1";
}
}
Log.v("TAG", tel + "\n");
return tel;
}

}

+ 37
- 15
MobileApp/app/src/main/java/com/example/lfrmobileapp/DataTransferAsyncTask.java View File

@@ -1,33 +1,55 @@
package com.example.lfrmobileapp;

import android.os.AsyncTask;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

private class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> {
private static final int sendRatePerSecond = 2;
private String hostname;
public class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> {
private static final int sendRatePerSecond = 4;
private String ipAdress;
private int portNumber;
DataTransferAsyncTask(String hostname, int portNumber) {
this.hostname = hostname;
private String telegram;
private boolean connected;

public DataTransferAsyncTask(String ipAdress, int portNumber) {
this.ipAdress = ipAdress;
this.portNumber = portNumber;
this.telegram = "";
this.connected = false;
}

public void writeTelegram(String telegram) {
this.telegram = telegram;
}

@Override
protected Void doInBackground(Void... params) {
try {
Socket socket = new Socket(hostname, portNumber);
OutputStream outputStream = socket.getOutputStream();
while (!isCancelled()) {
outputStream.write("data".getBytes());
outputStream.flush();
Thread.sleep(1000 / sendRatePerSecond);
while(!isCancelled()) {
try {
Socket socket = new Socket(ipAdress, portNumber);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
connected = true;
while (!isCancelled() && telegram.length() > 0) {
Log.v("TAG", telegram + "\n");
outputStream.writeBytes(telegram);
outputStream.flush();
Thread.sleep(1000 / sendRatePerSecond);
}
socket.close();
} catch (Exception e) {
e.printStackTrace();
connected = false;
}
socket.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
}


public boolean getConnectionState(){
return connected;
}
}

+ 9
- 2
MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/dashboard/DashboardFragment.java View File

@@ -18,6 +18,7 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.lfrmobileapp.Communication;
import com.example.lfrmobileapp.DataTransferAsyncTask;
import com.example.lfrmobileapp.R;
import com.example.lfrmobileapp.databinding.FragmentAutomatikBinding;

@@ -41,6 +42,8 @@ public class DashboardFragment extends Fragment {
//Keys for Shared Preferences
String listKey = "listkey";

DataTransferAsyncTask dataTransferAsyncTask;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
DashboardViewModel dashboardViewModel =
@@ -62,14 +65,17 @@ public class DashboardFragment extends Fragment {
ipAddress = sharedPref.getString(ipKey, "");
port = sharedPref.getInt(portKey, -1);

dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
dataTransferAsyncTask.execute();

binding.startAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
com.sendData(com.telegram(true, autoList), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(true, autoList));
}else{
com.sendData(com.telegram(false, autoList), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(false, autoList));
}
}
});
@@ -156,6 +162,7 @@ public class DashboardFragment extends Fragment {
@Override
public void onDestroyView() {
super.onDestroyView();
dataTransferAsyncTask.cancel(false);
binding = null;
}


+ 45
- 18
MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/home/HomeFragment.java View File

@@ -1,9 +1,11 @@
package com.example.lfrmobileapp.ui.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@@ -15,6 +17,7 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.lfrmobileapp.Communication;
import com.example.lfrmobileapp.DataTransferAsyncTask;
import com.example.lfrmobileapp.R;
import com.example.lfrmobileapp.databinding.FragmentManuellBinding;
import com.example.lfrmobileapp.ui.notifications.NotificationsFragment;
@@ -32,6 +35,7 @@ public class HomeFragment extends Fragment {
int port = 8000;

Communication com = new Communication();
DataTransferAsyncTask dataTransferAsyncTask;

private FragmentManuellBinding binding;

@@ -54,20 +58,42 @@ public class HomeFragment extends Fragment {
final TextView textView = binding.textHome;
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

binding.rotateLeft.setOnClickListener(new View.OnClickListener(){
dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
dataTransferAsyncTask.execute();

binding.rotateLeft.setOnTouchListener(new View.OnTouchListener(){
@SuppressLint("ClickableViewAccessibility")
@Override
public void onClick(View v){
com.sendData(com.telegram(new int[]{-1, 1, -1, 1}, 100), ipAddress, port);
};
});
public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == MotionEvent.ACTION_UP){
binding.rotateLeft.performClick();
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 0, 0, 0}, 0));
return true;
}else{
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, 1, -1, 1}, 100));
}
return false;
}
});

binding.rotateRight.setOnClickListener(new View.OnClickListener(){
binding.rotateRight.setOnTouchListener(new View.OnTouchListener(){
@SuppressLint("ClickableViewAccessibility")
@Override
public void onClick(View v){
com.sendData(com.telegram(new int[]{1, -1, 1, -1}, 100), ipAddress, port);
};
public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == MotionEvent.ACTION_UP){
binding.rotateRight.performClick();
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 0, 0, 0}, 0));
return true;
}else{
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, -1, 1, -1}, 100));
}
return false;
}
});


JoystickView joystick = (JoystickView) binding.joystick;
joystick.setOnMoveListener(new JoystickView.OnMoveListener() {
@Override
@@ -76,32 +102,32 @@ public class HomeFragment extends Fragment {
//8 segments of the joystick
if((angle >= 338 || angle < 22) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right);
com.sendData(com.telegram(new int[]{1, -1, -1, 1}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, -1, -1, 1}, strength));
}else if((angle >= 22 && angle < 67) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right_forward);
com.sendData(com.telegram(new int[]{1, 0, 0, 1}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, 0, 0, 1}, strength));
}else if((angle >=67 && angle < 112) && strength != 0){
joystick.setBackgroundResource(R.mipmap.forward);
com.sendData(com.telegram(new int[]{1, 1, 1, 1}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, 1, 1, 1}, strength));
}else if((angle >=112 && angle < 157) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left_forward);
com.sendData(com.telegram(new int[]{0, 1, 1, 0}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 1, 1, 0}, strength));
}else if((angle >=157 && angle < 202) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left);
com.sendData(com.telegram(new int[]{-1, 1, 1, -1}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, 1, 1, -1}, strength));
}else if((angle >=202 && angle < 247) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left_back);
com.sendData(com.telegram(new int[]{-1, 0, 0, -1}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, 0, 0, -1}, strength));
}else if((angle >=247 && angle < 292) && strength != 0){
joystick.setBackgroundResource(R.mipmap.back);
com.sendData(com.telegram(new int[]{-1, -1, -1, -1}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, -1, -1, -1}, strength));
}else if((angle >=292 && angle < 337) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right_back);
com.sendData(com.telegram(new int[]{0, -1, -1, 0}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, -1, -1, 0}, strength));
}
else{
joystick.setBackgroundResource(R.mipmap.blank);
com.sendData(com.telegram(new int[]{0, 0, 0, 0}, strength), ipAddress, port);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 0, 0, 0}, strength));
}
}
});
@@ -112,6 +138,7 @@ public class HomeFragment extends Fragment {
@Override
public void onDestroyView() {
super.onDestroyView();
dataTransferAsyncTask.cancel(false);
binding = null;
}
}

+ 15
- 2
MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/notifications/NotificationsFragment.java View File

@@ -16,12 +16,15 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.lfrmobileapp.Communication;
import com.example.lfrmobileapp.DataTransferAsyncTask;
import com.example.lfrmobileapp.R;
import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding;


public class NotificationsFragment extends Fragment {

Boolean connected = false;

String ipKey = "ipkey";
String portKey = "portkey";
String connectionFailed = "Verbindung fehlgeschlagen.";
@@ -31,6 +34,7 @@ public class NotificationsFragment extends Fragment {
int port = 8000;

int duration = Toast.LENGTH_LONG;
DataTransferAsyncTask dataTransferAsyncTask;

private FragmentEinstellungenBinding binding;

@@ -56,13 +60,21 @@ public class NotificationsFragment extends Fragment {
binding.port.setText("");
}

dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);



final Button button = binding.connect;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ipAddress = String.valueOf(binding.ipAdress.getText());
port = Integer.parseInt(String.valueOf(binding.port.getText()));
if(com.sendData("", ipAddress, port)){

dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
dataTransferAsyncTask.writeTelegram("Verbindung aufbauen");
dataTransferAsyncTask.execute();

if(dataTransferAsyncTask.getConnectionState()){
editor.putString(ipKey, ipAddress);
editor.putInt(portKey, port);
editor.apply();
@@ -71,17 +83,18 @@ public class NotificationsFragment extends Fragment {
}else{
Toast toast = Toast.makeText(v.getContext(), connectionFailed, duration);
toast.show();
dataTransferAsyncTask.cancel(true);
}
}
});


return root;
}

@Override
public void onDestroyView() {
super.onDestroyView();
dataTransferAsyncTask.cancel(true);
binding = null;
}
}

+ 1
- 1
MobileApp/app/src/main/res/layout/fragment_automatik.xml View File

@@ -85,7 +85,7 @@

<ListView
android:id="@+id/automaticList"
android:layout_width="413dp"
android:layout_width="match_parent"
android:layout_height="470dp"
android:layout_marginTop="110dp"
app:layout_constraintEnd_toEndOf="parent"

Loading…
Cancel
Save