diff --git a/AppData.cs b/AppData.cs index dc52016..6bfaaa3 100644 --- a/AppData.cs +++ b/AppData.cs @@ -42,7 +42,7 @@ namespace SafeBagDE70 { try { - WaitSQLStart(); + CDMSTATE = new CDMSTATE(); DeviceStatus = new DeviceStatus { @@ -71,77 +71,52 @@ namespace SafeBagDE70 private static void InitUsers() { - try + int retryCount = 0; + int maxRetries = 20; // Max retries before giving up + int delay = 5000; // Delay between retries (5 seconds) + while (retryCount < maxRetries) { - - customerAccounts = new List(); - using (var context = new CDMDbContext()) + try { - var customers = context.Customers; - foreach (var customer in customers) + + customerAccounts = new List(); + using (var context = new CDMDbContext()) { - var ca = new CustomerAccount + var customers = context.Customers; + foreach (var customer in customers) { - Name = customer.TenantName, - CustomerId = customer.AccountNumber, - QRCode = customer.AccountNumber, - PINCode = customer.PinCode - }; - customerAccounts.Add(ca); + var ca = new CustomerAccount + { + Name = customer.TenantName, + CustomerId = customer.AccountNumber, + QRCode = customer.AccountNumber, + PINCode = customer.PinCode + }; + customerAccounts.Add(ca); + } } + CurrentCustomerAccount = customerAccounts[0]; + Log.Information("InitUsers thành công"); } - CurrentCustomerAccount = customerAccounts[0]; - } - catch(Exception ex) - { - Log.Fatal(ex, "InitUsers: " + ex.Message); - if (DeviceStatus != null) + catch (Exception ex) { - DeviceStatus.Description = "Exception in InitUsers: " + ex.Message; - APIModels.SendEvent(DeviceStatus); + retryCount++; + + if (retryCount >= maxRetries) + { + Log.Fatal(ex, "InitUsers: " + ex.Message); + if (DeviceStatus != null) + { + DeviceStatus.Description = "Exception in InitUsers: " + ex.Message; + APIModels.SendEvent(DeviceStatus); + } + break; + } + // Wait for the next retry + Thread.Sleep(delay); } } - - } - private static void WaitSQLStart() - { - string serviceName = "MSSQLSERVER"; // Default SQL Server service name, change if necessary - - // Create a ServiceController instance for the SQL Server service - ServiceController serviceController = new ServiceController(serviceName); - - try - { - Log.Information("Checking the status of the SQL Server service..."); - - // Loop to wait until the service is running - while (serviceController.Status != ServiceControllerStatus.Running) - { - //if (serviceController.Status == ServiceControllerStatus.Stopped) - //{ - // Log.Information("SQL Server service is stopped. Starting it..."); - // serviceController.Start(); - //} - - // Refresh the controller to get the updated status - serviceController.Refresh(); - - Log.Information($"Current status: {serviceController.Status}"); - Thread.Sleep(5000); // Wait for 5 seconds before checking again - } - - Log.Information("SQL Server service is running successfully."); - } - catch (InvalidOperationException ex) - { - Console.WriteLine($"Error: Could not access the service '{serviceName}'. {ex.Message}"); - } - catch (Exception ex) - { - Console.WriteLine($"Error: {ex.Message}"); - } } - public static CDMSTATE? CDMSTATE = null; private static DateTime CutOffTime = new DateTime(1, 1, 1, 12, 0, 0); // 12:00 noon as cut-off time