Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…anion into dev
  • Loading branch information
GamerClassN7 committed May 24, 2024
2 parents 3501f06 + f817f56 commit ba8c82b
Show file tree
Hide file tree
Showing 52 changed files with 2,523 additions and 42 deletions.
4 changes: 3 additions & 1 deletion BUILD.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
## Build Command:
`dotnet publish HA -r win-x64 -c Release /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true --output .\ha\bin\net6.0-windows10.0.17763.0\release\publish_self_contained`
```powersehll
dotnet publish HADC_REBORN -r win-x64 -c Release --self-contained /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true --output .\HADC_REBORN\bin\Publish\net8.0-windows10.0.17763.0\
```
6 changes: 6 additions & 0 deletions HA.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HA", "HA\HA.csproj", "{F4675A4D-373E-4C21-B77D-529E88181E5F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HADC_REBORN", "HADC_REBORN\HADC_REBORN.csproj", "{3C0F1F5F-1ADE-4CF2-A5B5-348A41ABCD84}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{F4675A4D-373E-4C21-B77D-529E88181E5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4675A4D-373E-4C21-B77D-529E88181E5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4675A4D-373E-4C21-B77D-529E88181E5F}.Release|Any CPU.Build.0 = Release|Any CPU
{3C0F1F5F-1ADE-4CF2-A5B5-348A41ABCD84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C0F1F5F-1ADE-4CF2-A5B5-348A41ABCD84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C0F1F5F-1ADE-4CF2-A5B5-348A41ABCD84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C0F1F5F-1ADE-4CF2-A5B5-348A41ABCD84}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion HA/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public string GetRootDir()

public bool Start(bool sleepRecover = false)
{
Logger.init(appDir + "/log.log");
Logger.init();

string token = "";
string url = "";
Expand Down
29 changes: 24 additions & 5 deletions HA/Class/Helpers/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
Expand All @@ -18,6 +19,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[] { };

Expand All @@ -36,13 +38,28 @@ 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).AddDays(-3).ToString("MM_dd_yyyy") + "_log.log")).ToString();
if (File.Exists(pathToLogToDelete))
{
File.Delete(pathToLogToDelete);
}

string oldPathToLogToDelete = Path.Combine(appDir, "log.log").ToString();
if (File.Exists(oldPathToLogToDelete))
{
File.Delete(oldPathToLogToDelete);
}

initialized = true;
}

Expand All @@ -55,7 +72,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();
}
Expand All @@ -72,7 +90,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();
}
Expand Down
48 changes: 26 additions & 22 deletions HA/Class/HomeAssistant/HomeAssistantWS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HA.Class.HomeAssistant.Objects;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -46,10 +47,9 @@ public class HomeAssistantWS
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void keybd_event(uint bVk, uint bScan, uint dwFlags, uint dwExtraInfo);


public HomeAssistantWS(string apiUrl, string webhookId, string apiToken)
{
url = apiUrl;
url = apiUrl.TrimEnd('/');
token = apiToken;
webhook = webhookId;

Expand All @@ -67,9 +67,9 @@ public async Task registerAsync()
try
{
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 = new ClientWebSocket();
socket.Options.KeepAliveInterval = TimeSpan.Zero;

socket.ConnectAsync(wsAddress, CancellationToken.None).Wait();
Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task registerAsync()
isPingEnabled = true;

StartPingAsyncTask();

isConnected = true;
retryCount = 0;

Expand All @@ -122,11 +122,11 @@ public async Task registerAsync()
Logger.write("WS State " + socket.State);

Close();
if (retryCount <= 5 && socket.State == WebSocketState.Closed)
if (retryCount <= 5 && (socket.State == WebSocketState.Closed || socket.State == WebSocketState.Aborted))
{
retryCount++;
registerAsync();
}
}
}
}

Expand All @@ -141,18 +141,18 @@ private JObject sendAndRecieveAsync(dynamic payloadObj)

socket.SendAsync(BYTEPayload, WebSocketMessageType.Text, true, CancellationToken.None).Wait();

string JSONRecievedpayload = "";

Logger.write("SEND/RECIEVING");
interactions = interactions + 1;
string JSONRecievedpayload = "";

WebSocketReceiveResult result = socket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None).Result;
Logger.write("SEND/RECIEVING");
interactions = interactions + 1;

JSONRecievedpayload = Encoding.UTF8.GetString(buffer, 0, result.Count);
WebSocketReceiveResult result = socket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None).Result;

JSONRecievedpayload = Encoding.UTF8.GetString(buffer, 0, result.Count);

Logger.write("RECIEVE");
Logger.write(JSONRecievedpayload);

Logger.write("RECIEVE");
Logger.write(JSONRecievedpayload);


return JObject.Parse(JSONRecievedpayload);
}
Expand All @@ -171,7 +171,7 @@ private async Task<JObject> RecieveAsync()

private async Task Send(dynamic payloadObj)
{
try {
try {
string JSONPayload = JsonConvert.SerializeObject(payloadObj, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }).ToString();

Logger.write("SEND");
Expand All @@ -189,9 +189,9 @@ private async Task Send(dynamic payloadObj)

public bool getConectionStatus()
{
return (isConnected && isSubscribed);
return (isConnected && isSubscribed);
}

private async Task StartPingAsyncTask()
{
Logger.write("Initializing Ping");
Expand Down Expand Up @@ -293,7 +293,7 @@ private void HandleEvent(JObject payloadObj)

app.ShowNotification(msg_title, msg_text, msg_image, msg_audio);
}

if (eventData.ContainsKey("data")) {
if (eventData["data"].ToObject<JObject>().ContainsKey("key"))
{
Expand All @@ -312,11 +312,15 @@ public bool Close()

Logger.write("WS state " + socket.State);

updatePingTimer.Stop();
if (socket.State == WebSocketState.Open || socket.State == WebSocketState.CloseSent)
if (socket.State == WebSocketState.Open || socket.State == WebSocketState.CloseSent || socket.State == WebSocketState.Aborted)
{
updatePingTimer.Stop();
socket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
socket.Dispose();
socket = null;
Thread.Sleep(5000);
Logger.write("WS closed");
registerAsync();
}

return true;
Expand Down
4 changes: 1 addition & 3 deletions HA/Class/Sensors/IpLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace HA.Class.Sensors
{
internal class IpLocation
internal class Notification
{
public static string getData()
{
Expand All @@ -29,6 +29,4 @@ public static void test()

}
}


}
4 changes: 2 additions & 2 deletions HA/HA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<UseWindowsForms>true</UseWindowsForms>
<PackageIcon>ha_logo.ico</PackageIcon>
<ApplicationIcon>ha_logo.ico</ApplicationIcon>
<AssemblyVersion>0.0.12.1</AssemblyVersion>
<FileVersion>0.0.12.1</FileVersion>
<AssemblyVersion>0.0.13.0</AssemblyVersion>
<FileVersion>0.0.13.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="Resources\ha_loading.gif" />
Expand Down
2 changes: 1 addition & 1 deletion HA/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<TextBlock Grid.Column="0" x:Name="api_status" TextWrapping="Wrap" FontWeight="Bold" FontSize="13" Foreground="Red" x:FieldModifier="public" Margin="2,0,0,0">
<Run Language="cs-cz" Text="Api"/>
</TextBlock>
<TextBlock Grid.Column="1" x:Name="ws_status" TextWrapping="Wrap" FontWeight="Bold" FontSize="13" Foreground="Red" x:FieldModifier="public" HorizontalAlignment="Right" Margin="0,0,2,0">
<TextBlock Grid.Column="1" x:Name="ws_status" TextWrapping="Wrap" FontWeight="Bold" FontSize="13" Foreground="Red" x:FieldModifier="public" HorizontalAlignment="Right" Margin="0,0,2,0" MouseDown="api_status_MouseDown">
<Run Language="cs-cz" Text="WebSocket"/>
</TextBlock>
</Grid>
Expand Down
6 changes: 6 additions & 0 deletions HA/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,11 @@ public void settings_Save()
loadingScreenStatus.Content = "Initialization Failed!";
loading_Hide();
}

private void api_status_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
app.ws.Close();
app.Start();
}
}
}
2 changes: 1 addition & 1 deletion HA/class/HomeAssistant/HomeAssistantAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion HA/meta.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>0.0.12.1</version>
<version>0.0.13.0</version>
<url>https://github.com/GamerClassN7/HA_Desktop_Companion/releases/latest/download/HA_Self_Contained.zip</url>
<changelog>https://github.com/GamerClassN7/HA_Desktop_Companion/releases/latest</changelog>
<mandatory>false</mandatory>
Expand Down
11 changes: 11 additions & 0 deletions HADC_REBORN/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="url" value=""/>
<add key="token" value=""/>
<add key="webhook_id" value=""/>
<add key="remote_url" value=""/>
<add key="cloud_url" value=""/>
<add key="secret" value=""/>
</appSettings>
</configuration>
10 changes: 10 additions & 0 deletions HADC_REBORN/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Application x:Class="HADC_REBORN.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HADC_REBORN"
StartupUri="MainWindow.xaml"
ShutdownMode="OnExplicitShutdown" Exit="Application_Exit" Startup="Application_Startup">
<Application.Resources>

</Application.Resources>
</Application>
Loading

0 comments on commit ba8c82b

Please sign in to comment.