diff --git a/AppData.cs b/AppData.cs index 2afc816..3eed7c9 100644 --- a/AppData.cs +++ b/AppData.cs @@ -38,7 +38,8 @@ namespace SafeBagDE70 public static string BaseURL = ""; public static int TenantId = 0; public static bool IsInternetOk = false; - static Task? resendTask = null; + static Task? resendTaskHistories = null; + static Task? resendTaskEvents = null; static Task? commandTask = null; public static DeviceStatus? _DeviceStatus = null; public static DeviceStatus? DeviceStatus @@ -265,13 +266,19 @@ namespace SafeBagDE70 return str.Trim(','); } public static void ResendData() + { + ResendDepositHistories(); + ResendEvents(); + } + + private static void ResendDepositHistories() { if (!AppData.IsInTransaction && AppData.IsInternetOk) { - if (resendTask == null || resendTask.IsCompleted) + if (resendTaskHistories == null || resendTaskHistories.IsCompleted) { - resendTask = Task.Run(async () => + resendTaskHistories = Task.Run(async () => { try { @@ -289,33 +296,86 @@ namespace SafeBagDE70 if (bResult) { item.IsSent = true; - context.Update(item); + context.Histories.Update(item); context.SaveChanges(); - Log.Information("Resend: " + item.Data + " successfully"); + Log.Information("ResendHistories: " + item.Data + " successfully"); } else { - Log.Error("Resend: " + item.Data + " failed"); + Log.Error("ResendHistories: " + item.Data + " failed"); } } } } catch (Exception ex) { - Log.Fatal(ex, "Resend failed"); + Log.Fatal(ex, "ResendHistories failed"); } }); } else { - Log.Information("Previous resend are running"); + Log.Information("Previous ResendHistories are running"); } } else { - Log.Error($"Resend could not start IsInTransaction: {AppData.IsInTransaction} IsInternetOk: {AppData.IsInternetOk}"); + Log.Information($"ResendHistories could not start IsInTransaction: {AppData.IsInTransaction} IsInternetOk: {AppData.IsInternetOk}"); } } + private static void ResendEvents() + { + if (!AppData.IsInTransaction && AppData.IsInternetOk) + { + + if (resendTaskEvents == null || resendTaskEvents.IsCompleted) + { + resendTaskEvents = Task.Run(async () => + { + try + { + using (var context = new CDMDbContext()) + { + var startTime = DateTime.Now.Date.AddDays(-2).Date; + var unsentHist = context.DeviceEvents + .Where(x => x.DeviceTime >= startTime) + .Where(x => ((x.IsSent == null || x.IsSent == false))) + .ToList(); + foreach (var item in unsentHist) + { + var data2Send = JsonConvert.DeserializeObject(item.Data); + var bResult = await APIModels.TaskSendEvent(data2Send); + if (bResult) + { + item.IsSent = true; + context.DeviceEvents.Update(item); + context.SaveChanges(); + Log.Information("ResendEvents: " + item.Data + " successfully"); + } + else + { + Log.Error("ResendEvents: " + item.Data + " failed"); + } + } + } + } + catch (Exception ex) + { + Log.Fatal(ex, "ResendEvents failed"); + } + }); + } + else + { + Log.Information("Previous ResendEvents are running"); + } + } + else + { + Log.Information($"ResendEvents could not start IsInTransaction: {AppData.IsInTransaction} IsInternetOk: {AppData.IsInternetOk}"); + } + } + public static void GetandExecuteCommands() { if (IsInternetOk && (commandTask == null || commandTask.IsCompleted))