@@ -19,6 +19,7 @@ import { SharedVariableService } from "@shared/shared-variable/shared-variable.s
1919import { Observable , Subscription } from "rxjs" ;
2020import { map } from "rxjs/operators" ;
2121import { ApplicationChangeOrganizationDialogComponent } from "../application-change-organization-dialog/application-change-organization-dialog.component" ;
22+ import moment from "moment/moment" ;
2223
2324@Component ( {
2425 selector : "app-application" ,
@@ -53,12 +54,12 @@ export class ApplicationDetailComponent implements OnInit, OnDestroy, AfterViewI
5354 public canEdit = false ;
5455 public devices : IotDevicesApplicationMapResponse [ ] ;
5556 public coordinateList = [ ] ;
56- private deviceSubscription : Subscription ;
57- private gatewaysSubscription : Subscription ;
5857 public gateways : Gateway [ ] ;
5958 public redMarker = "/assets/images/red-marker.png" ;
6059 public greenMarker = "/assets/images/green-marker.png" ;
6160 public greyMarker = "/assets/images/grey-marker.png" ;
61+ private deviceSubscription : Subscription ;
62+ private gatewaysSubscription : Subscription ;
6263
6364 constructor (
6465 private applicationService : ApplicationService ,
@@ -125,6 +126,57 @@ export class ApplicationDetailComponent implements OnInit, OnDestroy, AfterViewI
125126 } ) ;
126127 }
127128
129+ onDeleteApplication ( ) {
130+ this . deleteDialogService . showApplicationDialog ( this . application ) . subscribe ( response => {
131+ if ( response ) {
132+ this . applicationService . deleteApplication ( this . application . id ) . subscribe ( response => {
133+ if ( response . ok && response . body . affected > 0 ) {
134+ console . log ( "delete application with id:" + this . application . id . toString ( ) ) ;
135+ this . router . navigate ( [ "applications" ] ) ;
136+ } else {
137+ this . errorMessage = response ?. error ?. message ;
138+ }
139+ } ) ;
140+ } else {
141+ console . log ( response ) ;
142+ }
143+ } ) ;
144+ }
145+
146+ onOpenChangeOrganizationDialog ( ) {
147+ this . changeOrganizationDialog . open ( ApplicationChangeOrganizationDialogComponent , {
148+ data : {
149+ applicationId : this . id ,
150+ organizationId : this . application . belongsTo . id ,
151+ } as ApplicationDialogModel ,
152+ } ) ;
153+ }
154+
155+ bindApplication ( id : number ) : void {
156+ this . applicationsSubscription = this . applicationService . getApplication ( id ) . subscribe ( application => {
157+ this . application = application ;
158+ this . cdr . detectChanges ( ) ;
159+ } ) ;
160+ }
161+
162+ getDevices ( ) : Observable < IotDevicesApplicationMapResponse [ ] > {
163+ return this . restService . get ( `application/${ this . id } /iot-devices-map` ) . pipe (
164+ map ( ( data : IotDevicesApplicationMapResponse [ ] ) => {
165+ // For some reason, the backend is not capable to sort MQTT_EXTERNAL_BROKER and MQTT_INTERNAL_BROKER.
166+ // Therefore we do it manually in the frontend.
167+ return data ;
168+ } )
169+ ) ;
170+ }
171+
172+ ngOnDestroy ( ) {
173+ this . gatewaysSubscription . unsubscribe ( ) ;
174+ this . deviceSubscription ?. unsubscribe ( ) ;
175+ if ( this . applicationsSubscription ) {
176+ this . applicationsSubscription . unsubscribe ( ) ;
177+ }
178+ }
179+
128180 private getGateways ( ) : void {
129181 this . gatewaysSubscription = this . chirpstackGatewayService
130182 . getForMaps ( )
@@ -140,6 +192,10 @@ export class ApplicationDetailComponent implements OnInit, OnDestroy, AfterViewI
140192 if ( ! dev . location ) {
141193 return ;
142194 }
195+ const isActive = dev . latestSentMessage
196+ ? moment ( dev . latestSentMessage ) . unix ( ) > moment ( new Date ( ) ) . subtract ( 1 , "day" ) . unix ( )
197+ : false ;
198+
143199 tempCoordinateList . push ( {
144200 longitude : dev . location . coordinates [ 0 ] ,
145201 latitude : dev . location . coordinates [ 1 ] ,
@@ -148,9 +204,10 @@ export class ApplicationDetailComponent implements OnInit, OnDestroy, AfterViewI
148204 useGeolocation : false ,
149205 markerInfo : {
150206 name : dev . name ,
151- active : dev . type ,
207+ active : isActive ,
152208 id : dev . id ,
153209 isDevice : true ,
210+ isGateway : false ,
154211 internalOrganizationId : this . sharedVariableService . getSelectedOrganisationId ( ) ,
155212 networkTechnology : dev . type ,
156213 lastActive : dev . latestSentMessage ,
@@ -174,6 +231,8 @@ export class ApplicationDetailComponent implements OnInit, OnDestroy, AfterViewI
174231 name : gateway . name ,
175232 active : this . getGatewayStatus ( gateway ) ,
176233 id : gateway . gatewayId ,
234+ isDevice : false ,
235+ isGateway : true ,
177236 internalOrganizationId : gateway . organizationId ,
178237 internalOrganizationName : gateway . organizationName ,
179238 } ,
@@ -185,55 +244,4 @@ export class ApplicationDetailComponent implements OnInit, OnDestroy, AfterViewI
185244 private getGatewayStatus ( gateway : Gateway ) : boolean {
186245 return this . chirpstackGatewayService . isGatewayActive ( gateway ) ;
187246 }
188-
189- onDeleteApplication ( ) {
190- this . deleteDialogService . showApplicationDialog ( this . application ) . subscribe ( response => {
191- if ( response ) {
192- this . applicationService . deleteApplication ( this . application . id ) . subscribe ( response => {
193- if ( response . ok && response . body . affected > 0 ) {
194- console . log ( "delete application with id:" + this . application . id . toString ( ) ) ;
195- this . router . navigate ( [ "applications" ] ) ;
196- } else {
197- this . errorMessage = response ?. error ?. message ;
198- }
199- } ) ;
200- } else {
201- console . log ( response ) ;
202- }
203- } ) ;
204- }
205-
206- onOpenChangeOrganizationDialog ( ) {
207- this . changeOrganizationDialog . open ( ApplicationChangeOrganizationDialogComponent , {
208- data : {
209- applicationId : this . id ,
210- organizationId : this . application . belongsTo . id ,
211- } as ApplicationDialogModel ,
212- } ) ;
213- }
214-
215- bindApplication ( id : number ) : void {
216- this . applicationsSubscription = this . applicationService . getApplication ( id ) . subscribe ( application => {
217- this . application = application ;
218- this . cdr . detectChanges ( ) ;
219- } ) ;
220- }
221-
222- getDevices ( ) : Observable < IotDevicesApplicationMapResponse [ ] > {
223- return this . restService . get ( `application/${ this . id } /iot-devices-map` ) . pipe (
224- map ( ( data : IotDevicesApplicationMapResponse [ ] ) => {
225- // For some reason, the backend is not capable to sort MQTT_EXTERNAL_BROKER and MQTT_INTERNAL_BROKER.
226- // Therefore we do it manually in the frontend.
227- return data ;
228- } )
229- ) ;
230- }
231-
232- ngOnDestroy ( ) {
233- this . gatewaysSubscription . unsubscribe ( ) ;
234- this . deviceSubscription ?. unsubscribe ( ) ;
235- if ( this . applicationsSubscription ) {
236- this . applicationsSubscription . unsubscribe ( ) ;
237- }
238- }
239247}
0 commit comments