<manifest xmlns:android="http://schemas.android.com/apk/res/android" | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||||
package="com.example.greenwatch"> | package="com.example.greenwatch"> | ||||
<uses-permission android:name="android.permission.INTERNET" /> | |||||
<application | <application | ||||
android:allowBackup="true" | android:allowBackup="true" | ||||
android:icon="@mipmap/ic_launcher" | android:icon="@mipmap/ic_launcher" | ||||
android:roundIcon="@mipmap/ic_launcher_round" | android:roundIcon="@mipmap/ic_launcher_round" | ||||
android:supportsRtl="true" | android:supportsRtl="true" | ||||
android:theme="@style/Theme.GreenWatch"> | android:theme="@style/Theme.GreenWatch"> | ||||
<activity | |||||
android:name=".ConnectionActivity" | |||||
android:exported="false" /> | |||||
<activity | <activity | ||||
android:name=".VideodetectionAndAccelerometerActivity" | android:name=".VideodetectionAndAccelerometerActivity" | ||||
android:exported="false" /> | android:exported="false" /> |
package com.example.greenwatch; | |||||
import androidx.appcompat.app.AppCompatActivity; | |||||
import android.os.Bundle; | |||||
import android.view.View; | |||||
import android.widget.Button; | |||||
public class ConnectionActivity extends AppCompatActivity { | |||||
private Button backToMainActivity; | |||||
@Override | |||||
protected void onCreate(Bundle savedInstanceState) { | |||||
super.onCreate(savedInstanceState); | |||||
setContentView(R.layout.activity_connection); | |||||
backToMainActivity = (Button) findViewById(R.id.connectionBackToMainActivity); | |||||
backToMainActivity.setOnClickListener(new View.OnClickListener() { | |||||
@Override | |||||
public void onClick(View v) { | |||||
finish(); | |||||
} | |||||
}); | |||||
} | |||||
} |
import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
import androidx.lifecycle.Observer; | import androidx.lifecycle.Observer; | ||||
import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||
import androidx.recyclerview.widget.LinearLayoutManager; | |||||
import androidx.recyclerview.widget.RecyclerView; | |||||
import android.content.Intent; | import android.content.Intent; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.widget.TextView; | import android.widget.TextView; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import com.example.greenwatch.adapters.DeviceListAdapter; | |||||
import com.example.greenwatch.models.Device; | import com.example.greenwatch.models.Device; | ||||
import com.example.greenwatch.viewmodels.MainActivityViewModel; | import com.example.greenwatch.viewmodels.MainActivityViewModel; | ||||
mMainActivityViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() { | mMainActivityViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() { | ||||
@Override | @Override | ||||
public void onChanged(List<Device> devices) { | public void onChanged(List<Device> devices) { | ||||
deviceListAdapter.setDevices(devices); | |||||
StringBuilder sb = new StringBuilder(); | StringBuilder sb = new StringBuilder(); | ||||
sb.append("Time Stamp: "); | sb.append("Time Stamp: "); | ||||
sb.append(mMainActivityViewModel.getTimeStamp(mMainActivityViewModel.getLocalDeviceUUID())); | sb.append(mMainActivityViewModel.getTimeStamp(mMainActivityViewModel.getLocalDeviceUUID())); | ||||
startActivity(intent); | startActivity(intent); | ||||
} | } | ||||
public void openVideodetectionAndAccelerometerActivity(){ | public void openVideodetectionAndAccelerometerActivity(){ | ||||
Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class); | |||||
startActivity(intent); | |||||
//Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class); | |||||
//startActivity(intent); | |||||
mMainActivityViewModel.setDevice(mMainActivityViewModel.getLocalDeviceUUID(), "24:00", false, "Video", 0); | |||||
} | } | ||||
public void openConnectionActivity(){ | public void openConnectionActivity(){ | ||||
Intent intent = new Intent(this, ConnectionActivity.class); | |||||
startActivity(intent); | |||||
//Intent intent = new Intent(this, ConnectionActivity.class); | |||||
//startActivity(intent); | |||||
mMainActivityViewModel.setDevice(mMainActivityViewModel.getLocalDeviceUUID(), "10:51", true, "Audio", 10); | |||||
} | } | ||||
} | } |
package com.example.greenwatch.communication; | |||||
import com.example.greenwatch.repositories.DeviceRepository; | |||||
import java.io.IOException; | |||||
import java.net.DatagramPacket; | |||||
import java.net.DatagramSocket; | |||||
import java.net.Inet4Address; | |||||
import java.net.InetAddress; | |||||
import java.net.NetworkInterface; | |||||
import java.net.SocketException; | |||||
import java.net.UnknownHostException; | |||||
import java.util.Enumeration; | |||||
public class WiFiCommunication { | |||||
private final InetAddress address; | |||||
private final DatagramSocket socket; | |||||
private final int port; | |||||
private String rxString; | |||||
private String sendMessage; | |||||
//private String sendMsg = "default"; | |||||
private boolean isNewMessage; | |||||
volatile private boolean running; | |||||
private static WiFiCommunication wifiCommunicationInstance; | |||||
private DeviceRepository mDeviceRepository; | |||||
private WiFiCommunication() { | |||||
port = 1234; | |||||
try { | |||||
socket = new DatagramSocket(port); | |||||
socket.setBroadcast(true); | |||||
address = InetAddress.getByName("255.255.255.255"); | |||||
running = true; | |||||
isNewMessage = false; | |||||
new ReceiveThread().start(); | |||||
new SendThread().start(); | |||||
} catch (SocketException | UnknownHostException e) { | |||||
throw new RuntimeException(e); | |||||
} | |||||
} | |||||
public static synchronized WiFiCommunication getInstance() { | |||||
if (wifiCommunicationInstance == null){ | |||||
wifiCommunicationInstance = new WiFiCommunication(); | |||||
} | |||||
return wifiCommunicationInstance; | |||||
} | |||||
public void setDeviceRepository(DeviceRepository deviceRepository) { | |||||
this.mDeviceRepository = deviceRepository; | |||||
} | |||||
public void setNewMessage(String newMessage, boolean isNewMessage){ | |||||
this.sendMessage = newMessage; | |||||
this.isNewMessage = isNewMessage; | |||||
} | |||||
public static String getLocalIpAddress() { | |||||
try { | |||||
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { | |||||
NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement(); | |||||
for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) { | |||||
InetAddress inetAddress = addresses.nextElement(); | |||||
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { | |||||
return inetAddress.getHostAddress(); | |||||
} | |||||
} | |||||
} | |||||
} catch (SocketException ex) { | |||||
ex.printStackTrace(); | |||||
} | |||||
return null; | |||||
} | |||||
public void stopCommunication() { | |||||
running = false; | |||||
socket.close(); | |||||
} | |||||
private class SendThread extends Thread { | |||||
private int tmpCnt = 0; | |||||
@Override | |||||
public void run() { | |||||
try { | |||||
do { | |||||
if(isNewMessage) | |||||
{ | |||||
isNewMessage = false; | |||||
//todo: adapt send String | |||||
String txString = getLocalIpAddress() + " sends. #" + tmpCnt++ + sendMessage; | |||||
byte[] txBuffer = txString.getBytes(); | |||||
DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port); | |||||
for(int i = 0; i < 20; i++) { | |||||
socket.send(txPacket); | |||||
} | |||||
} | |||||
} while (running); | |||||
} catch (IOException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} | |||||
private class ReceiveThread extends Thread { | |||||
private String rxString = ""; | |||||
private String previousRxString = ""; | |||||
@Override | |||||
public void run() { | |||||
try { | |||||
do { | |||||
byte[] rxBuffer = new byte[1024]; | |||||
DatagramPacket packet = new DatagramPacket(rxBuffer, rxBuffer.length); | |||||
socket.receive(packet); | |||||
rxString = new String(packet.getData(), 0, packet.getLength()); | |||||
if(!previousRxString.equals(rxString)){ | |||||
mDeviceRepository.getNewReceivedMessage(rxString); | |||||
} | |||||
previousRxString = rxString; | |||||
} while (running); | |||||
} | |||||
catch (IOException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} | |||||
} |
import androidx.lifecycle.MutableLiveData; | import androidx.lifecycle.MutableLiveData; | ||||
import com.example.greenwatch.models.Device; | import com.example.greenwatch.models.Device; | ||||
import com.example.greenwatch.communication.WiFiCommunication; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
public class DeviceRepository { | public class DeviceRepository { | ||||
private String delimiter = ", "; | |||||
private String deviceUUID; | |||||
private final String delimiter = ", "; | |||||
private final String SensorStatusKey = "An"; | |||||
private String localDeviceUUID; | |||||
private static DeviceRepository deviceRepositoryInstance; | private static DeviceRepository deviceRepositoryInstance; | ||||
private WiFiCommunication mWiFiCommunication; | |||||
private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>(); | private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>(); | ||||
private HashMap<String, Device> connectedDevicesList = new HashMap<>(); | private HashMap<String, Device> connectedDevicesList = new HashMap<>(); | ||||
private HashMap<String, String> deviceIDMapper = new HashMap<>(); | private HashMap<String, String> deviceIDMapper = new HashMap<>(); | ||||
private DeviceRepository() { | |||||
setLocalDeviceUUID(); | |||||
} | |||||
public static synchronized DeviceRepository getInstance() { | public static synchronized DeviceRepository getInstance() { | ||||
if (deviceRepositoryInstance == null){ | if (deviceRepositoryInstance == null){ | ||||
deviceRepositoryInstance = new DeviceRepository(); | deviceRepositoryInstance = new DeviceRepository(); | ||||
deviceRepositoryInstance.setDeviceUUID(); | |||||
} | } | ||||
return deviceRepositoryInstance; | return deviceRepositoryInstance; | ||||
} | } | ||||
public void setWiFiCommunication(WiFiCommunication wiFiCommunication) { | |||||
this.mWiFiCommunication = wiFiCommunication; | |||||
} | |||||
public MutableLiveData<List<Device>> getConnectedDeviceList() { | public MutableLiveData<List<Device>> getConnectedDeviceList() { | ||||
setMutableLiveDataValue(); | setMutableLiveDataValue(); | ||||
return deviceList; | return deviceList; | ||||
public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){ | public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){ | ||||
Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage); | Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage); | ||||
setDeviceIDMapper(deviceID); | |||||
addToConnectedDeviceList(newDevice.getDeviceID(), newDevice); | addToConnectedDeviceList(newDevice.getDeviceID(), newDevice); | ||||
setMutableLiveDataValue(); | setMutableLiveDataValue(); | ||||
} | } | ||||
public void createNewRemoteDevice(String message) { | |||||
String[] messageString = messageStringSplitter(message); | |||||
String timeStamp = messageString[0]; | |||||
String deviceID = messageString[1]; | |||||
boolean sensorStatus = Boolean.valueOf(messageString[1]); | |||||
String sensorType = messageString[3]; | |||||
int sensorMassage = Integer.valueOf(messageString[4]); | |||||
if(!connectedDevicesList.containsKey(deviceID)) { | |||||
createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage); | |||||
} | |||||
} | |||||
public String messageStringBuilder(String deviceID) { | |||||
private String messageStringBuilder(String deviceID) { | |||||
StringBuilder message = new StringBuilder(); | StringBuilder message = new StringBuilder(); | ||||
Device device = connectedDevicesList.get(deviceID); | Device device = connectedDevicesList.get(deviceID); | ||||
if(device != null) { | if(device != null) { | ||||
.append(device.getSensorMassage()); | .append(device.getSensorMassage()); | ||||
return message.toString(); | return message.toString(); | ||||
} | } | ||||
return message.toString(); | |||||
else { | |||||
message.append("") | |||||
.append(delimiter) | |||||
.append("") | |||||
.append(delimiter) | |||||
.append("") | |||||
.append(delimiter) | |||||
.append("") | |||||
.append(delimiter) | |||||
.append(""); | |||||
return message.toString(); | |||||
} | |||||
} | } | ||||
public String getLocalDeviceUUID() { | public String getLocalDeviceUUID() { | ||||
return deviceUUID; | |||||
return localDeviceUUID; | |||||
} | } | ||||
public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { | |||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { | |||||
String checkedDeviceID = checkDeviceID(deviceID); | String checkedDeviceID = checkDeviceID(deviceID); | ||||
Device device = connectedDevicesList.get(checkedDeviceID); | Device device = connectedDevicesList.get(checkedDeviceID); | ||||
if(device != null) { | if(device != null) { | ||||
private void addToConnectedDeviceList(String key, Device device) { | private void addToConnectedDeviceList(String key, Device device) { | ||||
connectedDevicesList.put(key, device); | connectedDevicesList.put(key, device); | ||||
if (key.equals(checkDeviceID(localDeviceUUID))) { | |||||
mWiFiCommunication.setNewMessage(messageStringBuilder(device.getDeviceID()),true); | |||||
} | |||||
} | } | ||||
private String checkDeviceID(String deviceID) { | private String checkDeviceID(String deviceID) { | ||||
if(!deviceIDMapper.isEmpty() && deviceID.equals(deviceUUID)) { | |||||
if(!deviceIDMapper.isEmpty() && deviceID.equals(localDeviceUUID)) { | |||||
return deviceIDMapper.get(deviceID); | return deviceIDMapper.get(deviceID); | ||||
} | } | ||||
return deviceID; | return deviceID; | ||||
} | } | ||||
private void setDeviceIDMapper(String deviceID) { | private void setDeviceIDMapper(String deviceID) { | ||||
deviceIDMapper.put(deviceUUID, deviceID); | |||||
deviceIDMapper.put(localDeviceUUID, deviceID); | |||||
} | } | ||||
private void setDeviceUUID(){ | |||||
this.deviceUUID = UUID.randomUUID().toString(); | |||||
private void setLocalDeviceUUID(){ | |||||
this.localDeviceUUID = UUID.randomUUID().toString(); | |||||
} | } | ||||
private String[] messageStringSplitter(String message) { | private String[] messageStringSplitter(String message) { | ||||
return message.split(delimiter); | return message.split(delimiter); | ||||
} | } | ||||
public void getNewReceivedMessage(String newMessage) { | |||||
String[] messageString = messageStringSplitter(newMessage); | |||||
String timeStamp = messageString[0]; | |||||
String deviceID = messageString[1]; | |||||
boolean sensorStatus = convertSensorStatus(messageString[2]); | |||||
String sensorType = messageString[3]; | |||||
int sensorMassage = Integer.valueOf(messageString[4]); | |||||
if (deviceID.equals(checkDeviceID(localDeviceUUID))) { | |||||
return; | |||||
} | |||||
if(!connectedDevicesList.containsKey(deviceID)) { | |||||
createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage); | |||||
} | |||||
else { | |||||
updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); | |||||
} | |||||
} | |||||
private boolean convertSensorStatus(String status) { | |||||
return status.equals(SensorStatusKey); | |||||
} | |||||
} | } |
} | } | ||||
public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { | public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { | ||||
mDeviceRepository.setDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); | |||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); | |||||
} | } | ||||
public void setTimeStamp(String deviceID, String timeStamp) { | public void setTimeStamp(String deviceID, String timeStamp) { |
import androidx.lifecycle.MutableLiveData; | import androidx.lifecycle.MutableLiveData; | ||||
import androidx.lifecycle.ViewModel; | import androidx.lifecycle.ViewModel; | ||||
import com.example.greenwatch.communication.WiFiCommunication; | |||||
import com.example.greenwatch.models.Device; | import com.example.greenwatch.models.Device; | ||||
import com.example.greenwatch.repositories.DeviceRepository; | import com.example.greenwatch.repositories.DeviceRepository; | ||||
private MutableLiveData<List<Device>> mDeviceList; | private MutableLiveData<List<Device>> mDeviceList; | ||||
private DeviceRepository mDeviceRepository; | private DeviceRepository mDeviceRepository; | ||||
public void init() { | public void init() { | ||||
WiFiCommunication mWiFiCommunication; | |||||
if(mDeviceList != null) { | if(mDeviceList != null) { | ||||
return; | return; | ||||
} | } | ||||
//todo: check if WiFi instanz can be hold only by the repository | |||||
mDeviceRepository = DeviceRepository.getInstance(); | mDeviceRepository = DeviceRepository.getInstance(); | ||||
mDeviceRepository.createNewDevice("", mDeviceRepository.getLocalDeviceUUID(), false, "", 0); | |||||
mWiFiCommunication = WiFiCommunication.getInstance(); | |||||
mDeviceRepository.setWiFiCommunication(mWiFiCommunication); | |||||
mWiFiCommunication.setDeviceRepository(mDeviceRepository); | |||||
mDeviceRepository.createNewDevice("", mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0); | |||||
mDeviceList = mDeviceRepository.getConnectedDeviceList(); | mDeviceList = mDeviceRepository.getConnectedDeviceList(); | ||||
} | } | ||||
} | } | ||||
public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { | public void setDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { | ||||
mDeviceRepository.setDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); | |||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); | |||||
} | } | ||||
public void setTimeStamp(String deviceID, String timeStamp) { | public void setTimeStamp(String deviceID, String timeStamp) { |
<?xml version="1.0" encoding="utf-8"?> | |||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||||
xmlns:app="http://schemas.android.com/apk/res-auto" | |||||
xmlns:tools="http://schemas.android.com/tools" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="match_parent" | |||||
android:orientation="vertical" | |||||
android:padding="10dp" | |||||
tools:context=".ConnectionActivity"> | |||||
<TextView | |||||
android:id="@+id/tvConnectionStatusmessage" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="wrap_content" | |||||
android:text="Connection Activity"> | |||||
</TextView> | |||||
<Button | |||||
android:id="@+id/connectionBackToMainActivity" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="wrap_content" | |||||
android:text="Back to MainActivity"> | |||||
</Button> | |||||
</LinearLayout> |