Commit 925e79d0 authored by 潘栩锋's avatar 潘栩锋 🚴

1. 添加 OBJComponets.BufferSQLite 支持写入数据库

parent 3961213e
using FLY.Thick.RemoteHistory;
using FObjBase;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -9,8 +10,9 @@ using System.Threading.Tasks;
namespace FLY.OBJComponents.Common
{
public class FlyData_WarningHistory : IFlyData
public class FlyData_WarningHistory : IFlyData,IDbBase
{
public Int64 ID { get; set; }
/// <summary>
/// 时间
/// </summary>
......
......@@ -19,7 +19,7 @@ namespace FLY.OBJComponents.Common
public class NotifyBufferChangedEventArgs<T> : EventArgs
{
public NotifyBufferChangedAction Action { get; set; }
public IList<T> Items { get; set; }
public IEnumerable<T> Items { get; set; }
public int EndingID { get; set; }
}
......
......@@ -9,5 +9,6 @@ namespace FLY.OBJComponents.IService
public interface IBufferAdd<T> : IBuffer<T>
{
void Add(T t);
void AddRange(IEnumerable<T> t);
}
}
......@@ -75,6 +75,7 @@
<Compile Include="Server.OBJProxy\SyncProp_OBJProxy.cs" />
<Compile Include="Server.OBJProxy\WarningSystem_OBJProxy.cs" />
<Compile Include="Server\Buffer.cs" />
<Compile Include="Server\BufferSQLite.cs" />
<Compile Include="Server\BufferStorage.cs" />
<Compile Include="Server\BufferError.cs" />
<Compile Include="Server\History.cs" />
......
......@@ -77,7 +77,7 @@ namespace FLY.OBJComponents.Server
EndingID = NewestID
});
}
public void AddRange(IList<T> array)
public void AddRange(IEnumerable<T> array)
{
list.AddRange(array);
......
......@@ -12,135 +12,34 @@ using System.Threading.Tasks;
namespace FLY.OBJComponents.Server
{
public class BufferError : IBufferAdd<FlyData_WarningHistory>
public class BufferError : BufferSQLite<FlyData_WarningHistory, DB_Error>
{
IErrorDBModel dBModel;
public int NewestID { get; private set; }
public int Count { get; private set; }
public int Capacity { get; set; } = int.MaxValue;
public event NotifyBufferChangedEventHandler<FlyData_WarningHistory> BufferChanged;
public event PropertyChangedEventHandler PropertyChanged;
public BufferError()
{
}
public void Init(IErrorDBModel dBModel)
{
this.dBModel = dBModel;
Load();
}
void Load()
{
var n = dBModel.sqliteHelper.ExecuteScalar($"SELECT COUNT(*) FROM {dBModel.TbError.TableName}");
int count = System.Convert.ToInt32(n);
NewestID = count - 1;
Count = count;
base.Init(dBModel.TbError, mapLc2Db, mapDb2Lc);
}
public void Add(FlyData_WarningHistory t)
static DB_Error mapLc2Db(FlyData_WarningHistory lc)
{
var db_errorData = new DB_Error()
return new DB_Error()
{
ID = dBModel.TbError.FreeID,//添加 数据库必要的 field
Time = t.Time,
ErrCode = t.ErrCode,
Descrption = t.Description,
IsOn = t.State == ERR_STATE.ON
ID = lc.ID,
Time = lc.Time,
ErrCode = lc.ErrCode,
Descrption = lc.Description,
IsOn = lc.State == ERR_STATE.ON
};
//SQLs
List<string> sqls = new List<string>();
sqls.Add(SQLiteHelper.GetInsertCommandText(db_errorData));
dBModel.sqliteHelper.QueryTranAsync(sqls);
NewestID = Count;
Count++;
}
/// <summary>
/// 清空全部数据
/// </summary>
public void Reset()
{
//SQLs
List<string> sqls = new List<string>();
sqls.Add($"DELETE FROM {dBModel.TbError.TableName}");
dBModel.sqliteHelper.QueryTranAsync(sqls);
NewestID = 0;
Count = 0;
}
/// <summary>
/// 获取指定位置的N个数据
/// </summary>
/// <param name="last_id">最后的ID</param>
/// <param name="count">数量</param>
/// <param name="asyncCB">回调</param>
/// <param name="asyncContext">回调里面的上下文</param>
public async void GetRecord(int last_id, int count, AsyncCBHandler asyncCB, object asyncContext)
{
if (Count == 0)
{
asyncCB(asyncContext, new GetRecordReponse<FlyData_WarningHistory>()
{
LastID = 0,
Items = null
});
return;
}
await Task.Factory.StartNew(() => {
var reponse = new GetRecordReponse<FlyData_WarningHistory>();
var db_errors = dBModel.TbError.Find($"ORDER BY ID LIMIT {last_id-count+1},{count}");
reponse.LastID = last_id;
reponse.Items = Map_DB_Errors2FlyData_WarningHistorys(db_errors);
asyncCB(asyncContext, reponse);
});
}
/// <summary>
/// 获取最新的N个数据
/// </summary>
/// <param name="count">数量</param>
/// <param name="asyncCB">回调</param>
/// <param name="asyncContext">回调里面的上下文</param>
public void GetRecord(int count, AsyncCBHandler asyncCB, object asyncContext)
{
GetRecord(NewestID, count, asyncCB, asyncContext);
}
FlyData_WarningHistory Map_DB_Error2FlyData_WarningHistory(DB_Error dB_Error)
static FlyData_WarningHistory mapDb2Lc(DB_Error db)
{
return new FlyData_WarningHistory()
{
Time = dB_Error.Time,
ErrCode = dB_Error.ErrCode,
State = dB_Error.IsOn ? ERR_STATE.ON : ERR_STATE.OFF,
Description = dB_Error.Descrption,
Accessory = dB_Error.Accessory
ID = db.ID,
Time = db.Time,
ErrCode = db.ErrCode,
Description = db.Descrption,
State = db.IsOn? ERR_STATE.ON: ERR_STATE.OFF
};
}
List<FlyData_WarningHistory> Map_DB_Errors2FlyData_WarningHistorys(List<DB_Error> dB_Errors)
{
List<FlyData_WarningHistory> flyDatas = new List<FlyData_WarningHistory>();
foreach (var db_error in dB_Errors)
{
flyDatas.Add(Map_DB_Error2FlyData_WarningHistory(db_error));
}
return flyDatas;
}
}
}
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FObjBase;
using SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.OBJComponents.Server
{
public class BufferSQLite<TLc, TDb> : IBufferAdd<TLc>
where TLc : IDbBase, new()
where TDb : IDbBase, new()
{
DBTable<TDb> dbTable;
SQLiteHelper sqliteHelper;
Func<TLc, TDb> mapLc2Db;
Func<TDb, TLc> mapDb2Lc;
public int NewestID => Count - 1;
public int Count { get; private set; }
/// <summary>
/// 不能设置,设了也没有用
/// </summary>
public int Capacity { get; set; } = int.MaxValue;
public event NotifyBufferChangedEventHandler<TLc> BufferChanged;
public event PropertyChangedEventHandler PropertyChanged;
public BufferSQLite()
{
}
public void Init(DBTable<TDb> dbTable,
Func<TLc, TDb> mapLc2Db, Func<TDb, TLc> mapDb2Lc)
{
this.dbTable = dbTable;
this.sqliteHelper = dbTable.sqliteHelper;
this.mapLc2Db = mapLc2Db;
this.mapDb2Lc = mapDb2Lc;
Load();
}
void Load()
{
var n = sqliteHelper.ExecuteScalar($"SELECT COUNT(*) FROM {dbTable.TableName}");
int count = System.Convert.ToInt32(n);
Count = count;
}
public void Add(TLc lc)
{
//添加 数据库必要的 field
lc.ID = dbTable.FreeID;
//SQLs
List<string> sqls = new List<string>();
sqls.Add(SQLiteHelper.GetInsertCommandText(mapLc2Db(lc)));
sqliteHelper.QueryTranAsync(sqls);
Count++;
}
public void AddRange(IEnumerable<TLc> lcs)
{
List<string> sqls = new List<string>();
foreach (var lc in lcs)
{
//添加 数据库必要的 field
lc.ID = dbTable.FreeID;
//SQLs
sqls.Add(SQLiteHelper.GetInsertCommandText(mapLc2Db(lc)));
}
sqliteHelper.QueryTranAsync(sqls);
Count += lcs.Count();
}
/// <summary>
/// 清空全部数据
/// </summary>
public void Reset()
{
//SQLs
List<string> sqls = new List<string>();
sqls.Add($"DELETE FROM {dbTable.TableName}");
sqliteHelper.QueryTranAsync(sqls);
Count = 0;
}
/// <summary>
/// 获取指定位置的N个数据
/// </summary>
/// <param name="last_id">最后的ID</param>
/// <param name="count">数量</param>
/// <param name="asyncCB">回调</param>
/// <param name="asyncContext">回调里面的上下文</param>
public async void GetRecord(int last_id, int count, AsyncCBHandler asyncCB, object asyncContext)
{
if (Count == 0)
{
asyncCB(asyncContext, new GetRecordReponse<TLc>()
{
LastID = 0,
Items = null
});
return;
}
await Task.Factory.StartNew(() => {
var reponse = new GetRecordReponse<TLc>();
var dbs = dbTable.Find($"ORDER BY ID LIMIT {last_id - count + 1},{count}");
reponse.LastID = last_id;
List<TLc> lcs = new List<TLc>();
foreach (var db in dbs)
{
lcs.Add(mapDb2Lc(db));
}
reponse.Items = lcs;
asyncCB(asyncContext, reponse);
});
}
/// <summary>
/// 获取最新的N个数据
/// </summary>
/// <param name="count">数量</param>
/// <param name="asyncCB">回调</param>
/// <param name="asyncContext">回调里面的上下文</param>
public void GetRecord(int count, AsyncCBHandler asyncCB, object asyncContext)
{
GetRecord(NewestID, count, asyncCB, asyncContext);
}
}
public class BufferSQLite<TDb> : IBufferAdd<TDb>
where TDb : IDbBase, new()
{
SQLiteDbContext dbModel;
DBTable<TDb> dbTable;
public int NewestID => Count - 1;
public int Count { get; private set; }
/// <summary>
/// 不能设置,设了也没有用
/// </summary>
public int Capacity { get; set; } = int.MaxValue;
public event NotifyBufferChangedEventHandler<TDb> BufferChanged;
public event PropertyChangedEventHandler PropertyChanged;
public BufferSQLite()
{
}
public void Init(SQLiteDbContext dbModel, DBTable<TDb> dbTable)
{
this.dbModel = dbModel;
this.dbTable = dbTable;
Load();
}
void Load()
{
var n = dbModel.sqliteHelper.ExecuteScalar($"SELECT COUNT(*) FROM {dbTable.TableName}");
int count = System.Convert.ToInt32(n);
Count = count;
}
public void Add(TDb db)
{
//添加 数据库必要的 field
db.ID = dbTable.FreeID;
//SQLs
List<string> sqls = new List<string>();
sqls.Add(SQLiteHelper.GetInsertCommandText(db));
dbModel.sqliteHelper.QueryTranAsync(sqls);
Count++;
}
public void AddRange(IEnumerable<TDb> dbs)
{
List<string> sqls = new List<string>();
foreach (var db in dbs)
{
//添加 数据库必要的 field
db.ID = dbTable.FreeID;
//SQLs
sqls.Add(SQLiteHelper.GetInsertCommandText(db));
}
dbModel.sqliteHelper.QueryTranAsync(sqls);
Count += dbs.Count();
}
/// <summary>
/// 清空全部数据
/// </summary>
public void Reset()
{
//SQLs
List<string> sqls = new List<string>();
sqls.Add($"DELETE FROM {dbTable.TableName}");
dbModel.sqliteHelper.QueryTranAsync(sqls);
Count = 0;
}
/// <summary>
/// 获取指定位置的N个数据
/// </summary>
/// <param name="last_id">最后的ID</param>
/// <param name="count">数量</param>
/// <param name="asyncCB">回调</param>
/// <param name="asyncContext">回调里面的上下文</param>
public async void GetRecord(int last_id, int count, AsyncCBHandler asyncCB, object asyncContext)
{
if (Count == 0)
{
asyncCB(asyncContext, new GetRecordReponse<TDb>()
{
LastID = 0,
Items = null
});
return;
}
await Task.Factory.StartNew(() => {
var reponse = new GetRecordReponse<TDb>();
var dbs = dbTable.Find($"ORDER BY ID LIMIT {last_id - count + 1},{count}");
reponse.LastID = last_id;
reponse.Items = dbs;
asyncCB(asyncContext, reponse);
});
}
/// <summary>
/// 获取最新的N个数据
/// </summary>
/// <param name="count">数量</param>
/// <param name="asyncCB">回调</param>
/// <param name="asyncContext">回调里面的上下文</param>
public void GetRecord(int count, AsyncCBHandler asyncCB, object asyncContext)
{
GetRecord(NewestID, count, asyncCB, asyncContext);
}
}
}
......@@ -13,7 +13,7 @@ namespace FLY.OBJComponents.Server.Model
/// 异常记录
/// </summary>
[Table("Error")]
public class DB_Error
public class DB_Error:IDbBase
{
[Key]
[PropertyIndex(0)]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment