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

支持sqlite数据库的报警系统

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