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.
lj_plc/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/ManualControl/InitAction.cs

638 lines
20 KiB
C#

using DataBlockHelper;
using DataBlockHelper.DBHelpers;
using DataBlockHelper.Entity.DB2106Entity;
using HslCommunication;
using HslCommunication.Instrument.DLT;
using log4net;
using log4net.Config;
using Mesnac.Action.Base;
using Mesnac.Action.ChemicalWeighing.FreeDb;
using Mesnac.Action.ChemicalWeighing.InterfaceDocking;
using Mesnac.Action.ChemicalWeighing.InterfaceDocking.DockingEntity;
using Mesnac.Action.ChemicalWeighing.LjMaterial;
using Mesnac.Action.ChemicalWeighing.LjReport.OpenDoor;
using Mesnac.Basic;
using Mesnac.Controls.Base;
using Mesnac.Controls.Default;
using Mesnac.Core.Service;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;
namespace Mesnac.Action.ChemicalWeighing.ManualControl
{
public class InitAction : ChemicalWeighingAction, IAction
{
DB2107Helper DB2107;
DB2103Helper DB2103;
List<DbMCControl> McControllist;
List<DbMCControl> _McControllist;
List<Control> allControl;
OperateResult<byte[]> Read;
byte[] content;
DbMCControl materialGridControl;
Timer timer;
int flag = 0;
int no = 0;
IBaseControl WaterActualValue1;
IBaseControl WaterActualValue2;
IBaseControl WaterActualValue3;
IBaseControl WaterActualValue4;
IBaseControl WaterMetValue1;
IBaseControl WaterMetValue2;
IBaseControl WaterMetValue3;
IBaseControl WaterMetValue4;
IBaseControl SpiralActValue1;
IBaseControl SpiralActValue2;
IBaseControl SpiralActValue3;
IBaseControl SpiralActValue4;
IBaseControl MetageState1;
IBaseControl MetageState2;
IBaseControl MetageState3;
IBaseControl MetageState4;
IBaseControl WaterOutState1;
IBaseControl WaterOutState2;
IBaseControl WaterOutState3;
IBaseControl WaterOutState4;
IBaseControl SpiralState1;
IBaseControl SpiralState2;
IBaseControl SpiralState3;
IBaseControl SpiralState4;
IBaseControl SetWaterValue1;
IBaseControl SetWaterValue2;
IBaseControl SetWaterValue3;
IBaseControl SetWaterValue4;
IBaseControl SetSpiralValue1;
IBaseControl SetSpiralValue2;
IBaseControl SetSpiralValue3;
IBaseControl SetSpiralValue4;
bool State1;
bool State2;
byte Byt;
Color backColor;
MCRadioButton Gel1;
MCRadioButton Gel2;
MCRadioButton Gel3;
MCRadioButton Gel4;
MCRadioButton Gel5;
MCRadioButton Gel6;
MCRadioButton Gel7;
MCRadioButton Gel8;
MCRadioButton Wet1;
MCRadioButton Wet2;
MCRadioButton Wet3;
MCRadioButton Wet4;
MCRadioButton Wet5;
MCRadioButton Wet6;
MCRadioButton Wet7;
MCRadioButton Wet8;
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime); //必须要调用的
McControllist = GetAllDbMCControlsByOption(DbOptionTypes.InitData);
_McControllist = GetAllDbMCControlsByOption(DbOptionTypes.Modify);
allControl = GetAllControls();
WaterActualValue1 = GetBaseControl("WaterActualValue1");
WaterActualValue2 = GetBaseControl("WaterActualValue2");
WaterActualValue3 = GetBaseControl("WaterActualValue3");
WaterActualValue4 = GetBaseControl("WaterActualValue4");
WaterMetValue1 = GetBaseControl("WaterMetValue1");
WaterMetValue2 = GetBaseControl("WaterMetValue2");
WaterMetValue3 = GetBaseControl("WaterMetValue3");
WaterMetValue4 = GetBaseControl("WaterMetValue4");
SpiralActValue1 = GetBaseControl("SpiralActValue1");
SpiralActValue2 = GetBaseControl("SpiralActValue2");
SpiralActValue3 = GetBaseControl("SpiralActValue3");
SpiralActValue4 = GetBaseControl("SpiralActValue4");
MetageState1 = GetBaseControl("MetageState1");
MetageState2 = GetBaseControl("MetageState2");
MetageState3 = GetBaseControl("MetageState3");
MetageState4 = GetBaseControl("MetageState4");
WaterOutState1 = GetBaseControl("WaterOutState1");
WaterOutState2 = GetBaseControl("WaterOutState2");
WaterOutState3 = GetBaseControl("WaterOutState3");
WaterOutState4 = GetBaseControl("WaterOutState4");
SpiralState1 = GetBaseControl("SpiralState1");
SpiralState2 = GetBaseControl("SpiralState2");
SpiralState3 = GetBaseControl("SpiralState3");
SpiralState4 = GetBaseControl("SpiralState4");
SetWaterValue1 = _GetBaseControl("SetWaterValue1");
SetWaterValue2 = _GetBaseControl("SetWaterValue2");
SetWaterValue3 = _GetBaseControl("SetWaterValue3");
SetWaterValue4 = _GetBaseControl("SetWaterValue4");
SetSpiralValue1 = _GetBaseControl("SetSpiralValue1");
SetSpiralValue2 = _GetBaseControl("SetSpiralValue2");
SetSpiralValue3 = _GetBaseControl("SetSpiralValue3");
SetSpiralValue4 = _GetBaseControl("SetSpiralValue4");
materialGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "ManualLogGridView").FirstOrDefault();
LogInsertToDatabase();
SetWaterValue1.MCValue = Setting.Default.SetWaterValue1;
SetWaterValue2.MCValue = Setting.Default.SetWaterValue2;
SetWaterValue3.MCValue = Setting.Default.SetWaterValue3;
SetWaterValue4.MCValue = Setting.Default.SetWaterValue4;
SetSpiralValue1.MCValue = Setting.Default.SetSpiralValue1;
SetSpiralValue2.MCValue = Setting.Default.SetSpiralValue2;
SetSpiralValue3.MCValue = Setting.Default.SetSpiralValue3;
SetSpiralValue4.MCValue = Setting.Default.SetSpiralValue4;
MesnacServiceManager.Instance.LoggingService.Info("配置已加载");
ManualLogControl("配置已加载 ");
Gel1 = GetMCRadioButton("Gel1");
backColor = Gel1.BackColor;
Gel1.CheckedChanged += McradionA_CheckedChanged;
Gel2 = GetMCRadioButton("Gel2");
Gel2.CheckedChanged += McradionA_CheckedChanged;
Gel3 = GetMCRadioButton("Gel3");
Gel3.CheckedChanged += McradionA_CheckedChanged;
Gel4 = GetMCRadioButton("Gel4");
Gel4.CheckedChanged += McradionA_CheckedChanged;
Gel5 = GetMCRadioButton("Gel5");
Gel5.CheckedChanged += McradionB_CheckedChanged;
Gel6 = GetMCRadioButton("Gel6");
Gel6.CheckedChanged += McradionB_CheckedChanged;
Gel7 = GetMCRadioButton("Gel7");
Gel7.CheckedChanged += McradionB_CheckedChanged;
Gel8 = GetMCRadioButton("Gel8");
Gel8.CheckedChanged += McradionB_CheckedChanged;
Wet1 = GetMCRadioButton("Wet1");
Wet1.CheckedChanged += Wet1_CheckedChanged;
Wet2 = GetMCRadioButton("Wet2");
Wet2.CheckedChanged += Wet1_CheckedChanged;
Wet3 = GetMCRadioButton("Wet3");
Wet3.CheckedChanged += Wet3_CheckedChanged;
Wet4 = GetMCRadioButton("Wet4");
Wet4.CheckedChanged += Wet3_CheckedChanged;
Wet5 = GetMCRadioButton("Wet5");
Wet5.CheckedChanged += Wet5_CheckedChanged;
Wet6 = GetMCRadioButton("Wet6");
Wet6.CheckedChanged += Wet5_CheckedChanged;
Wet7 = GetMCRadioButton("Wet7");
Wet7.CheckedChanged += Wet7_CheckedChanged;
Wet8 = GetMCRadioButton("Wet8");
Wet8.CheckedChanged += Wet7_CheckedChanged;
timer = new Timer();
timer.Interval = 1000;
timer.Enabled = true;
timer.Tick += new EventHandler(ReadFromPlc);//添加事件
}
private void Wet1_CheckedChanged(object sender, EventArgs e)
{
MCRadioButton mCRadio = sender as MCRadioButton;
Wet1.BackColor = backColor;
Wet2.BackColor=backColor;
mCRadio.BackColor = Color.GreenYellow;
}
private void Wet3_CheckedChanged(object sender, EventArgs e)
{
MCRadioButton mCRadio = sender as MCRadioButton;
Wet3.BackColor = backColor;
Wet4.BackColor = backColor;
mCRadio.BackColor = Color.GreenYellow;
}
private void Wet5_CheckedChanged(object sender, EventArgs e)
{
MCRadioButton mCRadio = sender as MCRadioButton;
Wet5.BackColor = backColor;
Wet6.BackColor = backColor;
mCRadio.BackColor = Color.GreenYellow;
}
private void Wet7_CheckedChanged(object sender, EventArgs e)
{
MCRadioButton mCRadio = sender as MCRadioButton;
Wet7.BackColor = backColor;
Wet8.BackColor = backColor;
mCRadio.BackColor = Color.GreenYellow;
}
private void Timer1_Tick()
{
bool mixok1 = PlcConnect.Instance.ReadBool("DB3000.0.0").Content;
Time(1, mixok1?1:0);
bool mixok2 = PlcConnect.Instance.ReadBool("DB3000.0.1").Content;
Time(2, mixok2 ? 1 : 0);
bool mixok3 = PlcConnect.Instance.ReadBool("DB3000.0.2").Content;
Time(3, mixok3 ? 1 : 0);
bool mixok4 = PlcConnect.Instance.ReadBool("DB3000.0.3").Content;
Time(4, mixok4 ? 1 : 0);
bool mixok5 = PlcConnect.Instance.ReadBool("DB3000.0.4").Content;
Time(5, mixok5 ? 1 : 0);
bool mixok6 = PlcConnect.Instance.ReadBool("DB3000.0.5").Content;
Time(6, mixok6 ? 1 : 0);
bool mixok7 = PlcConnect.Instance.ReadBool("DB3000.0.6").Content;
Time(7, mixok7 ? 1 : 0);
bool mixok8 = PlcConnect.Instance.ReadBool("DB3000.0.7").Content;
Time(8, mixok8 ? 1 : 0);
}
private void Time(int deviceNo,int statu)
{
OpenDoorService.Insert(new OpenDoorEntity()
{
DeviceId = deviceNo,
OpenTime = DateTime.Now,
CreateTime = DateTime.Now,
Status = statu
});
TankIsDischargedSyncEntity tankIsDischargedSyncEntity = new TankIsDischargedSyncEntity()
{
reqCode = System.Guid.NewGuid().ToString(),
reqTime = DateTime.Now,
deviceNo = deviceNo.ToString(),
state = statu.ToString(),
};
//即使网络不同 也不会影响上位机的使用
ThreadPool.QueueUserWorkItem(delegate
{
HttpResponse httpResponse = new HttpResponse();
// MesnacServiceManager.Instance.LoggingService.Info($"湿混机{deviceNo}下发请求");
string returnTest = httpResponse.PostResponse(tankIsDischargedSyncEntity);
// ManualLogControl($"湿混机{deviceNo}返回数据 " + returnTest);
MesnacServiceManager.Instance.LoggingService.Info($"湿混机{deviceNo}返回数据" + returnTest);
});
}
private MCRadioButton GetMCRadioButton(string name)
{
var mcradion= allControl.Where(t => t.Name == name).FirstOrDefault() as MCRadioButton;
return mcradion;
}
private void McradionA_CheckedChanged(object sender, EventArgs e)
{
MCRadioButton mCRadio=sender as MCRadioButton;
Gel1.BackColor = backColor;
Gel2.BackColor = backColor;
Gel3.BackColor = backColor;
Gel4.BackColor = backColor;
mCRadio.BackColor = Color.GreenYellow;
}
private void McradionB_CheckedChanged(object sender, EventArgs e)
{
MCRadioButton mCRadio = sender as MCRadioButton;
Gel5.BackColor = backColor;
Gel6.BackColor = backColor;
Gel7.BackColor = backColor;
Gel8.BackColor = backColor;
mCRadio.BackColor = Color.GreenYellow;
}
private void ReadFromPlc(object sender, EventArgs e)
{
DB2107 = new DB2107Helper();
DB2103 = new DB2103Helper();
flag += 1;
if (flag > 10)
{
flag = 0;
//LogInsertToDatabase();
}
no += 1;
if(no > 5)
{
no = 0;
Timer1_Tick();
}
if (GetAllDbMCControlsByOption(DbOptionTypes.InitData).Count == 0)
{
timer.Stop();
timer.Dispose();
}
WaterActualValue1.MCValue = DB2107.Weight.WScale_3.ActWeight;
WaterActualValue2.MCValue = DB2107.Weight.WScale_1.ActWeight;
WaterActualValue3.MCValue = DB2107.Weight.WScale_4.ActWeight;
WaterActualValue4.MCValue = DB2107.Weight.WScale_2.ActWeight;
Read = PlcConnect.Instance.Read("DB2119.24.0", 2);
content = Read.Content;
WaterMetValue1.MCValue = PlcConnect.Instance.ByteTransform.TransInt16(content, 0);
Read = PlcConnect.Instance.Read("DB2119.4.0", 2);
content = Read.Content;
WaterMetValue2.MCValue = PlcConnect.Instance.ByteTransform.TransInt16(content, 0);
Read = PlcConnect.Instance.Read("DB2119.34.0", 2);
content = Read.Content;
WaterMetValue3.MCValue = PlcConnect.Instance.ByteTransform.TransInt16(content, 0);
Read = PlcConnect.Instance.Read("DB2119.14.0", 2);
content = Read.Content;
WaterMetValue4.MCValue = PlcConnect.Instance.ByteTransform.TransInt16(content, 0);
SpiralActValue1.MCValue = DB2103.DM2ASF01.ACT_Speed;
SpiralActValue2.MCValue = DB2103.DM2BSF01.ACT_Speed;
SpiralActValue3.MCValue = DB2103.DM2CSF01.ACT_Speed;
SpiralActValue4.MCValue = DB2103.DM2DSF01.ACT_Speed;
Read = PlcConnect.Instance.Read("DB2119.28.0", 1);
content = Read.Content;
Byt = content[0];
State1 = Byt.GetBit(2);
State2 = Byt.GetBit(6);
if (State1)
{
MetageState1.MCValue = "正在称量";
}
else
{
MetageState1.MCValue = "停止称量";
}
if (State2)
{
WaterOutState1.MCValue = "正在排水";
}
else
{
WaterOutState1.MCValue = "停止排水";
}
Read = PlcConnect.Instance.Read("DB2119.8.0", 1);
content = Read.Content;
Byt = content[0];
State1 = Byt.GetBit(2);
State2 = Byt.GetBit(6);
if (State1)
{
MetageState2.MCValue = "正在称量";
}
else
{
MetageState2.MCValue = "停止称量";
}
if (State2)
{
WaterOutState2.MCValue = "正在排水";
}
else
{
WaterOutState2.MCValue = "停止排水";
}
Read = PlcConnect.Instance.Read("DB2119.38.0", 1);
content = Read.Content;
Byt = content[0];
State1 = Byt.GetBit(2);
State2 = Byt.GetBit(6);
if (State1)
{
MetageState3.MCValue = "正在称量";
}
else
{
MetageState3.MCValue = "停止称量";
}
if (State2)
{
WaterOutState3.MCValue = "正在排水";
}
else
{
WaterOutState3.MCValue = "停止排水";
}
Read = PlcConnect.Instance.Read("DB2119.18.0", 1);
content = Read.Content;
Byt = content[0];
State1 = Byt.GetBit(2);
State2 = Byt.GetBit(6);
if (State1)
{
MetageState4.MCValue = "正在称量";
}
else
{
MetageState4.MCValue = "停止称量";
}
if (State2)
{
WaterOutState4.MCValue = "正在排水";
}
else
{
WaterOutState4.MCValue = "停止排水";
}
Read = PlcConnect.Instance.Read("DB2119.60.0", 1);
content = Read.Content;
Byt = content[0];
State1 = Byt.GetBit(2);
if (State1)
{
SpiralState1.MCValue = "正在螺旋";
}
else
{
SpiralState1.MCValue = "停止螺旋";
}
Read = PlcConnect.Instance.Read("DB2119.82.0", 1);
content = Read.Content;
Byt = content[0];
State1 = Byt.GetBit(2);
if (State1)
{
SpiralState2.MCValue = "正在螺旋";
}
else
{
SpiralState2.MCValue = "停止螺旋";
}
Read = PlcConnect.Instance.Read("DB2119.104.0", 1);
content = Read.Content;
Byt = content[0];
State1 = Byt.GetBit(2);
if (State1)
{
SpiralState3.MCValue = "正在螺旋";
}
else
{
SpiralState3.MCValue = "停止螺旋";
}
//Read = PlcConnect.Instance.Read("DB2119..0", 1);
//content = Read.Content;
//Byt = content[0];
//State1 = Byt.GetBit(2);
//if (State1)
//{
// MetageState4.MCValue = "正在螺旋";
//}
//else
//{
// MetageState4.MCValue = "停止螺旋";
//}
}
private IBaseControl GetBaseControl(string name)
{
return McControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey == name).FirstOrDefault().BaseControl;
}
private IBaseControl _GetBaseControl(string name)
{
return _McControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey == name).FirstOrDefault().BaseControl;
}
private void LogInsertToDatabase()
{
DataTable table = DBHelp.GetTable(@"select top 200 id, logtext, logtime from Manual_Log order by id desc");
if (this.materialGridControl != null && this.materialGridControl.BaseControl != null)
{
this.materialGridControl.BaseControl.BindDataSource = null;
this.materialGridControl.BaseControl.BindDataSource = table;
}
else
{
ICSharpCode.Core.LoggingService<InitAction>.Warn("操作日志信息控件为Null...");
}
}
private void ManualLogControl(string logName)
{
var Log = new ManualLogEntity()
{
logtext = logName,
logtime = DateTime.Now,
userid = string.IsNullOrWhiteSpace(UserInfo.Instance.UserName) ? "Edit" : UserInfo.Instance.UserName
};
FreeSqlUnit.Instance.Insert<ManualLogEntity>(Log).ExecuteIdentity();
}
}
}