Skip to content

Commit

Permalink
better connection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
schmic committed Dec 11, 2023
1 parent 218dd47 commit 1267e6e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
8 changes: 4 additions & 4 deletions HaPlugin/HaConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

public class HaConfig
{
public String Url { private get; set; }
public String Url { get; set; }

public Uri Uri
public String ApiUrl
{
get
{
var builder = new UriBuilder(this.Url.Replace("http", "ws"))
{
{
Path = "/api/websocket",
};
return builder.Uri;
return builder.Uri.ToString();
}
}

Expand Down
34 changes: 28 additions & 6 deletions HaPlugin/HaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Loupedeck.HomeAssistant
{
using System;
using System.Collections.Generic;
using System.Security.Authentication;

using Loupedeck.HomeAssistant.Events;
using Loupedeck.HomeAssistant.Json;
Expand Down Expand Up @@ -36,9 +37,19 @@ public override void Load()
this.OnPluginStatusChanged(Loupedeck.PluginStatus.Error, "Configuration could not be read.", "https://github.com/schmic/Loupedeck-HomeAssistant", "Help");
return;
}
else if (Config.Token.IsNullOrEmpty())
{
this.OnPluginStatusChanged(Loupedeck.PluginStatus.Error, "Configuration is missing token.", "https://github.com/schmic/Loupedeck-HomeAssistant", "Help");
return;
}
else if (Config.Url.IsNullOrEmpty())
{
this.OnPluginStatusChanged(Loupedeck.PluginStatus.Error, "Configuration is missing url.", "https://github.com/schmic/Loupedeck-HomeAssistant", "Help");
return;
}

this.Token = Config.Token;
this.SetupWebsocket(Config.Uri.ToString());
this.SetupWebsocket(Config.ApiUrl);
}

public override void Unload() => this.WebSocket.Close();
Expand All @@ -57,16 +68,27 @@ private enum Events : Int32

private void SetupWebsocket(String url)
{
PluginLog.Verbose($"SetupWebsocket(): [url: {url}]");
PluginLog.Verbose($"SetupWebsocket(): [url: {url}] [token: {this.Token.Substring(0, 16)}...]");
this.WebSocket = new WebSocket(url);
this.WebSocket.Log.Level = LogLevel.Info;
this.WebSocket.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
this.WebSocket.SslConfiguration.ServerCertificateValidationCallback = null;
this.WebSocket.Log.Level = LogLevel.Trace;

if (url.StartsWith("wss://"))
{
this.WebSocket.SslConfiguration.EnabledSslProtocols = SslProtocols.None;
this.WebSocket.SslConfiguration.ServerCertificateValidationCallback = null;
}

this.WebSocket.OnError += this.OnErrorHdl;
this.WebSocket.OnMessage += this.OnMessageHdl;

this.WebSocket.Connect();
try
{
this.WebSocket.Connect();
}
catch (Exception ex)
{
PluginLog.Error($"### Error: {ex.Message}\n{ex.InnerException}");
}
}

private void OnErrorHdl(Object sender, ErrorEventArgs e) => PluginLog.Error($"### Error: {e.Message}\n{e.Exception}");
Expand Down
2 changes: 1 addition & 1 deletion package.ps1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
LoupedeckPluginTool.exe pack -input=".\HaPlugin\bin\" -output="..\HomeAssistant-0.3.lplug4"
LoupedeckPluginTool.exe pack -input=".\HaPlugin\bin\" -output="..\HomeAssistant-0.4.lplug4"

0 comments on commit 1267e6e

Please sign in to comment.