Commit b42aa275 authored by 潘栩锋's avatar 潘栩锋 🚴

支持sqlite数据库的报警系统

parent 3afe9982
...@@ -21,7 +21,7 @@ namespace FLY.OBJComponents.Client ...@@ -21,7 +21,7 @@ namespace FLY.OBJComponents.Client
#region IWarningService #region IWarningService
public bool Enable { get; set; } public bool Enable { get; set; }
public bool IsRinging { get; protected set; }
private BufferServiceClient<FlyData_WarningHistory> reasonlist; private BufferServiceClient<FlyData_WarningHistory> reasonlist;
/// <summary> /// <summary>
...@@ -70,7 +70,11 @@ namespace FLY.OBJComponents.Client ...@@ -70,7 +70,11 @@ namespace FLY.OBJComponents.Client
CurrObjSys.CallFunctionEx(mConn, mServerID, ID, CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
WARNING_OBJ_INTERFACE.CALL_RESET, null); WARNING_OBJ_INTERFACE.CALL_RESET, null);
} }
public void Silence()
{
CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
WARNING_OBJ_INTERFACE.CALL_SILENCE, null);
}
public override void Dispose() public override void Dispose()
{ {
CurrObjSys.ObjRemove( CurrObjSys.ObjRemove(
......
...@@ -9,7 +9,7 @@ using System.Threading.Tasks; ...@@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace FLY.OBJComponents.Common namespace FLY.OBJComponents.Common
{ {
public class FlyData_WarningHistory : IFlyData, IPack2 public class FlyData_WarningHistory : IFlyData
{ {
/// <summary> /// <summary>
/// 时间 /// 时间
...@@ -19,7 +19,7 @@ namespace FLY.OBJComponents.Common ...@@ -19,7 +19,7 @@ namespace FLY.OBJComponents.Common
/// <summary> /// <summary>
/// 出错码 /// 出错码
/// </summary> /// </summary>
public byte ErrCode { get; set; } public int ErrCode { get; set; }
/// <summary> /// <summary>
/// 出错状态 /// 出错状态
/// </summary> /// </summary>
...@@ -28,7 +28,10 @@ namespace FLY.OBJComponents.Common ...@@ -28,7 +28,10 @@ namespace FLY.OBJComponents.Common
/// 描述 /// 描述
/// </summary> /// </summary>
public string Description { get; set; } public string Description { get; set; }
/// <summary>
/// 附加信息, json格式
/// </summary>
public string Accessory { get; set; }
public FlyData_WarningHistory Clone() public FlyData_WarningHistory Clone()
{ {
return new FlyData_WarningHistory() return new FlyData_WarningHistory()
...@@ -41,23 +44,22 @@ namespace FLY.OBJComponents.Common ...@@ -41,23 +44,22 @@ namespace FLY.OBJComponents.Common
} }
public string GetHeader() public string GetHeader()
{ {
string header = "时间, 出错码,状态,描述"; return "时间,出错码,状态,描述";
return header;
} }
public bool TryParse(string header, string str) public bool TryParse(string header, string str)
{ {
string[] items = str.Split(new char[] { ',' }); string[] items = str.Split(new char[] { ',' });
if (items.Length != 4) if (items.Length < 4)
return false; return false;
DateTime dt; DateTime dt;
if (DateTime.TryParse(items[0], out dt)) if (DateTime.TryParse(items[0], out dt))
Time = dt; Time = dt;
else else
return false; return false;
byte b; int b;
if (byte.TryParse(items[1], out b)) if (int.TryParse(items[1], out b))
ErrCode = b; ErrCode = b;
else else
return false; return false;
...@@ -67,6 +69,9 @@ namespace FLY.OBJComponents.Common ...@@ -67,6 +69,9 @@ namespace FLY.OBJComponents.Common
else else
return false; return false;
Description = items[3]; Description = items[3];
return true; return true;
} }
...@@ -76,65 +81,9 @@ namespace FLY.OBJComponents.Common ...@@ -76,65 +81,9 @@ namespace FLY.OBJComponents.Common
str = Time.ToString(); str = Time.ToString();
str += "," + ErrCode.ToString(); str += "," + ErrCode.ToString();
str += "," + State.ToString(); str += "," + State.ToString();
str += "," + Description.ToString(); str += "," + Description;
return str; return str;
} }
public byte[] ToBytes()
{
List<byte> buf = new List<byte>();
buf.AddRange(BitConverter.GetBytes(Time.Ticks));
buf.Add(ErrCode);
buf.AddRange(BitConverter.GetBytes((int)State));
byte[] bs = Misc.Converter.StringToBytes(Description);
int len;
if (bs == null)
len = 0;
else
len = bs.Length;
buf.AddRange(BitConverter.GetBytes(len));
if (len > 0)
buf.AddRange(bs);
return buf.ToArray();
}
public bool TryParse(byte[] value, int index, out int cnt)// ref int idx)
{
cnt = 8 + 1 + 4 + 4;
if (value.Length - index < cnt)
return false;
int idx = index;
Time = new DateTime(BitConverter.ToInt64(value, idx));
idx += 8;
ErrCode = value[idx];
idx++;
State = (ERR_STATE)BitConverter.ToInt32(value, idx);
idx += 4;
int len = BitConverter.ToInt32(value, idx);
idx += 4;
cnt += len;
if (value.Length - index < cnt)
return false;
if (len == 0)
Description = "";
else
{
Description = Misc.Converter.BytesToString(value, idx, len);
}
idx += len;
return true;
}
public bool TryParse(byte[] value)
{
int cnt;
return TryParse(value, 0, out cnt);
}
} }
/// <summary> /// <summary>
...@@ -149,11 +98,7 @@ namespace FLY.OBJComponents.Common ...@@ -149,11 +98,7 @@ namespace FLY.OBJComponents.Common
/// <summary> /// <summary>
/// 关闭 /// 关闭
/// </summary> /// </summary>
OFF, OFF
/// <summary>
/// 只发生了一次
/// </summary>
ONCE
} }
/// <summary> /// <summary>
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.OBJComponents.IService
{
public interface IBufferAdd<T> : IBuffer<T>
{
void Add(T t);
}
}
...@@ -19,7 +19,21 @@ namespace FLY.OBJComponents.IService ...@@ -19,7 +19,21 @@ namespace FLY.OBJComponents.IService
/// </summary> /// </summary>
bool Enable { get; set; } bool Enable { get; set; }
/// <summary>
/// 正在响铃
/// </summary>
bool IsRinging { get; }
/// <summary>
/// 复位,把全部报警消失
/// </summary>
void Reset(); void Reset();
/// <summary>
/// 静音,不消失报警,只是静音,新的报警来,还是会响
/// </summary>
void Silence();
/// <summary> /// <summary>
/// 当前报警列表 /// 当前报警列表
/// </summary> /// </summary>
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
...@@ -57,6 +58,7 @@ ...@@ -57,6 +58,7 @@
<Compile Include="Common\FlyData_WarningHistory.cs" /> <Compile Include="Common\FlyData_WarningHistory.cs" />
<Compile Include="Common\PropertiesManager.cs" /> <Compile Include="Common\PropertiesManager.cs" />
<Compile Include="IService\IBuffer.cs" /> <Compile Include="IService\IBuffer.cs" />
<Compile Include="IService\IBufferAdd.cs" />
<Compile Include="IService\IPLCProxySystemService.cs" /> <Compile Include="IService\IPLCProxySystemService.cs" />
<Compile Include="IService\IPropertyOpt.cs" /> <Compile Include="IService\IPropertyOpt.cs" />
<Compile Include="IService\IRemoteHistory.cs" /> <Compile Include="IService\IRemoteHistory.cs" />
...@@ -74,7 +76,10 @@ ...@@ -74,7 +76,10 @@
<Compile Include="Server.OBJProxy\WarningSystem_OBJProxy.cs" /> <Compile Include="Server.OBJProxy\WarningSystem_OBJProxy.cs" />
<Compile Include="Server\Buffer.cs" /> <Compile Include="Server\Buffer.cs" />
<Compile Include="Server\BufferStorage.cs" /> <Compile Include="Server\BufferStorage.cs" />
<Compile Include="Server\BufferError.cs" />
<Compile Include="Server\History.cs" /> <Compile Include="Server\History.cs" />
<Compile Include="Server\Model\DB_Error.cs" />
<Compile Include="Server\Model\IErrorDBModel.cs" />
<Compile Include="Server\PLCProxySystem.cs" /> <Compile Include="Server\PLCProxySystem.cs" />
<Compile Include="Server\WarningSystem.cs" /> <Compile Include="Server\WarningSystem.cs" />
</ItemGroup> </ItemGroup>
...@@ -96,6 +101,10 @@ ...@@ -96,6 +101,10 @@
<Project>{6d4b9bda-2a66-4583-b244-758bc4213d9f}</Project> <Project>{6d4b9bda-2a66-4583-b244-758bc4213d9f}</Project>
<Name>FLY.ModbusMapper</Name> <Name>FLY.ModbusMapper</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\..\Project.SQLiteHelper\SQLiteHelper\SQLiteHelper.csproj">
<Project>{4CBABFAA-1C62-4510-AC63-A51EE5FD50FF}</Project>
<Name>SQLiteHelper</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json"> <PackageReference Include="Newtonsoft.Json">
......
...@@ -35,6 +35,10 @@ namespace FLY.OBJComponents.OBJ_INTERFACE ...@@ -35,6 +35,10 @@ namespace FLY.OBJComponents.OBJ_INTERFACE
/// 复位 /// 复位
/// </summary> /// </summary>
public const UInt16 CALL_RESET = 1; public const UInt16 CALL_RESET = 1;
/// <summary>
/// 消音
/// </summary>
public const UInt16 CALL_SILENCE = 2;
#endregion #endregion
} }
} }
...@@ -99,6 +99,9 @@ namespace FLY.OBJComponents.Server.OBJProxy ...@@ -99,6 +99,9 @@ namespace FLY.OBJComponents.Server.OBJProxy
case WARNING_OBJ_INTERFACE.CALL_RESET: case WARNING_OBJ_INTERFACE.CALL_RESET:
mWarningSystem.Reset(); mWarningSystem.Reset();
break; break;
case WARNING_OBJ_INTERFACE.CALL_SILENCE:
mWarningSystem.Silence();
break;
} }
} }
} }
......
...@@ -15,7 +15,7 @@ using System.Windows.Threading; ...@@ -15,7 +15,7 @@ using System.Windows.Threading;
namespace FLY.OBJComponents.Server namespace FLY.OBJComponents.Server
{ {
public class Buffer<T> : IBuffer<T> public class Buffer<T> : IBufferAdd<T>
{ {
public List<T> list = new List<T>(); public List<T> list = new List<T>();
#region property #region property
......
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.Server.Model;
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 BufferError : IBufferAdd<FlyData_WarningHistory>
{
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;
}
public void Add(FlyData_WarningHistory t)
{
var db_errorData = new DB_Error()
{
ID = dBModel.TbError.FreeID,//添加 数据库必要的 field
Time = t.Time,
ErrCode = t.ErrCode,
Descrption = t.Description,
IsOn = t.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($"DELECT 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)
{
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
};
}
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 SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.OBJComponents.Server.Model
{
/// <summary>
/// 异常记录
/// </summary>
[Table("Error")]
public class DB_Error
{
[Key]
[PropertyIndex(0)]
public Int64 ID { get; set; }
/// <summary>
/// 发生的时间
/// </summary>
[PropertyIndex(1)]
public DateTime Time { get; set; }
/// <summary>
/// 异常代码
/// </summary>
[PropertyIndex(2)]
public int ErrCode { get; set; }
/// <summary>
/// true=异常是发生了,false=异常关闭
/// </summary>
[PropertyIndex(3)]
public bool IsOn { get; set; }
/// <summary>
/// 异常描述
/// </summary>
[PropertyIndex(4)]
public string Descrption { get; set; }
/// <summary>
/// 附加信息
/// </summary>
[PropertyIndex(5)]
public string Accessory { get; set; }
}
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.OBJComponents.Server.Model
{
public interface IErrorDBModel
{
SQLiteHelper sqliteHelper { get; }
DBTable<DB_Error> TbError { get; }
}
}
...@@ -20,7 +20,10 @@ namespace FLY.OBJComponents.Server ...@@ -20,7 +20,10 @@ namespace FLY.OBJComponents.Server
/// 使能 /// 使能
/// </summary> /// </summary>
public bool Enable { get; set; } public bool Enable { get; set; }
/// <summary>
/// 正在响铃
/// </summary>
public bool IsRinging { get; private set; }
/// <summary> /// <summary>
/// 当前正在报警的!!!!!! /// 当前正在报警的!!!!!!
/// </summary> /// </summary>
...@@ -41,38 +44,52 @@ namespace FLY.OBJComponents.Server ...@@ -41,38 +44,52 @@ namespace FLY.OBJComponents.Server
ReasonList = new Buffer<FlyData_WarningHistory>(); ReasonList = new Buffer<FlyData_WarningHistory>();
NewestList = new BufferStorage<FlyData_WarningHistory>("warning_newest.csv"); NewestList = new BufferStorage<FlyData_WarningHistory>("warning_newest.csv");
} }
/// <summary>
/// 报警复位!!!! public WarningSystem(IBufferAdd<FlyData_WarningHistory> newestList)
/// </summary> {
public Action ResetEvent; Enable = true;
ReasonList = new Buffer<FlyData_WarningHistory>();
NewestList = newestList;
}
public void Reset() public void Reset()
{ {
ReasonList.Reset(); ReasonList.Reset();
ResetEvent?.Invoke(); IsRinging = false;
}
public void Silence()
{
IsRinging = false;
} }
/// <summary>
/// 正在报警!!!
/// </summary>
public Action RingEvent;
#region IWarningServiceSimple #region IWarningServiceSimple
public void Add(byte errcode, string description, ERR_STATE state) public void Add(int errcode, string description, ERR_STATE state)
{ {
Add(errcode, description, state, "");
}
public void Add(int errcode, string description, ERR_STATE state, string accessory)
{
if (!Enable)
return;
FlyData_WarningHistory reason = new FlyData_WarningHistory(); FlyData_WarningHistory reason = new FlyData_WarningHistory();
reason.Time = DateTime.Now; reason.Time = DateTime.Now;
reason.ErrCode = errcode; reason.ErrCode = errcode;
reason.Description = description; reason.Description = description;
reason.State = state; reason.State = state;
reason.Accessory = accessory;
FlyData_WarningHistory error = null; FlyData_WarningHistory error = null;
Buffer<FlyData_WarningHistory> reasonList = ReasonList as Buffer<FlyData_WarningHistory>; Buffer<FlyData_WarningHistory> reasonList = ReasonList as Buffer<FlyData_WarningHistory>;
int error_id = 0; int error_id = 0;
if (reasonList.Count > 0) if (reasonList.Count > 0)
{ {
int idx = reasonList.list.FindIndex(e => e.ErrCode == errcode); int idx = reasonList.list.FindIndex(e => e.ErrCode == errcode);
if (idx >= 0) if (idx >= 0)
{ {
...@@ -85,7 +102,7 @@ namespace FLY.OBJComponents.Server ...@@ -85,7 +102,7 @@ namespace FLY.OBJComponents.Server
case ERR_STATE.ON: case ERR_STATE.ON:
if (error == null) if (error == null)
{ {
((Buffer<FlyData_WarningHistory>)ReasonList).Add(reason.Clone()); reasonList.Add(reason.Clone());
} }
else else
...@@ -103,22 +120,24 @@ namespace FLY.OBJComponents.Server ...@@ -103,22 +120,24 @@ namespace FLY.OBJComponents.Server
else else
{ {
error.State = state; error.State = state;
((Buffer<FlyData_WarningHistory>)ReasonList).Remove(error_id); reasonList.Remove(error_id);
} }
break; break;
} }
((Buffer<FlyData_WarningHistory>)NewestList).Add(reason); var newestList = NewestList as IBufferAdd<FlyData_WarningHistory>;
newestList.Add(reason);
if (reasonList.Count > 0) if (reasonList.Count > 0)
{ {
RingEvent?.Invoke(); IsRinging = true;
} }
else else
{ {
Reset(); Reset();
} }
} }
#endregion #endregion
/// <summary> /// <summary>
...@@ -132,11 +151,7 @@ namespace FLY.OBJComponents.Server ...@@ -132,11 +151,7 @@ namespace FLY.OBJComponents.Server
}; };
} }
#region INotifyPropertyChanged 成员 #region INotifyPropertyChanged 成员
protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
#endregion #endregion
......
...@@ -10,7 +10,7 @@ namespace SQLite ...@@ -10,7 +10,7 @@ namespace SQLite
public abstract class SQLiteDbContext public abstract class SQLiteDbContext
{ {
public List<IDBTable> DbSet = new List<IDBTable>(); public List<IDBTable> DbSet = new List<IDBTable>();
public SQLiteHelper sqliteHelper; public SQLiteHelper sqliteHelper { get; private set; }
string ConnectionString string ConnectionString
{ {
get get
......
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