import axios , { AxiosRequestConfig } from 'axios' ; import * as vscode from 'vscode' ; import { OH_CONFIG_DEPRECATED , OH_CONFIG_PARAMETERS , OH_MESSAGESTRINGS } from './types' ; import * as utils from './Utils' ; author Jerome Luckenbach - Initial contribution
 *
  export class ConfigManager { private static instance : ConfigManager | undefined ; private currentConfig : vscode . WorkspaceConfiguration ; private static ENCODING_MATCH : RegExp = /^(username|password)$/ ; private deprecationWarningShown : boolean ; private static DEPRECATION_WARNING_MESSAGE = `You are using deprecated config values for the openHAB Extension!\n
Those values are still used for the moment, but will be removed in newer extension versions.\n
Please take a look at the current extension settings\nand update to the new config parameters and also remove the deprecated ones.` ; /**
     * Initialize the ConfigManager
     */ private constructor ( ) { this . deprecationWarningShown = false ; this . updateConfig ( ) ; let deprecatedParameters = this . hasDeprecatedParameters ( ) ; if ( deprecatedParameters ) { if ( Array . isArray ( deprecatedParameters ) ) { deprecatedParameters . forEach ( ( parameter ) => { utils . appendToOutput ( `Usage of deprecated config => openhab.${ parameter } <= detected.` ) ; } ) ; } this . showDeprecationWarning ( ) ; } } returns { ConfigManager } private static getInstance ( ) { if ( ! ConfigManager . instance ) { ConfigManager . instance = new ConfigManager ( ) ; } return ConfigManager . instance ; } /**
     * Fetch and store the latest WorkspaceConfiguration
     */ private updateConfig ( ) { this . currentConfig = vscode . workspace . getConfiguration ( 'openhab' ) ; console . log ( "Update Config Manager" ) ; } param configParameter returns public static get ( configParameter ) { let config = ConfigManager . getInstance ( ) . currentConfig ; if ( config . has ( configParameter ) && config . get ( configParameter ) !== null ) { if ( configParameter == OH_CONFIG_PARAMETERS . connection . authToken && ! ConfigManager . tokenAuthAvailable ( ) ) return null ; return config . get ( configParameter ) ; } let parameterObject = configParameter . split ( '.' ) ; let parameter = parameterObject [ parameterObject . length - 1 ] ; let returnValue = null ; switch ( parameterObject [ 0 ] ) { case 'connection' : returnValue = this . checkAndGet ( config , parameter ) ; break ; case 'consoleCommand' : returnValue = this . checkAndGet ( config , OH_CONFIG_DEPRECATED . karafCommand ) ; break ; case 'languageserver' : switch ( parameter ) { case 'remoteEnabled' : returnValue = this . checkAndGet ( config , OH_CONFIG_DEPRECATED . remoteLspEnabled ) ; break ; case 'remotePort' : returnValue = this . checkAndGet ( config , OH_CONFIG_DEPRECATED . remoteLspPort ) ; break ; } break ; } if ( returnValue !== null ) { utils . appendToOutput ( `Usage of deprecated config => openhab.${ parameter } <= detected.` ) ; } return returnValue ; } returns { boolean } public static tokenAuthAvailable ( ) { let tokenResult = ConfigManager . getInstance ( ) . currentConfig . get ( OH_CONFIG_PARAMETERS . connection . authToken , null ) ; return ( ! tokenResult || tokenResult === '' ) ? false : true ; } param config param parameter returns private static checkAndGet ( config , parameter ) { if ( config . has ( parameter ) && config . get ( parameter ) !== null ) { if ( parameter . match ( ConfigManager . ENCODING_MATCH ) ) return encodeURIComponent ( config . get ( parameter ) ) ; return config . get ( parameter ) ; } return null ; } /**
     * Updates an openHAB specific config parameter.
     */ public static update ( configParameter , value , target ? ) { let config = vscode . workspace . getConfiguration ( 'openhab' ) ; if ( config . has ( configParameter ) ) { if ( target == undefined ) { config . update ( configParameter , value ) ; } else { config . update ( configParameter , value , target ) ; } ConfigManager . getInstance ( ) . updateConfig ( ) ; } } param context public static attachConfigChangeWatcher ( context ) { let instance = ConfigManager . getInstance ( ) ; context . subscriptions . push ( vscode . workspace . onDidChangeConfiguration ( e => { if ( e . affectsConfiguration ( 'openhab' ) ) { instance . updateConfig ( ) ; } if ( e . affectsConfiguration ( 'openhab.connection.authToken' ) ) { console . debug ( `Auth token config has been changed. Validating token now.` ) ; const token = instance . currentConfig . get ( OH_CONFIG_PARAMETERS . connection . authToken , null ) ; let config = { url : utils . getHost ( ) + '/rest/auth/apitokens' , headers : { 'X-OPENHAB-TOKEN' : `${ token }` } } ; axios ( config ) . then ( ( _response ) => { utils . appendToOutput ( `Newly configured auth token validated successfully!` ) ; } ) . catch ( ( error ) => { if ( error . response . status === 401 ) { console . error ( `Could not validate configured auth token.` , error ) ; utils . appendToOutput ( `Could not validate configured auth token.` ) ; ConfigManager . getInstance ( ) . handleConfigError ( error , `Could not validate configured auth token.` ) ; } else { utils . handleRequestError ( error ) ; } } ) ; } if ( e . affectsConfiguration ( 'openhab.connection' ) ) { console . debug ( "openHAB Extension configuration has changed." ) ; vscode . commands . executeCommand ( 'openhab.command.refreshEntry' ) ; } } ) ) ; } param err param message param baseMessage private async handleConfigError ( err , message = OH_MESSAGESTRINGS . moreInfo , baseMessage = OH_MESSAGESTRINGS . errors . configValidation ) { const showOutput = 'Show Output' ; const result = await vscode . window . showErrorMessage ( `${ baseMessage }\n\n${ message }` , showOutput ) ; if ( result == showOutput ) utils . getOutputChannel ( ) . show ( ) ; } returns private hasDeprecatedParameters ( ) { let currentConfig = this . currentConfig ; let deprecatedParameters = [ ] ; for ( const parameter in OH_CONFIG_DEPRECATED ) { if ( currentConfig . has ( parameter ) ) { let inspectresult = currentConfig . inspect ( parameter ) ; if ( inspectresult . globalValue != undefined || inspectresult . workspaceValue != undefined ) { console . log ( `openHAB Extension: Parameter ${ parameter } has been recognised as deprecated.\nComplete Parameter Data:\n` ) ; console . log ( inspectresult ) ; deprecatedParameters . push ( parameter ) ; } } } return ( deprecatedParameters . length != 0 ) ? deprecatedParameters : false ; } /**
     * Show a warning message, when deprecated config values are used
     */ private async showDeprecationWarning ( ) { if ( ! this . deprecationWarningShown ) { this . deprecationWarningShown = true ; const showOutput = 'Show Output' ; let result = await vscode . window . showWarningMessage ( ConfigManager . DEPRECATION_WARNING_MESSAGE , showOutput ) ; if ( result == showOutput ) utils . getOutputChannel ( ) . show ( ) ; } } }