Skip to content

Commit

Permalink
[WIP] REST Configuration fix (#68)
Browse files Browse the repository at this point in the history
* REST Configuration fix

Tweaks making remote access (with Basic Auth) possible

Signed-off-by: Kuba Wolanin <hi@kubawolanin.com>

* Fixed docs URL

Signed-off-by: Kuba Wolanin <hi@kubawolanin.com>
  • Loading branch information
kubawolanin authored Dec 21, 2017
1 parent 0922b70 commit 1b02f71
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 51 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ If you don't want to have your openHAB files validated by Language Server, simpl
}
```

## Accessing remote openHAB

Accessing remote openHAB from service like myopenhab is not possible, given that you need an access to the file system in order to read and write to the configuration files.
Although, you can access the Items and Things through the REST API.
Note that LSP (content assist for rules and syntax validation) won't be exposed, so you'll need to disable it too.

The following configuration will allow you to access REST API remotely:

```
"openhab.host": "https://home.myopenhab.org",
"openhab.port": 80,
"openhab.lspEnabled": false,
"openhab.username": "your_myopenhab_email",
"openhab.password": "your_myopenhab_password",
```

## Sitemap preview with Basic UI

openHAB VS Code Extension allows you to preview the [sitemap structure](http://docs.openhab.org/configuration/sitemaps.html) in the [Basic UI](http://docs.openhab.org/addons/uis/basic/readme.html) running on your openHAB server instance.
Expand Down
5 changes: 3 additions & 2 deletions src/ItemsExplorer/ItemsCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

import { Item } from './Item'
import { ItemsModel } from './ItemsModel'
import { getHost } from './../Utils'
import * as _ from 'lodash'

/**
Expand All @@ -21,9 +22,9 @@ import * as _ from 'lodash'
*/
export class ItemsCompletion implements CompletionItemProvider {

constructor(private openhabHost: string) {
constructor() {
if (!this.model) {
this.model = new ItemsModel(this.openhabHost)
this.model = new ItemsModel(getHost())
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/ItemsExplorer/ItemsExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

import { Item } from './Item'
import { ItemsModel } from './ItemsModel'
import { getHost } from './../Utils'
import * as path from 'path'

/**
Expand All @@ -21,9 +22,12 @@ export class ItemsExplorer implements TreeDataProvider<Item> {
private _onDidChangeTreeData: EventEmitter<any> = new EventEmitter<any>()
readonly onDidChangeTreeData: Event<any> = this._onDidChangeTreeData.event

constructor(private openhabHost: string) {
constructor() {
this.openhabHost = getHost()
}

private openhabHost: string

private model: ItemsModel

refresh(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/ItemsExplorer/ItemsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ItemsModel {
* @param item openHAB root Item
*/
public getChildren(item: Item): Thenable<Item[]> {
return this.sendRequest(item.link, (item: Item) => {
return this.sendRequest(this.host + '/rest/items/' + item.name, (item: Item) => {
let itemsMap = item.members.map(item => new Item(item))
return itemsMap
})
Expand Down
6 changes: 5 additions & 1 deletion src/ThingsExplorer/ThingsExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as _ from 'lodash'
import { Channel } from './Channel'
import { Thing } from './Thing'
import { ThingsModel } from './ThingsModel'
import { getHost } from './../Utils'

/**
* Produces a tree view of openHAB things
Expand All @@ -23,9 +24,12 @@ export class ThingsExplorer implements TreeDataProvider<Thing|Channel> {
private _onDidChangeTreeData: EventEmitter<any> = new EventEmitter<any>()
readonly onDidChangeTreeData: Event<any> = this._onDidChangeTreeData.event

constructor(private openhabHost: string) {
constructor() {
this.openhabHost = getHost()
}

private openhabHost: string

private model: ThingsModel

refresh(): void {
Expand Down
5 changes: 3 additions & 2 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function getHost() {
let authentication = (username || '') + (password ? ':' + password : '')
authentication += authentication ? '@' : ''

return protocol + '://' + authentication + host + ':' + port
return protocol + '://' + authentication + host + (port === 80 ? '' : ':' + port)
}

export function getBuildVersion(): Thenable<string> {
Expand Down Expand Up @@ -102,7 +102,8 @@ export async function handleRequestError(err) {
let config = workspace.getConfiguration('openhab')
const setHost = 'Set openHAB host'
const disableRest = 'Disable REST API'
const result = await window.showErrorMessage('Error while connecting to openHAB REST API. ', setHost, disableRest)
const message = typeof err.error === 'string' ? err.error : err.error.message
const result = await window.showErrorMessage(`Error while connecting to openHAB REST API. ${message || ''}`, setHost, disableRest)
switch (result) {
case setHost:
commands.executeCommand('workbench.action.openWorkspaceSettings')
Expand Down
85 changes: 41 additions & 44 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import {
} from './ContentProvider/openHAB'

import {
getHost,
hasExtension,
getBuildVersion,
openBrowser,
openHtml,
openUI
Expand All @@ -42,13 +39,9 @@ import * as _ from 'lodash'
import * as ncp from 'copy-paste'
import * as path from 'path'

async function init(context: ExtensionContext, disposables: Disposable[]): Promise<void> {
let ui = new OpenHABContentProvider()
let registration = workspace.registerTextDocumentContentProvider(SCHEME, ui)
let config = workspace.getConfiguration('openhab')
const itemsExplorer = new ItemsExplorer(getHost())
const thingsExplorer = new ThingsExplorer(getHost())
const itemsCompletion = new ItemsCompletion(getHost())
async function init(context: ExtensionContext, disposables: Disposable[], config): Promise<void> {
const ui = new OpenHABContentProvider()
const registration = workspace.registerTextDocumentContentProvider(SCHEME, ui)

disposables.push(commands.registerCommand('openhab.basicUI', () => {
let editor = window.activeTextEditor
Expand Down Expand Up @@ -98,7 +91,7 @@ async function init(context: ExtensionContext, disposables: Disposable[]): Promi
let route = `/${paperPath}/index.html%23/configuration/`

if (query.UID) {
title = `${query.label} - Paper UI`
title = `${query.label} - Paper UI`
route += `things/view/${query.UID}`
} else {
route += `item/edit/${param}`
Expand All @@ -111,57 +104,61 @@ async function init(context: ExtensionContext, disposables: Disposable[]): Promi
return config.paperInBrowser ? openBrowser(route.replace(/%23/g, '#')) : openUI(options, title)
}))

disposables.push(commands.registerCommand('openhab.command.refreshEntry', (query) => {
itemsExplorer.refresh()
thingsExplorer.refresh()
}))
disposables.push(commands.registerCommand('openhab.command.things.docs', (query: Thing) =>
openBrowser(`https://docs.openhab.org/addons/bindings/${query.binding}/readme.html`)))

disposables.push(commands.registerCommand('openhab.command.copyName', (query) =>
ncp.copy(query.name || query.label)))
if (config.useRestApi) {
const itemsExplorer = new ItemsExplorer()
const thingsExplorer = new ThingsExplorer()
const itemsCompletion = new ItemsCompletion()

disposables.push(commands.registerCommand('openhab.command.items.copyState', (query: Item) =>
ncp.copy(query.state)))
disposables.push(window.registerTreeDataProvider('openhabItems', itemsExplorer))
disposables.push(window.registerTreeDataProvider('openhabThings', thingsExplorer))
disposables.push(commands.registerCommand('openhab.command.refreshEntry', (query) => {
itemsExplorer.refresh()
thingsExplorer.refresh()
}))

disposables.push(commands.registerCommand('openhab.command.items.addRule', (query: Item) => {
let ruleProvider = new RuleProvider(query)
ruleProvider.addRule()
}))
disposables.push(commands.registerCommand('openhab.command.copyName', (query) =>
ncp.copy(query.name || query.label)))

disposables.push(commands.registerCommand('openhab.command.items.addToSitemap', (query: Item) => {
let sitemapProvider = new SitemapPartialProvider(query)
sitemapProvider.addToSitemap()
}))
disposables.push(commands.registerCommand('openhab.command.items.copyState', (query: Item) =>
ncp.copy(query.state)))

disposables.push(commands.registerCommand('openhab.command.things.docs', (query: Thing) =>
openBrowser(`http://docs.openhab.org/addons/bindings/${query.binding}/readme.html`)))
disposables.push(commands.registerCommand('openhab.command.items.addRule', (query: Item) => {
const ruleProvider = new RuleProvider(query)
ruleProvider.addRule()
}))

disposables.push(commands.registerCommand('openhab.command.things.addItems', (query: Thing | Channel) => {
let itemsProvider = new ItemsProvider(query)
itemsProvider.addToItems()
}))
disposables.push(commands.registerCommand('openhab.command.items.addToSitemap', (query: Item) => {
const sitemapProvider = new SitemapPartialProvider(query)
sitemapProvider.addToSitemap()
}))

disposables.push(commands.registerCommand('openhab.command.things.copyUID', (query) =>
ncp.copy(query.UID || query.uid)))
disposables.push(commands.registerCommand('openhab.command.things.addItems', (query: Thing | Channel) => {
const itemsProvider = new ItemsProvider(query)
itemsProvider.addToItems()
}))

if (config.useRestApi) {
disposables.push(window.registerTreeDataProvider('openhabItems', itemsExplorer))
disposables.push(window.registerTreeDataProvider('openhabThings', thingsExplorer))
disposables.push(commands.registerCommand('openhab.command.things.copyUID', (query) =>
ncp.copy(query.UID || query.uid)))

if (config.restCompletions) {
disposables.push(languages.registerCompletionItemProvider('openhab', itemsCompletion))
}
}

if (config.lspEnabled) {
let languageClientProvider = new LanguageClientProvider()
const languageClientProvider = new LanguageClientProvider()
disposables.push(languageClientProvider.connect())
}

if (config.useRestApi && config.restCompletions) {
disposables.push(languages.registerCompletionItemProvider('openhab', itemsCompletion))
}
}
export function activate(context: ExtensionContext) {
const disposables: Disposable[] = [];
let config = workspace.getConfiguration('openhab')
context.subscriptions.push(new Disposable(() => Disposable.from(...disposables).dispose()))

init(context, disposables)
init(context, disposables, config)
.catch(err => console.error(err));
}

Expand Down

0 comments on commit 1b02f71

Please sign in to comment.