import { DataPoint , DataSource , DataSourceConfiguration , Mapping } from "../../.." ; import { throwError , toQueryString } from "../../utils" ; import { AssetManagementModels } from "../asset/asset-models" ; import { SdkClient } from "../common/sdk-client" ; import { MindConnectApiModels } from "./mcapi-models" ; export class extends SdkClient export class MindConnectApiClient extends SdkClient { private _baseUrl : string = "/api/mindconnect/v3" ; param { string } agentId returns { Promise < MindConnectApiModels . DiagnosticActivation > } memberOf public async PostDiagnosticActivation ( agentId ) { const body = { agentId : agentId } ; return ( await this . HttpAction ( { verb : "POST" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/diagnosticActivations` , body : body , message : "PostDiagnosticActivation" , } ) ) as MindConnectApiModels . DiagnosticActivation ; } param { { size ? : number ; page ? : number ; sort ? : string ; * } } optional returns { Promise < MindConnectApiModels . PagedDiagnosticActivation > } memberOf public async GetDiagnosticActivations ( optional ? ) { const qs = toQueryString ( optional ) ; return ( await this . HttpAction ( { verb : "GET" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/diagnosticActivations?${ qs }` , message : "GetDiagnosticActivations" , } ) ) as MindConnectApiModels . PagedDiagnosticActivation ; } param { string } id param { MindConnectApiModels . DiagnosticActivationStatus } diagnosticStatus returns { Promise < MindConnectApiModels . DiagnosticActivation > } memberOf public async PutDiagnosticActivation ( id , diagnosticStatus ) { const body = diagnosticStatus ; return ( await this . HttpAction ( { verb : "PUT" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/diagnosticActivations/${ id }` , body : body , message : "PutDiagnosticActivation" , } ) ) as MindConnectApiModels . DiagnosticActivation ; } param { string } id returns { Promise < MindConnectApiModels . DiagnosticActivation > } memberOf public async GetDiagnosticActivation ( id ) { return ( await this . HttpAction ( { verb : "GET" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/diagnosticActivations/${ id }` , message : "GetDiagnosticActivation" , } ) ) as MindConnectApiModels . DiagnosticActivation ; } param { string } id returns memberOf public async DeleteDiagnosticActivation ( id ) { return await this . HttpAction ( { verb : "DELETE" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/diagnosticActivations/${ id }` , message : "DeleteDiagnosticActivation" , noResponse : true , } ) ; } param { string } id param { { filter ? : string ; size ? : number ; page ? : number ; sort ? : string } } optional returns { Promise < MindConnectApiModels . PagedDiagnosticInformationMessages > } memberOf public async GetDiagnosticActivationMessages ( id , optional ? ) { const qs = toQueryString ( optional ) ; return ( await this . HttpAction ( { verb : "GET" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/diagnosticActivations/${ id }/messages?${ qs }` , message : "GetDiagnosticActivationMessages" , } ) ) as MindConnectApiModels . PagedDiagnosticInformationMessages ; } deprecated param { { filter ? : string ; size ? : number ; page ? : number ; sort ? : string ; * } } optional returns { Promise < MindConnectApiModels . PagedDiagnosticInformationMessages > } memberOf public async GetDiagnosticInformation ( optional ? ) { const qs = toQueryString ( optional ) ; return ( await this . HttpAction ( { verb : "GET" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/diagnosticInformation?${ qs }` , message : "GetDiagnosticInformation" , } ) ) as MindConnectApiModels . PagedDiagnosticInformationMessages ; } memberOf public async DeleteAllDiagnosticActivations ( ) { const activations = await this . GetDiagnosticActivations ( ) ; for ( const activation of activations . content ) { if ( activation . id ) await this . DeleteDiagnosticActivation ( activation . id ) ; } } param { string } agentId param { ( x : MindConnectApiModels . DiagnosticInformation [ ] , ... args : any [ ] ) => any } callback param { * } callbackOptions param { boolean } skipToLast returns memberOf public async GetAllDiagnosticInformation ( agentId , callback ? , callbackOptions ? , skipToLast = true ) { let pagedInformation ; let page = 0 ; do { pagedInformation = await this . GetDiagnosticActivationMessages ( agentId , { page : page ++ } ) ; if ( skipToLast ) { page = pagedInformation . totalPages - 2 ; if ( page < 0 ) page = 0 ; skipToLast = false ; if ( ! pagedInformation . last ) { continue ; } } if ( callback ) { callback ( pagedInformation . content , callbackOptions ) ; } } while ( ! pagedInformation . last ) ; return pagedInformation ; } param { MindConnectApiModels . Mapping } mapping returns memberOf public async PostDataPointMapping ( mapping , optional ? ) { const body = mapping ; const parameters = optional || { } ; const { noAgentValidation } = parameters ; return await this . HttpAction ( { verb : "POST" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/dataPointMappings?${ toQueryString ( { noAgentValidation } ) }` , body : body , message : "PostDataPointMapping" , noResponse : true , ignoreCodes : optional ?. ignoreCodes , } ) ; } param { { filter ? : string ; size ? : number ; page ? : number ; sort ? : string ; * } } optional returns { Promise < MindConnectApiModels . PagedMapping > } memberOf public async GetDataPointMappings ( optional ? ) { const qs = toQueryString ( optional ) ; return ( await this . HttpAction ( { verb : "GET" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/dataPointMappings?${ qs }` , message : "GetDataPointMappings" , } ) ) as MindConnectApiModels . PagedMapping ; } param { string } id returns { Promise < MindConnectApiModels . Mapping > } memberOf public async GetDataPointMapping ( id ) { return ( await this . HttpAction ( { verb : "GET" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/dataPointMappings/${ id }` , message : "GetDataPointMapping" , } ) ) as MindConnectApiModels . Mapping ; } param { string } id returns memberOf public async DeleteDataMapping ( id , optional ? ) { return await this . HttpAction ( { verb : "DELETE" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , baseUrl : `${ this . _baseUrl }/dataPointMappings/${ id }` , message : "DeleteDataMapping" , noResponse : true , ignoreCodes : optional ?. ignoreCodes , } ) ; } param { AssetManagementModels . AssetTypeResource } assetType param { ( "NUMERICAL" | "DESCRIPTIVE" ) } mode returns { Promise < DataSourceConfiguration > } memberOf public GenerateDataSourceConfiguration ( assetType , mode = "DESCRIPTIVE" ) { let dataSourceConfiguration = { configurationId : mode === "NUMERICAL" ? "CF0001" : `CF-${ assetType ! . id ! . toString ( ) . substr ( 0 , 30 ) }` , dataSources : [ ] , } ; const dynamicAspects = assetType . aspects ?. filter ( ( x ) => x . aspectType ! . category === AssetManagementModels . AspectType . CategoryEnum . Dynamic ) || [ ] ; let ds = 0 , dp = 0 ; dynamicAspects ! . forEach ( ( aspect ) => { const aspectType = ( aspect . aspectType as unknown ) as AssetManagementModels . AspectTypeResource ; const dataSource = { name : mode === "NUMERICAL" ? `DS${ ( ++ ds ) . toString ( ) . padStart ( 5 , "0" ) }` : `DS-${ aspect . name ! . substr ( 0 , 61 ) }` , dataPoints : [ ] , customData : { aspect : aspect . name ! , } , } ; aspectType . variables . forEach ( ( variable ) => { dataSource . dataPoints . push ( { id : mode === "NUMERICAL" ? `DP${ ( ++ dp ) . toString ( ) . padStart ( 5 , "0" ) }` : `DP-${ variable ! . name ! . substr ( 0 , 33 ) }` , name : variable ! . name ! . substr ( 0 , 64 ) , type : ( variable . dataType as unknown ) as DataPoint . TypeEnum , unit : variable . unit , customData : { variable : `${ variable . name }` , } , } ) ; } ) ; dataSourceConfiguration . dataSources . push ( dataSource ) ; } ) ; dataSourceConfiguration = mode === "NUMERICAL" ? dataSourceConfiguration : this . CreateUniqueDataPoints ( dataSourceConfiguration ) ; return dataSourceConfiguration ; } private CreateUniqueDataPoints ( configuration ) { const datasources = configuration . dataSources . map ( ( x ) => x . name ) ; const duplicates = this . getDuplicates ( datasources ) ; duplicates . forEach ( ( duplicate ) => { let ds = 0 ; configuration . dataSources . filter ( ( x ) => x . name === duplicate ) . forEach ( ( d ) => { d . name = `${ d . name . substr ( 0 , 58 ) }${ ( ++ ds ) . toString ( ) . padStart ( 5 , "0" ) }` ; } ) ; } ) ; const dataPointIds = [ ] ; configuration . dataSources . forEach ( ( ds ) => { const ids = ds . dataPoints . map ( ( x ) => x . id ) ; dataPointIds . push ( ... ids ) ; } ) ; const dataPointDuplicates = this . getDuplicates ( dataPointIds ) ; dataPointDuplicates . forEach ( ( duplicate ) => { let dp = 0 ; configuration . dataSources . forEach ( ( ds ) => { const dataPoints = ds . dataPoints . filter ( ( x ) => x . id === duplicate ) ; dataPoints . forEach ( ( p ) => { p . id = `${ p . id . substr ( 0 , 28 ) }${ ( ++ dp ) . toString ( ) . padStart ( 5 , "0" ) }` ; } ) ; } ) ; } ) ; return configuration ; } getDuplicates ( x ) { return [ ... new Set ( x . filter ( ( e , i , a ) => a . indexOf ( e ) !== i ) ) ] ; } param { DataSourceConfiguration } dataSourceConfiguration param { string } agentId param { string } targetAssetId returns { Mapping [ ] } memberOf public GenerateMappings ( dataSourceConfiguration , agentId , targetAssetId ) { const mappings = [ ] ; ! dataSourceConfiguration && throwError ( "no data source configuration! (have you forgotten to create / generate the data source configuration first?" ) ; for ( const dataSource of dataSourceConfiguration ! . dataSources ) { ( ! dataSource . customData || ! dataSource . customData ! . aspect ! ) && throwError ( "GenerateMappings works only on configurations created with GenerateDataSourceConfiguration method!" ) ; for ( const datapoint of dataSource . dataPoints ) { ( ! datapoint . customData || ! datapoint . customData ! . variable ) && throwError ( "GenerateMappings works only on configurations created with GenerateDataSourceConfiguration method!" ) ; mappings . push ( { agentId : agentId , dataPointId : datapoint . id , entityId : targetAssetId , propertyName : datapoint . customData ! . variable , propertySetName : dataSource . customData ! . aspect , keepMapping : true , } ) ; } } return mappings ; } param { string } senderId param { MindConnectApiModels . IngestionTimeseries } message returns { Promise < Response > } memberOf public async Ingest ( senderId , message ) { return ( await ( this . HttpAction ( { verb : "POST" , gateway : this . GetGateway ( ) , authorization : await this . GetToken ( ) , body : message , baseUrl : `/api/mindconnect/v3/ingest?${ toQueryString ( { senderId : senderId } ) }` , rawResponse : true , } ) as unknown ) ) as Response ; } }