using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Action.Feeding.BasicInfo;
namespace Mesnac.Action.Feeding.FinishBatch
{
///
/// PLC每车报表存盘数据区解析类
///
public class SaveDataPerLotHandler
{
#region 字段定义
private int _nowche; //密炼完成次数
private int _rubFinshNum; //胶料完成次数
private int _cBFinshNum; //炭黑完成次数
private int _fLFinshNum; //粉料完成次数
private int _oilFinshNum; //油1完成次数
private byte _rubStatus; //胶料秤称量状态
private byte _cBStatus; //炭黑秤称量状态
private byte _fLStatus; //粉料秤称量状态
private byte _oilStatus; //油料秤称量状态
private byte _mixStatus; //密炼机状态
private double _powerOpenDoor; //排胶功率
private double _energyOpenDoor; //排胶能量
private double _tempOpenDoor; //排胶温度
private int _timeOpenDoor; //排胶时间
private int _startYear; //密炼开始年份
private int _startMonth; //密炼开始月份
private int _startDay; //密炼开始天
private int _startHour; //密炼开始小时
private int _startMinute; //密炼开始分钟
private int _startSecond; //密炼开始秒
private int _done_allrtime; //炼胶总时间
private int _rubTime; //加胶时间
private int _cBTime; //加炭黑时间
private int _oilTime; //加油1时间
private int _oil2FinshNum; //油2完成数
private byte _oil2Status; //油秤2称量状态
private int _oil2Time; //加油2时间
private int _flTime; //加粉料时间
private string _strTime; //日期时间格式:yyyy-MM-dd HH:mm:ss
private static int _recipeTime; //实时保存配方时间
#endregion
#region 构造方法
///
/// 构造方法
///
public SaveDataPerLotHandler() { this.ParseSaveDataPerLotFromPlc(); }
#endregion
#region 属性定义
///
/// 密炼完成次数
///
public int Nowche
{
get { return _nowche; }
set { _nowche = value; }
}
///
/// 胶料完成次数
///
public int RubFinshNum
{
get { return _rubFinshNum; }
set { _rubFinshNum = value; }
}
///
/// 炭黑完成次数
///
public int CBFinshNum
{
get { return _cBFinshNum; }
set { _cBFinshNum = value; }
}
///
/// 粉料完成次数
///
public int FLFinshNum
{
get { return _fLFinshNum; }
set { _fLFinshNum = value; }
}
///
/// 油料1完成次数
///
public int OilFinshNum
{
get { return _oilFinshNum; }
set { _oilFinshNum = value; }
}
///
/// 胶料秤称量状态
///
public byte RubStatus
{
get { return _rubStatus; }
set { _rubStatus = value; }
}
///
/// 炭黑秤称量状态
///
public byte CBStatus
{
get { return _cBStatus; }
set { _cBStatus = value; }
}
///
/// 粉料秤称量状态
///
public byte FLStatus
{
get { return _fLStatus; }
set { _fLStatus = value; }
}
///
/// 油料秤1称量状态
///
public byte OilStatus
{
get { return _oilStatus; }
set { _oilStatus = value; }
}
///
/// 密炼机状态
///
public byte MixStatus
{
get { return _mixStatus; }
set { _mixStatus = value; }
}
///
/// 炼胶时间
///
public int TimeOpenDoor
{
get { return _timeOpenDoor; }
set { _timeOpenDoor = value; }
}
///
/// 排胶温度
///
public double TempOpenDoor
{
get { return _tempOpenDoor; }
set { _tempOpenDoor = value; }
}
///
/// 排胶功率
///
public double PowerOpenDoor
{
get { return _powerOpenDoor; }
set { _powerOpenDoor = value; }
}
///
/// 排胶能量
///
public double EnergyOpenDoor
{
get { return _energyOpenDoor; }
set { _energyOpenDoor = value; }
}
///
/// 密炼开始年份
///
public int StartYear
{
get { return _startYear; }
set { _startYear = value; }
}
///
/// 密炼开始月份
///
public int StartMonth
{
get { return _startMonth; }
set { _startMonth = value; }
}
///
/// 密炼开始天
///
public int StartDay
{
get { return _startDay; }
set { _startDay = value; }
}
///
/// 密炼开始小时
///
public int StartHour
{
get { return _startHour; }
set { _startHour = value; }
}
///
/// 密炼开始分钟
///
public int StartMinute
{
get { return _startMinute; }
set { _startMinute = value; }
}
///
/// 密炼开始秒
///
public int StartSecond
{
get { return _startSecond; }
set { _startSecond = value; }
}
///
/// 炼胶总时间(卸料门关到位时刻)
///
public int Done_allrtime
{
get { return _done_allrtime; }
set { _done_allrtime = value; }
}
///
/// 加胶时间
///
public int RubTime
{
get { return _rubTime; }
set { _rubTime = value; }
}
///
/// 加炭黑时间
///
public int CBTime
{
get { return _cBTime; }
set { _cBTime = value; }
}
///
/// 加油1时间
///
public int OilTime
{
get { return _oilTime; }
set { _oilTime = value; }
}
///
/// 油2完成数
///
public int Oil2FinshNum
{
get { return _oil2FinshNum; }
set { _oil2FinshNum = value; }
}
///
/// 油秤2称量状态
///
public byte Oil2Status
{
get { return _oil2Status; }
set { _oil2Status = value; }
}
///
/// 加油2时间
///
public int Oil2Time
{
get { return _oil2Time; }
set { _oil2Time = value; }
}
///
/// 加粉料时间
///
public int FlTime
{
get { return _flTime; }
set { _flTime = value; }
}
///
/// 密开始时间;日期时间格式:yyyy-MM-dd HH:mm:ss
///
public string StrTime
{
get { return _strTime; }
set { _strTime = value; }
}
///
/// 用于实时保存配方时间
///
public static int RecipeTime
{
get { return SaveDataPerLotHandler._recipeTime; }
set { SaveDataPerLotHandler._recipeTime = value; }
}
#endregion
#region 解析方法
///
/// 解析方法
///
private void ParseSaveDataPerLotFromPlc()
{
object saveDataPerLot = PlcData.Instance.SaveDataPerLot.LastValue.ToObject();
if (saveDataPerLot == null)
{
ICSharpCode.Core.LoggingService.Warn("从PLC中获取每车存盘(SaveDataPerLot)数据失败_NULL!");
return;
}
if (!(saveDataPerLot is object[]))
{
ICSharpCode.Core.LoggingService.Warn("从PLC中获取每车存盘(SaveDataPerLot)数据失败_Not Array!");
return;
}
object[] values = saveDataPerLot as object[];
if (values.Length != 28)
{
ICSharpCode.Core.LoggingService.Warn("从PLC中获取每车存盘(SaveDataPerLot)数据失败_长度不争取,应该为28个字!");
return;
}
this._nowche = this.ToInt(values[0]); //密炼完成次数
this._rubFinshNum = this.ToInt(values[1]); //胶料完成次数
this._cBFinshNum = this.ToInt(values[2]); //炭黑完成次数
this._fLFinshNum = this.ToInt(values[3]); //粉料完成数
this._oilFinshNum = this.ToInt(values[4]); //油1完成次数
this._rubStatus = this.ToByte(values[5]); //胶料秤称量状态
this._cBStatus = this.ToByte(values[6]); //炭黑秤称量状态
this._fLStatus = this.ToByte(values[7]); //粉料秤称量状态
this._oilStatus = this.ToByte(values[8]); //油1秤称量状态
this._mixStatus = this.ToByte(values[9]); //密炼机状态
this._timeOpenDoor = this.ToInt(values[10]); //炼胶时间(卸料门开到位时刻)
this._tempOpenDoor = this.ToDouble(values[11]) / 10.0; //排胶温度
this._powerOpenDoor = this.ToDouble(values[12]); //排胶功率
this._energyOpenDoor = this.ToDouble(values[13]) / 10.0; //排胶能量
this._startYear = this.ToInt(values[14]); //密炼开始年份
this._startMonth = this.ToInt(values[15]); //密炼开始月份
this._startDay = this.ToInt(values[16]); //密炼开始天
this._startHour = this.ToInt(values[17]); //密炼开始小时
this._startMinute = this.ToInt(values[18]); //密炼开始分钟
this._startSecond = this.ToInt(values[19]); //密炼开始秒
this._done_allrtime = this.ToInt(values[20]); //炼胶总时间(卸料门关到位时刻)
this._rubTime = this.ToInt(values[21]); //加胶时间
this._cBTime = this.ToInt(values[22]); //一次加炭黑
this._oilTime = this.ToInt(values[23]); //1号油秤加油时间
this._oil2FinshNum = this.ToInt(values[24]); //油2完成数
this._oil2Status = this.ToByte(values[25]); //油秤2称量状态
this._oil2Time = this.ToInt(values[26]); //2号油秤加油时间
this._flTime = this.ToInt(values[27]); //加粉料时间
this._strTime = String.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", this._startYear, this._startMonth, this._startDay, this._startHour, this._startMinute, this._startSecond);
//DateTime nowtime = DateTime.Now;
//DateTime mixtime = Convert.ToDateTime(_strTime);
ICSharpCode.Core.LoggingService.Debug("=================================================");
ICSharpCode.Core.LoggingService.Debug("密炼开始时间(PLC原始值):" + this._strTime);
DateTime strTime = DateTime.Now;
if (DateTime.TryParse(this._strTime, out strTime))
{
if ((DateTime.Now - Convert.ToDateTime(this._strTime)).TotalSeconds > 300 || (DateTime.Now - Convert.ToDateTime(this._strTime)).TotalSeconds < 0)
{
this._strTime = String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now.AddSeconds(-this._done_allrtime));
}
}
else
{
this._strTime = String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now.AddSeconds(-this._done_allrtime));
ICSharpCode.Core.LoggingService.Warn("PLC密炼开始时间无法转换为日期格式!");
}
ICSharpCode.Core.LoggingService.Debug("密炼开始时间(处理后的值):" + this._strTime);
ICSharpCode.Core.LoggingService.Debug("=================================================");
}
private byte ToByte(object value)
{
byte result = 0;
if (value != null && value != System.DBNull.Value)
{
byte.TryParse(value.ToString(), out result);
}
return result;
}
private int ToInt(object value)
{
int result = 0;
if (value != null && value != System.DBNull.Value)
{
int.TryParse(value.ToString(), out result);
}
return result;
}
private double ToDouble(object value)
{
double result = 0;
if (value != null && value != System.DBNull.Value)
{
double.TryParse(value.ToString(), out result);
}
return result;
}
#endregion
}
}