v2rayN/v2rayN/ServiceLib/Handler/ProfileExHandler.cs

183 lines
5.4 KiB
C#
Raw Normal View History

2023-02-20 12:15:08 +00:00
using System.Collections.Concurrent;
2024-08-19 10:15:54 +00:00
//using System.Reactive.Linq;
namespace ServiceLib.Handler
{
2024-08-19 10:15:54 +00:00
public class ProfileExHandler
{
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();
public static ProfileExHandler Instance => _instance.Value;
2025-01-03 07:02:31 +00:00
private static readonly string _tag = "ProfileExHandler";
public ProfileExHandler()
{
//Init();
2024-10-22 09:50:42 +00:00
}
public async Task Init()
2024-10-22 09:50:42 +00:00
{
await InitData();
_ = 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);
await SaveQueueIndexIds();
2023-02-20 12:15:08 +00:00
}
});
}
public async Task<ConcurrentBag<ProfileExItem>> GetProfileExs()
{
return await Task.FromResult(_lstProfileEx);
}
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);
}
}
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();
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)
{
profileEx = new()
{
IndexId = indexId,
Delay = 0,
Speed = 0,
Sort = 0
};
_lstProfileEx.Add(profileEx);
2023-02-20 12:15:08 +00:00
IndexIdEnqueue(indexId);
}
2024-10-20 03:51:05 +00:00
public async Task ClearAll()
{
2024-10-20 03:51:05 +00:00
await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileExItem ");
_lstProfileEx = new();
}
2024-10-20 03:51:05 +00:00
public async Task SaveTo()
{
try
{
2024-10-20 03:51:05 +00:00
await SaveQueueIndexIds();
}
catch (Exception ex)
{
2025-01-03 07:02:31 +00:00
Logging.SaveLog(_tag, ex);
}
}
2023-02-20 12:15:08 +00:00
public void SetTestDelay(string indexId, string delayVal)
{
var profileEx = _lstProfileEx.FirstOrDefault(t => t.IndexId == indexId);
if (profileEx == null)
{
AddProfileEx(indexId, ref profileEx);
}
int.TryParse(delayVal, out int delay);
profileEx.Delay = delay;
2023-02-20 12:15:08 +00:00
IndexIdEnqueue(indexId);
}
2023-02-20 12:15:08 +00:00
public void SetTestSpeed(string indexId, string speedVal)
{
var profileEx = _lstProfileEx.FirstOrDefault(t => t.IndexId == indexId);
if (profileEx == null)
{
AddProfileEx(indexId, ref profileEx);
}
decimal.TryParse(speedVal, out decimal speed);
profileEx.Speed = speed;
2023-02-20 12:15:08 +00:00
IndexIdEnqueue(indexId);
}
public void SetSort(string indexId, int sort)
{
var profileEx = _lstProfileEx.FirstOrDefault(t => t.IndexId == indexId);
if (profileEx == null)
{
AddProfileEx(indexId, ref profileEx);
}
profileEx.Sort = sort;
2023-02-20 12:15:08 +00:00
IndexIdEnqueue(indexId);
}
public int GetSort(string indexId)
{
var profileEx = _lstProfileEx.FirstOrDefault(t => t.IndexId == indexId);
if (profileEx == null)
{
return 0;
}
return profileEx.Sort;
}
2023-04-14 12:49:36 +00:00
public int GetMaxSort()
{
if (_lstProfileEx.Count <= 0)
{
return 0;
}
return _lstProfileEx.Max(t => t == null ? 0 : t.Sort);
}
}
2023-04-14 12:49:36 +00:00
}