Projekt

Obecné

Profil

Stáhnout (5.75 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 675bbb8c hlavja
  }
52
53
  ngOnInit(): void {
54 278f83f2 Lukáš Moučka
    this.setUser();
55 8c28e5ab hlavja
    this.getUnits();
56 675bbb8c hlavja
  }
57
58 278f83f2 Lukáš Moučka
  setUser(){
59
    this.authService.getUserState().subscribe(res => {
60
      if(res){
61
        this.loggedUser = res;
62
      }
63
    });
64
  }
65
66 675bbb8c hlavja
  getUnits() {
67 773d3c89 hlavja
    this.dataService.getData().subscribe(data => {
68
      this.units = data;
69
      this.units.forEach(unit => unit.sensors.sort((a, b)  => a.sensorId - b.sensorId));
70
    }, err => this.toastService.showError(err.error.message));
71 675bbb8c hlavja
  }
72
73 773d3c89 hlavja
  editUnitPopup($event: MouseEvent, unit: Unit) {
74 bb7795cd hlavja
    $event.stopPropagation();
75
    this.editedUnit = unit;
76 a3ae1cab hlavja
    this.showEditUnitPopup = true;
77
  }
78
79 773d3c89 hlavja
  insertSensorPopup($event: any, unit: Unit) {
80 533869de hlavja
    $event.stopPropagation();
81 b5525aef hlavja
    this.sensorService.getSensorTypes().subscribe(
82
      response => this.sensorTypes = response
83
    )
84 533869de hlavja
    this.showInsertSensorPopup = true;
85
    this.editedUnit = unit;
86
  }
87 ea0e5344 hlavja
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 b5525aef hlavja
        this.processUnitDeletion(unit);
96 ea0e5344 hlavja
      },
97
      reject: () => {
98 773d3c89 hlavja
        this.toastService.operationRejected();
99 ea0e5344 hlavja
      },
100
      key: 'positionDialog'
101
    });
102
  }
103
104 b5525aef hlavja
  processUnitDeletion(unit: Unit) {
105
    this.managementService.deleteUnit$Response({body: {
106
      unit: {
107
        unit_id: unit.unitId
108
      }}
109
    }).pipe(
110 773d3c89 hlavja
      map((response: HttpResponse<any>) => {
111
        if (response.status === 200) {
112
          this.toastService.showSuccessMessage(response.body.message);
113 b5525aef hlavja
          this.units = this.units.filter(testedUnit => testedUnit.unit.unitId !== unit.unitId);
114 773d3c89 hlavja
        } else {
115
        }
116
      })
117
    ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
118 ea0e5344 hlavja
  }
119 c835a12d hlavja
120
  showItems($event: any, unit: Unit) {
121
    $event.stopPropagation();
122
    this.items = [
123 278f83f2 Lukáš Moučka
      {label: 'Edit unit', icon: 'pi pi-cog', command: () => {
124 c835a12d hlavja
          event.stopPropagation();
125 278f83f2 Lukáš Moučka
          this.editUnitPopup($event, unit);
126 c835a12d hlavja
        }},
127 b5525aef hlavja
      {label: 'Insert position', icon: 'pi pi-cog', command: () => {
128
          event.stopPropagation();
129
          this.insertPosition($event, unit);
130
        }},
131 c835a12d hlavja
      {label: 'Delete unit', icon: 'pi pi-times', command: () => {
132
          event.stopPropagation();
133
          this.deleteUnit($event, unit);
134
        }}
135
    ]
136
  }
137 b5525aef hlavja
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 675bbb8c hlavja
}