/// <summary> /// Runs the agent /// </summary> public void Run() { Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "Thread Started"), LogType.Info.ToString()); while (!_forceStop) { lock (_locker) { try { // If we are restarting, reset _manualReset.Reset(); Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "Thread Woken and Lock Obtained"), LogType.Info.ToString()); _clientInfoForm.ScheduleStatus = "Running: Get Data from Xibo Server"; using (xmds.xmds xmds = new xmds.xmds()) { xmds.Credentials = null; xmds.Url = ApplicationSettings.Default.R23SignageClient_xmds_xmds; xmds.UseDefaultCredentials = false; string scheduleXml = xmds.Schedule(ApplicationSettings.Default.ServerKey, _hardwareKey); // Set the flag to indicate we have a connection to XMDS ApplicationSettings.Default.XmdsLastConnection = DateTime.Now; _clientInfoForm.ScheduleStatus = "Running: Data Received"; // Hash of the result string md5NewSchedule = Hashes.MD5(scheduleXml); string md5CurrentSchedule = Hashes.MD5(ScheduleManager.GetScheduleXmlString(_scheduleLocation)); // Compare the results of the HASH if (md5CurrentSchedule != md5NewSchedule) { Trace.WriteLine(new LogMessage("Schedule Agent - Run", "Received new schedule")); _clientInfoForm.ScheduleStatus = "Running: New Schedule Received"; // Write the result to the schedule xml location ScheduleManager.WriteScheduleXmlToDisk(_scheduleLocation, scheduleXml); // Indicate to the schedule manager that it should read the XML file _scheduleManager.RefreshSchedule = true; } _clientInfoForm.ScheduleStatus = "Sleeping"; } } catch (WebException webEx) { // Increment the quantity of XMDS failures and bail out ApplicationSettings.Default.IncrementXmdsErrorCount(); // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "WebException in Run: " + webEx.Message), LogType.Error.ToString()); _clientInfoForm.ScheduleStatus = "Error: " + webEx.Message; } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString()); _clientInfoForm.ScheduleStatus = "Error. " + ex.Message; } } // Sleep this thread until the next collection interval _manualReset.WaitOne((int)(ApplicationSettings.Default.CollectInterval * ApplicationSettings.Default.XmdsCollectionIntervalFactor() * 1000)); } Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "Thread Stopped"), LogType.Info.ToString()); }
/// <summary> /// Schedule Agent /// </summary> private void scheduleAgent() { try { // If we are restarting, reset _manualReset.Reset(); Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "Thread Woken and Lock Obtained"), LogType.Audit.ToString()); _clientInfoForm.ScheduleStatus = "Running: Get Data from Xibo Server"; using (xmds.xmds xmds = new xmds.xmds()) { xmds.Credentials = null; xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds + "&method=schedule"; xmds.UseDefaultCredentials = false; string scheduleXml = xmds.Schedule(ApplicationSettings.Default.ServerKey, _hardwareKey); // Set the flag to indicate we have a connection to XMDS ApplicationSettings.Default.XmdsLastConnection = DateTime.Now; _clientInfoForm.ScheduleStatus = "Running: Data Received"; // Hash of the result string md5NewSchedule = Hashes.MD5(scheduleXml); string md5CurrentSchedule = Hashes.MD5(ScheduleManager.GetScheduleXmlString(_scheduleLocation)); // Compare the results of the HASH if (md5CurrentSchedule != md5NewSchedule) { Trace.WriteLine(new LogMessage("Schedule Agent - Run", "Received new schedule")); _clientInfoForm.ScheduleStatus = "Running: New Schedule Received"; // Write the result to the schedule xml location ScheduleManager.WriteScheduleXmlToDisk(_scheduleLocation, scheduleXml); // Indicate to the schedule manager that it should read the XML file _scheduleManager.RefreshSchedule = true; } _clientInfoForm.ScheduleStatus = "Sleeping"; } } catch (WebException webEx) { // Increment the quantity of XMDS failures and bail out ApplicationSettings.Default.IncrementXmdsErrorCount(); // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "WebException in Run: " + webEx.Message), LogType.Info.ToString()); _clientInfoForm.ScheduleStatus = "Error: " + webEx.Message; } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString()); _clientInfoForm.ScheduleStatus = "Error. " + ex.Message; } }
/// <summary> /// Schedule Agent /// </summary> private void scheduleAgent() { try { if (!ShouldCheckSchedule()) { ClientInfo.Instance.ScheduleStatus = "Sleeping: last check was not required."; return; } Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "Thread Woken and Lock Obtained"), LogType.Audit.ToString()); ClientInfo.Instance.ScheduleStatus = "Running: Get Data from Xibo Server"; using (xmds.xmds xmds = new xmds.xmds()) { xmds.Credentials = null; xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds + "&method=schedule"; xmds.UseDefaultCredentials = false; string scheduleXml = xmds.Schedule(ApplicationSettings.Default.ServerKey, _hardwareKey); // Set the flag to indicate we have a connection to XMDS ApplicationSettings.Default.XmdsLastConnection = DateTime.Now; ClientInfo.Instance.ScheduleStatus = "Running: Data Received"; // Calculate and store a CRC32 _lastCheckSchedule = Crc32Algorithm.Compute(Encoding.UTF8.GetBytes(scheduleXml)).ToString(); // Hash of the result // TODO: we can probably remove this at some point in the future, given later CMS instances output CRC32's to indicate whether // the schedule has changed. string md5NewSchedule = Hashes.MD5(scheduleXml); string md5CurrentSchedule = Hashes.MD5(ScheduleManager.GetScheduleXmlString(_scheduleLocation)); // Compare the results of the HASH if (md5CurrentSchedule != md5NewSchedule) { Trace.WriteLine(new LogMessage("Schedule Agent - Run", "Received new schedule")); ClientInfo.Instance.ScheduleStatus = "Running: New Schedule Received"; // Write the result to the schedule xml location ScheduleManager.WriteScheduleXmlToDisk(_scheduleLocation, scheduleXml); // Indicate to the schedule manager that it should read the XML file _scheduleManager.RefreshSchedule = true; } ClientInfo.Instance.ScheduleStatus = "Sleeping"; } } catch (WebException webEx) { // Increment the quantity of XMDS failures and bail out ApplicationSettings.Default.IncrementXmdsErrorCount(); // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "WebException in Run: " + webEx.Message), LogType.Info.ToString()); ClientInfo.Instance.ScheduleStatus = "Error: " + webEx.Message; } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("ScheduleAgent - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString()); ClientInfo.Instance.ScheduleStatus = "Error. " + ex.Message; } }