1- import { Component , OnInit , Input , ViewChild } from '@angular/core' ;
1+ import { Component , OnInit , Input , ViewChild , OnDestroy } from '@angular/core' ;
22import { Datatarget } from 'src/app/models/datatarget' ;
33import { Subscription } from 'rxjs' ;
44import { ActivatedRoute } from '@angular/router' ;
@@ -12,20 +12,22 @@ import { Application } from '@app/models/application';
1212import { IotDevice } from '@my-applications/iot-devices/iot-device.model' ;
1313import { PayloadDecoderService } from '@shared/services/payload-decoder.service' ;
1414import { 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' ;
1616import { faTimesCircle } from '@fortawesome/free-solid-svg-icons' ;
1717import { 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 ,
0 commit comments