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/LjDeliver/DeliverInitAction.cs

179 lines
4.8 KiB
C#

using DevExpress.Data.Filtering.Helpers;
using DevExpress.Utils.FormShadow;
using Mesnac.Action.Base;
using Mesnac.Action.ChemicalWeighing.LjDeliver.Entity;
using Mesnac.Controls.Base;
using Mesnac.Controls.ChemicalWeighing;
using Mesnac.Controls.Default;
using Mesnac.Controls.Feeding;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;
namespace Mesnac.Action.ChemicalWeighing.LjDeliver
{
public class DeliverInitAction : ChemicalWeighingAction, IAction
{
List<Control> Controls;
HslBottleEntity bottleE = new HslBottleEntity();
SwitchLightEntity SwitchlightE = new SwitchLightEntity();
JzfEntity JzfE = new JzfEntity();
ThreeWayValveEntity ThreeWayValveE = new ThreeWayValveEntity();
LjSwitchEntity LjSwitchE = new LjSwitchEntity();
RSCREWBIGEntity RSCREWBIGE = new RSCREWBIGEntity();
MCLabelEntity LabelE = new MCLabelEntity();
private Timer timer;
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime); //必须调用
Controls = GetAllControls();
SetAllControls();
//wangsr();
timer = new Timer();
timer.Interval = 1000;
timer.Enabled = true;
timer.Tick += GetPlcValue;//添加事件
}
private void SetAllControls()
{
ControlsHelper.ControlImport<HslBottle>(bottleE, Controls);
ControlsHelper.ControlImport<SwitchLight>(SwitchlightE, Controls);
ControlsHelper.ControlImport<Jzf>(JzfE, Controls);
ControlsHelper.ControlImport<ThreeWayValve>(ThreeWayValveE, Controls);
ControlsHelper.ControlImport<LjSwith>(LjSwitchE, Controls);
ControlsHelper.ControlImport<RSCREWBIG>(RSCREWBIGE, Controls);
ControlsHelper.ControlImport<MCLabel>(LabelE, Controls);
}
private void GetPlcValue(object sender, EventArgs e)
{
if (GetAllDbMCControlsByOption(DbOptionTypes.QueryAndModify).Count == 0)
{
timer.Stop();
timer.Dispose();
}
}
/// <summary>
/// 测试程序
/// </summary>
private void wangsr()
{
Random rd = new Random();
bool[] bottleFlag = new bool[15];
for (int i = 0; i < bottleFlag.Length; i++)
{
bottleFlag[i] = Convert.ToBoolean(rd.Next(0, 2));
}
Type bottleType = bottleE.GetType();
PropertyInfo[] properties = bottleType.GetProperties();
ThreadStart threadStart = new ThreadStart(() =>
{
double n = 100 / 15;
int i = 0;
foreach (PropertyInfo sourceProperty in properties)
{
string name = sourceProperty.Name;
PropertyInfo property = bottleType.GetProperty(name);
HslBottle bottle = (HslBottle)property.GetValue(bottleE);
//bottle.Value = n * i++;
bottle.Value = rd.Next(100);
property.SetValue(bottleE, bottle);
}
int num;
var obj = Application.OpenForms["LjDeliver"];
while (obj != null)
{
num = 0;
foreach (PropertyInfo sourceProperty in properties)
{
string name = sourceProperty.Name;
PropertyInfo property = bottleType.GetProperty(name);
HslBottle bottle = (HslBottle)property.GetValue(bottleE);
if (bottle.Value <= 0.2)
{
bottleFlag[num] = true;
}
if (bottle.Value >= 99.8)
{
bottleFlag[num] = false;
}
if (bottleFlag[num])
{
bottle.Value += 0.2;
}
else
{
bottle.Value -= 0.2;
}
property.SetValue(bottleE, bottle);
num++;
}
Thread.Sleep(1);
obj = Application.OpenForms["LjDeliver"];
}
});
Thread thread = new Thread(threadStart);
thread.Start();
}
}
}