Threadsave socket
This commit is contained in:
parent
8831c130cd
commit
e248dc3e0c
2
MobileApp/.idea/misc.xml
generated
2
MobileApp/.idea/misc.xml
generated
@ -25,7 +25,7 @@
|
|||||||
</map>
|
</map>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
@ -7,39 +7,38 @@ 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;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> {
|
public class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||||
private static final int sendRatePerSecond = 4;
|
private static final double sendRatePerSecond = 4.0;
|
||||||
private String ipAdress;
|
|
||||||
private int portNumber;
|
|
||||||
private String telegram;
|
|
||||||
private boolean connected;
|
private boolean connected;
|
||||||
|
private final ExtendedSocket socket;
|
||||||
|
private BlockingQueue<String> blockingQueue;
|
||||||
|
|
||||||
public DataTransferAsyncTask(String ipAdress, int portNumber) {
|
public DataTransferAsyncTask(ExtendedSocket socket) {
|
||||||
this.ipAdress = ipAdress;
|
this.socket = socket;
|
||||||
this.portNumber = portNumber;
|
this.connected = true;
|
||||||
this.telegram = "";
|
this.blockingQueue = new LinkedBlockingQueue<>();
|
||||||
this.connected = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeTelegram(String telegram) {
|
public void writeTelegram(String telegram) {
|
||||||
this.telegram = telegram;
|
// Schau mal hier bei Fehler
|
||||||
|
blockingQueue.offer(telegram);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected Void doInBackground(Void... params) {
|
||||||
while(!isCancelled()) {
|
while(!isCancelled() && connected) {
|
||||||
try {
|
try {
|
||||||
Socket socket = new Socket(ipAdress, portNumber);
|
synchronized (socket) {
|
||||||
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
|
String message = blockingQueue.poll((long) (1000.0/sendRatePerSecond), TimeUnit.MILLISECONDS);
|
||||||
connected = true;
|
if(message != null){
|
||||||
while (!isCancelled() && telegram.length() > 0) {
|
Log.v("TAG", message + "\n");
|
||||||
Log.v("TAG", telegram + "\n");
|
socket.sendMessage(message);
|
||||||
outputStream.writeBytes(telegram);
|
}
|
||||||
outputStream.flush();
|
|
||||||
Thread.sleep(1000 / sendRatePerSecond);
|
|
||||||
}
|
}
|
||||||
socket.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
connected = false;
|
connected = false;
|
||||||
@ -47,9 +46,4 @@ public class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean getConnectionState(){
|
|
||||||
return connected;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,25 @@
|
|||||||
package com.example.lfrmobileapp;public class ExtendedSocket {
|
package com.example.lfrmobileapp;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class ExtendedSocket {
|
||||||
|
private final Socket socket;
|
||||||
|
|
||||||
|
public ExtendedSocket(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessage(String message) throws IOException {
|
||||||
|
synchronized (socket){
|
||||||
|
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
|
||||||
|
outputStream.writeBytes(message);
|
||||||
|
outputStream.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void closeSocket() throws IOException {
|
||||||
|
this.socket.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.example.lfrmobileapp;
|
|||||||
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.widget.Toast;
|
||||||
|
|
||||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||||
|
|
||||||
@ -14,11 +15,17 @@ import androidx.navigation.ui.NavigationUI;
|
|||||||
|
|
||||||
import com.example.lfrmobileapp.databinding.ActivityMainBinding;
|
import com.example.lfrmobileapp.databinding.ActivityMainBinding;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private ActivityMainBinding binding;
|
private ActivityMainBinding binding;
|
||||||
|
private ExtendedSocket socket;
|
||||||
|
private DataOutputStream outputStream;
|
||||||
|
private DataTransferAsyncTask dataTransferAsyncTask;
|
||||||
|
Boolean connected = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -38,5 +45,30 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
NavigationUI.setupWithNavController(binding.navView, navController);
|
NavigationUI.setupWithNavController(binding.navView, navController);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean connectToWifi(String ipAddress, int port){
|
||||||
|
|
||||||
|
new Thread(new Runnable(){
|
||||||
|
public void run(){
|
||||||
|
try {
|
||||||
|
Socket tempSocket = new Socket(ipAddress, port);
|
||||||
|
socket = new ExtendedSocket(tempSocket);
|
||||||
|
socket.sendMessage("Verbindung aufbauen");
|
||||||
|
connected = true;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
connected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
if(connected){
|
||||||
|
dataTransferAsyncTask = new DataTransferAsyncTask(socket);
|
||||||
|
dataTransferAsyncTask.execute();
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,13 +29,6 @@ import java.util.Arrays;
|
|||||||
import kotlin.collections.UArraySortingKt;
|
import kotlin.collections.UArraySortingKt;
|
||||||
|
|
||||||
public class DashboardFragment extends Fragment {
|
public class DashboardFragment extends Fragment {
|
||||||
|
|
||||||
String ipKey = "ipkey";
|
|
||||||
String portKey = "portkey";
|
|
||||||
|
|
||||||
String ipAddress = "192.168.0.1";
|
|
||||||
int port = 8000;
|
|
||||||
|
|
||||||
Communication com = new Communication();
|
Communication com = new Communication();
|
||||||
private FragmentAutomatikBinding binding;
|
private FragmentAutomatikBinding binding;
|
||||||
|
|
||||||
@ -62,12 +55,6 @@ public class DashboardFragment extends Fragment {
|
|||||||
//Get Arraylist from Shared Preferences String
|
//Get Arraylist from Shared Preferences String
|
||||||
ArrayList<String> autoList = stringToArraylist(sharedPref.getString(listKey, ""));
|
ArrayList<String> autoList = stringToArraylist(sharedPref.getString(listKey, ""));
|
||||||
|
|
||||||
ipAddress = sharedPref.getString(ipKey, "");
|
|
||||||
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
|
||||||
|
@ -18,6 +18,7 @@ import androidx.lifecycle.ViewModelProvider;
|
|||||||
|
|
||||||
import com.example.lfrmobileapp.Communication;
|
import com.example.lfrmobileapp.Communication;
|
||||||
import com.example.lfrmobileapp.DataTransferAsyncTask;
|
import com.example.lfrmobileapp.DataTransferAsyncTask;
|
||||||
|
import com.example.lfrmobileapp.MainActivity;
|
||||||
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;
|
||||||
@ -34,11 +35,19 @@ public class HomeFragment extends Fragment {
|
|||||||
String ipAddress = "192.168.0.1";
|
String ipAddress = "192.168.0.1";
|
||||||
int port = 8000;
|
int port = 8000;
|
||||||
|
|
||||||
|
MainActivity mainActivity;
|
||||||
|
|
||||||
Communication com = new Communication();
|
Communication com = new Communication();
|
||||||
DataTransferAsyncTask dataTransferAsyncTask;
|
DataTransferAsyncTask dataTransferAsyncTask;
|
||||||
|
|
||||||
private FragmentManuellBinding binding;
|
private FragmentManuellBinding binding;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
mainActivity = (MainActivity) context;
|
||||||
|
}
|
||||||
|
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||||
ViewGroup container, Bundle savedInstanceState) {
|
ViewGroup container, Bundle savedInstanceState) {
|
||||||
HomeViewModel homeViewModel =
|
HomeViewModel homeViewModel =
|
||||||
@ -58,8 +67,7 @@ public class HomeFragment extends Fragment {
|
|||||||
final TextView textView = binding.textHome;
|
final TextView textView = binding.textHome;
|
||||||
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
|
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
|
||||||
|
|
||||||
dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
|
|
||||||
dataTransferAsyncTask.execute();
|
|
||||||
|
|
||||||
binding.rotateLeft.setOnTouchListener(new View.OnTouchListener(){
|
binding.rotateLeft.setOnTouchListener(new View.OnTouchListener(){
|
||||||
@SuppressLint("ClickableViewAccessibility")
|
@SuppressLint("ClickableViewAccessibility")
|
||||||
@ -138,7 +146,7 @@ public class HomeFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroyView() {
|
public void onDestroyView() {
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
dataTransferAsyncTask.cancel(false);
|
// dataTransferAsyncTask.cancel(false);
|
||||||
binding = null;
|
binding = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import androidx.lifecycle.ViewModelProvider;
|
|||||||
|
|
||||||
import com.example.lfrmobileapp.Communication;
|
import com.example.lfrmobileapp.Communication;
|
||||||
import com.example.lfrmobileapp.DataTransferAsyncTask;
|
import com.example.lfrmobileapp.DataTransferAsyncTask;
|
||||||
|
import com.example.lfrmobileapp.MainActivity;
|
||||||
import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding;
|
import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding;
|
||||||
|
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
@ -51,6 +52,7 @@ import java.net.Socket;
|
|||||||
public class NotificationsFragment extends Fragment {
|
public class NotificationsFragment extends Fragment {
|
||||||
|
|
||||||
Boolean connected = false;
|
Boolean connected = false;
|
||||||
|
MainActivity mainActivity;
|
||||||
|
|
||||||
String ipKey = "ipkey";
|
String ipKey = "ipkey";
|
||||||
String portKey = "portkey";
|
String portKey = "portkey";
|
||||||
@ -65,6 +67,12 @@ public class NotificationsFragment extends Fragment {
|
|||||||
|
|
||||||
private FragmentEinstellungenBinding binding;
|
private FragmentEinstellungenBinding binding;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
mainActivity = (MainActivity) context;
|
||||||
|
}
|
||||||
|
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||||
ViewGroup container, Bundle savedInstanceState) {
|
ViewGroup container, Bundle savedInstanceState) {
|
||||||
NotificationsViewModel notificationsViewModel =
|
NotificationsViewModel notificationsViewModel =
|
||||||
@ -87,9 +95,7 @@ public class NotificationsFragment extends Fragment {
|
|||||||
binding.port.setText("");
|
binding.port.setText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
|
// 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() {
|
||||||
@ -97,22 +103,7 @@ public class NotificationsFragment extends Fragment {
|
|||||||
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()));
|
||||||
|
|
||||||
new Thread(new Runnable(){
|
connected = mainActivity.connectToWifi(ipAddress, port);
|
||||||
public void run(){
|
|
||||||
try {
|
|
||||||
Socket socket = new Socket(ipAddress, port);
|
|
||||||
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
|
|
||||||
outputStream.writeBytes("Verbindung aufbauen");
|
|
||||||
outputStream.flush();
|
|
||||||
socket.close();
|
|
||||||
connected = true;
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
connected = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
|
|
||||||
if(connected){
|
if(connected){
|
||||||
editor.putString(ipKey, ipAddress);
|
editor.putString(ipKey, ipAddress);
|
||||||
@ -123,7 +114,7 @@ public class NotificationsFragment extends Fragment {
|
|||||||
}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);
|
// dataTransferAsyncTask.cancel(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -134,7 +125,7 @@ public class NotificationsFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroyView() {
|
public void onDestroyView() {
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
dataTransferAsyncTask.cancel(true);
|
// dataTransferAsyncTask.cancel(true);
|
||||||
binding = null;
|
binding = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user