add resend Event
This commit is contained in:
+69
-9
@@ -38,7 +38,8 @@ namespace SafeBagDE70
|
|||||||
public static string BaseURL = "";
|
public static string BaseURL = "";
|
||||||
public static int TenantId = 0;
|
public static int TenantId = 0;
|
||||||
public static bool IsInternetOk = false;
|
public static bool IsInternetOk = false;
|
||||||
static Task? resendTask = null;
|
static Task? resendTaskHistories = null;
|
||||||
|
static Task? resendTaskEvents = null;
|
||||||
static Task? commandTask = null;
|
static Task? commandTask = null;
|
||||||
public static DeviceStatus? _DeviceStatus = null;
|
public static DeviceStatus? _DeviceStatus = null;
|
||||||
public static DeviceStatus? DeviceStatus
|
public static DeviceStatus? DeviceStatus
|
||||||
@@ -265,13 +266,19 @@ namespace SafeBagDE70
|
|||||||
return str.Trim(',');
|
return str.Trim(',');
|
||||||
}
|
}
|
||||||
public static void ResendData()
|
public static void ResendData()
|
||||||
|
{
|
||||||
|
ResendDepositHistories();
|
||||||
|
ResendEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ResendDepositHistories()
|
||||||
{
|
{
|
||||||
if (!AppData.IsInTransaction && AppData.IsInternetOk)
|
if (!AppData.IsInTransaction && AppData.IsInternetOk)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (resendTask == null || resendTask.IsCompleted)
|
if (resendTaskHistories == null || resendTaskHistories.IsCompleted)
|
||||||
{
|
{
|
||||||
resendTask = Task.Run(async () =>
|
resendTaskHistories = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -289,33 +296,86 @@ namespace SafeBagDE70
|
|||||||
if (bResult)
|
if (bResult)
|
||||||
{
|
{
|
||||||
item.IsSent = true;
|
item.IsSent = true;
|
||||||
context.Update(item);
|
context.Histories.Update(item);
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
Log.Information("Resend: " + item.Data + " successfully");
|
Log.Information("ResendHistories: " + item.Data + " successfully");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Log.Error("Resend: " + item.Data + " failed");
|
Log.Error("ResendHistories: " + item.Data + " failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Log.Fatal(ex, "Resend failed");
|
Log.Fatal(ex, "ResendHistories failed");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Log.Information("Previous resend are running");
|
Log.Information("Previous ResendHistories are running");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
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<DeviceStatus?>(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()
|
public static void GetandExecuteCommands()
|
||||||
{
|
{
|
||||||
if (IsInternetOk && (commandTask == null || commandTask.IsCompleted))
|
if (IsInternetOk && (commandTask == null || commandTask.IsCompleted))
|
||||||
|
|||||||
Reference in New Issue
Block a user