天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
8.8 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ptl.Device;
using System.Xml.Serialization;
using System.IO;
using QMFrameWork.Log;
using QMAPP.FJC.Entity.FwMark;
namespace QMAPP.FJC.BLL.X_Gate
{
public class LightController
{
static System.Threading.Timer timer;
static Ptl.Device.InstallProject Project;
static bool XGateStarted = false;
static LightController()
{
try
{
timer = new System.Threading.Timer(new System.Threading.TimerCallback(timer_tick));
Project = new Ptl.Device.InstallProject();
//反序列化工程文件
string projectFilePath = System.Configuration.ConfigurationManager.AppSettings["XGateProjectPath"];
XmlSerializer projectSerializer = new XmlSerializer(typeof(InstallProject));
using (FileStream fileStream = new FileStream(projectFilePath, FileMode.Open, FileAccess.Read))
{
Project = (InstallProject)projectSerializer.Deserialize(fileStream);
}
Ptl.Device.Log.Logger.HoldingPeriodInDays = 7;
XGateStarted = false;
StartUnicastCommandQueue();
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "XGate指示灯控制"
});
}
}
static bool on;
static void timer_tick(object obj)
{
if (Project != null)
{
foreach (XGate xgate in Project.XGates)
{
if (xgate.XGateUnicastCommunicationClient.Connected == true)
{
if (on)
{
string colorstr = System.Configuration.ConfigurationManager.AppSettings["XGatePanelLight"];
Ptl.Device.Communication.Command.LightColor color = Ptl.Device.Communication.Command.LightColor.Cyan;
Enum.TryParse(colorstr, out color);
xgate.DisplayLight(new Ptl.Device.Communication.Command.LightMode { Color = color, Period = Ptl.Device.Communication.Command.LightOnOffPeriod.Period1000 });
}
else
{
xgate.ClearLight();
}
on = !on;
}
else
{
//启动所有总线的通讯
//xgate.StartUnicastCommandQueue();
}
}
}
}
static void StartUnicastCommandQueue()
{
try
{
foreach (XGate xgate in Project.XGates)
{
//启动所有总线的通讯
xgate.StartUnicastCommandQueue();
xgate.DisplayLight(new Ptl.Device.Communication.Command.LightMode { Color = Ptl.Device.Communication.Command.LightColor.Cyan, Period = Ptl.Device.Communication.Command.LightOnOffPeriod.Period1000 });
}
XGateStarted = true;
timer.Change(1000, 5000);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "XGate指示灯控制"
});
}
}
//static ~LightController()
//{
// if (timer != null)
// {
// timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
// timer.Dispose();
// }
// if (Project != null)
// {
// foreach (XGate xgate in Project.XGates)
// xgate.Dispose();
// Project = null;
// }
//}
/// <summary>
/// 重新加载项目
/// </summary>
public void ReloadProject()
{
try
{
if (Project != null)
{
foreach (XGate xgate in Project.XGates)
xgate.Dispose();
Project = null;
}
Project = new Ptl.Device.InstallProject();
//反序列化工程文件
string projectFilePath = System.Configuration.ConfigurationManager.AppSettings["XGateProjectPath"];
XmlSerializer projectSerializer = new XmlSerializer(typeof(InstallProject));
using (FileStream fileStream = new FileStream(projectFilePath, FileMode.Open, FileAccess.Read))
{
Project = (InstallProject)projectSerializer.Deserialize(fileStream);
}
Ptl.Device.Log.Logger.HoldingPeriodInDays = 7;
XGateStarted = false;
StartUnicastCommandQueue();
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "XGate指示灯控制"
});
}
}
public static void Load()
{
}
public void LightOnCell(string workcellcode, List<String> materials)
{
try
{
List<FWMarkParameter> lightsOnCell = new List<FWMarkParameter>();
lightsOnCell = new DAL.Basic.MarkAddrDAL().GetMarkOnCell(workcellcode);
foreach (var gate in lightsOnCell.GroupBy(p => p.URL))
{
if (Project.XGates[gate.Key] == null)
break;
foreach (var bus in gate.GroupBy(p => p.PORT))
{
byte busindex = 0;
if (!byte.TryParse(bus.Key, out busindex))
break;
if (Project.XGates[gate.Key].Buses.Count <= busindex)
break;
var addrs = bus.Select(p => byte.Parse(p.MARKADDR)).ToList();
Project.XGates[gate.Key].Buses[busindex].MulticastClear900U(addrs);
}
}
System.Threading.Thread.Sleep(200);
if (materials == null || materials.Count == 0)
return;
foreach (var gate in lightsOnCell.GroupBy(p => p.URL))
{
if (Project.XGates[gate.Key] == null)
break;
foreach (var bus in gate.GroupBy(p => p.PORT))
{
byte busindex = 0;
if (!byte.TryParse(bus.Key, out busindex))
break;
if (Project.XGates[gate.Key].Buses.Count <= busindex)
break;
var addrs = bus
.Where(p => materials.Exists(m => m == p.PARTSCODE))
.Select(p => byte.Parse(p.MARKADDR))
.ToList();
if (addrs.Count > 0)
{
var colorstr = bus.FirstOrDefault(p => materials.Exists(m => m == p.PARTSCODE)).COLOR;
var color=Ptl.Device.Communication.Command.LightColor.Green;
Enum.TryParse(colorstr,true,out color);
//Project.XGates[gate.Key].Buses[busindex].MulticastClear900U(addrs);
Project
.XGates[gate.Key]
.Buses[busindex]
.MulticastDisplay900U(addrs
, Ptl900UType.P03
, new Ptl.Device.Communication.Command.Display900UItem { }
, new Ptl.Device.Communication.Command.LightMode
{
Color = color
});
}
}
}
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "XGate指示灯控制"
});
}
}
}
}