VS Code openHAB Extension!

@kubawolanin just set up SSH to connect to openHAB with private key and use the tail for the log. i also set up sftp to automattically transfer files. I will post my solution here maybe you think it could be helpfull to be added to the github readme. Or have some ideas to improve the Setting up proccess to make it easier?

  1. Setting up logging without exposing system to the public, and keeps working after upgrade. One time setup. openhab-cli command had some Problems but a good solution could be found.

1.1 PC used to edeit code with VSCode, generate key, this is necessary to avoid typing in the password every time.

ssh-keygen

1.2 Copy content of public key id_XXXXX.pub to Openhab Server

// Linux
ssh-copy-id -i ~/.ssh/mykey user@host
// Windows
scp "<PathToFile>/id_XXXXX.pub" pi@<Ip-of-openHAB-Server>:~/.ssh/authorized_keys
//scp "<PathToFile>/id_XXXXX.pub" openhabian@openhabianpi:~/.ssh/authorized_keys
// manually 
// copy content of `id_XXXXX.pub` to   
// existing or created file `/.ssh/authorized_keys`

1.3 Change shh config on server

sudo nano /etc/ssh/sshd_config
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys .ssh/authorized_keys2

1.4 Set Karaf command in VSCode, press ctrl+comma and add at the left left side

// I am not a fan of this, needs extra configuration, is not persistent when updating, and opens system to public
"openhab.karafCommand": "ssh username@%openhabhost% -p 8101 -t 'log:tail'",
// Don't do this there are too much drawbags
"openhab.karafCommand": "ssh username@%openhabhost% openhab-cli console -p habopen `'log:tail'`",
// do this, one drawback it is not colored. 
"openhab.karafCommand": "ssh username@%openhabhost% tail -q -f /var/log/openhab2/events.log -f /var/log/openhab2/openhab.log",

Drawbacks solution two

  1. Do not append 'log:tail' there seems a bug which will prevent the log from beeing continously written. Do we have to type something again. No password but log:tail.
  2. A second bug seems to be ansi color codes which i did not get to work propperly yet. The escape sequence for Ansi codes get stripped of when piped to the terminal console from the ssh session. But the Code part will persist and clutter the Output.
  3. Karaf console default timeout is set to 30 minutes, so it has to be reopened often or the default settings have to be changed.
  4. It is a ssh connection in a ssh connection.

Restart VSCode, openhab extension seems to have a missing feature which reads in configuration when they are changed it only loads new/changed configs when restarted.

1.5 Try to run Karaf ctrl+shift+p type openh select openHAB: Open Karaf Console.
1.6 Wait for the connection and the tailing log.

The best thing is you have access to the console changing debug levels retrieve infos etc, imho very ussefull. You can use the terminal like an ssh session.

Additional information i use windows 10 build in openSSH.

  1. Add folder /etc/openhab2 as sftp to automatically transfer files when you save from/to Server to/from pc wich edits the files.

2.1 install SFTP extension, restart VSCode
2.2 Open a folder which is destined to be your new openhab content folder. Menu File/Open Folder
2.3 Configure it SFTP, press ctrl+shift+p type sftp choose SFTP: config
My basic setting looks as follows, have a look at the project page for more details

{
    "protocol": "sftp",
    "host": "<IP>",
    "username": "<Username e.g pi>",
    "password": "<password e.g raspberry>",
    "context": "<path to your local openHAB files e.g D:/CODE/OpenHAB_FILES>",
    "remotePath": "<path to remote openHAB files e.g /etc/openhab2/>",
    "uploadOnSave": true
}

2.3 Fetch remote files, press ctrl+shift+p type sftp choose SFTP: Download
all your remote files should be in your local folder now.
2.4 Edit a file and watch the log go crazy! :wink:

@kubawolanin edited the logging now it works very nice. This is now a very neat working environment, do you think this could be part of the readme?

2 Likes