Skip to content

Commit

Permalink
Added Logging. Improved update check.
Browse files Browse the repository at this point in the history
  • Loading branch information
sabaatworld committed Oct 18, 2017
1 parent ed7e0a3 commit 8575479
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 92 deletions.
34 changes: 26 additions & 8 deletions HyperionScreenCap/ApiServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,49 @@
using Grapevine.Server;
using Grapevine.Server.Attributes;
using Grapevine.Shared;
using log4net;

namespace HyperionScreenCap
{
class ApiServer
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(ApiServer));

private RestServer _server;

public void StartServer(string hostname, string port)
{
try
{
if (_server == null)
if ( _server == null )
{
LOG.Info($"Starting API server: {hostname}:{port}");
_server = new RestServer
{
Host = hostname,
Port = port
};

_server.Start();
LOG.Info("API server started");
}
}
catch (Exception){
catch ( Exception ex )
{
LOG.Error("Failed to start API server", ex);
}
}

public void StopServer()
{
LOG.Info("Stopping API server");
_server?.Stop();
LOG.Info("API server stopped");
}

public void RestartServer(string hostname, string port)
{
LOG.Info("Restarting API server");
StopServer();
StartServer(hostname, port);
}
Expand All @@ -46,29 +56,32 @@ public class Resources
[RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/API")]
public IHttpContext API(IHttpContext context)
{
LOG.Info("API server command received");
context.Response.ContentType = ContentType.TEXT;
string responseText = "No valid API command received.";
string command = context.Request.QueryString["command"] ?? "";
string force = context.Request.QueryString["force"] ?? "false";

if (!string.IsNullOrEmpty(command))
if ( !string.IsNullOrEmpty(command) )
{
LOG.Info($"Processing API command: {command}");
// Only process valid commands
if (command == "ON" || command == "OFF")
if ( command == "ON" || command == "OFF" )
{

// Check for deactivate API between certain times
if (SettingsManager.ApiExcludedTimesEnabled && force.ToLower() == "false")
if ( SettingsManager.ApiExcludedTimesEnabled && force.ToLower() == "false" )
{
if ((DateTime.Now.TimeOfDay >= SettingsManager.ApiExcludeTimeStart.TimeOfDay &&
if ( (DateTime.Now.TimeOfDay >= SettingsManager.ApiExcludeTimeStart.TimeOfDay &&
DateTime.Now.TimeOfDay <= SettingsManager.ApiExcludeTimeEnd.TimeOfDay) ||
((SettingsManager.ApiExcludeTimeStart.TimeOfDay > SettingsManager.ApiExcludeTimeEnd.TimeOfDay) &&
((DateTime.Now.TimeOfDay <= SettingsManager.ApiExcludeTimeStart.TimeOfDay &&
DateTime.Now.TimeOfDay <= SettingsManager.ApiExcludeTimeEnd.TimeOfDay) ||
(DateTime.Now.TimeOfDay >= SettingsManager.ApiExcludeTimeStart.TimeOfDay &&
DateTime.Now.TimeOfDay >= SettingsManager.ApiExcludeTimeEnd.TimeOfDay))))
DateTime.Now.TimeOfDay >= SettingsManager.ApiExcludeTimeEnd.TimeOfDay))) )
{
responseText = "API exclude times enabled and within time range.";
LOG.Info($"Sending response: {responseText}");
context.Response.SendResponse(responseText);
return context;
}
Expand All @@ -78,11 +91,16 @@ public IHttpContext API(IHttpContext context)
responseText = $"API command {command} completed successfully.";
}

if (command == "STATE")
if ( command == "STATE" )
{
responseText = $"{MainForm.IsScreenCaptureRunning()}";
}
}
else
{
LOG.Warn("API Command Empty / Invalid");
}
LOG.Info($"Sending response: {responseText}");
context.Response.SendResponse(responseText);
return context;
}
Expand Down
21 changes: 12 additions & 9 deletions HyperionScreenCap/Capture/Dx9ScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
using HyperionScreenCap.Capture;
using SlimDX;
using System.Threading;
using log4net;

namespace HyperionScreenCap
{
public class DX9ScreenCapture : IScreenCapture
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(DX9ScreenCapture));

private readonly Device _device;
private Direct3D _direct3D;
private int _monitorIndex;
Expand Down Expand Up @@ -91,29 +94,29 @@ private static int GetMonitorIndex(int monitorIndex)
var monitorArray = DisplayMonitor.EnumerateMonitors();

// For anything other than index 0 (first screen) we do a lookup in monitor array
if (monitorIndex == 0)
if ( monitorIndex == 0 )
{
Debug.WriteLine($"Monitor index is 0, skipping lookup and using ==> device: {monitorArray[monitorIndex].DeviceName} | IsPrimary: {monitorArray[monitorIndex].IsPrimary} | Handle: {monitorArray[monitorIndex].Handle}");
LOG.Info($"Monitor index is 0, skipping lookup and using ==> device: {monitorArray[monitorIndex].DeviceName} | IsPrimary: {monitorArray[monitorIndex].IsPrimary} | Handle: {monitorArray[monitorIndex].Handle}");
return monitorIndex;
}

// If we have only 1 monitor and monitor index is set higher fallback to first monitor
if (monitorArray.Count() == 1 && monitorIndex > 0)
if ( monitorArray.Count() == 1 && monitorIndex > 0 )
return 0;

if (monitorArray.Any())
if ( monitorArray.Any() )
{
foreach (var monitor in monitorArray)
foreach ( var monitor in monitorArray )
{
Debug.WriteLine($"Found ==> device: {monitor.DeviceName} | IsPrimary: {monitor.IsPrimary} | Handle: {monitor.Handle}");
LOG.Info($"Found ==> device: {monitor.DeviceName} | IsPrimary: {monitor.IsPrimary} | Handle: {monitor.Handle}");
var monitorShortname = monitor.DeviceName.Replace(@"\\.\DISPLAY", string.Empty);
var dmMonitorIndex = 0;
bool isdValidMonitorIndex = int.TryParse(monitorShortname, out dmMonitorIndex);
if (isdValidMonitorIndex)
if ( isdValidMonitorIndex )
{
if (dmMonitorIndex == monitorIndex)
if ( dmMonitorIndex == monitorIndex )
{
Debug.WriteLine($"Using ==> device: {monitor.DeviceName} | IsPrimary: {monitor.IsPrimary} | Handle: {monitor.Handle}");
LOG.Info($"Using ==> device: {monitor.DeviceName} | IsPrimary: {monitor.IsPrimary} | Handle: {monitor.Handle}");
return dmMonitorIndex;
}
}
Expand Down
5 changes: 5 additions & 0 deletions HyperionScreenCap/Config/AppConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ class AppConstants
public static string DEBUG_IMAGE_FILE_NAME = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
+ Path.DirectorySeparatorChar + "hyperion-capture-debug.png";

/// <summary>
/// Name of the application's log file.
/// </summary>
public static string LOG_FILE_NAME = "hyperion-screen-capture.log";

}
}
8 changes: 6 additions & 2 deletions HyperionScreenCap/Config/SettingsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HyperionScreenCap.Model;
using log4net;
using System;
using System.Configuration;
using System.Windows.Forms;
Expand All @@ -7,8 +8,7 @@ namespace HyperionScreenCap
{
internal static class SettingsManager
{
private static readonly Configuration Config =
ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
private static readonly ILog LOG = LogManager.GetLogger(typeof(SettingsManager));

// Generic
public static string HyperionServerIp;
Expand Down Expand Up @@ -42,6 +42,7 @@ internal static class SettingsManager

public static void SaveSettings()
{
LOG.Info("Saving settings to user.config");
Properties.Settings.Default.hyperionServerIP = HyperionServerIp;
Properties.Settings.Default.hyperionServerPort = HyperionServerPort;
Properties.Settings.Default.hyperionMessagePriority = HyperionMessagePriority;
Expand All @@ -67,10 +68,12 @@ public static void SaveSettings()
Properties.Settings.Default.dx11MonitorIndex = Dx11MonitorIndex;
Properties.Settings.Default.checkUpdateOnStartup = CheckUpdateOnStartup;
Properties.Settings.Default.Save();
LOG.Info("Settings saved to user.config");
}

public static void LoadSetttings()
{
LOG.Info("Loading settings from user.config");
HyperionServerIp = Properties.Settings.Default.hyperionServerIP;
HyperionServerPort = Properties.Settings.Default.hyperionServerPort;
HyperionMessagePriority = Properties.Settings.Default.hyperionMessagePriority;
Expand All @@ -95,6 +98,7 @@ public static void LoadSetttings()
Dx11AdapterIndex = Properties.Settings.Default.dx11AdapterIndex;
Dx11MonitorIndex = Properties.Settings.Default.dx11MonitorIndex;
CheckUpdateOnStartup = Properties.Settings.Default.checkUpdateOnStartup;
LOG.Info("Loaded settings from user.config");
}
}
}
Loading

0 comments on commit 8575479

Please sign in to comment.