You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeviceRepository.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package com.example.greenwatch.repositories;
  2. import androidx.lifecycle.MutableLiveData;
  3. import com.example.greenwatch.models.Device;
  4. import com.example.greenwatch.communication.WiFiCommunication;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.UUID;
  11. public class DeviceRepository {
  12. private final int maxAlarmHistoryListSize = 20;
  13. private final String delimiter = ", ";
  14. private final String sensorStatusKey = "An";
  15. private String localDeviceUUID;
  16. private static DeviceRepository deviceRepositoryInstance;
  17. private WiFiCommunication mWiFiCommunication;
  18. private boolean startAlarmRecordingValue;
  19. private MutableLiveData<Boolean> startAlarmRecording = new MutableLiveData<>();
  20. private MutableLiveData<List<Device>> deviceList = new MutableLiveData<>();
  21. private MutableLiveData<List<Device>> alarmHistoryList = new MutableLiveData<>();
  22. private HashMap<String, Device> connectedDevicesList = new HashMap<>();
  23. private HashMap<String, String> deviceIDMapper = new HashMap<>();
  24. private List<Device> alarmHistoryDeviceList = new ArrayList<>();
  25. private DeviceRepository() {
  26. setLocalDeviceUUID();
  27. startAlarmRecordingValue = false;
  28. }
  29. public static synchronized DeviceRepository getInstance() {
  30. if (deviceRepositoryInstance == null){
  31. deviceRepositoryInstance = new DeviceRepository();
  32. }
  33. return deviceRepositoryInstance;
  34. }
  35. public void setWiFiCommunication(WiFiCommunication wiFiCommunication) {
  36. this.mWiFiCommunication = wiFiCommunication;
  37. }
  38. public MutableLiveData<List<Device>> getConnectedDeviceList() {
  39. setMutableLiveDataDeviceList();
  40. return deviceList;
  41. }
  42. public MutableLiveData<List<Device>> getAlarmHistoryDeviceList() {
  43. setMutableLiveDataAlarmHistoryList();
  44. return alarmHistoryList;
  45. }
  46. public MutableLiveData<Boolean> getStartAlarmRecording() {
  47. setMutableLiveDataStartAlarmRecording();
  48. return startAlarmRecording;
  49. }
  50. public void createNewDevice(String timeStamp, String deviceID, boolean sensorStatus, String sensorType, int sensorMassage){
  51. Device newDevice = new Device(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
  52. if (sensorStatus) {
  53. setAlarmHistoryDeviceList(newDevice);
  54. }
  55. boolean newStartAlarmRecordingValue = checkDeviceStatus();
  56. if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
  57. startAlarmRecordingValue = newStartAlarmRecordingValue;
  58. setMutableLiveDataStartAlarmRecording();
  59. }
  60. addToConnectedDeviceList(newDevice.getDeviceID(), newDevice);
  61. setMutableLiveDataDeviceList();
  62. }
  63. public void getNewReceivedMessage(String newMessage) {
  64. String[] messageString = messageStringSplitter(newMessage);
  65. String timeStamp = messageString[0];
  66. String deviceID = messageString[1];
  67. boolean sensorStatus = convertSensorStatus(messageString[2]);
  68. String sensorType = messageString[3];
  69. int sensorMassage = Integer.valueOf(messageString[4]);
  70. if (deviceID.equals(checkDeviceID(localDeviceUUID))) {
  71. return;
  72. }
  73. if(!connectedDevicesList.containsKey(deviceID)) {
  74. createNewDevice(timeStamp, deviceID, sensorStatus, sensorType, sensorMassage);
  75. }
  76. else {
  77. updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage);
  78. }
  79. }
  80. private String messageStringBuilder(String deviceID) {
  81. StringBuilder message = new StringBuilder();
  82. Device device = connectedDevicesList.get(deviceID);
  83. if(device != null) {
  84. message.append(device.getTimeStamp())
  85. .append(delimiter)
  86. .append(device.getDeviceID())
  87. .append(delimiter)
  88. .append(device.getSensorStatus())
  89. .append(delimiter)
  90. .append(device.getSensorType())
  91. .append(delimiter)
  92. .append(device.getSensorMassage());
  93. }
  94. else {
  95. message.append("")
  96. .append(delimiter)
  97. .append("")
  98. .append(delimiter)
  99. .append("")
  100. .append(delimiter)
  101. .append("")
  102. .append(delimiter)
  103. .append("");
  104. }
  105. return message.toString();
  106. }
  107. public String getLocalDeviceUUID() {
  108. return localDeviceUUID;
  109. }
  110. public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) {
  111. String checkedDeviceID = checkDeviceID(deviceID);
  112. Device device = connectedDevicesList.get(checkedDeviceID);
  113. if(device != null) {
  114. device.setTimeStamp(timeStamp);
  115. device.setSensorType(sensorType);
  116. device.setSensorMassage(sensorMassage);
  117. if (!device.getSensorStatus() && sensorStatus) {
  118. setAlarmHistoryDeviceList(device);
  119. }
  120. device.setSensorStatus(sensorStatus);
  121. boolean newStartAlarmRecordingValue = checkDeviceStatus();
  122. if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
  123. startAlarmRecordingValue = newStartAlarmRecordingValue;
  124. setMutableLiveDataStartAlarmRecording();
  125. }
  126. addToConnectedDeviceList(checkedDeviceID, device);
  127. setMutableLiveDataDeviceList();
  128. }
  129. }
  130. public void setTimeStamp(String deviceID, String timeStamp) {
  131. String checkedDeviceID = checkDeviceID(deviceID);
  132. Device device = connectedDevicesList.get(checkedDeviceID);
  133. if(device != null) {
  134. device.setTimeStamp(timeStamp);
  135. addToConnectedDeviceList(checkedDeviceID, device);
  136. setMutableLiveDataDeviceList();
  137. }
  138. }
  139. public String getTimeStamp(String deviceID) {
  140. String checkedDeviceID = checkDeviceID(deviceID);
  141. Device device = connectedDevicesList.get(checkedDeviceID);
  142. if(device != null) {
  143. return device.getTimeStamp();
  144. }
  145. return "";
  146. }
  147. public void setDeviceID(String deviceID, String newDeviceID) {
  148. String checkedDeviceID = checkDeviceID(deviceID);
  149. Device device = connectedDevicesList.get(checkedDeviceID);
  150. if(device != null) {
  151. device.setDeviceID(newDeviceID);
  152. setDeviceIDMapper(newDeviceID);
  153. connectedDevicesList.remove(checkedDeviceID);
  154. addToConnectedDeviceList(newDeviceID, device);
  155. setMutableLiveDataDeviceList();
  156. }
  157. }
  158. public String getDeviceID(String deviceID) {
  159. String checkedDeviceID = checkDeviceID(deviceID);
  160. Device device = connectedDevicesList.get(checkedDeviceID);
  161. if(device != null) {
  162. return device.getDeviceID();
  163. }
  164. return "";
  165. }
  166. public void setSensorStatus(String deviceID, boolean sensorStatus) {
  167. String checkedDeviceID = checkDeviceID(deviceID);
  168. Device device = connectedDevicesList.get(checkedDeviceID);
  169. if(device != null) {
  170. if (!device.getSensorStatus() && sensorStatus) {
  171. setAlarmHistoryDeviceList(device);
  172. }
  173. device.setSensorStatus(sensorStatus);
  174. boolean newStartAlarmRecordingValue = checkDeviceStatus();
  175. if (startAlarmRecordingValue != newStartAlarmRecordingValue) {
  176. startAlarmRecordingValue = newStartAlarmRecordingValue;
  177. setMutableLiveDataStartAlarmRecording();
  178. }
  179. addToConnectedDeviceList(checkedDeviceID, device);
  180. setMutableLiveDataDeviceList();
  181. }
  182. }
  183. public boolean getSensorStatus(String deviceID) {
  184. String checkedDeviceID = checkDeviceID(deviceID);
  185. Device device = connectedDevicesList.get(checkedDeviceID);
  186. if(device != null) {
  187. return device.getSensorStatus();
  188. }
  189. return false;
  190. }
  191. public void setSensorType(String deviceID, String sensorType) {
  192. String checkedDeviceID = checkDeviceID(deviceID);
  193. Device device = connectedDevicesList.get(checkedDeviceID);
  194. if(device != null) {
  195. device.setSensorType(sensorType);
  196. addToConnectedDeviceList(checkedDeviceID, device);
  197. setMutableLiveDataDeviceList();
  198. }
  199. }
  200. public String getSensorType(String deviceID) {
  201. String checkedDeviceID = checkDeviceID(deviceID);
  202. Device device = connectedDevicesList.get(checkedDeviceID);
  203. if(device != null) {
  204. return device.getSensorType();
  205. }
  206. return "";
  207. }
  208. public void setSensorMassage(String deviceID, int sensorMessage) {
  209. String checkedDeviceID = checkDeviceID(deviceID);
  210. Device device = connectedDevicesList.get(checkedDeviceID);
  211. if(device != null) {
  212. device.setSensorMassage(sensorMessage);
  213. addToConnectedDeviceList(checkedDeviceID, device);
  214. setMutableLiveDataDeviceList();
  215. }
  216. }
  217. public int getSensorMassage(String deviceID) {
  218. String checkedDeviceID = checkDeviceID(deviceID);
  219. Device device = connectedDevicesList.get(checkedDeviceID);
  220. if(device != null) {
  221. return device.getSensorMassage();
  222. }
  223. return 0;
  224. }
  225. public String getSystemTimeStamp() {
  226. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  227. Date date = new Date(System.currentTimeMillis());
  228. return formatter.format(date);
  229. }
  230. private void setMutableLiveDataDeviceList() {
  231. List<Device> list = new ArrayList<>(connectedDevicesList.values());
  232. deviceList.setValue(list);
  233. }
  234. private void setMutableLiveDataAlarmHistoryList() {
  235. alarmHistoryList.setValue(alarmHistoryDeviceList);
  236. }
  237. private void setMutableLiveDataStartAlarmRecording() {
  238. startAlarmRecording.setValue(startAlarmRecordingValue);
  239. }
  240. private void addToConnectedDeviceList(String key, Device device) {
  241. connectedDevicesList.put(key, device);
  242. if (key.equals(checkDeviceID(localDeviceUUID))) {
  243. mWiFiCommunication.setNewMessage(messageStringBuilder(device.getDeviceID()),true);
  244. }
  245. }
  246. private String checkDeviceID(String deviceID) {
  247. if(!deviceIDMapper.isEmpty() && deviceID.equals(localDeviceUUID)) {
  248. return deviceIDMapper.get(deviceID);
  249. }
  250. return deviceID;
  251. }
  252. private void setDeviceIDMapper(String deviceID) {
  253. deviceIDMapper.put(localDeviceUUID, deviceID);
  254. }
  255. private void setLocalDeviceUUID(){
  256. this.localDeviceUUID = UUID.randomUUID().toString();
  257. }
  258. private String[] messageStringSplitter(String message) {
  259. return message.split(delimiter);
  260. }
  261. private boolean convertSensorStatus(String status) {
  262. return status.equals(sensorStatusKey);
  263. }
  264. private void setAlarmHistoryDeviceList(Device device) {
  265. if (alarmHistoryDeviceList.size() == maxAlarmHistoryListSize) {
  266. alarmHistoryDeviceList.remove(alarmHistoryDeviceList.size() -1);
  267. }
  268. Device alarmHistoryDevice = new Device(device.getTimeStamp(), device.getDeviceID(), device.getSensorStatus(), device.getSensorType(), device.getSensorMassage());
  269. alarmHistoryDeviceList.add(0, alarmHistoryDevice);
  270. setMutableLiveDataAlarmHistoryList();
  271. }
  272. private boolean checkDeviceStatus() {
  273. for (Device device : connectedDevicesList.values()) {
  274. if (device.getSensorStatus()) {
  275. return true;
  276. }
  277. }
  278. return false;
  279. }
  280. }