From f0e41e831d2f6e9a61c338e0a5e68c2e134086f9 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Fri, 1 Dec 2023 13:18:30 +0100 Subject: [PATCH 1/5] Fixes 0.0.12.1 --- HA/Class/HomeAssistant/HomeAssistantWS.cs | 75 ++++++++++++----------- HA/HA.csproj | 4 +- HA/meta.xml | 2 +- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/HA/Class/HomeAssistant/HomeAssistantWS.cs b/HA/Class/HomeAssistant/HomeAssistantWS.cs index 9fff81f..5185ee2 100644 --- a/HA/Class/HomeAssistant/HomeAssistantWS.cs +++ b/HA/Class/HomeAssistant/HomeAssistantWS.cs @@ -58,8 +58,6 @@ public HomeAssistantWS(string apiUrl, string webhookId, string apiToken) retryCount++; registerAsync(); } catch (Exception ex) { - - throw new Exception("unnable to connect to" + url); } } @@ -118,9 +116,7 @@ public async Task registerAsync() recieveLoopObject = ReceiveLoopAsync(); - } - catch (Exception ex) - { + } catch (Exception ex) { Logger.write("WS error " + ex.Message); Logger.write("WS URL " + url); Logger.write("WS State " + socket.State); @@ -175,21 +171,27 @@ private async Task RecieveAsync() private async Task Send(dynamic payloadObj) { - string JSONPayload = JsonConvert.SerializeObject(payloadObj, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }).ToString(); + try { + string JSONPayload = JsonConvert.SerializeObject(payloadObj, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }).ToString(); - Logger.write("SEND"); - Logger.write(JSONPayload); - interactions = interactions + 1; + Logger.write("SEND"); + Logger.write(JSONPayload); + interactions = interactions + 1; - ArraySegment BYTEPayload = new ArraySegment(Encoding.UTF8.GetBytes(JSONPayload)); + ArraySegment BYTEPayload = new ArraySegment(Encoding.UTF8.GetBytes(JSONPayload)); - socket.SendAsync(BYTEPayload, WebSocketMessageType.Text, true, CancellationToken.None).Wait(); + socket.SendAsync(BYTEPayload, WebSocketMessageType.Text, true, CancellationToken.None).Wait(); + } catch (Exception ex) { + Logger.write("SEND FAILED"); + Logger.write(ex.Message); + } } public bool getConectionStatus() { return (isConnected && isSubscribed); } + private async Task StartPingAsyncTask() { Logger.write("Initializing Ping"); @@ -206,6 +208,7 @@ private async void UpdatePingTick(object sender, EventArgs e) HAWSPing pingObj = new HAWSPing { }; pingObj.type = "ping"; pingObj.id = interactions; + Logger.write("Ping"); await Send(pingObj); } @@ -213,34 +216,35 @@ private async void UpdatePingTick(object sender, EventArgs e) private async Task ReceiveLoopAsync() { - try - { + try { Logger.write("WS RECEEVE LOOP STARTED"); var localBuffer = new ArraySegment(new byte[2048]); - do - { - WebSocketReceiveResult localResult; - using (var ms = new MemoryStream()) - { - do - { - localResult = await socket.ReceiveAsync(localBuffer, CancellationToken.None); - ms.Write(localBuffer.Array, localBuffer.Offset, localResult.Count); - } while (!localResult.EndOfMessage); - - if (localResult.MessageType == WebSocketMessageType.Close) - break; - - ms.Seek(0, SeekOrigin.Begin); - using (var reader = new StreamReader(ms, Encoding.UTF8)) - { - string jsonPayload = await reader.ReadToEndAsync(); - Logger.write("WS RECEEVED"); - Logger.write(JObject.Parse(jsonPayload).ToString()); - HandleEvent(JObject.Parse(jsonPayload)); + bool error = false; + do { + try { + WebSocketReceiveResult localResult; + using (var ms = new MemoryStream()) { + do { + localResult = await socket.ReceiveAsync(localBuffer, CancellationToken.None); + ms.Write(localBuffer.Array, localBuffer.Offset, localResult.Count); + } while (!localResult.EndOfMessage); + + if (localResult.MessageType == WebSocketMessageType.Close) + break; + + ms.Seek(0, SeekOrigin.Begin); + using (var reader = new StreamReader(ms, Encoding.UTF8)) { + string jsonPayload = await reader.ReadToEndAsync(); + Logger.write("WS RECEEVED"); + Logger.write(JObject.Parse(jsonPayload).ToString()); + HandleEvent(JObject.Parse(jsonPayload)); + } } + } catch (Exception ex) { + error = true; + Logger.write("WS RECEEVE ERROR" + ex.Message); } - } while (true); + } while (!error); } catch (Exception ex) { @@ -305,6 +309,7 @@ public bool Close() isSubscribed = false; isPingEnabled = false; isConnected = false; + Logger.write("WS state " + socket.State); updatePingTimer.Stop(); diff --git a/HA/HA.csproj b/HA/HA.csproj index dc581bc..600af3a 100644 --- a/HA/HA.csproj +++ b/HA/HA.csproj @@ -8,8 +8,8 @@ true ha_logo.ico ha_logo.ico - 0.0.12.0 - 0.0.12.0 + 0.0.12.1 + 0.0.12.1 diff --git a/HA/meta.xml b/HA/meta.xml index a84e4af..e226e05 100644 --- a/HA/meta.xml +++ b/HA/meta.xml @@ -1,6 +1,6 @@  - 0.0.11.0 + 0.0.12.1 https://github.com/GamerClassN7/HA_Desktop_Companion/releases/latest/download/HA_Self_Contained.zip https://github.com/GamerClassN7/HA_Desktop_Companion/releases/latest false From 20f5f3b09272c57632076e1e5cf62c46efb65fd4 Mon Sep 17 00:00:00 2001 From: RoboMagus <68224306+RoboMagus@users.noreply.github.com> Date: Tue, 2 Jan 2024 14:36:03 +0100 Subject: [PATCH 2/5] Fix websocket URL failure w/o trailing slash --- HA/Class/HomeAssistant/HomeAssistantWS.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HA/Class/HomeAssistant/HomeAssistantWS.cs b/HA/Class/HomeAssistant/HomeAssistantWS.cs index 5185ee2..86a3365 100644 --- a/HA/Class/HomeAssistant/HomeAssistantWS.cs +++ b/HA/Class/HomeAssistant/HomeAssistantWS.cs @@ -68,7 +68,7 @@ public async Task registerAsync() { Logger.write("WS INITIALIZATION"); - Uri wsAddress = new Uri(url + "api/websocket"); + Uri wsAddress = new Uri(url.TrimEnd('/') + "/api/websocket"); var exitEvent = new ManualResetEvent(false); socket.Options.KeepAliveInterval = TimeSpan.Zero; From 10c87053d913d8d0fcde2728ccc9e27eab33dcc3 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Tue, 16 Jan 2024 08:02:31 +0100 Subject: [PATCH 3/5] Log Class Fixes --- HA/App.xaml.cs | 2 +- HA/Class/Helpers/Logger.cs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/HA/App.xaml.cs b/HA/App.xaml.cs index 8c15dc9..a812fd0 100644 --- a/HA/App.xaml.cs +++ b/HA/App.xaml.cs @@ -181,7 +181,7 @@ public string GetRootDir() public bool Start(bool sleepRecover = false) { - Logger.init(appDir + "/log.log"); + Logger.init(); string token = ""; string url = ""; diff --git a/HA/Class/Helpers/Logger.cs b/HA/Class/Helpers/Logger.cs index b2e27ec..01b3f42 100644 --- a/HA/Class/Helpers/Logger.cs +++ b/HA/Class/Helpers/Logger.cs @@ -18,6 +18,7 @@ namespace HA.Class.Helpers public class Logger { private static bool initialized = false; + private static DateTime lastInit; private static string path1; private static string[] secreetsStrings = new string[] { }; @@ -36,13 +37,21 @@ public static void setSecreets(string[] strings) secreetsStrings = strings.Where(x => !string.IsNullOrEmpty(x)).ToArray(); } - public static void init(string path = "./log.log") + public static void init() { - path1 = Path.Combine(appDir, path).ToString(); + + path1 = Path.Combine(appDir, (DateTime.Now).ToString("MM_dd_yyyy_log.log")).ToString(); + lastInit = DateTime.Now; if (!File.Exists(path1)) { File.WriteAllText(path1, getMessage("Initialization", 0 /*info*/), System.Text.Encoding.UTF8); } + + string pathToLogToDelete = Path.Combine(appDir, (DateTime.Now).ToString("MM_dd_yyyy_log.log")).ToString(); + if (File.Exists(pathToLogToDelete)) + { + File.Delete(pathToLogToDelete); + } initialized = true; } @@ -55,7 +64,8 @@ public static void init(string path = "./log.log") public static void write(string msg, int level = 0) { - if (!initialized) + int initBeforeDays = (int)(DateTime.Now - lastInit).TotalDays; + if (!initialized || initBeforeDays >= 1) { init(); } From 6ad6cc3f2edfdb93eb2fa20ab06e0d5cc24186a8 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Tue, 16 Jan 2024 08:04:30 +0100 Subject: [PATCH 4/5] Fix Overload --- HA/Class/Helpers/Logger.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/HA/Class/Helpers/Logger.cs b/HA/Class/Helpers/Logger.cs index 01b3f42..49f269b 100644 --- a/HA/Class/Helpers/Logger.cs +++ b/HA/Class/Helpers/Logger.cs @@ -47,7 +47,7 @@ public static void init() File.WriteAllText(path1, getMessage("Initialization", 0 /*info*/), System.Text.Encoding.UTF8); } - string pathToLogToDelete = Path.Combine(appDir, (DateTime.Now).ToString("MM_dd_yyyy_log.log")).ToString(); + string pathToLogToDelete = Path.Combine(appDir, (DateTime.Now).AddDays(-3).ToString("MM_dd_yyyy_log.log")).ToString(); if (File.Exists(pathToLogToDelete)) { File.Delete(pathToLogToDelete); @@ -82,7 +82,8 @@ public static void write(string msg, int level = 0) public static void write(object msg, int level = 0) { - if (!initialized) + int initBeforeDays = (int)(DateTime.Now - lastInit).TotalDays; + if (!initialized || initBeforeDays >= 1) { init(); } From 1759c180574cc923aed23f6e9552627d2f825a4b Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Tue, 16 Jan 2024 19:35:09 +0100 Subject: [PATCH 5/5] Trimurls tailing / same vai for api and ws --- HA/Class/HomeAssistant/HomeAssistantWS.cs | 4 ++-- HA/class/HomeAssistant/HomeAssistantAPI.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HA/Class/HomeAssistant/HomeAssistantWS.cs b/HA/Class/HomeAssistant/HomeAssistantWS.cs index 86a3365..981b762 100644 --- a/HA/Class/HomeAssistant/HomeAssistantWS.cs +++ b/HA/Class/HomeAssistant/HomeAssistantWS.cs @@ -49,7 +49,7 @@ public class HomeAssistantWS public HomeAssistantWS(string apiUrl, string webhookId, string apiToken) { - url = apiUrl; + url = apiUrl.TrimEnd('/'); token = apiToken; webhook = webhookId; @@ -68,7 +68,7 @@ public async Task registerAsync() { Logger.write("WS INITIALIZATION"); - Uri wsAddress = new Uri(url.TrimEnd('/') + "/api/websocket"); + Uri wsAddress = new Uri(url + "/api/websocket"); var exitEvent = new ManualResetEvent(false); socket.Options.KeepAliveInterval = TimeSpan.Zero; diff --git a/HA/class/HomeAssistant/HomeAssistantAPI.cs b/HA/class/HomeAssistant/HomeAssistantAPI.cs index f17ffe4..ac199bc 100644 --- a/HA/class/HomeAssistant/HomeAssistantAPI.cs +++ b/HA/class/HomeAssistant/HomeAssistantAPI.cs @@ -69,7 +69,7 @@ public HomeAssistantAPI(string apiRootUrl, string apiToken) throw new Exception("unnabůle to connect to" + apiRootUrl); } - url = apiRootUrl; + url = apiRootUrl.TrimEnd('/'); token = apiToken; }