This repository has been archived by the owner on Jun 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bit janky, but it works. Needs comments, and optimising.
- Loading branch information
Showing
2 changed files
with
110 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,140 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using LogiFrame; | ||
using Newtonsoft.Json.Linq; | ||
using Timer = System.Threading.Timer; | ||
|
||
namespace LCDPihole | ||
{ | ||
class PiHoleApi | ||
public class PiHoleApi | ||
{ | ||
private readonly LCDApp _app; | ||
private readonly LCDLabel _lblTotDomains; | ||
private readonly LCDLabel _lblQueriesToday; | ||
private readonly LCDLabel _lblBlockedToday; | ||
private readonly LCDLabel _lblPercToday; | ||
|
||
private Timer _t; | ||
|
||
public PiHoleApi() | ||
{ | ||
using (WebClient wc = new WebClient()) | ||
_app = new LCDApp("Pi-hole Stats", false, false, false); | ||
// _app.WaitForClose(); | ||
_app.PushToForeground(); | ||
|
||
var lineVer = new LCDLine | ||
{ | ||
var json = wc.DownloadString("http://pi.hole/admin/api.php?summaryRaw"); | ||
Start = new Point((LCDApp.DefaultSize.Width / 2) - 1, 0), | ||
End = new Point((LCDApp.DefaultSize.Width / 2) - 1, LCDApp.DefaultSize.Height - 1) | ||
}; | ||
|
||
JObject j = JObject.Parse(json); | ||
var lineHor = new LCDLine | ||
{ | ||
Start = new Point(0, (LCDApp.DefaultSize.Height / 2) - 1), | ||
End = new Point(LCDApp.DefaultSize.Width - 1, (LCDApp.DefaultSize.Height / 2) - 1) | ||
}; | ||
|
||
var label = new LCDLabel | ||
{ | ||
Font = PixelFonts.Small, // The PixelFonts class contains various good fonts for LCD screens. | ||
Text = json, | ||
AutoSize = true, | ||
}; | ||
var tabPage = new LCDTabPage | ||
{ | ||
Icon = new LCDLabel | ||
{ | ||
AutoSize = true, | ||
Text = "A", | ||
Font = PixelFonts.Title | ||
} | ||
}; | ||
tabPage.Controls.Add(label); | ||
_lblTotDomains = new LCDLabel | ||
{ | ||
Font = PixelFonts.Small, | ||
AutoSize = true, | ||
Top = 0, | ||
Left = 0, | ||
Width = (LCDApp.DefaultSize.Width / 2) - 1 | ||
|
||
}; | ||
_lblQueriesToday = new LCDLabel | ||
{ | ||
Font = PixelFonts.Small, | ||
AutoSize = true, | ||
Top = LCDApp.DefaultSize.Height / 2, | ||
Left = 0 | ||
|
||
}; | ||
_lblBlockedToday = new LCDLabel | ||
{ | ||
Font = PixelFonts.Small, | ||
AutoSize = true, | ||
Top = 0, | ||
Left = LCDApp.DefaultSize.Width / 2 | ||
|
||
var tabPage2 = new LCDTabPage | ||
{ | ||
Icon = new LCDLabel | ||
{ | ||
AutoSize = true, | ||
Text = "B", | ||
Font = PixelFonts.Title | ||
} | ||
}; | ||
}; | ||
_lblPercToday = new LCDLabel | ||
{ | ||
Font = PixelFonts.Small, | ||
AutoSize = true, | ||
Top = LCDApp.DefaultSize.Height / 2, | ||
Left = LCDApp.DefaultSize.Width / 2 | ||
|
||
}; | ||
|
||
var tabControl = new LCDTabControl(); | ||
tabControl.TabPages.Add(tabPage); | ||
tabControl.TabPages.Add(tabPage2); | ||
// Create an app instance. | ||
|
||
tabControl.SelectedTab = tabPage; | ||
|
||
// Add the label control to the app. | ||
_app.Controls.Add(lineVer); | ||
_app.Controls.Add(lineHor); | ||
_app.Controls.Add(_lblTotDomains); | ||
_app.Controls.Add(_lblQueriesToday); | ||
_app.Controls.Add(_lblBlockedToday); | ||
_app.Controls.Add(_lblPercToday); | ||
|
||
UpdateVisibility(); | ||
|
||
// Create an app instance. | ||
var app = new LCDApp("Sample App", false, false, false); | ||
UpdateStats(); | ||
|
||
// Add the label control to the app. | ||
app.Controls.Add(tabControl); | ||
// Make the app the foreground app on the LCD screen. | ||
app.PushToForeground(); | ||
|
||
// A blocking call. Waits for the LCDApp instance to be disposed. (optional) | ||
app.WaitForClose(); | ||
TimerCallback tDelegate = TimerCallback; | ||
_t = new Timer(tDelegate, null, 0, 5000); | ||
|
||
} | ||
|
||
public void WaitForClose() | ||
{ | ||
_app.WaitForClose(); | ||
} | ||
|
||
private void UpdateVisibility() | ||
{ | ||
_app.UpdatePriority = UpdatePriority.Normal; | ||
} | ||
|
||
private void UpdateStats() | ||
{ | ||
using (var wc = new WebClient()) | ||
{ | ||
var json = wc.DownloadString("http://pi.hole/admin/api.php?summaryRaw"); | ||
|
||
var j = JObject.Parse(json); | ||
|
||
var DomainsBlocked = j["domains_being_blocked"].ToString(); | ||
var QueriesToday = j["dns_queries_today"].ToString(); | ||
var BlockedToday = j["ads_blocked_today"].ToString(); | ||
var PercToday = Math.Round(Convert.ToDecimal(j["ads_percentage_today"].ToString()), 2).ToString(); | ||
|
||
_lblTotDomains.Text = $"{DomainsBlocked}\nDomains Blocked"; | ||
_lblBlockedToday.Text = $"{BlockedToday}\nBlocked Today"; | ||
_lblQueriesToday.Text = $"{QueriesToday}\nQueries Today"; | ||
_lblPercToday.Text = $"{PercToday}%\nPercentage"; | ||
} | ||
|
||
|
||
} | ||
|
||
private void TimerCallback(object o) | ||
{ | ||
|
||
UpdateStats(); | ||
|
||
} | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters