private void UpdateStatusLogDatabase(string message, bool success) { try { DataRow log = m_adoDataConnection.RetrieveRow("SELECT * FROM StatusLog WITH (NOLOCK) WHERE DeviceID = {0}", int.Parse(m_deviceRecord["ID"].ToString())); if (success) { if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString() && m_lastFileDownloadedThisSession != string.Empty) { m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastSuccess = {0}, LastFile = {1}, FileDownloadTimestamp = {2} WHERE DeviceID = {3}", DateTime.UtcNow, m_lastFileDownloadedThisSession, DateTime.UtcNow, m_deviceRecord["ID"]); } else if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString() && m_lastFileDownloadedThisSession == string.Empty) { m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastSuccess = {0} WHERE DeviceID = {1}", DateTime.UtcNow, m_deviceRecord["ID"]); } else { m_adoDataConnection.ExecuteNonQuery("INSERT INTO StatusLog (LastSuccess , LastFile, FileDownloadTimestamp, DeviceID) VALUES ({0},{1},{2})", DateTime.UtcNow, m_lastFileDownloadedThisSession, DateTime.UtcNow, m_deviceRecord["ID"]); } if (m_lastFileDownloadedThisSession != string.Empty) { m_adoDataConnection.ExecuteNonQuery("INSERT INTO DownloadedFile (DeviceID , CreationTime, File, FileSize, Timestamp) VALUES ({0},{1},{2})", m_deviceRecord["ID"], DateTime.UtcNow, m_lastFileDownloadedThisSession, 0, DateTime.UtcNow); } int maxDownloadThreshold = Program.OpenMiConfigurationFile.Settings?["systemSettings"]["MaxDownloadThreshold"].ValueAsInt32() ?? 0; if (maxDownloadThreshold > 0) { DateTime timeWindow = DateTime.UtcNow.AddHours(-(Program.OpenMiConfigurationFile.Settings?["systemSettings"]["MaxDownloadThresholdTimeWindow"].ValueAsInt32() ?? 24)); int count = m_adoDataConnection.ExecuteScalar <int>("SELECT COUNT(*) FROM DownloadedFile WHERE Timestamp >= {0} AND DeviceID = {1}", timeWindow, m_deviceRecord["ID"]); if (count > maxDownloadThreshold) { m_adoDataConnection.ExecuteNonQuery("UPDATE Device SET Enabled = 0 WHERE ID = {0}", m_deviceRecord["ID"]); string fail = "Disabled due to excessive file production."; m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastFailure = {0}, Message = {1} WHERE DeviceID = {2}", DateTime.UtcNow, fail, m_deviceRecord["ID"]); Program.Log($"[{m_deviceRecord["Name"]} Disabled due to excessive file downloads. Setting: {maxDownloadThreshold}; Count: {count}"); } } } else { if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString()) { m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastFailure = {0}, Message = {1} WHERE DeviceID = {2}", DateTime.UtcNow, message, m_deviceRecord["ID"]); } else { m_adoDataConnection.ExecuteNonQuery("INSERT INTO StatusLog (LastFailure , Message, DeviceID) VALUES ({0},{1},{2})", DateTime.UtcNow, message, m_deviceRecord["ID"]); } } } catch (Exception ex) { Program.Log("StatusLog Update (" + m_siteName + ") failed: " + ex.Message, m_tempDirectoryName); } }
public BenRunner(int deviceId, int taskId) { m_adoDataConnection = new AdoDataConnection(Program.OpenMiConfigurationFile.Settings["systemSettings"]["ConnectionString"].Value, Program.OpenMiConfigurationFile.Settings["systemSettings"]["DataProviderString"].Value); try { string taskSettingsString = m_adoDataConnection.ExecuteScalar <string>("Select Settings From ConnectionProfileTask WHERE ID = {0}", taskId); m_connectionProfileTaskSettings = taskSettingsString.ParseKeyValuePairs(); m_deviceRecord = m_adoDataConnection.RetrieveRow("Select * From Device WHERE ID = {0}", deviceId); Dictionary <string, string> deviceConnection = m_deviceRecord["connectionString"].ToString().ParseKeyValuePairs(); m_connectionProfile = m_adoDataConnection.RetrieveRow("SELECT * FROM connectionProfile WHERE ID = {0}", deviceConnection["connectionProfileID"]); m_folder = m_deviceRecord["OriginalSource"].ToString(); m_ipAddress = deviceConnection["connectionUserName"].Split('&')[0]; m_localPath = m_connectionProfileTaskSettings["localPath"]; m_siteName = m_deviceRecord["Name"].ToString(); m_serialNumber = deviceConnection["connectionUserName"].Split('&')[1]; string tempDirectory = System.IO.Path.GetTempPath(); System.IO.Directory.CreateDirectory(tempDirectory + "\\BenDownloader\\" + m_siteName); m_tempDirectoryName = tempDirectory + "BenDownloader\\" + m_siteName + "\\"; //Console.WriteLine(m_tempDirectoryName); m_fileWatcher = new SafeFileWatcher(m_tempDirectoryName); m_fileWatcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName; m_fileWatcher.Changed += m_fileWatcher_Changed; m_fileWatcher.Created += m_fileWatcher_Changed; m_activityMonitor = new System.Timers.Timer(5000.0D); m_activityMonitor.Elapsed += m_activityMonitor_Elapsed; m_activityMonitor.AutoReset = true; m_lastFileDownloaded = GetLastDownloadedFile(); m_lastFileDownloadedThisSession = ""; } catch (Exception ex) { Program.Log(ex.ToString(), m_tempDirectoryName); } }
private void UpdateStatusLogDatabase(string message, bool success) { try { DataRow log = m_adoDataConnection.RetrieveRow("SELECT * FROM StatusLog WITH (NOLOCK) WHERE DeviceID = {0}", int.Parse(m_deviceRecord["ID"].ToString())); if (success) { if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString() && m_lastFileDownloadedThisSession != "") { m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastSuccess = {0}, LastFile = {1} WHERE DeviceID = {2}", DateTime.UtcNow, m_lastFileDownloadedThisSession, m_deviceRecord["ID"]); } else if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString() && m_lastFileDownloadedThisSession == "") { m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastSuccess = {0} WHERE DeviceID = {1}", DateTime.UtcNow, m_deviceRecord["ID"]); } else { m_adoDataConnection.ExecuteNonQuery("INSERT INTO StatusLog (LastSuccess , LastFile, DeviceID) VALUES ({0},{1},{2})", DateTime.UtcNow, m_lastFileDownloadedThisSession, m_deviceRecord["ID"]); } } else { if (m_deviceRecord["ID"].ToString() == log["DeviceID"].ToString()) { m_adoDataConnection.ExecuteNonQuery("Update StatusLog SET LastFailure = {0}, Message = {1} WHERE DeviceID = {2}", DateTime.UtcNow, message, m_deviceRecord["ID"]); } else { m_adoDataConnection.ExecuteNonQuery("INSERT INTO StatusLog (LastFailure , Message, DeviceID) VALUES ({0},{1},{2})", DateTime.UtcNow, message, m_deviceRecord["ID"]); } } } catch (Exception ex) { Program.Log("StatusLog Update (" + m_siteName + ") failed: " + ex.Message, m_tempDirectoryName); } }