//private static MonitorWebsite upnpSite = new MonitorWebsite(); static MonitoringManager() { StatusLED.SetColor(true, false, true); #if DEBUG _intervalSeconds = 10; #endif if (_lastUpdate == DateTime.MinValue || DateTime.Now.Subtract(_lastUpdate).TotalSeconds > _intervalSeconds) { //TODO: Update code for monitoring manager. } UpdateConnections(); BeginLogging(); StatusLED.SetColor(false, false, true); }
private static void LogLoop() { DateTime lastRun = DateTime.Now.Subtract(TimeSpan.FromSeconds(_intervalSeconds + 1)); while (_runLogging) { if (DateTime.Now.Subtract(lastRun).TotalSeconds > _intervalSeconds) { //StatusLED.SetColor (false, true, false); lastRun = DateTime.Now; // try // { // UpdateConnections(); // } // catch // { // } if (_connections != null) { var loggableConnections = _connections.Where(c => (c.IsChartable || c.IsInDashboard) && !c.IsJpegFrame); LogWriter.WriteLog(string.Format("Logging values for {0} devices.", loggableConnections.Count())); Type convertibleType = typeof(LoggableConnection <IConvertible>); foreach (ILoggableConnection conn in loggableConnections) { LogWriter.WriteLog(string.Format("Logging value for device {1} with key {0}.", conn.Key, conn.GetType().FullName)); try { IConvertible val = conn.GetAndLogValue(lastRun); //Add the value to the lastValues dictionary if (!_lastValues.ContainsKey(conn.Map.DeviceKey)) { _lastValues.Add(conn.Map.DeviceKey, val); } else { _lastValues [conn.Map.DeviceKey] = val; } } catch (Exception ex) { LogWriter.WriteLog(string.Format("An error was encountered attempting to log a {1} connection for the key {0}", conn.Key, conn.GetType().FullName), ex); } } StatusLED.SetColor(false, false, true); } //Calculate the time to sleep this thread until the next run. TimeSpan sleep = TimeSpan.FromSeconds(_intervalSeconds - 1); if (DateTime.Now.Subtract(lastRun).TotalSeconds <= sleep.TotalSeconds) { sleep = sleep.Subtract(DateTime.Now.Subtract(lastRun)); } else { sleep = TimeSpan.FromSeconds(1); } Thread.Sleep(sleep); } } }
public static void WriteLog(string logText, bool debug, bool error) { var previousLED = StatusLED.GetColor(); if (error) { StatusLED.SetColor(true, false, false); } else if (debug) { StatusLED.SetColor(false, true, true); } var currentLED = StatusLED.GetColor(); string logLine = ""; if (_logFileInfo == null || _logFileInfo.Name != LogFileName()) { _logFileInfo = new FileInfo(LogFileName()); if (_logWriter != null) { _logWriter.Flush(); _logWriter.Close(); } if (!_logFileInfo.Exists) { _logWriter = new StreamWriter(_logFileInfo.Create()); } else { _logWriter = new StreamWriter(_logFileInfo.OpenWrite()); } } if (!error) { if (!debug) { logLine = string.Format("{0} - INFO - {1}", DateTime.Now, logText); } else { logLine = string.Format("{0} - DEBUG - {1}", DateTime.Now, logText); } } else { logLine = string.Format("{0} - ERROR - {1}", DateTime.Now, logText); } _logWriter.WriteLine(logLine); _logWriter.Flush(); Console.WriteLine(logLine); Thread colorThread = new Thread(new ThreadStart(delegate() { Thread.Sleep(500); if (currentLED == StatusLED.GetColor()) { StatusLED.SetColor(previousLED); } })); colorThread.Start(); }