Skip to content

Commit

Permalink
Merge pull request #79 from GamerClassN7/dev
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
GamerClassN7 authored May 24, 2024
2 parents 341a11e + d83ad45 commit e2c9e9b
Show file tree
Hide file tree
Showing 53 changed files with 3,265 additions and 33 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 --self-contained /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true --output .\ha\bin\Publish\net6.0-windows10.0.17763.0\`
```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\
```
12 changes: 12 additions & 0 deletions HA.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HADC_REBORN", "HADC_REBORN\HADC_REBORN.csproj", "{3C0F1F5F-1ADE-4CF2-A5B5-348A41ABCD84}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{8FC5E1BA-0D50-4D77-B6F9-B395ABCD6259}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +19,14 @@ 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
{8FC5E1BA-0D50-4D77-B6F9-B395ABCD6259}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FC5E1BA-0D50-4D77-B6F9-B395ABCD6259}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FC5E1BA-0D50-4D77-B6F9-B395ABCD6259}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FC5E1BA-0D50-4D77-B6F9-B395ABCD6259}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
44 changes: 24 additions & 20 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,7 +47,6 @@ 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.TrimEnd('/');
Expand All @@ -67,9 +67,9 @@ public async Task registerAsync()
try
{
Logger.write("WS INITIALIZATION");

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()

}
}


}
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();
}
}
}
8 changes: 4 additions & 4 deletions HA/configuration.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

websocket:
ip_location:
debug:
keys:
websocket: true
# ip_location: false
debug: true
# keys: false

sensor:
- platform: wmic
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 e2c9e9b

Please sign in to comment.