Projekt

Obecné

Profil

Stáhnout (5.75 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
    $event.stopPropagation();
78
    this.editedUnit = unit;
79
    this.showEditUnitPopup = true;
80
  }
81

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

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

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

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

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

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

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

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