using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.OBJComponents.Server
{
///
/// 报警管理系统
///
[Obsolete("已经作废,应该使用WarningSystem2")]
public class WarningSystem : INotifyPropertyChanged, IWarningService
{
#region IWarningService
///
/// 使能
///
public bool Enable { get; set; }
///
/// 正在响铃
///
public bool IsRinging { get; private set; }
///
/// 当前正在报警的!!!!!!
///
public IBuffer ReasonList { get; private set; }
///
/// 最近报警列表
///
public IBuffer NewestList { get; private set; }
#endregion
public WarningSystem()
{
Enable = true;
ReasonList = new Buffer();
}
public void Init()
{
NewestList = new BufferStorage("warning_newest.csv");
}
public void Init(IBufferAdd newestList)
{
NewestList = newestList;
}
public void Reset()
{
ReasonList.Reset();
IsRinging = false;
}
public void Silence()
{
IsRinging = false;
}
#region IWarningServiceSimple
public void Add(int errcode, string description)
{
Add(errcode, description, ERR_STATE.ON, "");
}
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 reasonList = ReasonList as Buffer;
int error_id = 0;
if (reasonList.Count > 0)
{
int idx = reasonList.list.FindIndex(e => e.ErrCode == errcode);
if (idx >= 0)
{
error_id = reasonList.GetID(idx);
error = reasonList.list[idx];
}
}
switch (state)
{
case ERR_STATE.ON:
if (error == null)
{
reasonList.Add(reason.Clone());
}
else
{
//已经有了,再发生,不管它。
return;
}
break;
case ERR_STATE.OFF:
if (error == null)
{
//没有在报警,不管它。
return;
}
else
{
error.State = state;
reasonList.Remove(error_id);
}
break;
}
var newestList = NewestList as IBufferAdd;
newestList.Add(reason);
if (reasonList.Count > 0)
{
IsRinging = true;
}
else
{
Reset();
}
}
public void Remove(int errcode) {
FlyData_WarningHistory error = null;
Buffer reasonList = ReasonList as Buffer;
if (reasonList.Count == 0)
return;
int idx = reasonList.list.FindIndex(e => e.ErrCode == errcode);
if (idx < 0)
return;
int error_id = reasonList.GetID(idx);
error = reasonList.list[idx];
error.State = ERR_STATE.OFF;
reasonList.Remove(error_id);
var newestList = NewestList as IBufferAdd;
newestList.Add(new FlyData_WarningHistory
{
Time = DateTime.Now,
ErrCode = errcode,
Description = error.Description,
State = ERR_STATE.OFF
});
if (reasonList.Count > 0)
{
IsRinging = true;
}
else
{
Reset();
}
}
#endregion
///
/// 获取能被交易的属性
///
///
public string[] GetPropertys()
{
return new string[] {
"Enable"
};
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}