/// <summary> /// 增加设备 /// </summary> /// <param name="dev"></param> /// <returns>设备ID</returns> public string AddDevice(IRunDevice dev) { if (dev == null) { throw new ArgumentNullException("设备驱动", "参数为空"); } if (dev.DeviceParameter.DeviceID != dev.DeviceDynamic.DeviceID) { throw new NotEqualException("参数和动态数据的设备ID不相等"); } String devid = String.Empty; if (dev.DeviceParameter.DeviceID == String.Empty || dev.DeviceDynamic.DeviceID == String.Empty) { devid = this.DeviceManager.BuildDeviceID(); dev.DeviceParameter.DeviceID = devid; dev.DeviceDynamic.DeviceID = devid; try { dev.Initialize(devid); } catch { throw; } } if (DeviceManager.ContainDevice(devid)) { throw new ArgumentException("已经有相同的设备ID存在"); } if (ServerConfig.ControlMode == ControlMode.Singleton) { if (dev.CommunicateType == CommunicateType.NET) { IRunDevice[] netDevices = DeviceManager.GetDevices(CommunicateType.NET); if (netDevices.Length >= 1) //如果是Singleton模式只能有一个网络设备驱动 { throw new IndexOutOfRangeException("当为Singleton模式时,不能增加多个网络设备驱动"); } } } string desc = String.Empty; if (this.DeviceManager.AddDevice(dev.DeviceParameter.DeviceID, dev)) { dev.Setup(this); this.BindDeviceHandler(dev, dev.DeviceType, true); if (dev.DeviceType == DeviceType.Virtual) { desc = "增加虚拟设备"; } else { IController controller = null; if (dev.CommunicateType == CommunicateType.COM) { #region 串口 string key = ComUtils.PortToString(dev.DeviceParameter.COM.Port); IChannel channel = ChannelManager.GetChannel(key); if (channel == null) { IComSession comChannel = new ComSession(dev.DeviceParameter.COM.Port, dev.DeviceParameter.COM.Baud); comChannel.Setup(this); comChannel.Initialize(); comChannel.COMOpen += ComChannel_COMOpen; comChannel.COMClose += ComChannel_COMClose; comChannel.COMError += ComChannel_COMError; comChannel.Open(); channel = (IChannel)comChannel; ChannelManager.AddChannel(key, channel); } controller = ControllerManager.GetController(key); if (controller == null) { controller = new ComController((IComSession)channel); controller.Setup(this); if (ControllerManager.AddController(controller.Key, controller)) { controller.StartController(); } } else { IComController comController = (IComController)controller; if (comController.ComChannel.GetHashCode() != channel.GetHashCode()) { comController.ComChannel = (IComSession)channel; } } desc = String.Format("增加'{0}'串口设备,串口:{1} 波特率:{2}", dev.DeviceParameter.DeviceName, dev.DeviceParameter.COM.Port.ToString(), dev.DeviceParameter.COM.Baud.ToString()); #endregion } else if (dev.CommunicateType == CommunicateType.NET) { #region 网络 controller = ControllerManager.GetController(SocketController.ConstantKey); if (controller == null) { controller = new SocketController(); controller.Setup(this); if (ControllerManager.AddController(controller.Key, controller)) { controller.StartController(); } } desc = String.Format("增加'{0}'网络设备,IP地址:{1} 端口:{2}", dev.DeviceParameter.DeviceName, dev.DeviceParameter.NET.RemoteIP, dev.DeviceParameter.NET.RemotePort.ToString()); #endregion } else { desc = "无法识别设备的通讯类型"; } } if (_deviceContainer != null) { _deviceContainer.UpdateDevice(dev.DeviceParameter.DeviceID, dev.GetDeviceContainerItem()); } desc += ",成功"; OnAddDeviceCompleted(dev.DeviceParameter.DeviceID, dev.DeviceParameter.DeviceName, true); } else { desc += ",失败"; OnAddDeviceCompleted(dev.DeviceParameter.DeviceID, dev.DeviceParameter.DeviceName, false); } this.Logger.Info(true, desc); return(devid != String.Empty ? devid : dev.DeviceParameter.DeviceID); }