using NX_CommonClassLibrary;
using NX_LogClassLibrary;
using NX_ModelClassLibrary.CustomEnum;
using NX_ModelClassLibrary.CustomEvent;
using NX_ModelClassLibrary.OpcModel;
using OPCAutomation;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NX_OpcClassLibrary
{
///
/// 先导智能OPC初始化和OPCItem变量初始化
/// Copyright20181102 (C) sunyalong
/// 允许修改、添加满足自己项目的需要。
/// 添加、修改后请详细注释。违者会强制删除不予采用。
///
public class LeadOpcOperate
{
#region 单例模式
///
/// 单例模式对象
///
private static LeadOpcOperate _instance = null;
private static readonly object lockObj = new object();
///
/// 单例模式方法
///
public static LeadOpcOperate Instance
{
get
{
if (_instance == null)
{
lock (lockObj)
{
if (_instance == null)
{
_instance = new LeadOpcOperate();
}
}
}
return _instance;
}
}
#endregion
#region OPCItem变量申明区(所有的OPCItem变量均放于此区域)
///
/// OPC操作类对象
///
private readonly LeadOPCServer OPC = new LeadOPCServer();
///
/// OPC实体类对象集合
///
public List listOpcItem = new List();
///
/// 日志显示帮助类对象
///
public ShowLogToFrmHelper showLogToFrm = new ShowLogToFrmHelper();
///
/// 日志头部
///
private readonly string LogHeadText = "OPC业务操作类 ==>> ";
#endregion
#region OPC初始化
///
/// 初始化OPC服务器和OPCItem变量
///
/// OPC数据库配置表
///
public bool OpcInit(List wcsOpcItemMdLst)
{
try
{
//枚举本地OPC服务器,
//如果是西门子的OPC服务器一般用OPC.SimaticNET.1;
//如果是KEPServerEx的服务器一般用KEPware.KEPServerEx.V4;
List Opclist = OPC.GetLocalServer();
string OPCServerName = "KEPware.KEPServerEx.V4";
foreach (string tempOpc in Opclist)
{
if (tempOpc == OPCServerName)
{
if (OPC.ConnectRemoteServer("", tempOpc))
{
if (!OPC.CreateGroup())
{
return false;
}
else
{
//从数据库配置表查询OPC名称、地址等信息并进行配置。
int a = 1;
foreach (WcsOpcItemMd md in wcsOpcItemMdLst)
{
WcsOpcItemExtMd opcItem = SetChildPropertiesToParent(md);
OPCItem opcitem = OPC.OPCItems.AddItem(md.OpcItemPos, a);
opcItem.opc_item = opcitem;
listOpcItem.Add(opcItem);
a++;
}
}
}
}
}
ShowLogToForm($"初始化OPC变量操作类成功!", true, LogTypeEnum.Run);
return true;
}
catch (Exception ex)
{
ShowLogToForm($"初始化OPC变量操作类发生异常:【{ex.Message}】", true, LogTypeEnum.Err);
return false;
}
}
#endregion
#region 父类属性值赋值到子类属性
private WcsOpcItemExtMd SetChildPropertiesToParent(WcsOpcItemMd wcsOpcItemMd)
{
WcsOpcItemExtMd opc = new WcsOpcItemExtMd();
var ParentType = typeof(WcsOpcItemMd);
var Properties = ParentType.GetProperties();
foreach (var Propertie in Properties)
{
if (Propertie.CanRead && Propertie.CanWrite)
{
Propertie.SetValue(opc, Propertie.GetValue(wcsOpcItemMd, null), null);
}
}
return opc;
}
#endregion
#region OPC 读、写
///
/// 读取OPC变量值
///
/// opc变量对象
///
public object ReadOpcItemValue(WcsOpcItemMd wcsOpcItemMd)
{
WcsOpcItemExtMd opc = listOpcItem.FirstOrDefault(x => x.OpcItemCode == wcsOpcItemMd.OpcItemCode && x.OpcGroupCode == wcsOpcItemMd.OpcGroupCode && x.PlcCode == wcsOpcItemMd.PlcCode && x.DevCode == wcsOpcItemMd.DevCode);
if (opc == null)
{
return null;
}
else
{
return opc.opc_item.Value;
}
}
///
/// 写入OPC变量值
///
/// opc变量对象
/// 写入值
///
public bool WriteOpcItemValue(WcsOpcItemMd wcsOpcItemMd, object value)
{
try
{
WcsOpcItemExtMd opc = listOpcItem.FirstOrDefault(x => x.OpcItemCode == wcsOpcItemMd.OpcItemCode && x.OpcGroupCode == wcsOpcItemMd.OpcGroupCode && x.PlcCode == wcsOpcItemMd.PlcCode && x.DevCode == wcsOpcItemMd.DevCode);
if (opc == null)
{
return false;
}
else
{
opc.opc_item.Write(value);
return true;
}
}
catch
{
return false;
}
}
#endregion
#region 桌面显示Log、记录log到文本
///
/// 桌面显示Log、记录log到文本
///
/// log内容
/// 是否输出到桌面日志端。true:输出桌面;false:只记录文本
/// 日志类型枚举
private void ShowLogToForm(string msg, bool isShowFormFlag, LogTypeEnum logTypeEnum)
{
if (isShowFormFlag)
{
showLogToFrm.ShowLog(new ShowLogToFrmEventArgs(msg));
}
LogHelper.WriteLog(LogHeadText + msg, logTypeEnum);
}
#endregion
}
}