//constructs a new message from a JSON blob public static PlantronicsMessage ParseMessageFromJSON(String json) { if (json == null || json.Trim().Length == 0) { return(null); } Hashtable t = (Hashtable)JSON.JsonDecode(json); if (t == null) { return(null); } if (!t.ContainsKey(TYPE)) { return(null); } PlantronicsMessage message = new PlantronicsMessage((String)t[TYPE], (String)t[ID]); if (t.ContainsKey(PAYLOAD)) { Hashtable payload = (Hashtable)t[PAYLOAD]; foreach (String key in payload.Keys) { message.AddToPayload(key, payload[key]); } } return(message); }
static public void m_deviceButtonEvents(object sender, _DeviceListenerEventArgs e) { PlantronicsMessage eventMessage = new PlantronicsMessage(PlantronicsMessage.MESSAGE_TYPE_EVENT, PlantronicsMessage.EVENT_BUTTON_PRESS); switch (e.HeadsetButtonPressed) { case HeadsetButton.HeadsetButton_Talk: //WebSocket.buttonPressResponse(); eventMessage.AddToPayload("buttonName", "talk"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_Mute: eventMessage.AddToPayload("buttonName", "mute"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_VolumeUp: eventMessage.AddToPayload("buttonName", "volumeUp"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_VolumeDown: eventMessage.AddToPayload("buttonName", "volumeDown"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_MuteHeld: eventMessage.AddToPayload("buttonName", "muteHeld"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_VolumeUpHeld: eventMessage.AddToPayload("buttonName", "volumeUpHeld"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_VolumeDownHeld: eventMessage.AddToPayload("buttonName", "volumeDownHeld"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_Flash: eventMessage.AddToPayload("buttonName", "flash"); BroadcastMessage(eventMessage); break; } }
public static void m_deviceButtonEvents(object sender, _DeviceListenerEventArgs e) { PlantronicsMessage eventMessage = new PlantronicsMessage(PlantronicsMessage.MESSAGE_TYPE_EVENT, PlantronicsMessage.EVENT_BUTTON_PRESS); switch (e.HeadsetButtonPressed) { case HeadsetButton.HeadsetButton_Talk: //WebSocket.buttonPressResponse(); eventMessage.AddToPayload("buttonName", "talk"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_Mute: eventMessage.AddToPayload("buttonName", "mute"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_VolumeUp: eventMessage.AddToPayload("buttonName", "volumeUp"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_VolumeDown: eventMessage.AddToPayload("buttonName", "volumeDown"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_MuteHeld: eventMessage.AddToPayload("buttonName", "muteHeld"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_VolumeUpHeld: eventMessage.AddToPayload("buttonName", "volumeUpHeld"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_VolumeDownHeld: eventMessage.AddToPayload("buttonName", "volumeDownHeld"); BroadcastMessage(eventMessage); break; case HeadsetButton.HeadsetButton_Flash: eventMessage.AddToPayload("buttonName", "flash"); BroadcastMessage(eventMessage); break; } }
// print session events static void m_sessionEvents_CallStateChanged(object sender, _CallStateEventArgs e) { string id = e.CallId != null?e.CallId.Id.ToString() : "none"; IHostCommandExt hostCommandExt = m_activeDevice.HostCommand as IHostCommandExt; //the user has accepted the call if (e.Action.Equals(CallState.CallState_AcceptCall)) { Console.WriteLine("Call has been answered by headset - sending event to web browser, audio link state = " + hostCommandExt.AudioLinkState); PlantronicsMessage m = new PlantronicsMessage(PlantronicsMessage.MESSAGE_TYPE_EVENT, PlantronicsMessage.EVENT_ACCEPT_CALL); m.AddToPayload("offer", offer); BroadcastMessage(m); } else if (e.Action.Equals(CallState.CallState_TerminateCall)) { Console.WriteLine("Call has been terminated, killing audio channel"); PlantronicsMessage m = new PlantronicsMessage(PlantronicsMessage.MESSAGE_TYPE_EVENT, PlantronicsMessage.EVENT_CALL_TERMINATE); BroadcastMessage(m); offer = null; callId = 0; } }
//Responsible for processing messages that have arrived via a websocket connection private static void HandleMessage(String message) { if (m_activeDevice == null) { Console.WriteLine("no active device, ignoring message"); return; } PlantronicsMessage m = PlantronicsMessage.ParseMessageFromJSON(message); if (m.Type == PlantronicsMessage.MESSAGE_TYPE_SETTING) { if (m.Id == PlantronicsMessage.SETTING_HEADSET_INFO) { PlantronicsMessage response = new PlantronicsMessage(PlantronicsMessage.MESSAGE_TYPE_SETTING, PlantronicsMessage.SETTING_HEADSET_INFO); Device device = new Device(); device.InternalName = m_activeDevice.InternalName; device.ManufacturerName = m_activeDevice.ManufacturerName; device.ProductName = m_activeDevice.ProductName; device.VendorId = String.Format("0x{0:X}", m_activeDevice.VendorID); device.ProductId = String.Format("0x{0:X}", m_activeDevice.ProductID); device.VersionNumber = m_activeDevice.VersionNumber; //TODO fixup CAB - make this eventually come from Spokes or a dictionary lookup device.NumberOfChannels = 1; device.SampleRate = 16000; response.AddToPayload("device", device); BroadcastMessage(response); } } else if (m.Type == PlantronicsMessage.MESSAGE_TYPE_COMMAND) { if (m.Id == PlantronicsMessage.COMMAND_RING_ON) { Console.WriteLine("Ringing headset"); Object o = null; if (!m.Payload.TryGetValue("offer", out o)) { Console.WriteLine("Unable to get caller id from the message skipping headset ring operation"); return; } offer = o as Hashtable; ContactCOM contact = new ContactCOM() { Name = offer["from"] as String }; callId = (int)(double)m.Payload["callId"]; CallCOM call = new CallCOM() { Id = callId }; m_comSession.CallCommand.IncomingCall(call, contact, RingTone.RingTone_Unknown, AudioRoute.AudioRoute_ToHeadset); } else if (m.Id == PlantronicsMessage.COMMAND_HANG_UP) { CallCOM call = new CallCOM() { Id = callId }; m_comSession.CallCommand.TerminateCall(call); } else if (m.Id == PlantronicsMessage.COMMAND_RING_OFF) { Console.WriteLine("Turning ring off headset"); m_activeDevice.HostCommand.Ring(false); } else if (m.Id == PlantronicsMessage.COMMAND_MUTE_ON) { Console.WriteLine("Muting headset"); m_activeDevice.DeviceListener.Mute = true; } else if (m.Id == PlantronicsMessage.COMMAND_MUTE_OFF) { Console.WriteLine("Unmuting headset"); m_activeDevice.DeviceListener.Mute = false; } } else if (m.Type == PlantronicsMessage.MESSAGE_TYPE_EVENT) { Console.WriteLine("Event message received"); } }
// print session events static void m_sessionEvents_CallStateChanged(object sender, _CallStateEventArgs e) { string id = e.CallId != null ? e.CallId.Id.ToString() : "none"; IHostCommandExt hostCommandExt = m_activeDevice.HostCommand as IHostCommandExt; //the user has accepted the call if (e.Action.Equals(CallState.CallState_AcceptCall)) { Console.WriteLine("Call has been answered by headset - sending event to web browser, audio link state = " + hostCommandExt.AudioLinkState); PlantronicsMessage m = new PlantronicsMessage(PlantronicsMessage.MESSAGE_TYPE_EVENT, PlantronicsMessage.EVENT_ACCEPT_CALL); m.AddToPayload("offer", offer); BroadcastMessage(m); } else if (e.Action.Equals(CallState.CallState_TerminateCall)) { Console.WriteLine("Call has been terminated, killing audio channel"); PlantronicsMessage m = new PlantronicsMessage(PlantronicsMessage.MESSAGE_TYPE_EVENT, PlantronicsMessage.EVENT_CALL_TERMINATE); BroadcastMessage(m); offer = null; callId = 0; } }
//constructs a new message from a JSON blob public static PlantronicsMessage ParseMessageFromJSON(String json) { if (json == null || json.Trim().Length == 0) { return null; } Hashtable t = (Hashtable)JSON.JsonDecode(json); if (t == null) { return null; } if (!t.ContainsKey(TYPE)) { return null; } PlantronicsMessage message = new PlantronicsMessage((String)t[TYPE], (String)t[ID]); if (t.ContainsKey(PAYLOAD)) { Hashtable payload = (Hashtable)t[PAYLOAD]; foreach (String key in payload.Keys) { message.AddToPayload(key, payload[key]); } } return message; }