@@ -25,7 +25,7 @@ | |||
</map> | |||
</option> | |||
</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" /> | |||
</component> | |||
<component name="ProjectType"> |
@@ -7,39 +7,38 @@ import java.io.DataOutputStream; | |||
import java.io.IOException; | |||
import java.io.OutputStream; | |||
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> { | |||
private static final int sendRatePerSecond = 4; | |||
private String ipAdress; | |||
private int portNumber; | |||
private String telegram; | |||
private static final double sendRatePerSecond = 4.0; | |||
private boolean connected; | |||
private final ExtendedSocket socket; | |||
private BlockingQueue<String> blockingQueue; | |||
public DataTransferAsyncTask(String ipAdress, int portNumber) { | |||
this.ipAdress = ipAdress; | |||
this.portNumber = portNumber; | |||
this.telegram = ""; | |||
this.connected = false; | |||
public DataTransferAsyncTask(ExtendedSocket socket) { | |||
this.socket = socket; | |||
this.connected = true; | |||
this.blockingQueue = new LinkedBlockingQueue<>(); | |||
} | |||
public void writeTelegram(String telegram) { | |||
this.telegram = telegram; | |||
// Schau mal hier bei Fehler | |||
blockingQueue.offer(telegram); | |||
} | |||
@Override | |||
protected Void doInBackground(Void... params) { | |||
while(!isCancelled()) { | |||
while(!isCancelled() && connected) { | |||
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); | |||
synchronized (socket) { | |||
String message = blockingQueue.poll((long) (1000.0/sendRatePerSecond), TimeUnit.MILLISECONDS); | |||
if(message != null){ | |||
Log.v("TAG", message + "\n"); | |||
socket.sendMessage(message); | |||
} | |||
} | |||
socket.close(); | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
connected = false; | |||
@@ -47,9 +46,4 @@ public class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> { | |||
} | |||
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.SharedPreferences; | |||
import android.os.Bundle; | |||
import android.widget.Toast; | |||
import com.google.android.material.bottomnavigation.BottomNavigationView; | |||
@@ -14,11 +15,17 @@ import androidx.navigation.ui.NavigationUI; | |||
import com.example.lfrmobileapp.databinding.ActivityMainBinding; | |||
import java.io.DataOutputStream; | |||
import java.io.IOException; | |||
import java.net.Socket; | |||
public class MainActivity extends AppCompatActivity { | |||
private ActivityMainBinding binding; | |||
private ExtendedSocket socket; | |||
private DataOutputStream outputStream; | |||
private DataTransferAsyncTask dataTransferAsyncTask; | |||
Boolean connected = false; | |||
@Override | |||
protected void onCreate(Bundle savedInstanceState) { | |||
@@ -38,5 +45,30 @@ public class MainActivity extends AppCompatActivity { | |||
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; | |||
public class DashboardFragment extends Fragment { | |||
String ipKey = "ipkey"; | |||
String portKey = "portkey"; | |||
String ipAddress = "192.168.0.1"; | |||
int port = 8000; | |||
Communication com = new Communication(); | |||
private FragmentAutomatikBinding binding; | |||
@@ -62,12 +55,6 @@ public class DashboardFragment extends Fragment { | |||
//Get Arraylist from Shared Preferences String | |||
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() { | |||
@Override |
@@ -18,6 +18,7 @@ import androidx.lifecycle.ViewModelProvider; | |||
import com.example.lfrmobileapp.Communication; | |||
import com.example.lfrmobileapp.DataTransferAsyncTask; | |||
import com.example.lfrmobileapp.MainActivity; | |||
import com.example.lfrmobileapp.R; | |||
import com.example.lfrmobileapp.databinding.FragmentManuellBinding; | |||
import com.example.lfrmobileapp.ui.notifications.NotificationsFragment; | |||
@@ -34,11 +35,19 @@ public class HomeFragment extends Fragment { | |||
String ipAddress = "192.168.0.1"; | |||
int port = 8000; | |||
MainActivity mainActivity; | |||
Communication com = new Communication(); | |||
DataTransferAsyncTask dataTransferAsyncTask; | |||
private FragmentManuellBinding binding; | |||
@Override | |||
public void onAttach(Context context) { | |||
super.onAttach(context); | |||
mainActivity = (MainActivity) context; | |||
} | |||
public View onCreateView(@NonNull LayoutInflater inflater, | |||
ViewGroup container, Bundle savedInstanceState) { | |||
HomeViewModel homeViewModel = | |||
@@ -58,8 +67,7 @@ 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") | |||
@@ -138,7 +146,7 @@ public class HomeFragment extends Fragment { | |||
@Override | |||
public void onDestroyView() { | |||
super.onDestroyView(); | |||
dataTransferAsyncTask.cancel(false); | |||
// dataTransferAsyncTask.cancel(false); | |||
binding = null; | |||
} | |||
} |
@@ -15,6 +15,7 @@ import androidx.lifecycle.ViewModelProvider; | |||
import com.example.lfrmobileapp.Communication; | |||
import com.example.lfrmobileapp.DataTransferAsyncTask; | |||
import com.example.lfrmobileapp.MainActivity; | |||
import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding; | |||
import java.io.DataOutputStream; | |||
@@ -51,6 +52,7 @@ import java.net.Socket; | |||
public class NotificationsFragment extends Fragment { | |||
Boolean connected = false; | |||
MainActivity mainActivity; | |||
String ipKey = "ipkey"; | |||
String portKey = "portkey"; | |||
@@ -65,6 +67,12 @@ public class NotificationsFragment extends Fragment { | |||
private FragmentEinstellungenBinding binding; | |||
@Override | |||
public void onAttach(Context context) { | |||
super.onAttach(context); | |||
mainActivity = (MainActivity) context; | |||
} | |||
public View onCreateView(@NonNull LayoutInflater inflater, | |||
ViewGroup container, Bundle savedInstanceState) { | |||
NotificationsViewModel notificationsViewModel = | |||
@@ -87,9 +95,7 @@ public class NotificationsFragment extends Fragment { | |||
binding.port.setText(""); | |||
} | |||
dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port); | |||
// dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port); | |||
final Button button = binding.connect; | |||
button.setOnClickListener(new View.OnClickListener() { | |||
@@ -97,22 +103,7 @@ public class NotificationsFragment extends Fragment { | |||
ipAddress = String.valueOf(binding.ipAdress.getText()); | |||
port = Integer.parseInt(String.valueOf(binding.port.getText())); | |||
new Thread(new Runnable(){ | |||
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(); | |||
connected = mainActivity.connectToWifi(ipAddress, port); | |||
if(connected){ | |||
editor.putString(ipKey, ipAddress); | |||
@@ -123,7 +114,7 @@ public class NotificationsFragment extends Fragment { | |||
}else{ | |||
Toast toast = Toast.makeText(v.getContext(), connectionFailed, duration); | |||
toast.show(); | |||
dataTransferAsyncTask.cancel(true); | |||
// dataTransferAsyncTask.cancel(true); | |||
} | |||
} | |||
}); | |||
@@ -134,7 +125,7 @@ public class NotificationsFragment extends Fragment { | |||
@Override | |||
public void onDestroyView() { | |||
super.onDestroyView(); | |||
dataTransferAsyncTask.cancel(true); | |||
// dataTransferAsyncTask.cancel(true); | |||
binding = null; | |||
} | |||
} |