Projekt

Obecné

Profil

Stáhnout (5.68 KB) Statistiky
| Větev: | Tag: | Revize:
1 675bbb8c hlavja
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 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 675bbb8c hlavja
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 278f83f2 Lukáš Moučka
  loggedUser: User;
28 c835a12d hlavja
  items: MenuItem[] = [];
29 ea0e5344 hlavja
  position: 'bottom';
30 675bbb8c hlavja
  groups: Group[];
31
  units: Array<{ drivers?: Drivers; generalInfo?: GeneralInfo; holder?: any; lastpos?: Lastpos; sensors?: Array<Sensor>; unit?: Unit }>;
32 bb7795cd hlavja
  editedUnit: Unit;
33 a3ae1cab hlavja
  showEditUnitPopup = false;
34 533869de hlavja
  showInsertSensorPopup = false;
35 b5525aef hlavja
  showInsertPositionPopup = false;
36 a3ae1cab hlavja
  phenomenons: Phenomenon[];
37 b5525aef hlavja
  sensorTypes: SensorType[];
38 675bbb8c hlavja
39
  constructor(
40 a3ae1cab hlavja
    private dataService: DataService,
41 ea0e5344 hlavja
    private sensorService: SensorsService,
42
    private confirmationService: ConfirmationService,
43
    private messageService: MessageService,
44 773d3c89 hlavja
    private managementService: ManagementService,
45 278f83f2 Lukáš Moučka
    private toastService: ToastService,
46
    private authService: AuthService
47 675bbb8c hlavja
  ) {
48 a3ae1cab hlavja
    this.sensorService.getPhenomenons().subscribe(
49
      response => this.phenomenons = response
50
    );
51 c64ba540 hlavja
    this.sensorService.getSensorTypes().subscribe(
52
      response => this.sensorTypes = response
53
    );
54 675bbb8c hlavja
  }
55
56
  ngOnInit(): void {
57 278f83f2 Lukáš Moučka
    this.setUser();
58 8c28e5ab hlavja
    this.getUnits();
59 675bbb8c hlavja
  }
60
61 278f83f2 Lukáš Moučka
  setUser(){
62
    this.authService.getUserState().subscribe(res => {
63
      if(res){
64
        this.loggedUser = res;
65
      }
66
    });
67
  }
68
69 675bbb8c hlavja
  getUnits() {
70 773d3c89 hlavja
    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 675bbb8c hlavja
  }
75
76 773d3c89 hlavja
  editUnitPopup($event: MouseEvent, unit: Unit) {
77 bb7795cd hlavja
    this.editedUnit = unit;
78 a3ae1cab hlavja
    this.showEditUnitPopup = true;
79
  }
80
81 773d3c89 hlavja
  insertSensorPopup($event: any, unit: Unit) {
82 533869de hlavja
    this.showInsertSensorPopup = true;
83
    this.editedUnit = unit;
84
  }
85 ea0e5344 hlavja
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 b5525aef hlavja
        this.processUnitDeletion(unit);
93 ea0e5344 hlavja
      },
94
      reject: () => {
95 773d3c89 hlavja
        this.toastService.operationRejected();
96 ea0e5344 hlavja
      },
97
      key: 'positionDialog'
98
    });
99
  }
100
101 b5525aef hlavja
  processUnitDeletion(unit: Unit) {
102
    this.managementService.deleteUnit$Response({body: {
103
      unit: {
104
        unit_id: unit.unitId
105
      }}
106
    }).pipe(
107 773d3c89 hlavja
      map((response: HttpResponse<any>) => {
108
        if (response.status === 200) {
109
          this.toastService.showSuccessMessage(response.body.message);
110 b5525aef hlavja
          this.units = this.units.filter(testedUnit => testedUnit.unit.unitId !== unit.unitId);
111 773d3c89 hlavja
        } else {
112
        }
113
      })
114
    ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
115 ea0e5344 hlavja
  }
116 c835a12d hlavja
117
  showItems($event: any, unit: Unit) {
118
    $event.stopPropagation();
119
    this.items = [
120 278f83f2 Lukáš Moučka
      {label: 'Edit unit', icon: 'pi pi-cog', command: () => {
121
          this.editUnitPopup($event, unit);
122 c835a12d hlavja
        }},
123 b5525aef hlavja
      {label: 'Insert position', icon: 'pi pi-cog', command: () => {
124
          this.insertPosition($event, unit);
125
        }},
126 c835a12d hlavja
      {label: 'Delete unit', icon: 'pi pi-times', command: () => {
127
          this.deleteUnit($event, unit);
128 6bd78e65 hlavja
        }},
129
      {label: 'Add sensor', icon: 'pi pi-cog', command: () => {
130
          this.insertSensorPopup($event, unit);
131 c835a12d hlavja
        }}
132
    ]
133
  }
134 b5525aef hlavja
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 675bbb8c hlavja
}