Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Bit janky, but it works. Needs comments, and optimising.
Browse files Browse the repository at this point in the history
  • Loading branch information
PromoFaux committed Sep 15, 2016
1 parent b3f0f16 commit 868b94f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 44 deletions.
147 changes: 106 additions & 41 deletions LCD-Pihole/LCD-Pihole/PiHoleApi.cs
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();

}



}
}
7 changes: 4 additions & 3 deletions LCD-Pihole/LCD-Pihole/ProcessIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Windows.Forms;
using LCDPihole;
using LogiFrame;
using Timer = System.Threading.Timer;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -18,7 +18,7 @@ namespace LCDPlay
class ProcessIcon : IDisposable
{

private Timer t;



/// <summary>
Expand Down Expand Up @@ -49,7 +49,8 @@ public void Display()
// Attach a context menu.
ni.ContextMenuStrip = new ContextMenus().Create();

PiHoleApi p = new PiHoleApi();
var p = new PiHoleApi();
p.WaitForClose();


}
Expand Down

0 comments on commit 868b94f

Please sign in to comment.