package com.example.greenwatch.models; | |||||
public class Device { | |||||
private String timeStamp; | |||||
private String deviceID; | |||||
private boolean sensorStatus; | |||||
private String sensorType; | |||||
private int sensorMassage; | |||||
public Device(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage) { | |||||
this.timeStamp = timeStamp; | |||||
this.deviceID = deviceID; | |||||
this.sensorStatus = sensorStatus; | |||||
this.sensorType = sensorType; | |||||
this.sensorMassage = sensorMassage; | |||||
} | |||||
public void setTimeStamp(String timeStamp) { | |||||
this.timeStamp = timeStamp; | |||||
} | |||||
public String getTimeStamp() { | |||||
return timeStamp; | |||||
} | |||||
public void setDeviceID(String deviceID) { | |||||
this.deviceID = deviceID; | |||||
} | |||||
public String getDeviceID() { | |||||
return deviceID; | |||||
} | |||||
public void setSensorType(String sensorType) { | |||||
this.sensorType = sensorType; | |||||
} | |||||
public String getSensorType() { | |||||
return sensorType; | |||||
} | |||||
public void setSensorStatus(boolean sensorStatus) { | |||||
this.sensorStatus = sensorStatus; | |||||
} | |||||
public Boolean getSensorStatus() { | |||||
return sensorStatus; | |||||
} | |||||
public void setSensorMassage(int sensorMassage) { | |||||
this.sensorMassage = sensorMassage; | |||||
} | |||||
public int getSensorMassage() { | |||||
return sensorMassage; | |||||
} | |||||
} |
package com.example.greenwatch.mvc; | |||||
public class Device { | |||||
private String sensor; | |||||
private Boolean isActive; | |||||
public Device(String Sensor, Boolean Status) { | |||||
this.sensor = Sensor; | |||||
this.isActive = Status; | |||||
} | |||||
public void setSensor(String Sensor) { | |||||
this.sensor = Sensor; | |||||
} | |||||
public String getSensor() { | |||||
return sensor; | |||||
} | |||||
public void setIsActive(Boolean Status) { | |||||
this.isActive = Status; | |||||
} | |||||
public Boolean getIsActive() { | |||||
return isActive; | |||||
} | |||||
} |
package com.example.greenwatch.mvc; | |||||
public class DeviceController { | |||||
private DeviceModel deviceModel; | |||||
private DeviceView deviceView; | |||||
public DeviceController(DeviceView view) { | |||||
this.deviceModel = DeviceModel.getInstance(); | |||||
this.deviceView = view; | |||||
} | |||||
public void createDevice(String Sensor, Boolean Status) { | |||||
Device device = new Device(Sensor, Status); | |||||
deviceModel.addDevice(device); | |||||
// todo: Inform view and sender about changes | |||||
} | |||||
public void setDeviceSensor(String UUID, String Sensor) { | |||||
deviceModel.setDeviceSensor(UUID, Sensor); | |||||
} | |||||
public String getDeviceSensor(String UUID) { | |||||
return deviceModel.getDeviceSensor(UUID); | |||||
} | |||||
public void setDeviceStatus(String UUID, Boolean Status) { | |||||
deviceModel.setDeviceStatus(UUID, Status); | |||||
} | |||||
public Boolean getDeviceStatus(String UUID) { | |||||
return deviceModel.getDeviceStatus(UUID); | |||||
} | |||||
} |
package com.example.greenwatch.mvc; | |||||
import java.util.Vector; | |||||
public class DeviceModel { | |||||
private static DeviceModel itemModelInstance; | |||||
// todo: should be a hashmap with UUID Keys | |||||
private Vector<Device> connectetDevicesList; | |||||
private DeviceModel() { | |||||
connectetDevicesList = new Vector<Device>(); | |||||
} | |||||
public static synchronized DeviceModel getInstance() { | |||||
if (itemModelInstance == null){ | |||||
itemModelInstance = new DeviceModel(); | |||||
} | |||||
return itemModelInstance; | |||||
} | |||||
public void addDevice(Device device) { | |||||
// todo: Check if divece already exist | |||||
connectetDevicesList.add(device); | |||||
} | |||||
public Vector<Device> getConnectetDevicesList() { | |||||
return connectetDevicesList; | |||||
} | |||||
public void setDeviceSensor(String UUID, String Sensor) { | |||||
// todo: iterate through HashMap and find Divece for UUID | |||||
} | |||||
public String getDeviceSensor(String UUID) { | |||||
// todo: iterate through HashMap and find Divece for UUID | |||||
return "hasToBeDone"; | |||||
} | |||||
public void setDeviceStatus(String UUID, Boolean Status) { | |||||
// todo: iterate through HashMap and find Divece for UUID | |||||
} | |||||
public Boolean getDeviceStatus(String UUID) { | |||||
// todo: iterate through HashMap and find Divece for UUID | |||||
return false; | |||||
} | |||||
} |
package com.example.greenwatch.mvc; | |||||
import java.util.Vector; | |||||
public class DeviceView { | |||||
public void updateView(Vector<Device> deviceVector) { | |||||
// todo: what should be displayed | |||||
} | |||||
} |
package com.example.greenwatch.repositories; | |||||
import androidx.lifecycle.MutableLiveData; | |||||
import com.example.greenwatch.models.Device; | |||||
import java.util.ArrayList; | |||||
import java.util.HashMap; | |||||
import java.util.List; | |||||
import java.util.UUID; | |||||
public class DeviceRepository { | |||||
private String delimiter = ", "; | |||||
private String deviceUUID; | |||||
private static DeviceRepository deviceRepositoryInstance; | |||||
private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>(); | |||||
private HashMap<String, Device> connectedDevicesList = new HashMap<>(); | |||||
private HashMap<String, String> deviceIDMapper = new HashMap<>(); | |||||
public static synchronized DeviceRepository getInstance() { | |||||
if (deviceRepositoryInstance == null){ | |||||
deviceRepositoryInstance = new DeviceRepository(); | |||||
deviceRepositoryInstance.setDeviceUUID(); | |||||
} | |||||
return deviceRepositoryInstance; | |||||
} | |||||
public MutableLiveData<List<Device>> getConnectedDeviceList() { | |||||
setMutableLiveDataValue(); | |||||
return deviceList; | |||||
} | |||||
public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){ | |||||
Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage); | |||||
addToConnectedDeviceList(newDevice.getDeviceID(), newDevice); | |||||
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) { | |||||
StringBuilder message = new StringBuilder(); | |||||
Device device = connectedDevicesList.get(deviceID); | |||||
if(device != null) { | |||||
message.append(device.getTimeStamp()) | |||||
.append(delimiter) | |||||
.append(device.getDeviceID()) | |||||
.append(delimiter) | |||||
.append(device.getSensorStatus()) | |||||
.append(delimiter) | |||||
.append(device.getSensorType()) | |||||
.append(delimiter) | |||||
.append(device.getSensorMassage()); | |||||
return message.toString(); | |||||
} | |||||
return message.toString(); | |||||
} | |||||
public String getLocalDeviceUUID() { | |||||
return deviceUUID; | |||||
} | |||||
public void setDevice(String deviceID, String timeStamp, String newDeviceID, boolean sensorStatus, String sensorType, int sensorMassage) { | |||||
String checkedDeviceID = checkDeviceID(deviceID); | |||||
Device device = connectedDevicesList.get(checkedDeviceID); | |||||
if(device != null) { | |||||
device.setTimeStamp(timeStamp); | |||||
device.setDeviceID(newDeviceID); | |||||
device.setSensorStatus(sensorStatus); | |||||
device.setSensorType(sensorType); | |||||
device.setSensorMassage(sensorMassage); | |||||
setDeviceIDMapper(newDeviceID); | |||||
connectedDevicesList.remove(checkedDeviceID); | |||||
addToConnectedDeviceList(newDeviceID, device); | |||||
setMutableLiveDataValue(); | |||||
} | |||||
} | |||||
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); | |||||
setMutableLiveDataValue(); | |||||
} | |||||
} | |||||
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); | |||||
setMutableLiveDataValue(); | |||||
} | |||||
} | |||||
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) { | |||||
device.setSensorStatus(sensorStatus); | |||||
addToConnectedDeviceList(checkedDeviceID, device); | |||||
setMutableLiveDataValue(); | |||||
} | |||||
} | |||||
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); | |||||
setMutableLiveDataValue(); | |||||
} | |||||
} | |||||
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, int sensorMessage) { | |||||
String checkedDeviceID = checkDeviceID(deviceID); | |||||
Device device = connectedDevicesList.get(checkedDeviceID); | |||||
if(device != null) { | |||||
device.setSensorMassage(sensorMessage); | |||||
addToConnectedDeviceList(checkedDeviceID, device); | |||||
setMutableLiveDataValue(); | |||||
} | |||||
} | |||||
public int getSensorMassage(String deviceID) { | |||||
String checkedDeviceID = checkDeviceID(deviceID); | |||||
Device device = connectedDevicesList.get(checkedDeviceID); | |||||
if(device != null) { | |||||
return device.getSensorMassage(); | |||||
} | |||||
return 0; | |||||
} | |||||
private void setMutableLiveDataValue() { | |||||
List<Device> list = new ArrayList<>(connectedDevicesList.values()); | |||||
deviceList.setValue(list); | |||||
} | |||||
private void addToConnectedDeviceList(String key, Device device) { | |||||
connectedDevicesList.put(key, device); | |||||
} | |||||
private String checkDeviceID(String deviceID) { | |||||
if(!deviceIDMapper.isEmpty() && deviceID.equals(deviceUUID)) { | |||||
return deviceIDMapper.get(deviceID); | |||||
} | |||||
return deviceID; | |||||
} | |||||
private void setDeviceIDMapper(String deviceID) { | |||||
deviceIDMapper.put(deviceUUID, deviceID); | |||||
} | |||||
private void setDeviceUUID(){ | |||||
this.deviceUUID = UUID.randomUUID().toString(); | |||||
} | |||||
private String[] messageStringSplitter(String message) { | |||||
return message.split(delimiter); | |||||
} | |||||
} |
package com.example.greenwatch.viewmodels; | |||||
import androidx.lifecycle.LiveData; | |||||
import androidx.lifecycle.MutableLiveData; | |||||
import androidx.lifecycle.ViewModel; | |||||
import com.example.greenwatch.models.Device; | |||||
import com.example.greenwatch.repositories.DeviceRepository; | |||||
import java.util.List; | |||||
public class MainActivityViewModel extends ViewModel { | |||||
private MutableLiveData<List<Device>> mDeviceList; | |||||
private MutableLiveData<Device> mLocalDevice; | |||||
private DeviceRepository mDeviceRepository; | |||||
public void init() { | |||||
if(mDeviceList != null) { | |||||
return; | |||||
} | |||||
mDeviceRepository = DeviceRepository.getInstance(); | |||||
mDeviceRepository.createNewDevice("", mDeviceRepository.getLocalDeviceUUID(), false, "", 0); | |||||
mDeviceList = mDeviceRepository.getConnectedDeviceList(); | |||||
} | |||||
public LiveData<List<Device>> getConnectedDeviceList() { | |||||
return mDeviceList; | |||||
} | |||||
public void setDevice(String deviceID, String timeStamp, String newDeviceID, boolean sensorStatus, String sensorType, int sensorMassage) { | |||||
mDeviceRepository.setDevice(deviceID, timeStamp,newDeviceID, sensorStatus, sensorType, sensorMassage); | |||||
} | |||||
public void setTimeStamp(String deviceID, String timeStamp) { | |||||
mDeviceRepository.setTimeStamp(deviceID, timeStamp); | |||||
} | |||||
public String getTimeStamp(String deviceID) { | |||||
return mDeviceRepository.getTimeStamp(deviceID); | |||||
} | |||||
public void setDeviceID(String deviceID, String newDeviceID) { | |||||
mDeviceRepository.setDeviceID(deviceID, newDeviceID); | |||||
} | |||||
public String getDeviceID(String deviceID) { | |||||
return mDeviceRepository.getDeviceID(deviceID); | |||||
} | |||||
public void setSensorStatus(String deviceID, boolean sensorStatus) { | |||||
mDeviceRepository.setSensorStatus(deviceID, sensorStatus); | |||||
} | |||||
public boolean getSensorStatus(String deviceID) { | |||||
return mDeviceRepository.getSensorStatus(deviceID); | |||||
} | |||||
public void setSensorType(String deviceID, String sensorType) { | |||||
mDeviceRepository.setSensorType(deviceID, sensorType); | |||||
} | |||||
public String getSensorType(String deviceID) { | |||||
return mDeviceRepository.getSensorType(deviceID); | |||||
} | |||||
public void setSensorMassage(String deviceID, int sensorMessage) { | |||||
mDeviceRepository.setSensorMassage(deviceID, sensorMessage); | |||||
} | |||||
public int getSensorMassage(String deviceID) { | |||||
return mDeviceRepository.getSensorMassage(deviceID); | |||||
} | |||||
public String getLocalDeviceUUID() { | |||||
return mDeviceRepository.getLocalDeviceUUID(); | |||||
} | |||||
} |