</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"> |
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 String ipAdress; | |||||
private int portNumber; | |||||
private String telegram; | |||||
private static final double sendRatePerSecond = 4.0; | |||||
private boolean connected; | 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) { | 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); | |||||
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) { | } catch (Exception e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
connected = false; | connected = false; | ||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
public boolean getConnectionState(){ | |||||
return connected; | |||||
} | |||||
} | } |
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(); | |||||
} | |||||
} | } |
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; | ||||
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) { | ||||
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; | |||||
} | |||||
} | |||||
} | |||||
} |
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; | ||||
//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 |
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; | ||||
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 = | ||||
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") | ||||
@Override | @Override | ||||
public void onDestroyView() { | public void onDestroyView() { | ||||
super.onDestroyView(); | super.onDestroyView(); | ||||
dataTransferAsyncTask.cancel(false); | |||||
// dataTransferAsyncTask.cancel(false); | |||||
binding = null; | binding = null; | ||||
} | } | ||||
} | } |
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; | ||||
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"; | ||||
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 = | ||||
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() { | ||||
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(){ | |||||
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){ | if(connected){ | ||||
editor.putString(ipKey, ipAddress); | editor.putString(ipKey, ipAddress); | ||||
}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); | |||||
} | } | ||||
} | } | ||||
}); | }); | ||||
@Override | @Override | ||||
public void onDestroyView() { | public void onDestroyView() { | ||||
super.onDestroyView(); | super.onDestroyView(); | ||||
dataTransferAsyncTask.cancel(true); | |||||
// dataTransferAsyncTask.cancel(true); | |||||
binding = null; | binding = null; | ||||
} | } | ||||
} | } |