Compare commits

..

No commits in common. "2ee36032296ae6b64761b7f80d1319ca4d8f3ed1" and "60e19a8968ad06b09b137865c23222e6d67b4afa" have entirely different histories.

6 changed files with 48 additions and 136 deletions

View File

@ -11,39 +11,53 @@ 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;
}
}

View File

@ -1,55 +0,0 @@
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;
public class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> {
private static final int sendRatePerSecond = 4;
private String ipAdress;
private int portNumber;
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) {
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;
}
}
return null;
}
public boolean getConnectionState(){
return connected;
}
}

View File

@ -18,7 +18,6 @@ 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;
@ -42,8 +41,6 @@ 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 =
@ -65,17 +62,14 @@ 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){
dataTransferAsyncTask.writeTelegram(com.telegram(true, autoList));
com.sendData(com.telegram(true, autoList), ipAddress, port);
}else{
dataTransferAsyncTask.writeTelegram(com.telegram(false, autoList));
com.sendData(com.telegram(false, autoList), ipAddress, port);
}
}
});
@ -162,7 +156,6 @@ public class DashboardFragment extends Fragment {
@Override
public void onDestroyView() {
super.onDestroyView();
dataTransferAsyncTask.cancel(false);
binding = null;
}

View File

@ -1,11 +1,9 @@
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;
@ -17,7 +15,6 @@ 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;
@ -35,7 +32,6 @@ public class HomeFragment extends Fragment {
int port = 8000;
Communication com = new Communication();
DataTransferAsyncTask dataTransferAsyncTask;
private FragmentManuellBinding binding;
@ -58,42 +54,20 @@ public class HomeFragment extends Fragment {
final TextView textView = binding.textHome;
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
dataTransferAsyncTask.execute();
binding.rotateLeft.setOnTouchListener(new View.OnTouchListener(){
@SuppressLint("ClickableViewAccessibility")
binding.rotateLeft.setOnClickListener(new View.OnClickListener(){
@Override
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;
}
public void onClick(View v){
com.sendData(com.telegram(new int[]{-1, 1, -1, 1}, 100), ipAddress, port);
};
});
binding.rotateRight.setOnTouchListener(new View.OnTouchListener(){
@SuppressLint("ClickableViewAccessibility")
binding.rotateRight.setOnClickListener(new View.OnClickListener(){
@Override
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;
}
public void onClick(View v){
com.sendData(com.telegram(new int[]{1, -1, 1, -1}, 100), ipAddress, port);
};
});
JoystickView joystick = (JoystickView) binding.joystick;
joystick.setOnMoveListener(new JoystickView.OnMoveListener() {
@Override
@ -102,32 +76,32 @@ public class HomeFragment extends Fragment {
//8 segments of the joystick
if((angle >= 338 || angle < 22) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, -1, -1, 1}, strength));
com.sendData(com.telegram(new int[]{1, -1, -1, 1}, strength), ipAddress, port);
}else if((angle >= 22 && angle < 67) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right_forward);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, 0, 0, 1}, strength));
com.sendData(com.telegram(new int[]{1, 0, 0, 1}, strength), ipAddress, port);
}else if((angle >=67 && angle < 112) && strength != 0){
joystick.setBackgroundResource(R.mipmap.forward);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, 1, 1, 1}, strength));
com.sendData(com.telegram(new int[]{1, 1, 1, 1}, strength), ipAddress, port);
}else if((angle >=112 && angle < 157) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left_forward);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 1, 1, 0}, strength));
com.sendData(com.telegram(new int[]{0, 1, 1, 0}, strength), ipAddress, port);
}else if((angle >=157 && angle < 202) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, 1, 1, -1}, strength));
com.sendData(com.telegram(new int[]{-1, 1, 1, -1}, strength), ipAddress, port);
}else if((angle >=202 && angle < 247) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left_back);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, 0, 0, -1}, strength));
com.sendData(com.telegram(new int[]{-1, 0, 0, -1}, strength), ipAddress, port);
}else if((angle >=247 && angle < 292) && strength != 0){
joystick.setBackgroundResource(R.mipmap.back);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, -1, -1, -1}, strength));
com.sendData(com.telegram(new int[]{-1, -1, -1, -1}, strength), ipAddress, port);
}else if((angle >=292 && angle < 337) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right_back);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, -1, -1, 0}, strength));
com.sendData(com.telegram(new int[]{0, -1, -1, 0}, strength), ipAddress, port);
}
else{
joystick.setBackgroundResource(R.mipmap.blank);
dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 0, 0, 0}, strength));
com.sendData(com.telegram(new int[]{0, 0, 0, 0}, strength), ipAddress, port);
}
}
});
@ -138,7 +112,6 @@ public class HomeFragment extends Fragment {
@Override
public void onDestroyView() {
super.onDestroyView();
dataTransferAsyncTask.cancel(false);
binding = null;
}
}

View File

@ -16,15 +16,12 @@ 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.";
@ -34,7 +31,6 @@ public class NotificationsFragment extends Fragment {
int port = 8000;
int duration = Toast.LENGTH_LONG;
DataTransferAsyncTask dataTransferAsyncTask;
private FragmentEinstellungenBinding binding;
@ -60,21 +56,13 @@ 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()));
dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
dataTransferAsyncTask.writeTelegram("Verbindung aufbauen");
dataTransferAsyncTask.execute();
if(dataTransferAsyncTask.getConnectionState()){
if(com.sendData("", ipAddress, port)){
editor.putString(ipKey, ipAddress);
editor.putInt(portKey, port);
editor.apply();
@ -83,18 +71,17 @@ 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;
}
}

View File

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