HumitureBean.cs 8.3 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class HumitureBean
    {
        private string PortName = "";
        private string Name = "";
        public HumitureParam LastData = new HumitureParam(0, 0);
        internal HumitureBean(string port,string deviceName)
        {
            this.Name = deviceName;
            this.PortName = port;
        }

        public HumitureParam QueryData()
        {
            HumitureParam param = HumitureController.QueryData(PortName);
            return param;
          
        }
        internal void ProcessHumidityCMD(Operation resultOperation)
        {
            Dictionary<string, string> data = resultOperation.data;
            if (data != null && data.ContainsKey(ParamDefine.maxHumidity) && data.ContainsKey(ParamDefine.maxTemperature))
            {
                string maxHumidity = data[ParamDefine.maxHumidity];
                string maxTemp = data[ParamDefine.maxTemperature];
                LogUtil.info( Name+"收到服务器温湿度预警值:maxHumidity=" + maxHumidity + ",maxTemperature=" + maxTemp);

                try
                {
                    this.Max_Humidity = (float)Convert.ToDouble(maxHumidity);
                    this.Max_Temperature = (float)Convert.ToDouble(maxTemp);
                    LogUtil.info(Name+"保存温湿度预警值:Max_Humidity=" + Max_Humidity + ",Max_Temperature=" + Max_Temperature);
                }
                catch (Exception ex)
                {
                    LogUtil.error(Name+"转换温湿度失败:" + ex.ToString());
                }
            }
        }

        #region 温湿度处理
        /// <summary>
        /// 湿度标准,超过后需要报警
        /// </summary>
        private float Max_Humidity = 0;
        /// <summary>
        /// 温度标准,超过后需要报警
        /// </summary>
        private float Max_Temperature = 0;
        private bool IsInBlowing = false;
        private DateTime LastBeginBlowTime = DateTime.Now;
        private DateTime LastEndBlowTime = new DateTime(1997, 1, 1);
        private DateTime preLogTime = DateTime.Now;

        public  bool TempOrHumidityIsAlarm = false;
        public  DateTime TempAlarmTime = DateTime.Now;

        private float StartBlowValue = (float)ConfigAppSettings.GetNumValue(Setting_Init.StartBlowValue);
        private float StopBlowValue = (float)ConfigAppSettings.GetNumValue(Setting_Init.StopBlowValue);
        public string currTempStr = "";
        internal void HumidityProcess(BoxBean box)
        {
            //try
            //{
            //    if ((DateTime.Now - preLogTime).TotalSeconds > 10)
            //    {
            //        preLogTime = DateTime.Now;
            //        //用最大的湿度判断是否需要吹气,开始吹气的值=发过来的值-4
            //        //温湿度
            //        //ASTemperateParam param = HumitureServer.GetTemperateParam(Config.GetTempAddrList());
            //        HumitureParam param = QueryData();
            //        double humidity = 0;
            //        double temp = 0;
            //        if (param != null)
            //        {
            //            humidity = param.Humidity;
            //            temp = param.Temperate;
            //            currTempStr = Name + ("湿度:" + humidity.ToString() + ",温度:" + temp);
            //        }
            //        //double currMaxHumidity = HumitureServer.GetMaxHumidity(Config.GetTempAddrList());
            //        double currMaxHumidity = param.Humidity;
            //        float startBlowHumidity = Max_Humidity - StartBlowValue;
            //        float stopBlowHumidity = Max_Humidity - StopBlowValue;


            //        //判断是否需要吹气
            //        if (startBlowHumidity > 0 && startBlowHumidity < currMaxHumidity && IsInBlowing.Equals(false))
            //        {
            //            //判断是否距离上次结束指定的时间
            //            TimeSpan span = DateTime.Now - LastEndBlowTime;
            //            if (span.TotalMinutes > StoreManager.Config.BlowAir_Interval)
            //            {
            //                LogUtil.info(Name + "当前最大湿度:" + currMaxHumidity.ToString() + ",开始吹气湿度:" + startBlowHumidity + ",当前不在吹气中,且间隔超过" + StoreManager.Config.BlowAir_Interval + "分钟,开始吹气!");
            //                IsInBlowing = true;
            //                //Thread.Sleep(100);
            //                box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.HIGH);
            //                LastBeginBlowTime = DateTime.Now;
            //                LastEndBlowTime = DateTime.Now;
            //            }
            //        }

            //        if (IsInBlowing && stopBlowHumidity > currMaxHumidity)
            //        {
            //            LogUtil.info(Name + "当前最大湿度:" + currMaxHumidity.ToString() + ",停止吹气湿度:" + stopBlowHumidity + ",停止吹气!");
            //            IsInBlowing = false;
            //            box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW);
            //            LastEndBlowTime = DateTime.Now;
            //        }
            //        if (IsInBlowing)
            //        {
            //            //判断是否需要结束吹气
            //            TimeSpan span = DateTime.Now - LastBeginBlowTime;
            //            if (span.TotalMinutes > StoreManager.Config.BlowAir_Time)
            //            {
            //                LogUtil.info(Name + "已经吹气" + span.TotalMinutes + "分钟,超过配置的吹气时间" + StoreManager.Config.BlowAir_Time + "分钟,停止吹气!");
            //                IsInBlowing = false;
            //                //Thread.Sleep(100);
            //                box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW);
            //                LastEndBlowTime = DateTime.Now;
            //            }
            //        }
            //        bool needAlarm = false;
            //        //如果开始吹气并且当前达到报警值
            //        if (IsInBlowing && humidity > Max_Humidity)
            //        {
            //            needAlarm = true;
            //        }
            //        else if (temp > Max_Temperature && Max_Temperature > 0)
            //        {
            //            LogUtil.info(Name + "当前温度【" + param.Temperate + "】超过最高温度【" + Max_Temperature + "】,开始报警!");
            //            needAlarm = true;
            //            //Thread.Sleep(100);
            //            box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW);
            //        }
            //        else if (temp < Max_Temperature)
            //        {
            //            if (IsInBlowing.Equals(false) && TempOrHumidityIsAlarm)
            //            {
            //                LogUtil.info(Name + "不在吹气中,且当前温度【" + param.Temperate + "】低于【" + Max_Temperature + "】,关闭报警!");
            //                TempOrHumidityIsAlarm = false;
            //                //Thread.Sleep(100);
            //                box.IOMove(IO_Type.StartOrStopBlow, IO_VALUE.LOW);
            //            }
            //        }
            //        else
            //        {
            //            TempOrHumidityIsAlarm = false;
            //        }
            //        if (needAlarm)
            //        {
            //            HTAlarm();
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    LogUtil.error(Name + "HumidityProcess出错:" + ex.ToString());
            //}
        }
        private void HTAlarm()
        {
            if (TempOrHumidityIsAlarm)
            {
                return;
            }
            TempAlarmTime = DateTime.Now;
            TempOrHumidityIsAlarm = true;
        }

        internal bool NeedGetTem()
        {
            if (Max_Humidity <= 0 || (Max_Temperature <= 0))
            {
                return true;
            }
            return false;
        }
        #endregion

    }
}