@ -15,10 +15,21 @@
///
import { Component , Input , OnInit } from '@angular/core' ;
import { forkJoin } from 'rxjs' ;
import { PageComponent } from '@shared/components/page.component' ;
import { Authority } from '@shared/models/authority.enum' ;
import { getCurrentAuthUser } from '@core/auth/auth.selectors' ;
import { WidgetContext } from '@home/models/widget-component.models' ;
import { IotHubApiService } from '@core/http/iot-hub-api.service' ;
import { IotHubActionsService } from '@home/components/iot-hub/iot-hub-actions.service' ;
import { resolveIotHubItemImageUrl } from '@home/components/iot-hub/iot-hub-utils' ;
import { MpItemVersionQuery , MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models' ;
import { ItemType } from '@shared/models/iot-hub/iot-hub-item.models' ;
import { IotHubInstalledItem } from '@shared/models/iot-hub/iot-hub-installed-item.models' ;
import { PageLink } from '@shared/models/page/page-link' ;
import { Direction , SortOrder } from '@shared/models/page/sort-order' ;
const WIDGET_CARD_COUNT = 3 ;
@Component ( {
selector : 'tb-iot-hub-widget' ,
@ -37,7 +48,13 @@ export class IotHubWidgetComponent extends PageComponent implements OnInit {
hasIotHubAccess = true ;
constructor ( ) {
solutionTemplates : MpItemVersionView [ ] = [ ] ;
devices : MpItemVersionView [ ] = [ ] ;
installedSolutionTemplates : IotHubInstalledItem [ ] = [ ] ;
installedDeviceCounts : Record < string , number > = { } ;
constructor ( private iotHubApiService : IotHubApiService ,
private iotHubActions : IotHubActionsService ) {
super ( ) ;
}
@ -49,9 +66,66 @@ export class IotHubWidgetComponent extends PageComponent implements OnInit {
}
}
private load() {
openItemDetail ( item : MpItemVersionView ) : void {
this . iotHubActions . openItemDetail ( item , this . findInstalledSolutionTemplate ( item ) , this . findInstalledDeviceCount ( item ) )
. subscribe ( result = > {
if ( result === 'installed' || result === 'deleted' || result === 'updated' ) {
this . reloadInstalled ( item . type ) ;
}
} ) ;
}
getItemImage ( item : MpItemVersionView ) : string | null {
return resolveIotHubItemImageUrl ( item , this . iotHubApiService ) ;
}
}
isInstalled ( item : MpItemVersionView ) : boolean {
return item . type === ItemType . SOLUTION_TEMPLATE && ! ! this . findInstalledSolutionTemplate ( item ) ;
}
private findInstalledSolutionTemplate ( item : MpItemVersionView ) : IotHubInstalledItem | undefined {
return this . installedSolutionTemplates . find ( i = > i . itemId === item . itemId ) ;
}
private findInstalledDeviceCount ( item : MpItemVersionView ) : number {
return this . installedDeviceCounts [ item . itemId ] || 0 ;
}
private load ( ) : void {
const config = { ignoreLoading : true } ;
const sortOrder : SortOrder = { property : 'totalInstallCount' , direction : Direction.DESC } ;
const buildQuery = ( type : ItemType ) : MpItemVersionQuery = >
new MpItemVersionQuery ( new PageLink ( WIDGET_CARD_COUNT , 0 , null , sortOrder ) , { type } ) ;
const installedPageLink = new PageLink ( 10000 , 0 ) ;
forkJoin ( {
solutionTemplates : this.iotHubApiService.getPublishedVersions ( buildQuery ( ItemType . SOLUTION_TEMPLATE ) , config ) ,
devices : this.iotHubApiService.getPublishedVersions ( buildQuery ( ItemType . DEVICE ) , config ) ,
installedSolutionTemplates : this.iotHubApiService.getInstalledItems ( installedPageLink , ItemType . SOLUTION_TEMPLATE , undefined , config ) ,
installedDeviceCounts : this.iotHubApiService.getInstalledItemCounts ( ItemType . DEVICE , config )
} ) . subscribe ( result = > {
this . solutionTemplates = result . solutionTemplates . data ;
this . devices = result . devices . data ;
this . installedSolutionTemplates = result . installedSolutionTemplates . data ;
this . installedDeviceCounts = result . installedDeviceCounts ;
this . ctx . detectChanges ( ) ;
} ) ;
}
private reloadInstalled ( type : ItemType ) : void {
const config = { ignoreLoading : true } ;
if ( type === ItemType . SOLUTION_TEMPLATE ) {
const installedPageLink = new PageLink ( 10000 , 0 ) ;
this . iotHubApiService . getInstalledItems ( installedPageLink , ItemType . SOLUTION_TEMPLATE , undefined , config ) . subscribe ( data = > {
this . installedSolutionTemplates = data . data ;
this . ctx . detectChanges ( ) ;
} ) ;
} else if ( type === ItemType . DEVICE ) {
this . iotHubApiService . getInstalledItemCounts ( ItemType . DEVICE , config ) . subscribe ( counts = > {
this . installedDeviceCounts = counts ;
this . ctx . detectChanges ( ) ;
} ) ;
}
}
}