import java.util.Objects; | import java.util.Objects; | ||||
public class Communication { | 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){ | public String telegram(int[] wheels, int strenght){ | ||||
String tel = ""; | String tel = ""; | ||||
tel += "0;"; //Manuel mode | |||||
tel += "0"; //Manuel mode | |||||
for(int i = 0; i < wheels.length; i++){ | for(int i = 0; i < wheels.length; i++){ | ||||
if(wheels[i] == 1){ | if(wheels[i] == 1){ | ||||
tel += Float.toString(((float)strenght)/100) + ";"; | |||||
tel += ";" + Float.toString(((float)strenght)/100) ; | |||||
}else if(wheels[i] == -1) | }else if(wheels[i] == -1) | ||||
tel += Float.toString(((float)-strenght)/100) + ";"; | |||||
tel += ";" + Float.toString(((float)-strenght)/100); | |||||
else{ | else{ | ||||
tel += "0.00;"; | |||||
tel += ";0.00"; | |||||
} | } | ||||
} | } | ||||
Log.v("TAG", tel + "\n"); | |||||
return tel; | return tel; | ||||
} | } | ||||
public String telegram(boolean startStop, ArrayList<String> list){ | public String telegram(boolean startStop, ArrayList<String> list){ | ||||
String tel = ""; | String tel = ""; | ||||
tel += "1;"; //Autonomous mode | |||||
tel += "1"; //Autonomous mode | |||||
if(startStop){ | if(startStop){ | ||||
tel += "1;"; | |||||
tel += ";1"; | |||||
}else{ | }else{ | ||||
tel += "0;"; | |||||
tel += ";0"; | |||||
} | } | ||||
for (String i : list) { | for (String i : list) { | ||||
if(i.equals("Links")){ | if(i.equals("Links")){ | ||||
tel += "0;"; | |||||
tel += ";0"; | |||||
}else if (i.equals("Rechts")){ | }else if (i.equals("Rechts")){ | ||||
tel += "1;"; | |||||
tel += ";1"; | |||||
} | } | ||||
} | } | ||||
Log.v("TAG", tel + "\n"); | |||||
return tel; | return tel; | ||||
} | } | ||||
} | } |
package com.example.lfrmobileapp; | package com.example.lfrmobileapp; | ||||
import android.os.AsyncTask; | import android.os.AsyncTask; | ||||
import android.util.Log; | |||||
import java.io.DataOutputStream; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import java.net.Socket; | 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; | 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.portNumber = portNumber; | ||||
this.telegram = ""; | |||||
this.connected = false; | |||||
} | |||||
public void writeTelegram(String telegram) { | |||||
this.telegram = telegram; | |||||
} | } | ||||
@Override | @Override | ||||
protected Void doInBackground(Void... params) { | 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; | return null; | ||||
} | } | ||||
public boolean getConnectionState(){ | |||||
return connected; | |||||
} | |||||
} | } |
import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||
import com.example.lfrmobileapp.Communication; | import com.example.lfrmobileapp.Communication; | ||||
import com.example.lfrmobileapp.DataTransferAsyncTask; | |||||
import com.example.lfrmobileapp.R; | import com.example.lfrmobileapp.R; | ||||
import com.example.lfrmobileapp.databinding.FragmentAutomatikBinding; | import com.example.lfrmobileapp.databinding.FragmentAutomatikBinding; | ||||
//Keys for Shared Preferences | //Keys for Shared Preferences | ||||
String listKey = "listkey"; | String listKey = "listkey"; | ||||
DataTransferAsyncTask dataTransferAsyncTask; | |||||
public View onCreateView(@NonNull LayoutInflater inflater, | public View onCreateView(@NonNull LayoutInflater inflater, | ||||
ViewGroup container, Bundle savedInstanceState) { | ViewGroup container, Bundle savedInstanceState) { | ||||
DashboardViewModel dashboardViewModel = | DashboardViewModel dashboardViewModel = | ||||
ipAddress = sharedPref.getString(ipKey, ""); | ipAddress = sharedPref.getString(ipKey, ""); | ||||
port = sharedPref.getInt(portKey, -1); | port = sharedPref.getInt(portKey, -1); | ||||
dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port); | |||||
dataTransferAsyncTask.execute(); | |||||
binding.startAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | binding.startAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | ||||
@Override | @Override | ||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | ||||
if(isChecked){ | if(isChecked){ | ||||
com.sendData(com.telegram(true, autoList), ipAddress, port); | |||||
dataTransferAsyncTask.writeTelegram(com.telegram(true, autoList)); | |||||
}else{ | }else{ | ||||
com.sendData(com.telegram(false, autoList), ipAddress, port); | |||||
dataTransferAsyncTask.writeTelegram(com.telegram(false, autoList)); | |||||
} | } | ||||
} | } | ||||
}); | }); | ||||
@Override | @Override | ||||
public void onDestroyView() { | public void onDestroyView() { | ||||
super.onDestroyView(); | super.onDestroyView(); | ||||
dataTransferAsyncTask.cancel(false); | |||||
binding = null; | binding = null; | ||||
} | } | ||||
package com.example.lfrmobileapp.ui.home; | package com.example.lfrmobileapp.ui.home; | ||||
import android.annotation.SuppressLint; | |||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.SharedPreferences; | import android.content.SharedPreferences; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||
import android.view.MotionEvent; | |||||
import android.view.View; | import android.view.View; | ||||
import android.view.ViewGroup; | import android.view.ViewGroup; | ||||
import android.widget.Button; | import android.widget.Button; | ||||
import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||
import com.example.lfrmobileapp.Communication; | import com.example.lfrmobileapp.Communication; | ||||
import com.example.lfrmobileapp.DataTransferAsyncTask; | |||||
import com.example.lfrmobileapp.R; | import com.example.lfrmobileapp.R; | ||||
import com.example.lfrmobileapp.databinding.FragmentManuellBinding; | import com.example.lfrmobileapp.databinding.FragmentManuellBinding; | ||||
import com.example.lfrmobileapp.ui.notifications.NotificationsFragment; | import com.example.lfrmobileapp.ui.notifications.NotificationsFragment; | ||||
int port = 8000; | int port = 8000; | ||||
Communication com = new Communication(); | Communication com = new Communication(); | ||||
DataTransferAsyncTask dataTransferAsyncTask; | |||||
private FragmentManuellBinding binding; | private FragmentManuellBinding binding; | ||||
final TextView textView = binding.textHome; | final TextView textView = binding.textHome; | ||||
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); | 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 | @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 | @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; | JoystickView joystick = (JoystickView) binding.joystick; | ||||
joystick.setOnMoveListener(new JoystickView.OnMoveListener() { | joystick.setOnMoveListener(new JoystickView.OnMoveListener() { | ||||
@Override | @Override | ||||
//8 segments of the joystick | //8 segments of the joystick | ||||
if((angle >= 338 || angle < 22) && strength != 0){ | if((angle >= 338 || angle < 22) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.right); | 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){ | }else if((angle >= 22 && angle < 67) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.right_forward); | 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){ | }else if((angle >=67 && angle < 112) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.forward); | 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){ | }else if((angle >=112 && angle < 157) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.left_forward); | 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){ | }else if((angle >=157 && angle < 202) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.left); | 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){ | }else if((angle >=202 && angle < 247) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.left_back); | 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){ | }else if((angle >=247 && angle < 292) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.back); | 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){ | }else if((angle >=292 && angle < 337) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.right_back); | 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{ | else{ | ||||
joystick.setBackgroundResource(R.mipmap.blank); | 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)); | |||||
} | } | ||||
} | } | ||||
}); | }); | ||||
@Override | @Override | ||||
public void onDestroyView() { | public void onDestroyView() { | ||||
super.onDestroyView(); | super.onDestroyView(); | ||||
dataTransferAsyncTask.cancel(false); | |||||
binding = null; | binding = null; | ||||
} | } | ||||
} | } |
import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||
import com.example.lfrmobileapp.Communication; | import com.example.lfrmobileapp.Communication; | ||||
import com.example.lfrmobileapp.DataTransferAsyncTask; | |||||
import com.example.lfrmobileapp.R; | import com.example.lfrmobileapp.R; | ||||
import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding; | import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding; | ||||
public class NotificationsFragment extends Fragment { | public class NotificationsFragment extends Fragment { | ||||
Boolean connected = false; | |||||
String ipKey = "ipkey"; | String ipKey = "ipkey"; | ||||
String portKey = "portkey"; | String portKey = "portkey"; | ||||
String connectionFailed = "Verbindung fehlgeschlagen."; | String connectionFailed = "Verbindung fehlgeschlagen."; | ||||
int port = 8000; | int port = 8000; | ||||
int duration = Toast.LENGTH_LONG; | int duration = Toast.LENGTH_LONG; | ||||
DataTransferAsyncTask dataTransferAsyncTask; | |||||
private FragmentEinstellungenBinding binding; | private FragmentEinstellungenBinding binding; | ||||
binding.port.setText(""); | binding.port.setText(""); | ||||
} | } | ||||
dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port); | |||||
final Button button = binding.connect; | final Button button = binding.connect; | ||||
button.setOnClickListener(new View.OnClickListener() { | button.setOnClickListener(new View.OnClickListener() { | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
ipAddress = String.valueOf(binding.ipAdress.getText()); | ipAddress = String.valueOf(binding.ipAdress.getText()); | ||||
port = Integer.parseInt(String.valueOf(binding.port.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.putString(ipKey, ipAddress); | ||||
editor.putInt(portKey, port); | editor.putInt(portKey, port); | ||||
editor.apply(); | editor.apply(); | ||||
}else{ | }else{ | ||||
Toast toast = Toast.makeText(v.getContext(), connectionFailed, duration); | Toast toast = Toast.makeText(v.getContext(), connectionFailed, duration); | ||||
toast.show(); | toast.show(); | ||||
dataTransferAsyncTask.cancel(true); | |||||
} | } | ||||
} | } | ||||
}); | }); | ||||
return root; | return root; | ||||
} | } | ||||
@Override | @Override | ||||
public void onDestroyView() { | public void onDestroyView() { | ||||
super.onDestroyView(); | super.onDestroyView(); | ||||
dataTransferAsyncTask.cancel(true); | |||||
binding = null; | binding = null; | ||||
} | } | ||||
} | } |
<ListView | <ListView | ||||
android:id="@+id/automaticList" | android:id="@+id/automaticList" | ||||
android:layout_width="413dp" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="470dp" | android:layout_height="470dp" | ||||
android:layout_marginTop="110dp" | android:layout_marginTop="110dp" | ||||
app:layout_constraintEnd_toEndOf="parent" | app:layout_constraintEnd_toEndOf="parent" |