Skip to content

Commit aa9a958

Browse files
authored
Merge pull request #12 from PinchToDebug/dev
Dev
2 parents 846c213 + ef80cc1 commit aa9a958

5 files changed

Lines changed: 195 additions & 10 deletions

File tree

Pihole_Tray/App.xaml.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Configuration;
1+
using Microsoft.Toolkit.Uwp.Notifications;
2+
using System.Configuration;
23
using System.Data;
34
using System.Windows;
45

@@ -9,6 +10,23 @@ namespace Pihole_Tray
910
/// </summary>
1011
public partial class App : Application
1112
{
13+
protected override void OnStartup(StartupEventArgs e)
14+
{
15+
base.OnStartup(e);
16+
ToastNotificationManagerCompat.OnActivated += ToastActivatedHandler;
17+
}
18+
private void ToastActivatedHandler(ToastNotificationActivatedEventArgsCompat toastArgs)
19+
{
20+
var args = ToastArguments.Parse(toastArgs.Argument);
21+
Current.Dispatcher.Invoke(() =>
22+
{
23+
if (args.Contains("action") && args["action"] == "install_update")
24+
{
25+
Updater.InstallUpdate();
26+
}
27+
28+
});
29+
}
1230
}
1331

1432
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System.Windows.Controls;
2+
using System.Windows;
3+
using Newtonsoft.Json.Linq;
4+
using System.Diagnostics;
5+
using System.Windows.Media;
6+
using static System.Runtime.InteropServices.JavaScript.JSType;
7+
using Wpf.Ui.Controls;
8+
using TextBlock = System.Windows.Controls.TextBlock;
9+
using Newtonsoft.Json;
10+
11+
public class SysinfoType
12+
{
13+
public string CpuTemp { get; set; }
14+
public int CpuUsage { get; set; }
15+
public int RamUsage { get; set; }
16+
public int RamTotal { get; set; }
17+
public Brush Brush { get; set; }
18+
19+
}
20+
21+
22+
23+
24+
25+
public class SystemInfoLoader
26+
{
27+
public async Task LoadAsync(
28+
TextBlock CpuTempTB,
29+
TextBlock CpuUsageTB,
30+
System.Windows.Documents.Run ramUsedRun,
31+
System.Windows.Documents.Run ramTotalRun,
32+
dynamic cpuTemp,
33+
dynamic cpuRamUsage,
34+
bool isV6,
35+
Brush blueBrush)
36+
{
37+
var items = new List<SysinfoType>();
38+
await Task.Run(() =>
39+
{
40+
try
41+
{
42+
foreach (var item in cpuTemp.sensors.list)
43+
{
44+
if (item["name"]?.ToString() == "cpu0_thermal")
45+
{
46+
var tempsArray = item["temps"] as JArray;
47+
48+
if (tempsArray != null && tempsArray.Count > 0)
49+
{
50+
51+
var cpuTempValue = tempsArray[0]["value"];
52+
53+
if (cpuTempValue != null)
54+
{
55+
float cpuTemps = cpuTempValue.Value<float>();
56+
Application.Current.Dispatcher.Invoke(() =>
57+
{
58+
CpuTempTB.Text = cpuTemps.ToString("F1") + " °C";
59+
});
60+
}
61+
else
62+
{
63+
Application.Current.Dispatcher.Invoke(() =>
64+
{
65+
CpuTempTB.Text = "N/A";
66+
});
67+
}
68+
}
69+
else
70+
{
71+
Application.Current.Dispatcher.Invoke(() =>
72+
{
73+
CpuTempTB.Text = "N/A";
74+
});
75+
}
76+
break;
77+
}
78+
}
79+
80+
var totalMemory = cpuRamUsage.system.memory.ram.total / 1000;
81+
var usedMemory = cpuRamUsage.system.memory.ram.used / 1000;
82+
var cpuLoadRawFirst = (cpuRamUsage.system.cpu.load.raw[0]*100).ToString("F1")+" %";
83+
84+
Application.Current.Dispatcher.Invoke(() =>
85+
{
86+
CpuUsageTB.Text = cpuLoadRawFirst;
87+
ramUsedRun.Text = usedMemory;
88+
ramTotalRun.Text = totalMemory;
89+
});
90+
91+
92+
}
93+
catch (Exception e)
94+
{
95+
Debug.WriteLine(e.Message);
96+
97+
}
98+
});
99+
}
100+
}
101+

Pihole_Tray/Helper/Updater.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static async Task CheckUpdateAsync(string url)
4040

4141
if (!latestVersion.Contains(currentVersion))
4242
{
43-
var toastBuilder = new Microsoft.Toolkit.Uwp.Notifications.ToastContentBuilder()
43+
var toastBuilder = new ToastContentBuilder()
4444
.AddText($"{emoji} New release! {name}", AdaptiveTextStyle.Header)
4545
.AddText(description, AdaptiveTextStyle.Body)
4646
.AddButton(new ToastButton()
@@ -78,6 +78,7 @@ public static async Task CheckUpdateAsync(string url)
7878
}
7979
public static async Task InstallUpdate()
8080
{
81+
8182
using (var httpClient = new System.Net.Http.HttpClient())
8283
{
8384
Debug.WriteLine($"{Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion}");

Pihole_Tray/MainWindow.xaml

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545

4646

4747
</Grid.RowDefinitions>
48-
<ui:TextBlock Text="Home PI:" Name="CurrentNameTB" Grid.Column="0" Grid.Row="0" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" Cursor="Hand" MouseLeftButtonUp="Instance_MouseLeftButtonUp" />
49-
<TextBlock Text="Domains Blocked:" Grid.Column="0" Grid.Row="1" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" />
48+
<ui:TextBlock Text="Home PI:" Name="CurrentNameTB" Grid.Column="0" Grid.Row="0" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" Cursor="Hand" MouseLeftButtonUp="Instance_MouseLeftButtonUp" />
49+
<TextBlock Text="Domains Blocked:" Grid.Column="0" Grid.Row="1" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" />
5050
<TextBlock Name="GravityLB" Text="Gravity updated:" Grid.Column="0" Grid.Row="2" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" />
5151

5252

@@ -55,8 +55,6 @@
5555
<TextBlock Name="GravityTB" Text="-" Grid.Column="1" Grid.Row="2" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Right"/>
5656

5757

58-
59-
6058
</Grid>
6159
</ui:Card>
6260
<ui:Card Name="Basic2" Margin="0,0,0,0" VerticalAlignment="Top" Padding="14,5,14,7">
@@ -72,16 +70,60 @@
7270

7371

7472
</Grid.RowDefinitions>
75-
<TextBlock Text="DNS Queries:" Grid.Column="0" Grid.Row="0" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left"/>
76-
<TextBlock Text="Ads Blocked:" Grid.Column="0" Grid.Row="2" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" />
73+
<TextBlock Text="Total Queries:" Grid.Column="0" Grid.Row="0" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left"/>
74+
<TextBlock Text="Queries Blocked:" Grid.Column="0" Grid.Row="2" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" />
7775

78-
<TextBlock Name="DnsQueryTB" Text="-" Grid.Column="1" Grid.Row="0" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Right" Foreground="#FFABA4FF"/>
76+
<TextBlock Name="DnsQueryTB" Text="-" Grid.Column="1" Grid.Row="0" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Right" Foreground="#FFABA4FF"/>
7977
<TextBlock Name="AdsBlockedTB" Text="-" Grid.Column="1" Grid.Row="2" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,4,0,0" Foreground="#FFFFA4A4"/>
8078

8179

8280

8381
</Grid>
8482
</ui:Card>
83+
<Label Content="System Info"
84+
Name="SysInfoLBL"
85+
HorizontalAlignment="Center"
86+
Margin="0,10,0,5"
87+
/>
88+
<ui:Card Name="SysinfoCard" Margin="0,0,0,0" VerticalAlignment="Top" Padding="14,5,14,7">
89+
<Grid>
90+
<Grid.ColumnDefinitions>
91+
<ColumnDefinition/>
92+
<ColumnDefinition />
93+
</Grid.ColumnDefinitions>
94+
<Grid.RowDefinitions>
95+
96+
<RowDefinition Height="22"/>
97+
<RowDefinition Height="22"/>
98+
<RowDefinition Height="22"/>
99+
100+
101+
</Grid.RowDefinitions>
102+
<TextBlock Text="CPU Temp:" Grid.Column="0" Grid.Row="0" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left"/>
103+
<TextBlock Text="CPU:" Grid.Column="0" Grid.Row="1" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" ToolTip="1 minute load averages" Cursor="Help"/>
104+
<TextBlock Text="RAM:" Grid.Column="0" Grid.Row="2" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Left" />
105+
106+
107+
<TextBlock Name="CpuTempTB" Text="- °C" Grid.Column="1" Grid.Row="0" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Right" Foreground="#FF698EFF"/>
108+
109+
<TextBlock Name="CpuUsageTB" Grid.Column="1" Grid.Row="1" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,4,0,0" Foreground="#FF698EFF">
110+
<Run Text="- %" Foreground="#FFBBC4F7"/>
111+
</TextBlock>
112+
113+
114+
<TextBlock Name="RamUsageTB" Grid.Column="1" Grid.Row="2" FontSize="13" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,4,0,0">
115+
<Run Name="ramUsedRun" Text="353" Foreground="#FFBBC4F7"/>
116+
<TextBlock Text="MB" FontSize="10" Foreground="Gray" Margin="-3,0,0,0" VerticalAlignment="Center" />
117+
<Run Text=" / " Foreground="#FFBBC4F7" />
118+
<Run Name="ramTotalRun" Text="2372 " Foreground="#FFBBC4F7" />
119+
<TextBlock Text="MB" Margin="-3,0,0,0" FontSize="10" Foreground="Gray" VerticalAlignment="Center" />
120+
121+
</TextBlock>
122+
123+
</Grid>
124+
</ui:Card>
125+
126+
85127
<Label Content="Recent Blocks"
86128
Name="RecentBlockLBL"
87129
HorizontalAlignment="Center"
@@ -275,6 +317,7 @@
275317

276318

277319
<ui:MenuItem x:Name="Autorunh_Button" Height="38" Header="Elements" Icon="{ui:SymbolIcon Symbol=TextBulletListLtr20}" >
320+
<ui:ToggleSwitch Name="SysinfoTS" Content="System Info" IsChecked="true" Click="SysinfoTS_Click"/>
278321
<ui:ToggleSwitch Name="RecentBlocksTS" Content="Recent Blocks" IsChecked="true" Click="RecentBlocksTS_Click" />
279322
<ui:ToggleSwitch Name="SourcesTS" Content="Sources" IsChecked="true" Click="SourcesTS_Click" />
280323
<ui:ToggleSwitch Name="ForwardDestinationsTS" Content="Query Routes" IsChecked="true" Click="ForwardDestinationsTS_Click"/>

Pihole_Tray/MainWindow.xaml.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public MainWindow()
123123
Default_StackPanel.Visibility = Visibility.Visible;
124124
Info_StackPanel.Visibility = Visibility.Hidden;
125125

126+
if (reg.KeyExistsRoot("SysinfoTS")) SysinfoTS.IsChecked = (bool)reg.ReadKeyValueRoot("SysinfoTS"); HideShowElementPairs(SysInfoLBL, SysinfoCard, SysinfoTS);
126127
if (reg.KeyExistsRoot("RecentBlocksTS")) RecentBlocksTS.IsChecked = (bool)reg.ReadKeyValueRoot("RecentBlocksTS"); HideShowElementPairs(RecentBlockLBL, BlockHistoryCard, RecentBlocksTS);
127128
if (reg.KeyExistsRoot("QueryTS")) QueryTS.IsChecked = (bool)reg.ReadKeyValueRoot("QueryTS"); HideShowElementPairs(QueryLBL, QueryCard, QueryTS); Debug.WriteLine(" ");
128129
if (reg.KeyExistsRoot("SourcesTS")) SourcesTS.IsChecked = (bool)reg.ReadKeyValueRoot("SourcesTS"); HideShowElementPairs(SourcesLBL, SourcesCard, SourcesTS);
@@ -522,6 +523,8 @@ private async void UpdateInfo(Instance instance, CancellationToken token)
522523

523524
dynamic summary = new ExpandoObject();
524525
dynamic status = new ExpandoObject();
526+
dynamic cpuTemp = new ExpandoObject();
527+
dynamic cpuRamUsage = new ExpandoObject();
525528

526529

527530
//if (coldRun)
@@ -588,8 +591,15 @@ private async void UpdateInfo(Instance instance, CancellationToken token)
588591
status = JsonConvert.DeserializeObject<dynamic>(await httpClient.GetStringAsync($"{instance.Address}/dns/blocking"))!;
589592
await Task.Delay(50, token);
590593

594+
if ((bool)SysinfoTS.IsChecked!)
595+
{
596+
cpuTemp = JsonConvert.DeserializeObject<dynamic>(await httpClient.GetStringAsync($"{instance.Address}/info/sensors"));
597+
await Task.Delay(50, token);
591598

592-
if ((bool)RecentBlocksTS.IsChecked!)
599+
cpuRamUsage = JsonConvert.DeserializeObject<dynamic>(await httpClient.GetStringAsync($"{instance.Address}/info/system"));
600+
await Task.Delay(50, token);
601+
}
602+
if ((bool)RecentBlocksTS.IsChecked!)
593603
{
594604
response = JsonConvert.DeserializeObject<dynamic>(await httpClient.GetStringAsync($"{instance.Address}/queries?length=100&upstream=blocklist"));
595605
blocked = (JArray)response.queries;
@@ -731,6 +741,12 @@ private async void UpdateInfo(Instance instance, CancellationToken token)
731741
: new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF7B8BF5"));
732742

733743

744+
CpuTempTB.Foreground = blueBrush;
745+
CpuUsageTB.Foreground = blueBrush;
746+
RamUsageTB.Foreground = blueBrush;
747+
ramUsedRun.Foreground = blueBrush;
748+
ramTotalRun.Foreground = blueBrush;
749+
734750
if (instance.isV6 == true)
735751
{
736752
GravityLB.Visibility = Visibility.Collapsed;
@@ -755,6 +771,7 @@ private async void UpdateInfo(Instance instance, CancellationToken token)
755771
if (StatusTB.Text == "enabled") StatusTB.Foreground = greenBrush;
756772
else StatusTB.Foreground = redBrush;
757773

774+
if ((bool)SysinfoTS.IsChecked) await new SystemInfoLoader().LoadAsync(CpuTempTB,CpuUsageTB,ramUsedRun,ramTotalRun, cpuTemp, cpuRamUsage, (bool)instance.isV6, blueBrush);
758775
if ((bool)RecentBlocksTS.IsChecked) await new RecentBlocksLoader().LoadAsync(BlockHistoryItemsControl, blocked, (bool)instance.isV6, redBrush);
759776
if ((bool)SourcesTS.IsChecked) await new SourcesLoader().LoadAsync(SourcesItemsControl, topClients, (bool)instance.isV6, purpleBrush, blueBrush);
760777
if ((bool)ForwardDestinationsTS.IsChecked) await new DnsRoutesLoader().LoadAsync(ForwardDestinationsGrid, upStreams, (bool)instance.isV6, isDarkTheme);
@@ -1158,6 +1175,11 @@ private void Resize()
11581175
}
11591176

11601177

1178+
private void SysinfoTS_Click(object sender, RoutedEventArgs e)
1179+
{
1180+
HideShowElementPairs(SysInfoLBL, SysinfoCard, SysinfoTS);
1181+
1182+
}
11611183
private void RecentBlocksTS_Click(object sender, RoutedEventArgs e)
11621184
{
11631185
HideShowElementPairs(RecentBlockLBL, BlockHistoryCard, RecentBlocksTS);

0 commit comments

Comments
 (0)