/// <summary> /// 从指定的 XML 文件中加载设备的 OPC 标签信息 /// </summary> /// <param name="dataFileName"></param> public void Load(string dataFileName) { items.Clear(); XmlDocument xml = new XmlDocument(); try { xml.Load(dataFileName); } catch (Exception error) { Debug.WriteLine(string.Format("加载[{0}]文件时出错:[{1}]", dataFileName, error.Message)); return; } XmlNode devicesNode = xml.SelectSingleNode("root/Devices"); if (devicesNode == null) { Debug.WriteLine("文件[{0}]中没有 Devices 节点。"); return; } foreach (XmlNode child in devicesNode.ChildNodes) { TIRAPOPCLocDevice device = TIRAPOPCLocDevice.LoadFromXMLNode(child); if (device != null) { items.Add(device); } } }
/// <summary> /// 删除指定设备代码的设备及其标签信息 /// </summary> /// <param name="deviceCode">设备代码</param> /// <param name="dataFileName"></param> public void Remove(string deviceCode, string dataFileName) { TIRAPOPCLocDevice findRlt = null; foreach (TIRAPOPCLocDevice item in items) { if (item.DeviceCode == deviceCode) { findRlt = item; break; } } if (findRlt != null) { ModifyDataFile(2, findRlt, dataFileName); items.Remove(findRlt); } else { Exception error = new Exception(); error.Data["ErrCode"] = "900201"; error.Data["ErrText"] = string.Format("编号为[{0}]的设备未在系统中注册", deviceCode); throw error; } }
/// <summary> /// 新增一个设备及其标签信息 /// </summary> /// <param name="device">设备及其标签信息对象</param> /// <param name="dataFileName"></param> /// <returns></returns> public int Add(TIRAPOPCLocDevice device, string dataFileName) { ModifyDataFile(1, device, dataFileName); items.Add(device); return(items.Count - 1); }
//获取设备列表 public List <TIRAPOPCLocDevice> GetDevices() { items.Clear(); TGetDevicesContent content = new TGetDevicesContent(); content.Head.ExCode = "GetDevices"; content.Request.ExCode = "GetDevices"; content.ResolveResponse(OPCWSClient.Instance.WSCall(content.GenerateRequestContent())); if (content.Response.ErrCode == "0") { if (content.Response.Details.Count > 0) { items.Clear(); foreach (var d in content.Response.Details) { TIRAPOPCLocDevice device = new TIRAPOPCLocDevice(d); items.Add(device); } } ; } else { XtraMessageBox.Show(content.Response.ErrCode + ":" + content.Response.ErrText); } return(items); }
private void tsmiDeleteDevice_Click(object sender, EventArgs e) { DialogResult resault = XtraMessageBox.Show("是否删除所选设备?", "删除所选设备", MessageBoxButtons.OKCancel); if (resault == DialogResult.OK) { TIRAPOPCLocDevice device = IRAPOPCDevices.Instance.GetDeviceWithDeviceCode(tlDevices.FocusedNode.Tag.ToString()); TUpdateDeviceTagsContent content = new TUpdateDeviceTagsContent(); content.Head.ExCode = "UpdateDeviceTags"; content.Request.ExCode = "UpdateDeviceTags"; content.Request.CommunityID = 60010; content.Request.UpdateType = 3; content.Request.DeviceName = device.DeviceName; content.Request.DeviceCode = device.DeviceCode; content.Request.KepServAddr = device.KepServerAddr; content.Request.KepServChannel = device.KepServerChannel; content.Request.KepServDevice = device.KepServerDevice; content.Request.KepServName = device.KepServerName; content.ResolveResponse(OPCWSClient.Instance.WSCall(content.GenerateRequestContent())); if (content.Response.ErrCode == "0") { RefreshTreelist(); } else { MessageBox.Show(content.Response.ErrCode + ":" + content.Response.ErrText); } } }
public void Modify(TIRAPOPCLocDevice device, string dataFileName) { ModifyDataFile(1, device, dataFileName); TIRAPOPCLocDevice old = GetDeviceWithDeviceCode(device.DeviceCode); items.Remove(old); items.Add(device); }
static void Main(string[] args) { try { //ServiceHost _host = new ServiceHost(typeof(OPCGatewayService)); //_host.Open(); //TKepServerListener kepServer = new TKepServerListener(); //kepServer.Init("127.0.0.1", "Kepware.KEPServerEX.V5"); //Console.WriteLine("IRAP-OPC 服务网关已启动"); TOPCGatewayServiceControl.Instance.Start(); bool quitSystem = false; do { Console.WriteLine("按 <Q> 健退出服务"); string cmd = Console.ReadLine(); switch (cmd.ToUpper()) { case "Q": quitSystem = true; break; case "EDIT": //kepServer.WriteTagValue("Channel1.Device1.COMPOS_02", "true"); break; case "NEWDEVICE": TIRAPOPCLocDevice device = new TIRAPOPCLocDevice() { DeviceCode = "10010", KepServerAddr = "127.0.0.1", KepServerName = "KepServer.KEPServerEX.V5", }; TIRAPLocOPCDevices.Instance.ModifyDataFile( 1, device, string.Format( "{0}IRAP.BL.OPCGateway.xml", AppDomain.CurrentDomain.BaseDirectory)); break; } Console.WriteLine(""); } while (!quitSystem); TOPCGatewayServiceControl.Instance.Stop(); } catch (Exception error) { Console.WriteLine(error.Message); Console.WriteLine(error.StackTrace); Console.WriteLine("按 <Enter> 健退出服务"); Console.ReadLine(); } }
/// <summary> /// 编辑指定设备及其标签信息 /// </summary> /// <param name="type">编辑类别:1-保存;2-删除</param> /// <param name="device">设备对象</param> /// <param name="dataFileName">本地持久化文件名</param> public void ModifyDataFile(int type, TIRAPOPCLocDevice device, string dataFileName) { XmlDocument xml = new XmlDocument(); XmlNode rootNode = null; if (!File.Exists(dataFileName)) { xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", "")); rootNode = xml.CreateElement("root"); xml.AppendChild(rootNode); } else { try { xml.Load(dataFileName); } catch (Exception error) { Debug.WriteLine(string.Format("加载[{0}]文件时出错:[{1}]", dataFileName, error.Message)); return; } rootNode = xml.SelectSingleNode("root"); if (rootNode == null) { Debug.WriteLine( string.Format( "[{0}] 文件中不存在 root 根节点")); return; } } // 在第一层子节点中查找 Device 节点,若没有找到则创建该节点 XmlNode devicesNode = null; foreach (XmlNode node in rootNode.ChildNodes) { if (node.Name == "Devices") { devicesNode = node; break; } } if (devicesNode == null) { devicesNode = xml.CreateElement("Devices"); rootNode.AppendChild(devicesNode); } // 在 Devices 节点中查找指定 DeviceCode 的 Device 子节点,如果找到则从 Devices 节点中删除 XmlNode deviceNode = IRAPXMLUtils.GetChildNodeWithPropertyValue( devicesNode, "DeviceCode", device.DeviceCode); if (deviceNode != null) { devicesNode.RemoveChild(deviceNode); } if (type == 1) { #region 添加设备节点 deviceNode = xml.ImportNode(device.GenerateXMLNode(), true); devicesNode.AppendChild(deviceNode); #endregion } // 保存 XML 到文件 xml.Save(dataFileName); }
public dlgImportDeviceTag(string title, string DeviceCode) : base(title) { InitializeComponent(); SetLabelMouseDown(); this.device = IRAPOPCDevices.Instance.GetDeviceWithDeviceCode(DeviceCode); }
public string DoAction() { if (content.Request != null) { content.Response.ErrCode = "999999"; content.Response.ErrText = "功能正在开发中......"; try { if (content.Request.ExCode == "UpdateDeviceTags") { TIRAPOPCLocDevice device = null; switch (content.Request.UpdateType) { case 1: // 新增设备 device = TIRAPLocOPCDevices.Instance.GetDeviceWithDeviceCode( content.Request.DeviceCode); if (device != null) { Exception error = new Exception(); error.Data["ErrCode"] = "900202"; error.Data["ErrText"] = string.Format( "设备号[{0}]已经存在,对应的设备名称[{1}],不能新增", device.DeviceCode, device.DeviceName); throw error; } device = new TIRAPOPCLocDevice(content.Request); TIRAPLocOPCDevices.Instance.Add( device, TOPCGatewayGlobal.Instance.ConfigurationFile); content.Response.ErrCode = "0"; content.Response.ErrText = "设备信息及标签新增成功"; break; case 2: // 修改设备及标签信息 device = TIRAPLocOPCDevices.Instance.GetDeviceWithDeviceCode( content.Request.DeviceCode); if (device == null) { Exception error = new Exception(); error.Data["ErrCode"] = "121026"; error.Data["ErrText"] = string.Format( "无效的设备代码({0})", content.Request.DeviceCode); throw error; } device = new TIRAPOPCLocDevice(content.Request); TIRAPLocOPCDevices.Instance.Modify( device, TOPCGatewayGlobal.Instance.ConfigurationFile); content.Response.ErrCode = "0"; content.Response.ErrText = "设备信息及标签修改成功"; break; case 3: // 删除设备 string deviceCode = content.Request.DeviceCode; TIRAPLocOPCDevices.Instance.Remove( deviceCode, TOPCGatewayGlobal.Instance.ConfigurationFile); content.Response.ErrCode = "0"; content.Response.ErrText = "设备信息及标签删除成功"; break; } } else { content.Response.ErrCode = "900000"; content.Response.ErrText = "报文体中的交易代码和报文头中的交易代码不一致"; } } catch (Exception error) { content.Response.ErrText = string.Format("系统抛出的错误:[{0}]", error.Message); foreach (DictionaryEntry de in error.Data) { if (de.Key.ToString().ToUpper() == "ERRCODE") { content.Response.ErrCode = de.Value.ToString(); } if (de.Key.ToString().ToUpper() == "ERRTEXT") { content.Response.ErrText = de.Value.ToString(); } } } } return(content.GenerateResponseContent()); }