Skip to content

Commit

Permalink
Command/Collect Now Actions
Browse files Browse the repository at this point in the history
Keep alive and reconfigure events.
  • Loading branch information
dasgarner committed Nov 6, 2015
1 parent 668f69c commit 226df66
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 6 deletions.
31 changes: 27 additions & 4 deletions Action/XmrSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class XmrSubscriber
/// </summary>
public DateTime LastHeartBeat = DateTime.MinValue;

// Events
public delegate void OnCollectNowActionDelegate();
public event OnCollectNowActionDelegate OnCollectNowAction;

/// <summary>
/// Client Hardware key
/// </summary>
Expand Down Expand Up @@ -107,10 +111,29 @@ public void Run()
continue;
}

// Decide what to do with the message

// See what we need to do with this message
Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Message: " + opened), LogType.Error.ToString());
// Decide what to do with the message, probably raise events according to the type of message we have
switch (action.action)
{
case "commandAction":

// Create a schedule command out of the message
Dictionary<string, string> obj = JsonConvert.DeserializeObject<Dictionary<string, string>>(opened);
ScheduleCommand command = new ScheduleCommand();
string code;
obj.TryGetValue("commandCode", out code);
command.Code = code;

new Thread(new ThreadStart(command.Run)).Start();
break;

case "collectNow":
OnCollectNowAction();
break;

default:
Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unknown Message: " + action.action), LogType.Info.ToString());
break;
}
}
catch (Exception ex)
{
Expand Down
76 changes: 76 additions & 0 deletions Logic/Schedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ public Schedule(string scheduleLocation, ref CacheManager cacheManager, ref Clie

// Create a Register Agent
_registerAgent = new RegisterAgent();
_registerAgent.OnXmrReconfigure += _registerAgent_OnXmrReconfigure;
_registerAgentThread = new Thread(new ThreadStart(_registerAgent.Run));
_registerAgentThread.Name = "RegisterAgentThread";

// Create a schedule manager
_scheduleManager = new ScheduleManager(_cacheManager, scheduleLocation);
_scheduleManager.OnNewScheduleAvailable += new ScheduleManager.OnNewScheduleAvailableDelegate(_scheduleManager_OnNewScheduleAvailable);
_scheduleManager.OnRefreshSchedule += new ScheduleManager.OnRefreshScheduleDelegate(_scheduleManager_OnRefreshSchedule);
_scheduleManager.OnScheduleManagerCheckComplete += _scheduleManager_OnScheduleManagerCheckComplete;
_scheduleManager.ClientInfoForm = _clientInfoForm;

// Create a schedule manager thread
Expand Down Expand Up @@ -168,6 +170,9 @@ public Schedule(string scheduleLocation, ref CacheManager cacheManager, ref Clie
_xmrSubscriber = new XmrSubscriber();
_xmrSubscriber.HardwareKey = _hardwareKey;
_xmrSubscriber.ClientInfoForm = _clientInfoForm;
_xmrSubscriber.OnCollectNowAction += _xmrSubscriber_OnCollectNowAction;

// Thread start
_xmrSubscriberThread = new Thread(new ThreadStart(_xmrSubscriber.Run));
_xmrSubscriberThread.Name = "XmrSubscriber";
}
Expand Down Expand Up @@ -221,6 +226,77 @@ void _scheduleManager_OnRefreshSchedule()
_layoutSchedule = _scheduleManager.CurrentSchedule;
}

/// <summary>
/// Schedule Manager has completed a cycle
/// </summary>
void _scheduleManager_OnScheduleManagerCheckComplete()
{
try
{
// See if XMR should be running
if (!string.IsNullOrEmpty(ApplicationSettings.Default.XmrNetworkAddress) && _xmrSubscriber.LastHeartBeat != DateTime.MinValue)
{
// Check to see if the last update date was over 5 minutes ago
if (_xmrSubscriber.LastHeartBeat < DateTime.Now.AddSeconds(-90))
{
// Reconfigure it
_registerAgent_OnXmrReconfigure();
}
}
}
catch (Exception e)
{
Trace.WriteLine(new LogMessage("Schedule - OnScheduleManagerCheckComplete", "Error = " + e.Message), LogType.Error.ToString());
}
}

/// <summary>
/// XMR Reconfigure
/// </summary>
void _registerAgent_OnXmrReconfigure()
{
try
{
// Stop and start the XMR thread
if (_xmrSubscriberThread != null && _xmrSubscriberThread.IsAlive)
{
_xmrSubscriberThread.Abort();
}
}
catch (Exception e)
{
Trace.WriteLine(new LogMessage("Schedule - OnXmrReconfigure", "Unable to abort Subscriber. " + e.Message), LogType.Error.ToString());
}

try
{
// Reassert the hardware key, incase its changed at all
_xmrSubscriber.HardwareKey = _hardwareKey;

// Start the thread again
_xmrSubscriberThread = new Thread(new ThreadStart(_xmrSubscriber.Run));
_xmrSubscriberThread.Name = "XmrSubscriber";

_xmrSubscriberThread.Start();
}
catch (Exception e)
{
Trace.WriteLine(new LogMessage("Schedule - OnXmrReconfigure", "Unable to start Subscriber. " + e.Message), LogType.Error.ToString());
}
}

/// <summary>
/// Collect Now Action
/// </summary>
void _xmrSubscriber_OnCollectNowAction()
{
// Run all of the various agents
_registerAgent.WakeUp();
_scheduleAgent.WakeUp();
_requiredFilesAgent.WakeUp();
_logAgent.WakeUp();
}

/// <summary>
/// Moves the layout on
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions Logic/ScheduleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class ScheduleManager
public delegate void OnRefreshScheduleDelegate();
public event OnRefreshScheduleDelegate OnRefreshSchedule;

// Event for Subscriber inactive
public delegate void OnScheduleManagerCheckCompleteDelegate();
public event OnScheduleManagerCheckCompleteDelegate OnScheduleManagerCheckComplete;

// Member Varialbes
private string _location;
private Collection<LayoutSchedule> _layoutSchedule;
Expand Down Expand Up @@ -219,6 +223,9 @@ public void Run()
}
}

// Completed this check
OnScheduleManagerCheckComplete();

// Sleep this thread for 10 seconds
_manualReset.WaitOne(10 * 1000);
}
Expand Down
4 changes: 2 additions & 2 deletions XiboClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@
<Compile Include="Logic\BlackList.cs" />
<Compile Include="Logic\CacheManager.cs" />
<Compile Include="Action\Command.cs" />
<Compile Include="Action\CommandRs232.cs" />
<Compile Include="Action\CommandSchedule.cs" />
<Compile Include="Action\Rs232Command.cs" />
<Compile Include="Action\ScheduleCommand.cs" />
<Compile Include="Logic\KeyInterceptor.cs" />
<Compile Include="Logic\KeyStore.cs" />
<Compile Include="Logic\MouseInterceptor.cs" />
Expand Down
Binary file modified XiboClient.v11.suo
Binary file not shown.
8 changes: 8 additions & 0 deletions XmdsAgents/LogAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class LogAgent
private bool _forceStop = false;
private ManualResetEvent _manualReset = new ManualResetEvent(false);

/// <summary>
/// Wake Up
/// </summary>
public void WakeUp()
{
_manualReset.Set();
}

/// <summary>
/// Stops the thread
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions XmdsAgents/RegisterAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ class RegisterAgent
private bool _forceStop = false;
private ManualResetEvent _manualReset = new ManualResetEvent(false);

// Events
public delegate void OnXmrReconfigureDelegate();
public event OnXmrReconfigureDelegate OnXmrReconfigure;

/// <summary>
/// Wake Up
/// </summary>
public void WakeUp()
{
_manualReset.Set();
}

/// <summary>
/// Stops the thread
/// </summary>
Expand Down Expand Up @@ -72,6 +84,9 @@ public void Run()
xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds;
xmds.UseDefaultCredentials = false;

// Store the XMR address
string xmrAddress = ApplicationSettings.Default.XmrNetworkAddress;

RegisterAgent.ProcessRegisterXml(xmds.RegisterDisplay(
ApplicationSettings.Default.ServerKey,
key.Key,
Expand All @@ -93,6 +108,12 @@ public void Run()
ApplicationSettings.Default.ScreenShotRequested = false;
ScreenShot.TakeAndSend();
}

// Has the XMR address changed?
if (xmrAddress != ApplicationSettings.Default.XmrNetworkAddress)
{
OnXmrReconfigure();
}
}
}
catch (WebException webEx)
Expand Down
8 changes: 8 additions & 0 deletions XmdsAgents/RequiredFilesAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ public RequiredFilesAgent()
_requiredFiles = new RequiredFiles();
}

/// <summary>
/// Wake Up
/// </summary>
public void WakeUp()
{
_manualReset.Set();
}

/// <summary>
/// Stops the thread
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions XmdsAgents/ScheduleAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public ClientInfo ClientInfoForm
}
private ClientInfo _clientInfoForm;

/// <summary>
/// Wake Up
/// </summary>
public void WakeUp()
{
_manualReset.Set();
}

/// <summary>
/// Stops the thread
/// </summary>
Expand Down

0 comments on commit 226df66

Please sign in to comment.