Projekt

Obecné

Profil

Stáhnout (7.01 KB) Statistiky
| Větev: | Tag: | Revize:
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

    
21
@Component({
22
  selector: 'app-dashboard',
23
  templateUrl: './dashboard.component.html',
24
  styleUrls: ['./dashboard.component.scss']
25
})
26
export class DashboardComponent implements OnInit, OnDestroy {
27

    
28
  loggedUser: User;
29
  items: MenuItem[] = [];
30
  position: 'bottom';
31
  groups: Group[];
32
  units: Array<{ drivers?: Drivers; generalInfo?: GeneralInfo; holder?: any; lastpos?: Lastpos; sensors?: Array<Sensor>; unit?: Unit }>;
33
  editedUnit: Unit;
34
  showEditUnitPopup = false;
35
  showInsertSensorPopup = false;
36
  showInsertPositionPopup = false;
37
  phenomenons: Phenomenon[];
38
  sensorTypes: SensorType[];
39
  subscription: Subscription[] = [];
40

    
41
  constructor(
42
    private dataService: DataService,
43
    private sensorService: SensorsService,
44
    private confirmationService: ConfirmationService,
45
    private messageService: MessageService,
46
    private managementService: ManagementService,
47
    private toastService: ToastService,
48
    private authService: AuthService
49
  ) {
50
    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
    this.sensorService.getPhenomenons().subscribe(
68
      response => this.phenomenons = response
69
    );
70
    this.sensorService.getSensorTypes().subscribe(
71
      response => this.sensorTypes = response
72
    );
73
    this.setUser();
74
    this.getUnits();
75
  }
76

    
77
  /**
78
   * Get user from user state
79
   */
80
  setUser(){
81
    this.authService.getUserState().subscribe(res => {
82
      if(res){
83
        this.loggedUser = res;
84
      }
85
    });
86
  }
87

    
88
  /**
89
   * Get all units and theirs sensors from backend
90
   */
91
  getUnits() {
92
    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
  }
97

    
98
  /**
99
   * Show edit unit
100
   * @param $event click event
101
   * @param unit edited unit
102
   */
103
  editUnitPopup($event: MouseEvent, unit: Unit) {
104
    this.editedUnit = unit;
105
    this.showEditUnitPopup = true;
106
  }
107

    
108
  /**
109
   * Show insert unit
110
   * @param $event click event
111
   * @param unit unit for sensor insert
112
   */
113
  insertSensorPopup($event: any, unit: Unit) {
114
    this.showInsertSensorPopup = true;
115
    this.editedUnit = unit;
116
  }
117

    
118
  /**
119
   * Detele unit confirmation
120
   * @param $event click event
121
   * @param unit unit to delete
122
   */
123
  deleteUnit($event: any, unit: Unit) {
124
    this.confirmationService.confirm({
125
      message: 'Do you want to delete this unit?',
126
      header: 'Delete Unit Confirmation',
127
      icon: 'pi pi-info-circle',
128
      accept: () => {
129
        this.processUnitDeletion(unit);
130
      },
131
      reject: () => {
132
        this.toastService.operationRejected();
133
      },
134
      key: 'positionDialog'
135
    });
136
  }
137

    
138
  /**
139
   * Send delete unit request to backend
140
   * @param unit to delete
141
   */
142
  processUnitDeletion(unit: Unit) {
143
    this.managementService.deleteUnit$Response({body: {
144
      unit: {
145
        unit_id: unit.unitId
146
      }}
147
    }).pipe(
148
      map((response: HttpResponse<any>) => {
149
        if (response.status === 200) {
150
          this.toastService.showSuccessMessage(response.body.message);
151
          this.units = this.units.filter(testedUnit => testedUnit.unit.unitId !== unit.unitId);
152
        } else {
153
        }
154
      })
155
    ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
156
  }
157

    
158
  /**
159
   * Show menu items to manipulate with unit
160
   * @param $event click event
161
   * @param unit unit we want edit
162
   */
163
  showItems($event: any, unit: Unit) {
164
    $event.stopPropagation();
165
    this.items = [
166
      {label: 'Edit unit', icon: 'pi pi-cog', command: () => {
167
          this.editUnitPopup($event, unit);
168
        }},
169
      {label: 'Insert position', icon: 'pi pi-cog', command: () => {
170
          this.insertPosition($event, unit);
171
        }},
172
      {label: 'Delete unit', icon: 'pi pi-times', command: () => {
173
          this.deleteUnit($event, unit);
174
        }},
175
      {label: 'Add sensor', icon: 'pi pi-cog', command: () => {
176
          this.insertSensorPopup($event, unit);
177
        }}
178
    ]
179
  }
180

    
181
  /**
182
   * Add created unit to memory so we do not need call backend
183
   * @param inserted unit
184
   */
185
  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
  /**
207
   * Add created sensors to unit in memory so we do not need call backend
208
   * @param inserted sensors
209
   */
210
  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
  /**
224
   * Delete sensor from memory
225
   * @param unitId sensor unit
226
   * @param sensor sensor to delete
227
   */
228
  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
  /**
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
    $event.stopPropagation();
240
    this.showInsertPositionPopup = true;
241
    this.editedUnit = unit;
242
  }
243
}
(3-3/3)