package com.example.greenwatch.repositories; import android.os.Looper; import androidx.lifecycle.MutableLiveData; import com.example.greenwatch.models.Device; import com.example.greenwatch.communication.WiFiCommunication; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.UUID; public class DeviceRepository { private final int maxAlarmHistoryListSize = 20; private final String delimiter = ", "; private final String sensorStatusKey = "An"; private String localDeviceUUID; private static DeviceRepository deviceRepositoryInstance; private WiFiCommunication mWiFiCommunication; private boolean startAlarmRecordingValue; private MutableLiveData startAlarmRecording = new MutableLiveData<>(); private MutableLiveData> deviceList = new MutableLiveData<>(); private MutableLiveData> alarmHistoryList = new MutableLiveData<>(); private HashMap connectedDevicesList = new HashMap<>(); private HashMap deviceIDMapper = new HashMap<>(); private List alarmHistoryDeviceList = new ArrayList<>(); private DeviceRepository() { setLocalDeviceUUID(); startAlarmRecordingValue = false; } public static synchronized DeviceRepository getInstance() { if (deviceRepositoryInstance == null){ deviceRepositoryInstance = new DeviceRepository(); } return deviceRepositoryInstance; } public void setWiFiCommunication(WiFiCommunication wiFiCommunication) { if (mWiFiCommunication == null) { this.mWiFiCommunication = wiFiCommunication; } } public MutableLiveData> getConnectedDeviceList() { setMutableLiveDataDeviceList(); return deviceList; } public MutableLiveData> getAlarmHistoryDeviceList() { setMutableLiveDataAlarmHistoryList(); return alarmHistoryList; } public MutableLiveData getStartAlarmRecording() { setMutableLiveDataStartAlarmRecording(); return startAlarmRecording; } public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, float sensorMassage){ Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage); if (sensorStatus) { setAlarmHistoryDeviceList(newDevice); } boolean newStartAlarmRecordingValue = checkDeviceStatus(); if (startAlarmRecordingValue != newStartAlarmRecordingValue) { startAlarmRecordingValue = newStartAlarmRecordingValue; setMutableLiveDataStartAlarmRecording(); } addToConnectedDeviceList(newDevice.getDeviceID(), newDevice); setMutableLiveDataDeviceList(); } public void getNewReceivedMessage(String newMessage) { String[] messageString = messageStringSplitter(newMessage); if(messageString[0].equals("1") && messageString.length == 7) { String timeStamp = messageString[1]; String deviceID = messageString[3]; boolean sensorStatus = convertRecievedSensorStatus(messageString[4]); String sensorType = messageString[5]; float sensorMassage = Float.valueOf(messageString[6]); if (deviceID.equals(checkDeviceID(localDeviceUUID))) { return; } if (!connectedDevicesList.containsKey(deviceID)) { createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage); } else { updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); } } } private String messageStringBuilder(String deviceID) { StringBuilder message = new StringBuilder(); Device device = connectedDevicesList.get(deviceID); if(device != null) { message.append("1") .append(delimiter) .append(device.getTimeStamp()) .append(delimiter) .append("Gruppe3") .append(delimiter) .append(device.getDeviceID()) .append(delimiter) .append(convertSendSensorStatus(device.getSensorStatus())) .append(delimiter) .append(device.getSensorType()) .append(delimiter) .append(device.getSensorMassage()); } else { message.append("1") .append(delimiter) .append("") .append(delimiter) .append("Gruppe3") .append(delimiter) .append("") .append(delimiter) .append("") .append(delimiter) .append("") .append(delimiter) .append(""); } return message.toString(); } public String getLocalDeviceUUID() { return localDeviceUUID; } public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, float sensorMassage) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { device.setTimeStamp(timeStamp); device.setSensorType(sensorType); device.setSensorMassage(sensorMassage); if (!device.getSensorStatus() && sensorStatus) { setAlarmHistoryDeviceList(device); } device.setSensorStatus(sensorStatus); boolean newStartAlarmRecordingValue = checkDeviceStatus(); if (startAlarmRecordingValue != newStartAlarmRecordingValue) { startAlarmRecordingValue = newStartAlarmRecordingValue; setMutableLiveDataStartAlarmRecording(); } addToConnectedDeviceList(checkedDeviceID, device); setMutableLiveDataDeviceList(); } } public void setTimeStamp(String deviceID, String timeStamp) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { device.setTimeStamp(timeStamp); addToConnectedDeviceList(checkedDeviceID, device); setMutableLiveDataDeviceList(); } } public String getTimeStamp(String deviceID) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { return device.getTimeStamp(); } return ""; } public void setDeviceID(String deviceID, String newDeviceID) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { device.setDeviceID(newDeviceID); setDeviceIDMapper(newDeviceID); connectedDevicesList.remove(checkedDeviceID); addToConnectedDeviceList(newDeviceID, device); setMutableLiveDataDeviceList(); } } public String getDeviceID(String deviceID) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { return device.getDeviceID(); } return ""; } public void setSensorStatus(String deviceID, boolean sensorStatus) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { if (!device.getSensorStatus() && sensorStatus) { setAlarmHistoryDeviceList(device); } device.setSensorStatus(sensorStatus); boolean newStartAlarmRecordingValue = checkDeviceStatus(); if (startAlarmRecordingValue != newStartAlarmRecordingValue) { startAlarmRecordingValue = newStartAlarmRecordingValue; setMutableLiveDataStartAlarmRecording(); } addToConnectedDeviceList(checkedDeviceID, device); setMutableLiveDataDeviceList(); } } public boolean getSensorStatus(String deviceID) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { return device.getSensorStatus(); } return false; } public void setSensorType(String deviceID, String sensorType) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { device.setSensorType(sensorType); addToConnectedDeviceList(checkedDeviceID, device); setMutableLiveDataDeviceList(); } } public String getSensorType(String deviceID) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { return device.getSensorType(); } return ""; } public void setSensorMassage(String deviceID, float sensorMessage) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { device.setSensorMassage(sensorMessage); addToConnectedDeviceList(checkedDeviceID, device); setMutableLiveDataDeviceList(); } } public float getSensorMassage(String deviceID) { String checkedDeviceID = checkDeviceID(deviceID); Device device = connectedDevicesList.get(checkedDeviceID); if(device != null) { return device.getSensorMassage(); } return 0; } public String getSystemTimeStamp() { SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); Date date = new Date(System.currentTimeMillis()); return formatter.format(date); } private void setMutableLiveDataDeviceList() { List list = new ArrayList<>(connectedDevicesList.values()); if (Looper.myLooper() == Looper.getMainLooper()){ deviceList.setValue(list); } else { deviceList.postValue(list); } } private void setMutableLiveDataAlarmHistoryList() { alarmHistoryList.setValue(alarmHistoryDeviceList); } private void setMutableLiveDataStartAlarmRecording() { startAlarmRecording.setValue(startAlarmRecordingValue); } private void addToConnectedDeviceList(String key, Device device) { connectedDevicesList.put(key, device); if (key.equals(checkDeviceID(localDeviceUUID))) { mWiFiCommunication.setNewMessage(messageStringBuilder(device.getDeviceID()),true); } } private String checkDeviceID(String deviceID) { if(!deviceIDMapper.isEmpty() && deviceID.equals(localDeviceUUID)) { return deviceIDMapper.get(deviceID); } return deviceID; } private void setDeviceIDMapper(String deviceID) { deviceIDMapper.put(localDeviceUUID, deviceID); } private void setLocalDeviceUUID(){ this.localDeviceUUID = UUID.randomUUID().toString(); } private String[] messageStringSplitter(String message) { return message.split(delimiter); } private boolean convertRecievedSensorStatus(String status) { return status.equals(sensorStatusKey); } private String convertSendSensorStatus(boolean status) { if (status){ return "An"; } else { return "Aus"; } } private void setAlarmHistoryDeviceList(Device device) { if (alarmHistoryDeviceList.size() == maxAlarmHistoryListSize) { alarmHistoryDeviceList.remove(alarmHistoryDeviceList.size() -1); } Device alarmHistoryDevice = new Device(device.getTimeStamp(), device.getDeviceID(), device.getSensorStatus(), device.getSensorType(), device.getSensorMassage()); alarmHistoryDeviceList.add(0, alarmHistoryDevice); setMutableLiveDataAlarmHistoryList(); } private boolean checkDeviceStatus() { for (Device device : connectedDevicesList.values()) { if (device.getSensorStatus()) { return true; } } return false; } }