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

754 lines
20 KiB
C#

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using Mesnac.Controls.Base;
using System;
using Mesnac.Controls.Default;
using System.Drawing.Design;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Globalization;
using Mesnac.Basic;
using Mesnac.Codd.Session;
using System.Data;
using System.Text;
using Mesnac.Compressor.Entity;
using System.Threading;
using System.Reflection;
using Mesnac.Compressor.Station;
using PLCManager;
using System.IO;
namespace Mesnac.Controls.Compressor
{
[ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
public partial class MachineButton : Mesnac.Controls.Default.TextBox, IPurviewControl
{
private List<DesignAction> _clickActionList = new List<DesignAction>();
private List<DesignAction> _keyDownActionList = new List<DesignAction>();
private List<DesignAction> _keyUpActionList = new List<DesignAction>();
private bool _mcVisible = true; //保存可见性
private bool _mcEnabled = true; //保存可用性
private bool _isValid = true; //保存有效性
private object _mcvalue;
public event EventHandler ReshreshStationData;
public MachineButton()
{
InitializeComponent();
this.ReadOnly = true;
this.Multiline = true;
this.Size = new Size(150,150);
this.BackColor = Color.LightCyan;
SetDefautText();
}
public MachineButton(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public List<DesignAction> ClickActionList
{
get
{
return this._clickActionList;
}
set
{
this._clickActionList = value;
}
}
public List<DesignAction> KeyDownActionList
{
get { return _keyDownActionList; }
set { _keyDownActionList = value; }
}
public List<DesignAction> KeyUpActionList
{
get { return _keyUpActionList; }
set { _keyUpActionList = value; }
}
//业务
public string MCKey
{
get;
set;
}
/// <summary>
/// 机台号
/// </summary>
public string MCMachine
{
get;
set;
}
private MachineInfo machine;
public MachineInfo Machine
{
get
{
if (machine == null)
{
InitMachineInfo();
}
return machine;
}
}
public object MCValue
{
get { return _mcvalue; }
set
{
if (value == null)
return;
object[] objectarray;
if (!TryObject(value, out objectarray))
{
return;
}
_mcvalue = value;
DataFormat(objectarray);
SetText();
}
}
private void InitMachineInfo()
{
if (string.IsNullOrEmpty(MCMachine))
{
return;
}
DbHelper dbHelper = DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Server);
try
{
if (dbHelper == null)
{
return ;
}
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = "";
string sql = "Exec SP_Pro_getMachine @MachineID";
dbHelper.AddParameter("@MachineID", MCMachine);
dbHelper.CommandText = sql;
DataSet ds = (DataSet)dbHelper.ToDataSet();
GetInfoName(ds);
}
catch (Exception e)
{
ICSharpCode.Core.LoggingService.Error("网络服务器请求失败");
return ;
}
}
private void GetInfoName(DataSet ds)
{
machine = new MachineInfo();
//机台表
try
{
DataTable dt = ds.Tables[0];
if (dt != null && dt.Rows.Count > 0)
{
machine.MachineID = dt.Rows[0]["ID"].ToString();
machine.MachineName = dt.Rows[0]["machinename"].ToString();
}
}
catch
{
}
//工位表
DataTable dt2 = ds.Tables[1];
try
{
if (dt2 != null && dt2.Rows.Count > 0)
{
///工位一参数
machine.StationOne.stationID = dt2.Rows[0]["StationID"].ToString();
machine.StationOne.StationName = dt2.Rows[0]["StationName"].ToString();
machine.StationOne.TableName = dt2.Rows[0]["TableName"].ToString();
machine.StationOne.ParaCount = Convert.ToInt32(dt2.Rows[0]["paraCount"]);
machine.StationOne.StationType = dt2.Rows[0]["Type_Class"].ToString();
machine.StationOne.PLCName = dt2.Rows[0]["PLCName"].ToString();
machine.StationOne.PLCBlock = Convert.ToInt32(dt2.Rows[0]["PLCBlock"]);
machine.StationOne.NGTakeStation = Convert.ToInt32(dt2.Rows[0]["IsNGStation"])>0;
}
}
catch(Exception e)
{
}
try
{
//工位二参数
if (dt2 != null && dt2.Rows.Count > 1)
{
machine.StationTwo.stationID = dt2.Rows[1]["StationID"].ToString();
machine.StationTwo.StationName = dt2.Rows[1]["StationName"].ToString();
machine.StationTwo.TableName = dt2.Rows[1]["TableName"].ToString();
machine.StationTwo.ParaCount = Convert.ToInt32(dt2.Rows[1]["paraCount"]);
machine.StationTwo.StationType = dt2.Rows[1]["Type_Class"].ToString();
machine.StationOne.PLCName = dt2.Rows[0]["PLCName"].ToString();
machine.StationOne.PLCBlock = Convert.ToInt32(dt2.Rows[0]["PLCBlock"]);
machine.StationOne.NGTakeStation = Convert.ToInt32(dt2.Rows[0]["IsNGStation"]) > 0;
}
}
catch(Exception e)
{
}
}
public void OnRerefreshData()
{
//这里怎么弄需要给plc这个位置置0或1 肯定需要
if (machine.StationOne.Data.NewFlag )
{
ResetPLCState(StationOneAdrress);
ThreadPool.QueueUserWorkItem(new WaitCallback(SaveStationdata), machine.StationOne);
}
if(machine.StationTwo.Data.NewFlag)
{
ResetPLCState(StationTwoAddress);
ThreadPool.QueueUserWorkItem(new WaitCallback(SaveStationdata), machine.StationTwo);
}
if (ReshreshStationData != null)
{
ReshreshStationData(machine,null);
}
}
private void ResetPLCState(string EquipKey)
{
object[] data=floatToObject(1);
PlcData.Instance.PlcWriteByEquipKey(EquipKey, 4, data);
}
public void SaveStationdata(object o)
{
var station = o as StationInfo;
if (station == null)
{
return;
}
#region 生成动态处理类
string stationType = station.StationType;
//根据类型,动态生成业务类
Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集
string AssemName = "Mesnac.Compressor.Station." + stationType;
var obj = assembly.CreateInstance(AssemName, true);
var stationManager = obj as IStation;
//if (stationManager == null)
//{
// return;
//}
#endregion
stationManager = ReflectClass(stationType);
stationManager.HandleData(station);
}
private object _stationone;
public stationPara station1para
{
get;
set;
}
private int time = 0;
/// <summary>
///
/// </summary>
public object StationOne
{
get { return _stationone; }
set
{
if (value == null)
return;
Console.WriteLine("读取第"+time.ToString()+"次 时间:"+DateTime.Now.ToString());
time++;
object[] objectarray;
if (!TryObject(value, out objectarray))
{
return;
}
_stationone = value;
machine.StationOne.Data = ParaFormart(objectarray, machine.StationOne.ParaCount);
OnRerefreshData();
}
}
public string StationOneAdrress
{ get; set; }
public string StationTwoAddress { get; set; }
public stationPara station2para
{
get;
set;
}
private object _stationtwo = null;
/// <summary>
///
/// </summary>
public object StationTwo
{
get { return _mcvalue; }
set
{
if (value == null)
return;
object[] objectarray;
if (!TryObject(value, out objectarray))
{
return;
}
_mcvalue = value;
machine.StationTwo.Data = ParaFormart(objectarray, machine.StationTwo.ParaCount);
//OnRerefreshData();
}
}
/// <summary>
/// 单工位数据解析
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public stationPara ParaFormart(object [] obj,int paraCount)
{
stationPara sinfo = new stationPara();
int index = 0;
int istate=ConvertToInt(obj, index);
sinfo.state = stateString(istate);
index += 2;
sinfo.data = ConvertToString(obj, index);
//数据保存区
index += 2;
//sinfo.NewFlag = ConvertToInt(obj, index);
index += 2;
sinfo.ProductOkNg = ConvertToInt(obj, index);
index += 2;
sinfo.RFIDNo = "123456789012345678901234";
index += 2;
sinfo.productDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
index += 4;
sinfo.ProductBarcode = "44555666777888999000";
index += 90;
for (int i = 0; i < paraCount; i++)
{
dataUnite du = new dataUnite();
du.actValue = ConvertToString(obj, index);
du.MaxValue = ConvertToString(obj, index+2);
du.MinValue = ConvertToString(obj, index + 4);
du.result = ConvertToInt(obj, index+6);
du.Remark = ConvertToString(obj, index + 8);
index += 8;
sinfo.SavedataList.Add(du);
}
return sinfo;
}
private string ConvertToString(object[] obj, int index)
{
float data = convertTofloat(obj, index);
return data.ToString("F2");
}
private int ConvertToInt(object[] obj, int index)
{
try
{
float fstate = convertTofloat(obj, index);
return Convert.ToInt32(fstate);
}
catch
{
return 0;
}
}
private string stateString(int state)
{
switch (state)
{
case 0:
return "停止";
case 1:
return "手动";
case 2:
return "自动";
default:
return "未知";
}
}
public void DataFormat(object[] obj)
{
int state=ConvertToInt(obj,0);
this.State = stateString(state);
float quality = convertTofloat(obj, 2);
this.productCount = Convert.ToInt32(quality).ToString();
}
/// <summary>
/// 如果是数组获取第一个字的值如果是object获取Int值
/// </summary>
/// <param name="ob"></param>
/// <returns></returns>
private int GetValue(object ob)
{
//bool isArry = false;
int IntValue = 0;
try
{
object[] value = (object[])ob;
IntValue = Convert.ToInt32(value[0]);
}
catch
{
IntValue = Convert.ToInt32(ob);
}
return IntValue;
}
private string _machinename="";
public string MachineName
{
get
{
return _machinename;
}
set
{
_machinename = value;
}
}
private string _state = "";
public string State
{
get
{
return _state;
}
set
{
_state = value;
}
}
private string _productCount = "";
public string productCount
{
get
{
return _productCount;
}
set
{
_productCount = value;
}
}
public void SetText()
{
try
{
string txt = "机台名称:" + Machine.MachineName + "\r\n";
txt += "机台状态:" + State + "\r\n";
txt += "生产数量:" + productCount;
this.Text = txt;
}
catch
{
}
}
private void SetDefautText()
{
string txt = "工位名称:\r\n";
txt += "工位状态:未知\r\n";
txt += "生产数量0";
this.Text = txt;
}
public IBaseControl MCRoot
{
get
{
return null;
}
set
{
;
}
}
#region 数据库的屏蔽,现在用不着
[TypeConverter(typeof(DataSourceConverter))]
[Description("数据连接"), Category("Data")]
public string MCDataSourceID
{
get;
set;
}
public MCDataSource MCDataSource
{
get;
set;
}
[Description("是否为数据库控件"), Category("Data")]
public bool IsDbControl
{
get;
set;
}
[Description("初始化SQL"), Category("Data")]
public string InitDataSource
{
get;
set;
}
[Description("执行SQL"), Category("Data")]
public string ActionDataSource
{
get;
set;
}
[Description("绑定数据源"), Category("Data")]
public object BindDataSource
{
get;
set;
}
[Description("操作类型"), Category("Data")]
public DbOptionTypes DbOptionType
{
get;
set;
}
#endregion
[Description("是否可见"), Category("Behavior")]
public bool MCVisible
{
get
{
return this._mcVisible;
}
set
{
this._mcVisible = value == null ? true : value;
if (this.Site.DesignMode)
{
this.Visible = true;
}
else
{
this.Visible = this._mcVisible;
}
}
}
[Description("是否可用"), Category("Behavior")]
public bool MCEnabled
{
get
{
return this._mcEnabled;
}
set
{
this._mcEnabled = value == null ? true : value;
if (this.Site.DesignMode)
{
this.Enabled = true;
}
else
{
this.Enabled = this._mcEnabled;
}
}
}
[Description("是否控制权限"), Category("Behavior")]
public bool MCPurview
{
get;
set;
}
public bool IsValid
{
get { return _isValid; }
set { _isValid = value; }
}
private float convertTofloat(object[] buff,int startIndex)
{
try
{
int b = Convert.ToInt32(buff[startIndex]);
int a = Convert.ToInt32(buff[startIndex + 1]);
byte[] low = System.BitConverter.GetBytes(b);
byte[] high = System.BitConverter.GetBytes(a);
byte[] abc = new byte[4];
for (int i = 0; i < 2; i++)
{
abc[i] = low[i];
}
for (int i = 0; i < 2; i++)
{
abc[i + 2] = high[i];
}
return BitConverter.ToSingle(abc, 0);
}
catch
{
return -1;
}
}
public Object[] floatToObject(float ff)
{
//正负符号默认为0
byte flag = 0;
if (ff < 0)
{
flag = 128;
ff = ff * -1;
}
byte[] floatByte = BitConverter.GetBytes(ff);
byte[] lowhByte = new byte[4];
lowhByte[0] = floatByte[0];
lowhByte[1] = floatByte[1];
lowhByte[2] = 0;
lowhByte[3] = 0;
byte[] highByte = new byte[4];
highByte[0] = floatByte[2];
highByte[1] = floatByte[3];
highByte[2] = 0;
highByte[3] = 0;
highByte[1] = (byte)(highByte[1] + flag);
UInt32 low = BitConverter.ToUInt32(lowhByte, 0);
int high = BitConverter.ToInt16(highByte, 0);
object[] buff = new object[2];
buff[0] = low;
buff[1] = high;
return buff;
}
private bool TryObject(object value, out object[] arry)
{
arry = null;
try
{
arry = (object[])value;
return true;
}
catch
{
return false;
}
}
private IStation ReflectClass(string Name)
{
string thisPath = this.GetType().Assembly.Location;
string assPath = (new FileInfo(thisPath)).Directory + "\\Mesnac.Compressor.Station";
//string dllName = "Mesnac.Compressor.Station";
string path = assPath + ".dll";
string className = "Mesnac.Compressor.Station."+Name;
if (!File.Exists(path))
{
return null;
}
Assembly ass = Assembly.LoadFile(path);
if (ass == null)
{
return null;
}
var classname=ass.CreateInstance(className,true);
Type type = ass.GetType(className);
if (type == null)
{
return null;
}
IStation to = (IStation)Activator.CreateInstance(type);
return to;
}
private void batchFeeder1_Load(object sender, EventArgs e)
{
}
}
}