Skip to content

Commit 1477b8e

Browse files
author
Jeppe Elkjær Jørgensen
committed
IOT-262 add integration to backend
1 parent d9c9c3c commit 1477b8e

File tree

5 files changed

+157
-33
lines changed

5 files changed

+157
-33
lines changed

src/app/models/payload-device-data.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@ import { IotDevice } from '@my-applications/iot-devices/iot-device.model';
33
import { Datatarget } from './datatarget';
44

55
export class PayloadDeviceDatatarget {
6-
devices: number[];
6+
id: number;
7+
iotDeviceIds: number[];
78
payloadDecoderId: number;
8-
datatargetId: number;
9+
dataTargetId: number;
10+
}
11+
12+
export interface PayloadDeviceDatatargetGetByDataTargetResponse {
13+
data: PayloadDeviceDatatargetGetByDataTarget[]
14+
}
15+
16+
export interface PayloadDeviceDatatargetGetByDataTarget {
17+
id: number;
18+
iotDevices: IotDevice[];
19+
payloadDecoder: PayloadDecoder;
20+
dataTarget: Datatarget;
921
}

src/app/shared/form/form-body-datatarget/form-body-datatarget.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ <h4>Efter oprettelse af dit data target dette parres det med en payload decoder
6060
multiple
6161
name="devices"
6262
[compareWith]="compare"
63-
[(value)]="element.devices">
63+
[(value)]="element.iotDeviceIds">
6464
<mat-option disabled="disabled"
6565
class="filter-option">
6666
<button

src/app/shared/form/form-body-datatarget/form-body-datatarget.component.ts

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit, Input, ViewChild } from '@angular/core';
1+
import { Component, OnInit, Input, ViewChild, OnDestroy } from '@angular/core';
22
import { Datatarget } from 'src/app/models/datatarget';
33
import { Subscription } from 'rxjs';
44
import { ActivatedRoute } from '@angular/router';
@@ -12,20 +12,22 @@ import { Application } from '@app/models/application';
1212
import { IotDevice } from '@my-applications/iot-devices/iot-device.model';
1313
import { PayloadDecoderService } from '@shared/services/payload-decoder.service';
1414
import { PayloadDecoderResponse } from '@app/payload-decoder/payload-decoder.model';
15-
import { PayloadDeviceDatatarget } from '@app/models/payload-device-data';
15+
import { PayloadDeviceDatatarget, PayloadDeviceDatatargetGetByDataTarget, PayloadDeviceDatatargetGetByDataTargetResponse } from '@app/models/payload-device-data';
1616
import { faTimesCircle } from '@fortawesome/free-solid-svg-icons';
1717
import { NgModel } from '@angular/forms';
18+
import { PayloadDeviceDatatargetService } from '@shared/services/payloadDeviceDatatarget.service';
1819

1920
@Component({
2021
selector: 'app-form-body-datatarget',
2122
templateUrl: './form-body-datatarget.component.html',
2223
styleUrls: ['./form-body-datatarget.component.scss']
2324
})
24-
export class FormBodyDatatargetComponent implements OnInit {
25+
export class FormBodyDatatargetComponent implements OnInit, OnDestroy {
2526
@Input() submitButton: string;
2627
public datatarget: Datatarget = new Datatarget();
2728
faTimesCircle = faTimesCircle;
2829
public datatargetSubscription: Subscription;
30+
public relationSubscription: Subscription;
2931
public applicationSubscription: Subscription;
3032
public payloadDecoderSubscription: Subscription;
3133
public errorMessages: any;
@@ -46,7 +48,8 @@ export class FormBodyDatatargetComponent implements OnInit {
4648
private datatargetService: DatatargetService,
4749
private location: Location,
4850
private applicationService: ApplicationService,
49-
private payloadDecoderService: PayloadDecoderService
51+
private payloadDecoderService: PayloadDecoderService,
52+
private payloadDeviceDataTargetService: PayloadDeviceDatatargetService
5053
) { }
5154

5255
ngOnInit(): void {
@@ -55,30 +58,38 @@ export class FormBodyDatatargetComponent implements OnInit {
5558
this.applicationId = +this.route.snapshot.paramMap.get('id');
5659
if (this.datatargetid !== 0) {
5760
this.getDatatarget(this.datatargetid);
61+
this.getPayloadDeviceDatatarget(this.datatargetid);
5862
}
5963
if (this.applicationId !== 0) {
6064
this.getDevices();
6165
}
6266
this.getPayloadDecoders();
67+
console.log(this.devices, this.payloadDecoders);
6368
}
6469

6570
addRow() {
6671
if (!this.payloadDeviceDatatarget) {
6772
this.payloadDeviceDatatarget = [];
6873
}
69-
this.payloadDeviceDatatarget.push({devices: [], payloadDecoderId: null, datatargetId: this.datatargetid});
74+
this.payloadDeviceDatatarget.push({id: null, iotDeviceIds: [], payloadDecoderId: null, dataTargetId: this.datatargetid});
7075
}
7176

7277
deleteRow(index) {
7378
if (this.payloadDeviceDatatarget.length === 0) {
79+
} else if (this.payloadDeviceDatatarget[index]?.id === null) {
80+
this.payloadDeviceDatatarget.splice(index, 1);
7481
} else {
75-
this.payloadDeviceDatatarget.splice(index, 1);
82+
this.payloadDeviceDataTargetService.delete(this.payloadDeviceDatatarget[index].id)
83+
.subscribe( (response) => {
84+
this.payloadDeviceDatatarget.splice(index, 1);
85+
});
7686
}
7787
}
7888

7989
onSubmit(): void {
8090
if (this.datatargetid) {
8191
this.updateDatatarget();
92+
this.addPayloadDeviceDatatarget();
8293
} else {
8394
this.createDatatarget();
8495
}
@@ -93,7 +104,6 @@ export class FormBodyDatatargetComponent implements OnInit {
93104
.subscribe(
94105
(datatargetResponse: DatatargetResponse) => {
95106
this.datatarget = this.mapToDatatarget(datatargetResponse);
96-
this.routeBack();
97107
},
98108
(error: HttpErrorResponse) => {
99109
this.handleError(error);
@@ -102,6 +112,38 @@ export class FormBodyDatatargetComponent implements OnInit {
102112
);
103113
}
104114

115+
addPayloadDeviceDatatarget() {
116+
this.payloadDeviceDatatarget.forEach((relation) => {
117+
if (relation.id) {
118+
this.payloadDeviceDataTargetService.put(relation).subscribe(
119+
(response) => {
120+
console.log(response);
121+
},
122+
(error) => {
123+
this.handleError(error);
124+
}
125+
);
126+
} else {
127+
this.payloadDeviceDataTargetService.post(relation).subscribe(
128+
(res: any) => {
129+
console.log(res);
130+
},
131+
(error) => {
132+
this.handleError(error);
133+
}
134+
);
135+
}
136+
});
137+
}
138+
139+
getPayloadDeviceDatatarget(id: number) {
140+
this.relationSubscription = this.payloadDeviceDataTargetService
141+
.getByDataTarget(id)
142+
.subscribe((response: PayloadDeviceDatatargetGetByDataTargetResponse) => {
143+
this.mapToDatatargetDevicePayload(response);
144+
});
145+
}
146+
105147
createDatatarget() {
106148
this.datatarget.applicationId = this.applicationId;
107149
this.datatargetService.create(this.datatarget)
@@ -124,13 +166,13 @@ export class FormBodyDatatargetComponent implements OnInit {
124166
}
125167

126168
public selectAllDevices(index: number) {
127-
console.log(this.payloadDeviceDatatarget[0].devices);
128-
this.payloadDeviceDatatarget[index].devices = this.devices.map( device => device.id);
169+
console.log(this.payloadDeviceDatatarget[0].iotDeviceIds);
170+
this.payloadDeviceDatatarget[index].iotDeviceIds = this.devices.map( device => device.id);
129171
}
130172

131173
public deSelectAllDevices(index: number) {
132-
console.log(this.payloadDeviceDatatarget[0].devices);
133-
this.payloadDeviceDatatarget[index].devices = [];
174+
console.log(this.payloadDeviceDatatarget[0].iotDeviceIds);
175+
this.payloadDeviceDatatarget[index].iotDeviceIds = [];
134176
}
135177

136178
getPayloadDecoders() {
@@ -143,12 +185,16 @@ export class FormBodyDatatargetComponent implements OnInit {
143185
handleError(error: HttpErrorResponse) {
144186
this.errorFields = [];
145187
this.errorMessages = [];
146-
error.error.message.forEach((err) => {
147-
this.errorFields.push(err.property);
148-
this.errorMessages = this.errorMessages.concat(
149-
Object.values(err.constraints)
150-
);
151-
});
188+
if (typeof error.error.message === 'string') {
189+
this.errorMessages.push(error.error.message);
190+
} else {
191+
error.error.message.forEach((err) => {
192+
this.errorFields.push(err.property);
193+
this.errorMessages = this.errorMessages.concat(
194+
Object.values(err.constraints)
195+
);
196+
});
197+
}
152198
}
153199

154200
routeBack(): void {
@@ -174,6 +220,35 @@ export class FormBodyDatatargetComponent implements OnInit {
174220
});
175221
}
176222

223+
ngOnDestroy(): void {
224+
if (this.relationSubscription) {
225+
this.relationSubscription.unsubscribe();
226+
}
227+
if (this.applicationSubscription) {
228+
this.applicationSubscription.unsubscribe();
229+
}
230+
if (this.datatargetSubscription) {
231+
this.datatargetSubscription.unsubscribe();
232+
}
233+
if (this.payloadDecoderSubscription) {
234+
this.payloadDecoderSubscription.unsubscribe();
235+
}
236+
}
237+
238+
private mapToDatatargetDevicePayload(dto: PayloadDeviceDatatargetGetByDataTargetResponse): PayloadDeviceDatatarget[] {
239+
this.payloadDeviceDatatarget = [];
240+
dto.data.forEach(
241+
(element) => {
242+
this.payloadDeviceDatatarget.push({
243+
id: element.id,
244+
iotDeviceIds: element.iotDevices.map((x) => x.id),
245+
payloadDecoderId: element.payloadDecoder?.id,
246+
dataTargetId: element.dataTarget.id
247+
});
248+
}
249+
);
250+
}
251+
177252
private mapToDatatarget(data: DatatargetResponse): Datatarget {
178253
const dt: Datatarget = {
179254
id: data.id,

src/app/shared/services/datatarget.service.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,39 @@ import { DatatargetResponse } from 'src/app/models/datatarget-response';
1010
})
1111
export class DatatargetService {
1212

13-
private url: string = 'data-target';
13+
private dataTargetURL = 'data-target';
1414

1515
constructor(private restService: RestService) { }
1616

1717
get(id: number): Observable<DatatargetResponse> {
18-
return this.restService.get(this.url, null, id);
18+
return this.restService.get(this.dataTargetURL, null, id);
1919
}
2020

2121
getByApplicationId(
22-
limit: number,offset: number, applicationId: number
22+
limit: number, offset: number, applicationId: number
2323
): Observable<DatatargetData> {
2424
const body = {
25-
limit: limit,
26-
offset: offset,
27-
applicationId: applicationId
28-
//sort: sort,
29-
//orderOn: orderOn,
30-
//todo tilføj når iot-314 er tilføjet
25+
limit,
26+
offset,
27+
applicationId
28+
// sort: sort,
29+
// orderOn: orderOn,
30+
// todo tilføj når iot-314 er tilføjet
3131
};
32-
return this.restService.get(this.url, body)
32+
return this.restService.get(this.dataTargetURL, body);
3333
}
3434

3535
update(datatarget: Datatarget): Observable<DatatargetResponse> {
36-
return this.restService.put(this.url,datatarget,datatarget.id,{observe: 'response'});
36+
return this.restService.put(this.dataTargetURL, datatarget, datatarget.id, {observe: 'response'});
3737

3838
}
3939

4040
create(datatarget: Datatarget): Observable<any> {
41-
return this.restService.post(this.url,datatarget);
41+
return this.restService.post(this.dataTargetURL, datatarget);
4242
}
4343

4444
delete(id: number) {
45-
return this.restService.delete(this.url,id);
45+
return this.restService.delete(this.dataTargetURL, id);
4646
}
4747

4848
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Injectable } from '@angular/core';
2+
import { Observable } from 'rxjs';
3+
import { RestService } from './rest.service';
4+
import { PayloadDeviceDatatarget, PayloadDeviceDatatargetGetByDataTargetResponse } from '@app/models/payload-device-data';
5+
6+
@Injectable({
7+
providedIn: 'root'
8+
})
9+
export class PayloadDeviceDatatargetService {
10+
11+
private BASEURL = 'iot-device-payload-decoder-data-target-connection';
12+
private BYDATATARGETURL = '/byDataTarget';
13+
14+
constructor(private restService: RestService) { }
15+
16+
get(id: number): Observable<any> {
17+
return this.restService.get(this.BASEURL, null, id);
18+
}
19+
20+
put(payloadDeviceDatatarget: PayloadDeviceDatatarget): Observable<any> {
21+
return this.restService.put(this.BASEURL, payloadDeviceDatatarget, payloadDeviceDatatarget.id, {observe: 'response'});
22+
23+
}
24+
25+
post(payloadDeviceDatatarget: PayloadDeviceDatatarget): Observable<any> {
26+
return this.restService.post(this.BASEURL, payloadDeviceDatatarget);
27+
}
28+
29+
delete(id: number) {
30+
return this.restService.delete(this.BASEURL, id);
31+
}
32+
33+
getByDataTarget(id: number): Observable<PayloadDeviceDatatargetGetByDataTargetResponse> {
34+
return this.restService.get(this.BASEURL + this.BYDATATARGETURL, null, id);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)