using iWareOpc.Cache.Entity; using iWareOpc.Properties; using iWareOpc.Utils; using OpcAccess.Access.Entity; using OpcAccess.Access.Service; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace iWareOpc.Opc.Service { public class OpcService { private static object Lock = new object(); private OpcService() { } private static OpcService Instance = null; /// /// 获取单例的方法 /// /// Opc的单例实体 public static OpcService GetInstance() { if (Instance == null) { lock (Lock) { if (Instance == null) { Instance = new OpcService(); } } } return Instance; } /// /// 根据ip,群获取opc服务 /// /// /// /// public OpcAccessService GetOpcAccessService(string ip, List opcGroups) { var opcAccessService = new OpcAccessService(ip); opcAccessService.Init(opcGroups); return opcAccessService; } /// /// 写入OPC值 /// /// 组名 /// 项名 /// 写入值 /// 是否写入成功 public bool WriteValue(string groupName, string itemName, object value) { try { return CacheEntity.OpcAccessService.WriteValuePoint(groupName, itemName, value); } catch (Exception ex) { LogTextHelper.WriteLog(Resources.LogDir,this.ToString(),"WriteValue",ex.Message); return false; } } /// /// 批量写入OPC值 /// /// 组名 /// 项目名列表 /// 写入值列表 /// 是否写入成功 public bool WriteValue(string groupName, List itemNames, List values) { try { return CacheEntity.OpcAccessService.WriteValuePoint(groupName, itemNames, values); } catch (Exception ex) { LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "WriteValue", ex.Message); return false; } } /// /// 读取地址的值 /// /// 组名 /// 项目名 /// 读到的值 public object ReadValue(string groupName, string itemName) { try { var opcData = CacheEntity.OpcAccessService.ReadValuePoint(groupName, itemName); return opcData == null ? null : opcData.ItemValue; } catch (Exception ex) { LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "ReadValue", ex.Message); return null; } } /// /// 批量读取地址的值 /// /// 组名 /// 项目名 /// 读到的值列表 public List ReadValue(string groupName, List itemNames) { try { var values = new List(); var opcDatas = CacheEntity.OpcAccessService.ReadValuePoint(groupName, itemNames); opcDatas.ForEach(x => values.Add(x.ItemValue)); return values; } catch (Exception ex) { LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "ReadValues", ex.Message); return new List(); } } } }