using EC.AutoWeightServer.Model.Config;
using EC.AutoWeightServer.Model.State;
using EC.Entity.Organization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace EC.AutoWeightServer.Center
{
public class LocalAttrCenter
{
public CompanyEntity Company { get; set; }
public UserEntity User { get; set; }
public SiteEntity Site { get; set; }
#region Gather
///
/// 检测服务端配置
///
public void CheckSiteConfig()
{
Site = ServiceCenter.SiteService.GetSiteByName(ServerConfig.SiteName);
if (Site == null)
{
UiExtendCenter.ShowErrorMsgBox("站点认证失败!");
Environment.Exit(1);
}
//User = UiExtendCenter.LoginUser();
Company = ServiceCenter.CompanyService.SelectEntityByWhere($"F_CompanyId='{Site.CompanyId}'");
}
#endregion Gather
#region CarState
private CarState CarState { get; set; } = CarState.Empty;
private readonly object _lockCarStateObj = new object();
///
/// 锁读 CarState
///
///
internal CarState GetCarState()
{
lock (_lockCarStateObj)
{
return CarState;
}
}
///
/// 锁写 CarState
///
///
internal void SetCarState(CarState state)
{
lock (_lockCarStateObj)
{
CarState = state;
}
}
#endregion CarState
#region WeightList
private readonly List _weightList = new List();
private readonly object _lockWeightObj = new object();
private readonly int _lenLimit = ServerConfig.WtLenLimit; //用于判断的长度
private bool IsWeightListEmpty()
{
return _weightList.Count <= 0;
}
private bool IsWeightListFull()
{
return _weightList.Count >= _lenLimit;
}
///
/// 清理 WeightList
///
private void ClearWeightList()
{
_weightList.Clear();
}
///
/// 获取平均重量
///
///
private decimal GetAvgWeight()
{
return !IsWeightListEmpty() ? _weightList.Average() : 0;
}
///
/// 添加 Weight
///
///
public void AppendWeight(decimal weight)
{
lock (_lockWeightObj)
{
while (IsWeightListFull())
{
_weightList.RemoveAt(0);
}
_weightList.Add(weight);
}
}
///
/// 获取稳定重量
///
///
///
public decimal? GetStableWeight()
{
lock (_lockWeightObj)
{
if (!IsWeightListFull())
return null;
var lastWeight = _weightList.Last();
var avgWeight = GetAvgWeight();
if (avgWeight <= 0)
return null;
var diff = Math.Abs(lastWeight - avgWeight) / avgWeight;
return diff <= ServerConfig.WtDiff ? (decimal?)avgWeight : null;
}
}
///
/// 通过自动计重判断油车是否进入
///
///
public bool IsCarInDelayWeight()
{
lock (_lockWeightObj)
{
if (!IsWeightListFull())
return false;
var frontAvgWeight = _weightList.Take(_lenLimit / 2).Average();
return frontAvgWeight > 0;
}
}
///
/// 通过自动计重判断油车是否离开
///
///
public bool IsCarOutDelayWeight()
{
lock (_lockWeightObj)
{
if (!IsWeightListFull())
return false;
var backAvgWeight = _weightList.Skip(_lenLimit / 2).Average();
return backAvgWeight <= 0;
}
}
#endregion WeightList
#region ControlLightState
private bool _lightState;
private readonly ReaderWriterLockSlim _lightLock = new ReaderWriterLockSlim();
public void SetLightState(bool state)
{
lock (_lightLock)
{
_lightState = state;
}
}
public bool IsLightOn()
{
lock (_lightLock)
{
return Equals(_lightState, true);
}
}
public bool IsLightOff()
{
lock (_lightLock)
{
return Equals(_lightState, false);
}
}
#endregion ControlLightState
}
}