Projekt

Obecné

Profil

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