|
| 1 | +import { Component, Inject, OnDestroy, OnInit } from "@angular/core"; |
| 2 | +import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material/dialog"; |
| 3 | +import { MatSnackBar } from "@angular/material/snack-bar"; |
| 4 | +import { Organisation } from "@app/admin/organisation/organisation.model"; |
| 5 | +import { OrganisationService } from "@app/admin/organisation/organisation.service"; |
| 6 | +import { DeviceModelService } from "@app/device-model/device-model.service"; |
| 7 | +import { DeviceModel } from "@app/device-model/device.model"; |
| 8 | +import { Application } from "@applications/application.model"; |
| 9 | +import { ApplicationService } from "@applications/application.service"; |
| 10 | +import { TranslateService } from "@ngx-translate/core"; |
| 11 | +import { IoTDeviceApplicationDialogModel } from "@shared/models/dialog.model"; |
| 12 | +import { ReplaySubject } from "rxjs"; |
| 13 | +import { IotDevice, UpdateIoTDeviceApplication } from "../iot-device.model"; |
| 14 | +import { IoTDeviceService } from "../iot-device.service"; |
| 15 | +import { PayloadDecoder, PayloadDecoderMappedResponse } from "@payload-decoder/payload-decoder.model"; |
| 16 | +import { PayloadDecoderService } from "@payload-decoder/payload-decoder.service"; |
| 17 | +import { DatatargetService } from "@applications/datatarget/datatarget.service"; |
| 18 | +import { Datatarget } from "@applications/datatarget/datatarget.model"; |
| 19 | +import { PayloadDeviceDatatargetService } from "@payload-decoder/payload-device-datatarget.service"; |
| 20 | +import { faTimesCircle } from "@fortawesome/free-solid-svg-icons"; |
| 21 | + |
| 22 | +@Component({ |
| 23 | + selector: "app-iot-device-change-application-dialog", |
| 24 | + templateUrl: "./iot-device-change-application-dialog.component.html", |
| 25 | + styleUrls: ["./iot-device-change-application-dialog.component.scss"], |
| 26 | +}) |
| 27 | +export class IoTDeviceChangeApplicationDialogComponent implements OnInit, OnDestroy { |
| 28 | + public iotDevice: IotDevice; |
| 29 | + public iotDeviceUpdate: UpdateIoTDeviceApplication; |
| 30 | + public organizations: ReplaySubject<Organisation[]> = new ReplaySubject<Organisation[]>(1); |
| 31 | + public applications: Application[]; |
| 32 | + public filteredApplications: ReplaySubject<Application[]> = new ReplaySubject<Application[]>(1); |
| 33 | + public deviceModels: DeviceModel[]; |
| 34 | + public devices: IotDevice[] = []; |
| 35 | + public payloadDecoders: PayloadDecoder[] = []; |
| 36 | + public dataTargets: Datatarget[] = []; |
| 37 | + faTimesCircle = faTimesCircle; |
| 38 | + private subscriptions = []; |
| 39 | + |
| 40 | + constructor( |
| 41 | + private iotDeviceService: IoTDeviceService, |
| 42 | + private applicationService: ApplicationService, |
| 43 | + public translate: TranslateService, |
| 44 | + private organizationService: OrganisationService, |
| 45 | + private deviceModelService: DeviceModelService, |
| 46 | + private payloadDecoderService: PayloadDecoderService, |
| 47 | + private dateTargetService: DatatargetService, |
| 48 | + private payloadDeviceDatatargetService: PayloadDeviceDatatargetService, |
| 49 | + private snackBar: MatSnackBar, |
| 50 | + private dialog: MatDialogRef<IoTDeviceChangeApplicationDialogComponent>, |
| 51 | + @Inject(MAT_DIALOG_DATA) public dialogModel: IoTDeviceApplicationDialogModel |
| 52 | + ) {} |
| 53 | + |
| 54 | + ngOnInit(): void { |
| 55 | + this.translate.use("da"); |
| 56 | + this.iotDeviceUpdate = { |
| 57 | + deviceModelId: this.dialogModel.deviceId, |
| 58 | + applicationId: 0, |
| 59 | + organizationId: 0, |
| 60 | + dataTargetToPayloadDecoderIds: [], |
| 61 | + }; |
| 62 | + |
| 63 | + this.getIoTDeviceAndDefaultData(this.dialogModel.deviceId); |
| 64 | + } |
| 65 | + |
| 66 | + ngOnDestroy(): void { |
| 67 | + this.subscriptions.forEach(s => s?.unsubscribe()); |
| 68 | + } |
| 69 | + |
| 70 | + getIoTDeviceAndDefaultData(id: number): void { |
| 71 | + const iotDevicesSubscription = this.iotDeviceService.getIoTDevice(id).subscribe((iotDevice: IotDevice) => { |
| 72 | + this.iotDevice = iotDevice; |
| 73 | + this.getOrganizations(); |
| 74 | + this.getApplications(); |
| 75 | + this.getPayloadDecoders(); |
| 76 | + |
| 77 | + this.getDataTargets(this.iotDevice.application.id); |
| 78 | + this.getDeviceModels(this.iotDevice.application.belongsTo.id); |
| 79 | + |
| 80 | + this.iotDeviceUpdate.deviceModelId = this.iotDevice.deviceModel.id; |
| 81 | + this.iotDeviceUpdate.applicationId = this.iotDevice.application.id; |
| 82 | + this.iotDeviceUpdate.organizationId = this.iotDevice.application.belongsTo.id; |
| 83 | + |
| 84 | + this.getIoTDeviceCurrentDataTargetsAndPayloadDecoders(this.dialogModel.deviceId); |
| 85 | + }); |
| 86 | + |
| 87 | + this.subscriptions.push(iotDevicesSubscription); |
| 88 | + } |
| 89 | + |
| 90 | + getIoTDeviceCurrentDataTargetsAndPayloadDecoders(id: number): void { |
| 91 | + const payloadDeviceDatatargetSubscription = this.payloadDeviceDatatargetService |
| 92 | + .getByIoTDevice(id) |
| 93 | + .subscribe(dataTargetsAndPayloadDecoders => { |
| 94 | + const dataTargetsAndPayloadIds = dataTargetsAndPayloadDecoders.data.map(val => { |
| 95 | + return { dataTargetId: val.dataTarget.id, payloadDecoderId: val.payloadDecoder?.id }; |
| 96 | + }); |
| 97 | + this.iotDeviceUpdate.dataTargetToPayloadDecoderIds.push(...dataTargetsAndPayloadIds); |
| 98 | + }); |
| 99 | + |
| 100 | + this.subscriptions.push(payloadDeviceDatatargetSubscription); |
| 101 | + } |
| 102 | + |
| 103 | + getOrganizations() { |
| 104 | + const organizationsSubscription = this.organizationService.getMultipleWithApplicationAdmin().subscribe(res => { |
| 105 | + this.organizations.next(res.data.slice()); |
| 106 | + }); |
| 107 | + |
| 108 | + this.subscriptions.push(organizationsSubscription); |
| 109 | + } |
| 110 | + |
| 111 | + getApplications(): void { |
| 112 | + const applicationsSubscription = this.applicationService |
| 113 | + .getApplications(1000, 0, "asc", "id") |
| 114 | + .subscribe(applicationData => { |
| 115 | + this.applications = applicationData.data; |
| 116 | + this.filteredApplications.next( |
| 117 | + this.applications.filter(app => app.belongsTo.id === this.iotDevice.application.belongsTo.id) |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + this.subscriptions.push(applicationsSubscription); |
| 122 | + } |
| 123 | + |
| 124 | + getPayloadDecoders() { |
| 125 | + const payloadDecoderSubscription = this.payloadDecoderService |
| 126 | + .getMultiple(1000, 0, "id", "ASC") |
| 127 | + .subscribe((response: PayloadDecoderMappedResponse) => { |
| 128 | + this.payloadDecoders = response.data.sort((a, b) => a.name.localeCompare(b.name, "en", { numeric: true })); |
| 129 | + }); |
| 130 | + |
| 131 | + this.subscriptions.push(payloadDecoderSubscription); |
| 132 | + } |
| 133 | + |
| 134 | + getDataTargets(applicationId: number) { |
| 135 | + const dataTargetSubscription = this.dateTargetService |
| 136 | + .getByApplicationId(1000, 0, applicationId) |
| 137 | + .subscribe(response => { |
| 138 | + this.dataTargets = response.data; |
| 139 | + }); |
| 140 | + |
| 141 | + this.subscriptions.push(dataTargetSubscription); |
| 142 | + } |
| 143 | + |
| 144 | + getDeviceModels(organizationId: number) { |
| 145 | + const deviceModelSubscription = this.deviceModelService |
| 146 | + .getMultiple(1000, 0, "asc", "id", organizationId) |
| 147 | + .subscribe(res => { |
| 148 | + this.deviceModels = res.data.sort((a, b) => a.body.name.localeCompare(b.body.name, "en", { numeric: true })); |
| 149 | + }); |
| 150 | + |
| 151 | + this.subscriptions.push(deviceModelSubscription); |
| 152 | + } |
| 153 | + |
| 154 | + public addRow() { |
| 155 | + this.iotDeviceUpdate.dataTargetToPayloadDecoderIds.push({ |
| 156 | + dataTargetId: null, |
| 157 | + payloadDecoderId: null, |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + public deleteRow(index) { |
| 162 | + this.iotDeviceUpdate.dataTargetToPayloadDecoderIds.splice(index, 1); |
| 163 | + } |
| 164 | + |
| 165 | + onOrganizationChange() { |
| 166 | + this.filteredApplications.next( |
| 167 | + this.applications.filter(app => app.belongsTo.id === this.iotDeviceUpdate.organizationId) |
| 168 | + ); |
| 169 | + this.iotDeviceUpdate.applicationId = 0; |
| 170 | + this.iotDeviceUpdate.deviceModelId = 0; |
| 171 | + this.iotDeviceUpdate.dataTargetToPayloadDecoderIds = []; |
| 172 | + this.dataTargets = []; |
| 173 | + this.getDeviceModels(this.iotDeviceUpdate.organizationId); |
| 174 | + } |
| 175 | + |
| 176 | + onApplicationChange() { |
| 177 | + this.getDataTargets(this.iotDeviceUpdate.applicationId); |
| 178 | + this.iotDeviceUpdate.dataTargetToPayloadDecoderIds = []; |
| 179 | + } |
| 180 | + |
| 181 | + public compare(o1: any, o2: any): boolean { |
| 182 | + return o1 === o2; |
| 183 | + } |
| 184 | + |
| 185 | + onSubmit() { |
| 186 | + if ( |
| 187 | + !this.iotDeviceUpdate.applicationId || |
| 188 | + !this.iotDeviceUpdate.organizationId || |
| 189 | + this.iotDeviceUpdate.deviceModelId == null || |
| 190 | + this.iotDeviceUpdate.dataTargetToPayloadDecoderIds.some(val => !val.dataTargetId) |
| 191 | + ) |
| 192 | + return; |
| 193 | + |
| 194 | + const iotDevicesSubscription = this.iotDeviceService |
| 195 | + .changeIoTDeviceApplication(this.iotDevice.id, this.iotDeviceUpdate) |
| 196 | + .subscribe(savedIoTDevice => { |
| 197 | + this.snackBar.open( |
| 198 | + this.translate.instant("IOTDEVICE.CHANGE-APPLICATION.SNACKBAR-SAVED", { |
| 199 | + deviceName: savedIoTDevice.name, |
| 200 | + applicationName: savedIoTDevice.application.name, |
| 201 | + }), |
| 202 | + this.translate.instant("DIALOG.OK"), |
| 203 | + { |
| 204 | + duration: 10000, |
| 205 | + } |
| 206 | + ); |
| 207 | + this.dialog.close(true); |
| 208 | + this.snackBar._openedSnackBarRef.afterDismissed().subscribe(() => location.reload()); |
| 209 | + }); |
| 210 | + |
| 211 | + this.subscriptions.push(iotDevicesSubscription); |
| 212 | + } |
| 213 | +} |
0 commit comments