import * as vscode from 'vscode' ; import { OutputChannel } from 'vscode' ; import * as _ from 'lodash' ; import axios , { AxiosRequestConfig } from 'axios' ; import { PreviewPanel } from '../WebViews/PreviewPanel' ; import { ConfigManager } from './ConfigManager' ; import { OH_CONFIG_PARAMETERS , OH_MESSAGESTRINGS } from './types' ; /**
 * Create output channel as user display for relevant informations
 */ let extensionOutput = null ; let warningShownAlready = false ; param str export function humanize ( str ) { return _ . upperFirst ( str . trim ( ) . replace ( /([a-z\d])([A-Z]+)/g , '$1_$2' ) . replace ( /[-\s]+/g , '_' ) . toLowerCase ( ) . replace ( /([a-z\d])([A-Z]+)/g , '$1_$2' ) . replace ( /_id$/ , '' ) . replace ( /_/g , ' ' ) . replace ( /_id$/ , '' ) . replace ( /_/g , ' ' ) . trim ( ) ) ; } /**
 * Returns the host of the configured openHAB environment.
 * Return value may vary depending on the user configuration (e.g. Authentication settings)
 */ export function getHost ( ) { let host = ConfigManager . get ( OH_CONFIG_PARAMETERS . connection . host ) as string ; let port = ConfigManager . get ( OH_CONFIG_PARAMETERS . connection . port ) as number ; let protocol = 'http' ; if ( host . includes ( '://' ) ) { let split = host . split ( '://' ) ; host = split [ 1 ] ; protocol = split [ 0 ] ; } let generatedHost = protocol + '://' ; if ( ! ConfigManager . tokenAuthAvailable ( ) ) { let username = ConfigManager . get ( OH_CONFIG_PARAMETERS . connection . basicAuth . username ) as string | null ; if ( username != null && username != '' ) { let password = ConfigManager . get ( OH_CONFIG_PARAMETERS . connection . basicAuth . password ) as string | null ; let usernameSegments = username . split ( '.' ) ; if ( usernameSegments . length === 3 && usernameSegments [ 0 ] === 'oh' ) { const warningString = `Detected openHAB 3 token as username.\nConsider using the recommended **openhab.connection.authToken** config parameter instead.\n\n` ; appendToOutput ( warningString ) ; if ( ! warningShownAlready ) { vscode . window . showWarningMessage ( warningString ) ; warningShownAlready = true ; } } let basicAuth = ( username ? username : '' ) + ( password ? ':' + password : '' ) + '@' ; generatedHost += basicAuth ; } } generatedHost += host + ( port === 80 ? '' : ':' + port ) ; return generatedHost ; } /**
 * Returns all available sitemaps of the configured openHAB environment via rest api
 */ export function getSitemaps ( ) { return new Promise ( ( resolve , reject ) => { let config = { url : getHost ( ) + '/rest/sitemaps' , headers : { } } ; if ( ConfigManager . tokenAuthAvailable ( ) ) { config . headers = { 'X-OPENHAB-TOKEN' : ConfigManager . get ( OH_CONFIG_PARAMETERS . connection . authToken ) } ; } axios ( config ) . then ( ( response ) => { resolve ( response . data ) ; } ) . catch ( ( ) => reject ( [ ] ) ) ; } ) ; } param url export function openBrowser ( url ) { let editor = vscode . window . activeTextEditor ; if ( ! editor ) { vscode . window . showInformationMessage ( 'No editor is active' ) ; return ; } let selection = editor . selection ; let text = editor . document . getText ( selection ) ; url = url . startsWith ( 'http' ) ? url : getHost ( ) + url ; url = url . replace ( '%s' , text . replace ( ' ' , '%20' ) ) ; return vscode . commands . executeCommand ( 'vscode.open' , vscode . Uri . parse ( url ) ) ; } param extensionPath param query param title export function openUI ( extensionPath , query = "/basicui/app" , title ? ) { let srcPath = getHost ( ) . concat ( query ) ; appendToOutput ( `URL that will be opened is: ${ srcPath }` ) ; PreviewPanel . createOrShow ( extensionPath , ( title !== undefined ) ? title : undefined , srcPath ) ; } param err export async function handleRequestError ( err ) { const disableRest = 'Disable REST API' ; const showOutput = 'Show Output' ; const baseMessage = `Error while connecting to openHAB REST API.` ; const message = typeof err . isAxiosError === 'string' ? err . message : err . toString ( ) ; const result = await vscode . window . showErrorMessage ( `${ baseMessage }\n${ OH_MESSAGESTRINGS . moreInfo }` , disableRest , showOutput ) ; switch ( result ) { case disableRest : ConfigManager . update ( OH_CONFIG_PARAMETERS . useRestApi , false ) ; break ; case showOutput : extensionOutput . show ( ) ; break ; } appendToOutput ( `---
    Error:
        ${ baseMessage }

    Message:
        ${ message }
---` ) ; } param message export function appendToOutput ( message ) { getOutputChannel ( ) . appendLine ( message ) ; } returns export function getOutputChannel ( ) { if ( ! extensionOutput ) { extensionOutput = vscode . window . createOutputChannel ( "openHAB Extension" ) ; } return extensionOutput ; } param sleepTime export async function sleep ( sleepTime ) { return new Promise ( resolve => setTimeout ( resolve , sleepTime ) ) ; }