public PlantronicsManager(IStatusManager statusManager, IInteractionManager interactionManager, INotificationService notificationService, ITraceContext traceContext) { _statusManager = statusManager; _interactionManager = interactionManager; _traceContext = traceContext; _notificationService = notificationService; m_sessionComManager = new SessionComManagerClass(); m_sessionManagerEvents = m_sessionComManager as ISessionCOMManagerEvents_Event; m_comSession = m_sessionComManager.Register("Interaction Client Plantronics AddIn"); // Now check if our plugin session was created if (m_comSession != null) { // detect devices added/removed m_sessionManagerEvents.DeviceStateChanged += OnDeviceStateChanged; //Get current Device m_device = m_comSession.ActiveDevice; // if we have a device register for events if (m_device != null) { // Register for device events RegisterEvents(); } } }
/// <summary> /// 构造函数 /// </summary> /// <param name="port"></param> /// <param name="baud"></param> public ComController(IComSession com) : base() { if (com == null) { throw new ArgumentNullException("串口通道实例为空"); } _Com = com; IsExited = false; IsWorking = false; }
private static void InitializePlantronics() { try { m_sessionComManager = new SessionComManagerClass(); Console.WriteLine("Session Manager created"); m_sessionManagerEvents = m_sessionComManager as ISessionCOMManagerEvents_Event; if (m_sessionManagerEvents != null) { m_sessionManagerEvents.DeviceStateChanged += m_sessionComManager_DeviceStateChanged; Console.WriteLine("Attached to session manager events"); } else { Console.WriteLine("Error: Unable to attach to session manager events"); } //////////////////////////////////////////////////////////////////////////////////////// // register session to spokes m_comSession = m_sessionComManager.Register("COM Session"); if (m_comSession != null) { // attach to session call events m_sessionEvents = m_comSession.CallEvents as ICOMCallEvents_Event; if (m_sessionEvents != null) { m_sessionEvents.CallStateChanged += m_sessionEvents_CallStateChanged; Console.WriteLine("Attached to session call events"); } else { Console.WriteLine("Error: Unable to attach to session call events"); } // Attach to active device and print all device information m_activeDevice = m_comSession.ActiveDevice; AttachDevice(); } else { Console.WriteLine("Error: Unable to register session"); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); Console.Read(); } }
public override void ChangeDeviceComInfo(string devid, int oldCom, int oldBaud, int newCom, int newBaud) { IRunDevice dev = DeviceManager.GetDevice(devid); if (dev == null) { Logger.Info(true, String.Format("{0}号设备,改变串口信息不存在", devid.ToString())); } else { int oldComPort = oldCom; int oldComBaud = oldBaud; int newComPort = newCom; int newComBaud = newBaud; bool success = true; if (dev.CommunicateType == CommunicateType.COM) { if (oldComPort != newComPort) { #region 对旧串口进行处理 //--------------对旧串口进行处理----------------// IRunDevice[] oldComDevList = DeviceManager.GetDevices(oldComPort.ToString(), CommunicateType.COM); int oldComDevCount = oldComDevList.Count((d) => d.GetHashCode() != dev.GetHashCode()); //当前串口不等于当前设备的设备数 if (oldComDevCount <= 0) //先修改设备的串口参数,该串口没有可用的设备 { string oldKey = ComUtils.PortToString(oldComPort); IController oldComController = ControllerManager.GetController(oldKey); if (oldComController != null) { if (ControllerManager.RemoveController(oldComController.Key)) { oldComController.StopController(); oldComController.Dispose(); IComSession comChannel = (IComSession)((IComController)oldComController).ComChannel; comChannel.Close(); comChannel.COMOpen -= ComChannel_COMOpen; comChannel.COMClose -= ComChannel_COMClose; comChannel.COMError -= ComChannel_COMError; if (ChannelManager.RemoveChannel(comChannel.Key)) { comChannel.Close(); comChannel.Dispose(); } } else { success = false; } } else { Logger.Info(true, "该设备的串口控制器为空"); } } #endregion #region 对新串口进行处理 string newKey = ComUtils.PortToString(newComPort); //--------------对新串口进行处理----------------// bool newComControllerExist = ControllerManager.ContainController(newKey); if (!newComControllerExist) { IChannel channel = ChannelManager.GetChannel(newKey); if (channel == null) { IComSession comChannel = new ComSession(newComPort, newComBaud); comChannel.Setup(this); comChannel.Initialize(); comChannel.COMOpen += ComChannel_COMOpen; comChannel.COMClose += ComChannel_COMClose; comChannel.COMError += ComChannel_COMError; comChannel.Open(); channel = (IChannel)comChannel; ChannelManager.AddChannel(comChannel.Key, channel); } IController controller = ControllerManager.GetController(newKey); 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; } } } if (success) { dev.DeviceParameter.COM.Port = newComPort; Logger.Info(true, String.Format("{0},串口从{1}改为{2},成功", dev.DeviceParameter.DeviceName, oldComPort.ToString(), newComPort.ToString())); } else { Logger.Info(true, String.Format("{0},串口从{1}改为{2},失败", dev.DeviceParameter.DeviceName, oldComPort.ToString(), newComPort.ToString())); } #endregion } else { #region 波特率 if (oldComBaud != newComBaud) { IComSession comIO = (IComSession)ChannelManager.GetChannel(ComUtils.PortToString(oldComPort)); if (comIO != null) { success = comIO.Settings(newComBaud); if (success) { dev.DeviceParameter.COM.Baud = newComBaud; Logger.Info(true, String.Format("{0},串口{1}的波特率从{2}改为{3},成功", dev.DeviceParameter.DeviceName, oldComPort.ToString(), oldComBaud.ToString(), newComBaud.ToString())); } else { Logger.Info(true, String.Format("{0},串口{1}的波特率从{2}改为{3},失败", dev.DeviceParameter.DeviceName, oldComPort.ToString(), oldComBaud.ToString(), newComBaud.ToString())); } } } #endregion } } else { Logger.Info(true, String.Format("{0},不是串口类型的设备", dev.DeviceParameter.DeviceName)); } } }
internal override void ComChannel_COMError(IComSession com, int port, int baud, string error) { OnChannelChanged(ComUtils.PortToString(port), CommunicateType.COM, ChannelState.None); Logger.Error(true, String.Format("{0}-{1},{2}", port.ToString(), baud.ToString(), error)); }
private static void InitializePlantronics() { try { m_sessionComManager = new SessionComManagerClass(); Console.WriteLine("Session Manager created"); m_sessionManagerEvents = m_sessionComManager as ISessionCOMManagerEvents_Event; if (m_sessionManagerEvents != null) { m_sessionManagerEvents.DeviceStateChanged += m_sessionComManager_DeviceStateChanged; Console.WriteLine("Attached to session manager events"); } else { Console.WriteLine("Error: Unable to attach to session manager events"); } //////////////////////////////////////////////////////////////////////////////////////// // register session to spokes m_comSession = m_sessionComManager.Register("COM Session"); if (m_comSession != null) { // attach to session call events m_sessionEvents = m_comSession.CallEvents as ICOMCallEvents_Event; if (m_sessionEvents != null) { m_sessionEvents.CallStateChanged += m_sessionEvents_CallStateChanged; Console.WriteLine("Attached to session call events"); } else { Console.WriteLine("Error: Unable to attach to session call events"); } // Attach to active device and print all device information m_activeDevice = m_comSession.ActiveDevice; AttachDevice(); } else Console.WriteLine("Error: Unable to register session"); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); Console.Read(); } }
/** * Remove all of the event listeners and release the COM objects */ private static void CleanUp() { DetachDevice(); if (m_comSession != null) { if (m_sessionEvents != null) { // release session events Marshal.ReleaseComObject(m_sessionEvents); m_sessionEvents = null; } // unregister session m_sessionComManager.UnRegister(m_comSession); Marshal.ReleaseComObject(m_comSession); m_comSession = null; } if (m_sessionComManager != null) { Marshal.ReleaseComObject(m_sessionComManager); m_sessionComManager = null; } allSockets.Clear(); if (server != null) { server.Dispose(); } }
internal abstract void ComChannel_COMOpen(IComSession com, int port, int baud, bool opensuccess);
internal abstract void ComChannel_COMClose(IComSession com, int port, int baud, bool closesuccess);
internal abstract void ComChannel_COMError(IComSession com, int port, int baud, string error);
public void RemoveDevice(string devid) { IRunDevice dev = DeviceManager.GetDevice(devid); if (dev != null) { string desc = String.Empty; string devname = dev.DeviceParameter.DeviceName; if (DeviceManager.RemoveDevice(dev.DeviceParameter.DeviceID)) { if (dev.DeviceType == DeviceType.Virtual) { desc = "删除虚拟设备"; } else { #region if (dev.CommunicateType == CommunicateType.COM) { IRunDevice[] comDevices = DeviceManager.GetDevices(dev.DeviceParameter.COM.Port.ToString(), CommunicateType.COM); if (comDevices.Length == 0) { string key = ComUtils.PortToString(dev.DeviceParameter.COM.Port); IController controller = ControllerManager.GetController(key); if (controller != null) { controller.IsWorking = false; if (ControllerManager.RemoveController(controller.Key)) { controller.StopController(); controller.Dispose(); IComSession comChannel = (IComSession)((IComController)controller).ComChannel; comChannel.Close(); comChannel.COMOpen -= ComChannel_COMOpen; comChannel.COMClose -= ComChannel_COMClose; comChannel.COMError -= ComChannel_COMError; if (ChannelManager.RemoveChannel(comChannel.Key)) { comChannel.Close(); comChannel.Dispose(); } } } } desc = String.Format("{0},从串口'{1}'删除", dev.DeviceParameter.DeviceName, dev.DeviceParameter.COM.Port.ToString()); } else if (dev.CommunicateType == CommunicateType.NET) { desc = String.Format("{0}-{1},从网络中删除成功", dev.DeviceParameter.DeviceName, dev.DeviceParameter.NET.RemoteIP); } #endregion } RemoveDeviceFromShows(dev); RemoveDeviceFromServices(dev); dev.DeviceParameter.Delete(); dev.DeviceDynamic.Delete(); dev.Delete(); dev.Dispose(); BindDeviceHandler(dev, dev.DeviceType, false); desc += ",成功"; OnDeleteDeviceCompleted(dev.DeviceParameter.DeviceID, dev.DeviceParameter.DeviceName, true); } else { desc += ",失败"; OnDeleteDeviceCompleted(dev.DeviceParameter.DeviceID, dev.DeviceParameter.DeviceName, false); } Logger.Info(true, desc); } }
/// <summary> /// Instruct Spokes object to disconnect from Spokes runtime engine and unregister its /// session in Spokes. /// </summary> public void Disconnect() { DetachDevice(); try { if (m_comSession != null) { if (m_sessionEvents != null) { // release session events m_sessionEvents.CallRequested -= m_sessionEvents_CallRequested; m_sessionEvents.CallStateChanged -= m_sessionEvents_CallStateChanged; Marshal.ReleaseComObject(m_sessionEvents); m_sessionEvents = null; } // unregister session if (m_sessionEvents != null) { m_sessionManagerEvents.DeviceStateChanged -= m_sessionComManager_DeviceStateChanged; } m_sessionComManager.UnRegister(m_comSession); Marshal.ReleaseComObject(m_comSession); m_comSession = null; } if (m_sessionComManager != null) { Marshal.ReleaseComObject(m_sessionComManager); m_sessionComManager = null; } } catch (Exception e) { DebugPrint(MethodInfo.GetCurrentMethod().Name, "Exception caught in disconnect: " + e.ToString()); } }
/// <summary> /// Instruct Spokes object to connect to Spokes runtime engine and register itself /// so that it can begin to communicate with the attached Plantronics device. /// </summary> /// <param name="SessionName">Optional name of your appplication's session within Spokes runtime engine. If omitted it will default to "COM Session".</param> public bool Connect(string SessionName = "COM Session") { if (!IsSpokesInstalled()) { DebugPrint(MethodInfo.GetCurrentMethod().Name, "FATAL ERROR: cannot connect if Spokes COMSessionManager/SessionCOMManager class is not registered! Spokes not installed (or wrong major version installed for this Spokes Wrapper)!"); return false; // cannot connect if Spokes COM SessionManager class is not registered! Spokes not installed! } if (isConnected) return true; DeviceCapabilities = new SpokesDeviceCaps(false, false, false, false, false, false, false); // we don't yet know what the capabilities are OnCapabilitiesChanged(EventArgs.Empty); bool success = false; try { //////////////////////////////////////////////////////////////////////////////////////// // create session manager, and attach to session manager events m_sessionComManager = new SessionComManagerClass(); m_sessionManagerEvents = m_sessionComManager as ISessionCOMManagerEvents_Event; if (m_sessionManagerEvents != null) { m_sessionManagerEvents.CallStateChanged += m_sessionComManager_CallStateChanged; m_sessionManagerEvents.DeviceStateChanged += m_sessionComManager_DeviceStateChanged; } else success = false; //////////////////////////////////////////////////////////////////////////////////////// // register session to spokes m_comSession = m_sessionComManager.Register(SessionName); if (m_comSession != null) { // attach to session call events m_sessionEvents = m_comSession.CallEvents as ICOMCallEvents_Event; if (m_sessionEvents != null) { m_sessionEvents.CallRequested += m_sessionEvents_CallRequested; m_sessionEvents.CallStateChanged += m_sessionEvents_CallStateChanged; } else success = false; //////////////////////////////////////////////////////////////////////////////////////// // Attach to active device and print all device information // and registers for proximity (if supported by device) AttachDevice(); success = true; } } catch (System.Exception e) { success = false; throw new Exception("Failed to connect to Spokes", e); } return success; }