dashboard.component.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {Component, OnDestroy, OnInit} from '@angular/core';
  2. import {Group} from '../../shared/api/endpoints/models/group';
  3. import {Drivers} from '../../shared/api/endpoints/models/drivers';
  4. import {GeneralInfo} from '../../shared/api/endpoints/models/general-info';
  5. import {Lastpos} from '../../shared/api/endpoints/models/lastpos';
  6. import {Sensor} from '../../shared/api/endpoints/models/sensor';
  7. import {Unit} from '../../shared/api/endpoints/models/unit';
  8. import {DataService} from '../../shared/api/endpoints/services/data.service';
  9. import {Phenomenon} from '../../shared/api/endpoints/models/phenomenon';
  10. import {SensorsService} from '../../shared/api/endpoints/services/sensors.service';
  11. import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
  12. import {ManagementService} from '../../shared/api/endpoints/services/management.service';
  13. import {ToastService} from '../../shared/services/toast.service';
  14. import {map} from 'rxjs/operators';
  15. import {HttpResponse} from '@angular/common/http';
  16. import {AuthService} from '../../auth/services/auth.service';
  17. import {User} from '../../auth/models/user';
  18. import {SensorType} from '../../shared/api/endpoints/models/sensor-type';
  19. import {Subscription} from 'rxjs';
  20. @Component({
  21. selector: 'app-dashboard',
  22. templateUrl: './dashboard.component.html',
  23. styleUrls: ['./dashboard.component.scss']
  24. })
  25. export class DashboardComponent implements OnInit, OnDestroy {
  26. loggedUser: User;
  27. items: MenuItem[] = [];
  28. position: 'bottom';
  29. groups: Group[];
  30. units: Array<{ drivers?: Drivers; generalInfo?: GeneralInfo; holder?: any; lastpos?: Lastpos; sensors?: Array<Sensor>; unit?: Unit }>;
  31. editedUnit: Unit;
  32. showEditUnitPopup = false;
  33. showInsertSensorPopup = false;
  34. showInsertPositionPopup = false;
  35. phenomenons: Phenomenon[];
  36. sensorTypes: SensorType[];
  37. subscription: Subscription[] = [];
  38. constructor(
  39. private dataService: DataService,
  40. private sensorService: SensorsService,
  41. private confirmationService: ConfirmationService,
  42. private messageService: MessageService,
  43. private managementService: ManagementService,
  44. private toastService: ToastService,
  45. private authService: AuthService
  46. ) {
  47. this.initData();
  48. }
  49. ngOnInit(): void {
  50. }
  51. /**
  52. * Unsubscribe after leaving
  53. */
  54. ngOnDestroy(): void {
  55. this.subscription.forEach(subs => subs.unsubscribe());
  56. }
  57. /**
  58. * Get necessary data from backend
  59. */
  60. initData() {
  61. this.sensorService.getPhenomenons().subscribe(
  62. response => this.phenomenons = response
  63. );
  64. this.sensorService.getSensorTypes().subscribe(
  65. response => this.sensorTypes = response
  66. );
  67. this.setUser();
  68. this.getUnits();
  69. }
  70. /**
  71. * Get user from user state
  72. */
  73. setUser(){
  74. this.authService.getUserState().subscribe(res => {
  75. if(res){
  76. this.loggedUser = res;
  77. }
  78. });
  79. }
  80. /**
  81. * Get all units and theirs sensors from backend
  82. */
  83. getUnits() {
  84. this.dataService.getData().subscribe(data => {
  85. this.units = data;
  86. this.units.forEach(unit => unit.sensors.sort((a, b) => a.sensorId - b.sensorId));
  87. }, err => this.toastService.showError(err.error.message));
  88. }
  89. /**
  90. * Show edit unit
  91. * @param $event click event
  92. * @param unit edited unit
  93. */
  94. editUnitPopup($event: MouseEvent, unit: Unit) {
  95. this.editedUnit = unit;
  96. this.showEditUnitPopup = true;
  97. }
  98. /**
  99. * Show insert unit
  100. * @param $event click event
  101. * @param unit unit for sensor insert
  102. */
  103. insertSensorPopup($event: any, unit: Unit) {
  104. this.showInsertSensorPopup = true;
  105. this.editedUnit = unit;
  106. }
  107. /**
  108. * Detele unit confirmation
  109. * @param $event click event
  110. * @param unit unit to delete
  111. */
  112. deleteUnit($event: any, unit: Unit) {
  113. this.confirmationService.confirm({
  114. message: 'Do you want to delete this unit?',
  115. header: 'Delete unit confirmation',
  116. icon: 'pi pi-info-circle',
  117. accept: () => {
  118. this.processUnitDeletion(unit);
  119. },
  120. reject: () => {
  121. this.toastService.operationRejected();
  122. },
  123. key: 'positionDialog'
  124. });
  125. }
  126. /**
  127. * Send delete unit request to backend
  128. * @param unit to delete
  129. */
  130. processUnitDeletion(unit: Unit) {
  131. this.managementService.deleteUnit$Response({body: {
  132. unit: {
  133. unit_id: unit.unitId
  134. }}
  135. }).pipe(
  136. map((response: HttpResponse<any>) => {
  137. if (response.status === 200) {
  138. this.toastService.showSuccessMessage(response.body.message);
  139. this.units = this.units.filter(testedUnit => testedUnit.unit.unitId !== unit.unitId);
  140. } else {
  141. }
  142. })
  143. ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
  144. }
  145. /**
  146. * Show menu items to manipulate with unit
  147. * @param $event click event
  148. * @param unit unit we want edit
  149. */
  150. showItems($event: any, unit: Unit) {
  151. $event.stopPropagation();
  152. this.items = [
  153. {label: 'Edit unit', icon: 'pi pi-cog', command: () => {
  154. this.editUnitPopup($event, unit);
  155. }},
  156. {label: 'Insert position', icon: 'pi pi-cog', command: () => {
  157. this.insertPosition($event, unit);
  158. }},
  159. {label: 'Delete unit', icon: 'pi pi-times', command: () => {
  160. this.deleteUnit($event, unit);
  161. }},
  162. {label: 'Add sensor', icon: 'pi pi-cog', command: () => {
  163. this.insertSensorPopup($event, unit);
  164. }}
  165. ]
  166. }
  167. /**
  168. * Add created unit to memory so we do not need call backend
  169. * @param inserted unit
  170. */
  171. addUnit(inserted: any) {
  172. const sensors: Sensor[] = [];
  173. inserted.sensors.forEach(sens => {
  174. sensors.push({
  175. sensorId: sens.sensor_id,
  176. sensorType: sens.sensor_type,
  177. sensorName: sens.sensor_name,
  178. phenomenon: {
  179. phenomenonId: sens.phenomenon.phenomenon_id.toString()
  180. }
  181. })
  182. });
  183. this.units.push({
  184. unit: {
  185. unitId: inserted.unit.unit_id,
  186. description: inserted.unit.description
  187. },
  188. sensors
  189. })
  190. }
  191. /**
  192. * Add created sensors to unit in memory so we do not need call backend
  193. * @param inserted sensors
  194. */
  195. addSensors(inserted: any) {
  196. inserted.sensors.forEach(sens => {
  197. this.units.find(un => un.unit.unitId === inserted.unit.unit_id).sensors.push({
  198. sensorId: sens.sensor_id,
  199. sensorType: sens.sensor_type,
  200. sensorName: sens.sensor_name,
  201. phenomenon: {
  202. phenomenonId: sens.phenomenon.phenomenon_id.toString()
  203. }
  204. })
  205. });
  206. }
  207. /**
  208. * Delete sensor from memory
  209. * @param unitId sensor unit
  210. * @param sensor sensor to delete
  211. */
  212. deleteSensor(unitId: number, sensor: Sensor) {
  213. this.units.find(unit => unit.unit.unitId === unitId).sensors =
  214. this.units.find(unit => unit.unit.unitId === unitId).sensors.filter(testedSensor => testedSensor.sensorId !== sensor.sensorId);
  215. }
  216. /**
  217. * Show insert position popup
  218. * @param $event click event
  219. * @param unit unit to insert position for
  220. */
  221. insertPosition($event: any, unit: Unit) {
  222. $event.stopPropagation();
  223. this.showInsertPositionPopup = true;
  224. this.editedUnit = unit;
  225. }
  226. }