2023-02-20 12:15:08 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2023-02-19 02:43:42 +00:00
|
|
|
|
|
2024-08-19 10:15:54 +00:00
|
|
|
|
//using System.Reactive.Linq;
|
|
|
|
|
|
|
|
|
|
namespace ServiceLib.Handler
|
2023-02-19 02:43:42 +00:00
|
|
|
|
{
|
2024-08-19 10:15:54 +00:00
|
|
|
|
public class ProfileExHandler
|
2023-02-19 02:43:42 +00:00
|
|
|
|
{
|
2023-02-19 05:34:22 +00:00
|
|
|
|
private static readonly Lazy<ProfileExHandler> _instance = new(() => new());
|
2024-04-30 06:44:02 +00:00
|
|
|
|
private ConcurrentBag<ProfileExItem> _lstProfileEx = [];
|
2023-02-20 12:15:08 +00:00
|
|
|
|
private Queue<string> _queIndexIds = new();
|
2023-02-19 02:43:42 +00:00
|
|
|
|
public static ProfileExHandler Instance => _instance.Value;
|
2025-01-03 07:02:31 +00:00
|
|
|
|
private static readonly string _tag = "ProfileExHandler";
|
2023-02-19 02:43:42 +00:00
|
|
|
|
|
|
|
|
|
public ProfileExHandler()
|
|
|
|
|
{
|
2024-10-25 09:41:01 +00:00
|
|
|
|
//Init();
|
2024-10-22 09:50:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 09:41:01 +00:00
|
|
|
|
public async Task Init()
|
2024-10-22 09:50:42 +00:00
|
|
|
|
{
|
|
|
|
|
await InitData();
|
2025-01-15 09:26:15 +00:00
|
|
|
|
_ = Task.Run(async () =>
|
2023-02-20 12:15:08 +00:00
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2024-04-30 06:44:02 +00:00
|
|
|
|
await Task.Delay(1000 * 600);
|
2024-10-25 09:41:01 +00:00
|
|
|
|
await SaveQueueIndexIds();
|
2023-02-20 12:15:08 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 09:41:01 +00:00
|
|
|
|
public async Task<ConcurrentBag<ProfileExItem>> GetProfileExs()
|
|
|
|
|
{
|
2025-01-15 09:26:15 +00:00
|
|
|
|
return await Task.FromResult(_lstProfileEx);
|
2024-10-25 09:41:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 09:50:42 +00:00
|
|
|
|
private async Task InitData()
|
2024-04-30 06:44:02 +00:00
|
|
|
|
{
|
2024-10-20 03:51:05 +00:00
|
|
|
|
await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");
|
2024-04-30 06:44:02 +00:00
|
|
|
|
|
2024-10-21 01:45:33 +00:00
|
|
|
|
_lstProfileEx = new(await SQLiteHelper.Instance.TableAsync<ProfileExItem>().ToListAsync());
|
2024-04-30 06:44:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 12:15:08 +00:00
|
|
|
|
private void IndexIdEnqueue(string indexId)
|
|
|
|
|
{
|
2024-09-17 08:52:41 +00:00
|
|
|
|
if (Utils.IsNotEmpty(indexId) && !_queIndexIds.Contains(indexId))
|
2023-02-20 12:15:08 +00:00
|
|
|
|
{
|
|
|
|
|
_queIndexIds.Enqueue(indexId);
|
|
|
|
|
}
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-20 03:51:05 +00:00
|
|
|
|
private async Task SaveQueueIndexIds()
|
2024-04-30 06:44:02 +00:00
|
|
|
|
{
|
|
|
|
|
var cnt = _queIndexIds.Count;
|
|
|
|
|
if (cnt > 0)
|
|
|
|
|
{
|
2024-10-21 01:45:33 +00:00
|
|
|
|
var lstExists = await SQLiteHelper.Instance.TableAsync<ProfileExItem>().ToListAsync();
|
2024-04-30 06:44:02 +00:00
|
|
|
|
List<ProfileExItem> lstInserts = [];
|
|
|
|
|
List<ProfileExItem> lstUpdates = [];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < cnt; i++)
|
|
|
|
|
{
|
|
|
|
|
var id = _queIndexIds.Dequeue();
|
2024-10-23 09:00:33 +00:00
|
|
|
|
var item = lstExists.FirstOrDefault(t => t.IndexId == id);
|
|
|
|
|
var itemNew = _lstProfileEx?.FirstOrDefault(t => t.IndexId == id);
|
2024-04-30 06:44:02 +00:00
|
|
|
|
if (itemNew is null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item is not null)
|
|
|
|
|
{
|
|
|
|
|
lstUpdates.Add(itemNew);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lstInserts.Add(itemNew);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (lstInserts.Count() > 0)
|
2024-10-20 03:51:05 +00:00
|
|
|
|
await SQLiteHelper.Instance.InsertAllAsync(lstInserts);
|
2024-04-30 06:44:02 +00:00
|
|
|
|
|
|
|
|
|
if (lstUpdates.Count() > 0)
|
2024-10-20 03:51:05 +00:00
|
|
|
|
await SQLiteHelper.Instance.UpdateAllAsync(lstUpdates);
|
2024-04-30 06:44:02 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2024-04-30 06:44:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-22 08:03:25 +00:00
|
|
|
|
private void AddProfileEx(string indexId, ref ProfileExItem? profileEx)
|
2023-02-19 02:43:42 +00:00
|
|
|
|
{
|
|
|
|
|
profileEx = new()
|
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
IndexId = indexId,
|
|
|
|
|
Delay = 0,
|
|
|
|
|
Speed = 0,
|
|
|
|
|
Sort = 0
|
2023-02-19 02:43:42 +00:00
|
|
|
|
};
|
|
|
|
|
_lstProfileEx.Add(profileEx);
|
2023-02-20 12:15:08 +00:00
|
|
|
|
IndexIdEnqueue(indexId);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-20 03:51:05 +00:00
|
|
|
|
public async Task ClearAll()
|
2023-02-19 02:43:42 +00:00
|
|
|
|
{
|
2024-10-20 03:51:05 +00:00
|
|
|
|
await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileExItem ");
|
2023-02-19 02:43:42 +00:00
|
|
|
|
_lstProfileEx = new();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-20 03:51:05 +00:00
|
|
|
|
public async Task SaveTo()
|
2023-02-19 02:43:42 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-20 03:51:05 +00:00
|
|
|
|
await SaveQueueIndexIds();
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-03 07:02:31 +00:00
|
|
|
|
Logging.SaveLog(_tag, ex);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 12:15:08 +00:00
|
|
|
|
public void SetTestDelay(string indexId, string delayVal)
|
2023-02-19 02:43:42 +00:00
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
var profileEx = _lstProfileEx.FirstOrDefault(t => t.IndexId == indexId);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
if (profileEx == null)
|
|
|
|
|
{
|
|
|
|
|
AddProfileEx(indexId, ref profileEx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int.TryParse(delayVal, out int delay);
|
2024-10-23 09:00:33 +00:00
|
|
|
|
profileEx.Delay = delay;
|
2023-02-20 12:15:08 +00:00
|
|
|
|
IndexIdEnqueue(indexId);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 12:15:08 +00:00
|
|
|
|
public void SetTestSpeed(string indexId, string speedVal)
|
2023-02-19 02:43:42 +00:00
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
var profileEx = _lstProfileEx.FirstOrDefault(t => t.IndexId == indexId);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
if (profileEx == null)
|
|
|
|
|
{
|
|
|
|
|
AddProfileEx(indexId, ref profileEx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decimal.TryParse(speedVal, out decimal speed);
|
2024-10-23 09:00:33 +00:00
|
|
|
|
profileEx.Speed = speed;
|
2023-02-20 12:15:08 +00:00
|
|
|
|
IndexIdEnqueue(indexId);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetSort(string indexId, int sort)
|
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
var profileEx = _lstProfileEx.FirstOrDefault(t => t.IndexId == indexId);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
if (profileEx == null)
|
|
|
|
|
{
|
|
|
|
|
AddProfileEx(indexId, ref profileEx);
|
|
|
|
|
}
|
2024-10-23 09:00:33 +00:00
|
|
|
|
profileEx.Sort = sort;
|
2023-02-20 12:15:08 +00:00
|
|
|
|
IndexIdEnqueue(indexId);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetSort(string indexId)
|
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
var profileEx = _lstProfileEx.FirstOrDefault(t => t.IndexId == indexId);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
if (profileEx == null)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-10-23 09:00:33 +00:00
|
|
|
|
return profileEx.Sort;
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
|
2023-02-19 02:43:42 +00:00
|
|
|
|
public int GetMaxSort()
|
|
|
|
|
{
|
|
|
|
|
if (_lstProfileEx.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-10-23 09:00:33 +00:00
|
|
|
|
return _lstProfileEx.Max(t => t == null ? 0 : t.Sort);
|
2023-02-19 02:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 12:49:36 +00:00
|
|
|
|
}
|