using System; using System.Collections.Generic; using System.Net.NetworkInformation; using System.Text; using System.Linq; using System.Data; using System.Threading; using Mesnac.Action.Base; using Mesnac.Controls.Base; using System.Windows.Forms; using Mesnac.Codd.Session; using System.IO; using Mesnac.Action.Feeding.BasicInfo; namespace Mesnac.Action.Feeding.FeedingPlc { /// /// 更新设备运行状态 /// public class UpdateEquipState : FeedingAction, IAction { public void Run(RuntimeParameter runtime) { EquipStateInfo.Instance.EquipCode = base.CurrEquipCode; EquipStateInfo.Instance.dbHelper = NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Server); EquipStateInfo.Instance.EquipInfo( PlcData.Instance.CurrentMixingTime.NowValue.ToInt(), PlcData.Instance.CurrentMixingSpeedRotor.LastValue.ToInt(), PlcData.Instance.CurrentMixerCurrent.LastValue.ToDouble()); } #region 设备状态类(简化业务:每5秒更新一次设备运行状态) /// /// 设备状态类 /// private class EquipStateInfo { #region 单例模式 private static EquipStateInfo _this; public static EquipStateInfo Instance { get { if (null == _this) _this = new EquipStateInfo(); return _this; } } private EquipStateInfo() { this.dbHelper = null; this.RunfreeCurrent = 50; } #endregion public string EquipCode { get; set; } private DbHelper _dbHelper; public DbHelper dbHelper { get { return this._dbHelper; } set { if (this._dbHelper != null) { return; } this._dbHelper = value; } } //private DateTime _lastRunfreeTime; private DateTime _lastEquipStopTime; private int _currentTime; //当前密炼时间 private int _speedRotor; //当前转速 private double _mixerCurrent; //当前电流 private DateTime _lastEquipStateChanageTime = DateTime.Now.AddDays(-1); //最后一次更新设备状态的时间 private int _equipState = 0; //设备状态 /// /// 最小电流 /// public int RunfreeCurrent { get; set; } /// /// 设备状态属性 /// public int EquipState { get { return _equipState; } private set { this.EquipStateChanage(value); } } /// /// 更新数据库中的设备状态 /// /// private void EquipStateChanage(int state) { if ((DateTime.Now - this._lastEquipStateChanageTime).TotalSeconds >= 10) { try { //if (!PlanCommon.IsCanConnectServer()) //{ // return; //} this._dbHelper.CommandType = CommandType.Text; this._dbHelper.CommandText = "SELECT State,dianliu FROM dbo.Ppt_EquipState WHERE Equip_Code=@EquipCode"; this._dbHelper.AddParameter("@EquipCode", this.EquipCode); DataTable table = this._dbHelper.ToDataTable(); if (table != null && table.Rows.Count > 0) { this._equipState = Mesnac.Basic.DataProcessor.RowValue(table.Rows[0], "State", 0); this.RunfreeCurrent = Mesnac.Basic.DataProcessor.RowValue(table.Rows[0], "dianliu", 50); } if (this._equipState == state) { return; } this._equipState = state; string sqlstr = "UPDATE Ppt_EquipState SET State=" + state.ToString() + ",Update_Time=getdate() WHERE Equip_Code=@EquipCode"; this._dbHelper.CommandType = CommandType.Text; this._dbHelper.ClearParameter(); this._dbHelper.CommandText = sqlstr; this._dbHelper.AddParameter("@EquipCode", this.EquipCode); this._dbHelper.ExecuteNonQuery(); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("更新设备状态失败:" + ex.Message, ex); } finally { this._lastEquipStateChanageTime = DateTime.Now; } } } public void EquipInfo(int currentTime, int speedRotor, double mixerCurrent) { this._currentTime = currentTime; this._speedRotor = speedRotor; this._mixerCurrent = mixerCurrent; //自动密炼且当前炼胶时间大于0 if (this._currentTime > 0 && this._speedRotor > 0) { this.EquipState = 1; return; } //转速,电流小于等于0,为停机 if (this._mixerCurrent <= 0 && this._speedRotor <= 0) { this.EquipState = 0; return; } //电流大于空转电流 if (this._mixerCurrent > this.RunfreeCurrent) { this.EquipState = 1; return; } //密炼机空转 this.EquipState = 2; } } #endregion #region 设备状态类(旧业务注释) /// /// 设备状态类 /// // private class EquipStateInfo // { // #region 单例模式 // private static EquipStateInfo _this; // public static EquipStateInfo Instance // { // get // { // if (null == _this) // _this = new EquipStateInfo(); // return _this; // } // } // private EquipStateInfo() // { // this.dbHelper = null; // this.RunfreeCurrent = 50; // } // #endregion // public string EquipCode { get; set; } // private DbHelper _dbHelper; // public DbHelper dbHelper // { // get // { // return this._dbHelper; // } // set // { // if (this._dbHelper != null) // { // return; // } // this._dbHelper = value; // if (value == null) // { // } // else // { // this._dbHelper.CommandType = CommandType.Text; // this._dbHelper.CommandText = "SELECT dianliu FROM dbo.Ppt_EquipState WHERE Equip_Code='" + this.EquipCode + "'"; // object dianliu = this._dbHelper.ToScalar(); // if (dianliu != null && dianliu != DBNull.Value) // { // int d = 50; // if (int.TryParse(dianliu.ToString(), out d)) // { // this.RunfreeCurrent = d; // } // } // } // } // } // private DateTime _lastRunfreeTime; // private DateTime _lastEquipStopTime; // private int _currentTime; //当前密炼时间 // private int _speedRotor; //当前转速 // private double _mixerCurrent; //当前电流 // private DateTime _lastEquipStateChanageTime = DateTime.Now.AddDays(-1); // private int _equipState = 0; // private void EquipStateChanage(int state) // { // if (this._equipState == state) // { // return; // } // this._equipState = state; // if ((DateTime.Now - this._lastEquipStateChanageTime).TotalSeconds > 5) // { // string sqlstr = "UPDATE Ppt_EquipState SET State=" + state.ToString() + ",Update_Time=getdate() WHERE Equip_Code='" + this.EquipCode + "'"; // this._dbHelper.CommandType = CommandType.Text; // this._dbHelper.CommandText = sqlstr; // this._dbHelper.ExecuteNonQuery(); // } // this._lastEquipStateChanageTime = DateTime.Now; // } // private void RecodeEquipStop() // { // string sqlstr = "delete from Ppt_pmdownrecord where Mp_startdate<>@Mp_startdate and equip_Code=@equip_Code and isstop=1 and ISNULL(Mp_enddate,'')=''"; // this._dbHelper.CommandType = CommandType.Text; // this._dbHelper.CommandText = sqlstr; // this._dbHelper.ClearParameter(); // this._dbHelper.AddParameter("@Mp_startdate", this._lastEquipStopTime); // this._dbHelper.AddParameter("@equip_Code", this.EquipCode); // this._dbHelper.ExecuteNonQuery(); // sqlstr = "select * from Ppt_pmdownrecord where Mp_startdate=@Mp_startdate and equip_Code=@equip_Code"; // this._dbHelper.CommandType = CommandType.Text; // this._dbHelper.CommandText = sqlstr; // this._dbHelper.ClearParameter(); // this._dbHelper.AddParameter("@Mp_startdate", this._lastEquipStopTime); // this._dbHelper.AddParameter("@equip_Code", this.EquipCode); // DataTable dt = this._dbHelper.ToDataTable(); // if (dt.Rows.Count == 0) // { // sqlstr = @"SELECT a.ShiftID,a.ShiftClassID,b.ClassName FROM dbo.PptShiftTime a // LEFT JOIN dbo.PptClass b ON a.ShiftClassID=b.ObjID AND ProcedureID=1 // WHERE a.ShiftStart>@begintime AND a.ShiftEnd<@endtime"; // this._dbHelper.CommandType = CommandType.Text; // this._dbHelper.CommandText = sqlstr; // this._dbHelper.ClearParameter(); // this._dbHelper.AddParameter("@begintime", this._lastEquipStopTime); // this._dbHelper.AddParameter("@endtime", this._lastEquipStopTime); // dt = this._dbHelper.ToDataTable(); // DataRow dr = dt.Rows[0]; // sqlstr = @"INSERT INTO Ppt_pmdownrecord(Equip_code,Shift_id,Mp_startdate,Mp_enddate,Mp_ComCode,Handle_flag,shift_Class,From_Source,Soure_Flag) // VALUES (@Equip_code,@Shift_id,@Mp_startdate,@Mp_enddate,@Mp_ComCode,@Handle_flag,@shift_Class,@From_Source,@Soure_Flag)"; // this._dbHelper.CommandType = CommandType.Text; // this._dbHelper.CommandText = sqlstr; // this._dbHelper.ClearParameter(); // this._dbHelper.AddParameter("@Equip_code", this.EquipCode); // this._dbHelper.AddParameter("@Shift_id", dr["ShiftID"]); // this._dbHelper.AddParameter("@Mp_startdate", this._lastEquipStopTime); // this._dbHelper.AddParameter("@Mp_enddate", this._lastEquipStopTime); // this._dbHelper.AddParameter("@Mp_ComCode", ""); // this._dbHelper.AddParameter("@Handle_flag", "0"); // this._dbHelper.AddParameter("@shift_Class", dr["ShiftClassID"]); // this._dbHelper.AddParameter("@From_Source", this._lastEquipStopTime); // this._dbHelper.AddParameter("@Soure_Flag", "1"); // this._dbHelper.ExecuteNonQuery(); // } // } // public int EquipState // { // get // { // return _equipState; // } // private set // { // switch (value) // { // case 0: //密炼机停机 // { // this._lastRunfreeTime = DateTime.Now.AddDays(1); // if (this._lastEquipStopTime > DateTime.Now) // { // this._lastEquipStopTime = DateTime.Now; // } // if ((DateTime.Now - this._lastEquipStopTime).TotalSeconds >= 30) // { // EquipStateChanage(value); // //RecodeEquipStop(); // } // break; // } // case 1: //密炼机正常运行 // { // this._lastRunfreeTime = DateTime.Now.AddDays(1); // this._lastEquipStopTime = DateTime.Now.AddDays(1); // EquipStateChanage(value); // break; // } // case 2: //密炼机空转 // { // this._lastEquipStopTime = DateTime.Now.AddDays(1); // if (this._lastRunfreeTime > DateTime.Now) // { // this._lastRunfreeTime = DateTime.Now; // } // if ((DateTime.Now - this._lastRunfreeTime).TotalSeconds >= 30) // { // EquipStateChanage(value); // } // break; // } // default: // { // break; // } // } // } // } // public int RunfreeCurrent { get; set; } // public void EquipInfo(int currentTime, int speedRotor, double mixerCurrent) // { // this._currentTime = currentTime; // this._speedRotor = speedRotor; // this._mixerCurrent = mixerCurrent; // //自动密炼且当前炼胶时间大于0 // if (this._currentTime > 0 && this._speedRotor > 0) // { // this.EquipState = 1; // return; // } // //转速,电流小于等于0,为停机 // if (this._mixerCurrent <= 0 && this._speedRotor <= 0) // { // this.EquipState = 0; // return; // } // //电流大于空转电流 // if (this._mixerCurrent > this.RunfreeCurrent) // { // this.EquipState = 1; // return; // } // //密炼机空转 // this.EquipState = 2; // } // } #endregion } }