Projekt

Obecné

Profil

Stáhnout (5.68 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Component, 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

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

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

    
39
  constructor(
40
    private dataService: DataService,
41
    private sensorService: SensorsService,
42
    private confirmationService: ConfirmationService,
43
    private messageService: MessageService,
44
    private managementService: ManagementService,
45
    private toastService: ToastService,
46
    private authService: AuthService
47
  ) {
48
    this.sensorService.getPhenomenons().subscribe(
49
      response => this.phenomenons = response
50
    );
51
    this.sensorService.getSensorTypes().subscribe(
52
      response => this.sensorTypes = response
53
    );
54
  }
55

    
56
  ngOnInit(): void {
57
    this.setUser();
58
    this.getUnits();
59
  }
60

    
61
  setUser(){
62
    this.authService.getUserState().subscribe(res => {
63
      if(res){
64
        this.loggedUser = res;
65
      }
66
    });
67
  }
68

    
69
  getUnits() {
70
    this.dataService.getData().subscribe(data => {
71
      this.units = data;
72
      this.units.forEach(unit => unit.sensors.sort((a, b)  => a.sensorId - b.sensorId));
73
    }, err => this.toastService.showError(err.error.message));
74
  }
75

    
76
  editUnitPopup($event: MouseEvent, unit: Unit) {
77
    this.editedUnit = unit;
78
    this.showEditUnitPopup = true;
79
  }
80

    
81
  insertSensorPopup($event: any, unit: Unit) {
82
    this.showInsertSensorPopup = true;
83
    this.editedUnit = unit;
84
  }
85

    
86
  deleteUnit($event: any, unit: Unit) {
87
    this.confirmationService.confirm({
88
      message: 'Do you want to delete this unit?',
89
      header: 'Delete Unit Confirmation',
90
      icon: 'pi pi-info-circle',
91
      accept: () => {
92
        this.processUnitDeletion(unit);
93
      },
94
      reject: () => {
95
        this.toastService.operationRejected();
96
      },
97
      key: 'positionDialog'
98
    });
99
  }
100

    
101
  processUnitDeletion(unit: Unit) {
102
    this.managementService.deleteUnit$Response({body: {
103
      unit: {
104
        unit_id: unit.unitId
105
      }}
106
    }).pipe(
107
      map((response: HttpResponse<any>) => {
108
        if (response.status === 200) {
109
          this.toastService.showSuccessMessage(response.body.message);
110
          this.units = this.units.filter(testedUnit => testedUnit.unit.unitId !== unit.unitId);
111
        } else {
112
        }
113
      })
114
    ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
115
  }
116

    
117
  showItems($event: any, unit: Unit) {
118
    $event.stopPropagation();
119
    this.items = [
120
      {label: 'Edit unit', icon: 'pi pi-cog', command: () => {
121
          this.editUnitPopup($event, unit);
122
        }},
123
      {label: 'Insert position', icon: 'pi pi-cog', command: () => {
124
          this.insertPosition($event, unit);
125
        }},
126
      {label: 'Delete unit', icon: 'pi pi-times', command: () => {
127
          this.deleteUnit($event, unit);
128
        }},
129
      {label: 'Add sensor', icon: 'pi pi-cog', command: () => {
130
          this.insertSensorPopup($event, unit);
131
        }}
132
    ]
133
  }
134

    
135
  addUnit(inserted: any) {
136
    const sensors: Sensor[] = [];
137
    inserted.sensors.forEach(sens => {
138
      sensors.push({
139
        sensorId: sens.sensor_id,
140
        sensorType: sens.sensor_type,
141
        sensorName: sens.sensor_name,
142
        phenomenon: {
143
          phenomenonId: sens.phenomenon.phenomenon_id.toString()
144
        }
145
      })
146
    });
147
    this.units.push({
148
      unit: {
149
        unitId: inserted.unit.unit_id,
150
        description: inserted.unit.description
151
      },
152
      sensors
153
    })
154
  }
155

    
156
  addSensors(inserted: any) {
157
    inserted.sensors.forEach(sens => {
158
      this.units.find(un => un.unit.unitId === inserted.unit.unit_id).sensors.push({
159
        sensorId: sens.sensor_id,
160
        sensorType: sens.sensor_type,
161
        sensorName: sens.sensor_name,
162
        phenomenon: {
163
          phenomenonId: sens.phenomenon.phenomenon_id.toString()
164
        }
165
      })
166
    });
167
  }
168

    
169
  deleteSensor(unitId: number, sensor: Sensor) {
170
    this.units.find(unit => unit.unit.unitId === unitId).sensors =
171
      this.units.find(unit => unit.unit.unitId === unitId).sensors.filter(testedSensor => testedSensor.sensorId !== sensor.sensorId);
172
  }
173

    
174
  private insertPosition($event: any, unit: Unit) {
175
    $event.stopPropagation();
176
    this.showInsertPositionPopup = true;
177
    this.editedUnit = unit;
178
  }
179
}
(3-3/3)