You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
4.0 KiB

3 years ago
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
/// <summary>
/// 检测服务端配置
/// </summary>
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();
/// <summary>
/// 锁读 CarState
/// </summary>
/// <returns></returns>
internal CarState GetCarState()
{
lock (_lockCarStateObj)
{
return CarState;
}
}
/// <summary>
/// 锁写 CarState
/// </summary>
/// <param name="state"></param>
internal void SetCarState(CarState state)
{
lock (_lockCarStateObj)
{
CarState = state;
}
}
#endregion CarState
#region WeightList
private readonly List<decimal> _weightList = new List<decimal>();
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;
}
/// <summary>
/// 清理 WeightList
/// </summary>
private void ClearWeightList()
{
_weightList.Clear();
}
/// <summary>
/// 获取平均重量
/// </summary>
/// <returns></returns>
private decimal GetAvgWeight()
{
return !IsWeightListEmpty() ? _weightList.Average() : 0;
}
/// <summary>
/// 添加 Weight
/// </summary>
/// <param name="weight"></param>
public void AppendWeight(decimal weight)
{
lock (_lockWeightObj)
{
while (IsWeightListFull())
{
_weightList.RemoveAt(0);
}
_weightList.Add(weight);
}
}
/// <summary>
/// 获取稳定重量
/// </summary>
/// <param name="weight"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 通过自动计重判断油车是否进入
/// </summary>
/// <returns></returns>
public bool IsCarInDelayWeight()
{
lock (_lockWeightObj)
{
if (!IsWeightListFull())
return false;
var frontAvgWeight = _weightList.Take(_lenLimit / 2).Average();
return frontAvgWeight > 0;
}
}
/// <summary>
/// 通过自动计重判断油车是否离开
/// </summary>
/// <returns></returns>
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
}
}