public void OnCommand(Command cmd) { switch (cmd.MessageId) { case Command.COMMAND_RECIEVE_SETTINGS: ProcessSettingsRecieved(cmd.Message); break; } }
// Returns a command for the current settings private Command GetSettingsCommand() { // Create the message Command cmd = new Command(); cmd.Program = GlowPrograms.ManualColors; cmd.MessageId = Command.COMMAND_RECIEVE_SETTINGS; cmd.Message = Newtonsoft.Json.JsonConvert.SerializeObject(m_settings); return cmd; }
/// <summary> /// Requests the current settings from the server /// </summary> private void GetCurrentRandomSettings() { new Task(async () => { Command cmd = new Command(); cmd.MessageId = Command.COMMAND_GET_SETTINGS; cmd.Program = GlowCommon.GlowPrograms.RandomColor; await App.GlowBack.ConnectionManager.SendCommand(cmd); }).Start(); }
public Command CommandRecieved(Command command) { switch(command.MessageId) { case Command.COMMAND_GET_SETTINGS: return GetCurrentSettings(); case Command.COMMAND_RECIEVE_SETTINGS: HandleNewSettings(command.Message); break; } return null; }
public Command CommandRecieved(Command command) { // Switch on message type switch(command.MessageId) { case (uint)Command.COMMAND_RECIEVE_SETTINGS: UpdateSettings(command); break; case (uint)Command.COMMAND_GET_SETTINGS: return GetSettingsCommand(); } return null; }
/// <summary> /// Fired when we get a response from the server /// </summary> /// <param name="cmd"></param> public void OnCommand(Command cmd) { switch (cmd.MessageId) { case Command.COMMAND_RECIEVE_SETTINGS: // Parse the settings m_settings = Newtonsoft.Json.JsonConvert.DeserializeObject<RandomColorSettings>(cmd.Message); // Update the UI UpdateRandomColorSettingsUi(); break; } }
// Fired when a new command has arrived. // If a command is returned it will be sont back. public Command OnCommand(Command command) { // Make sure we have the program if(!m_programCache.ContainsKey(command.Program)) { return null; } // Check the version is current if(command.Version != Command.COMMAND_VERSION) { System.Diagnostics.Debug.WriteLine("A old message was thrown away."); return null; } // Get the program IProgram program = m_programCache[command.Program]; // Send the commands return program.CommandRecieved(command); }
public Command CommandRecieved(Command command) { switch(command.MessageId) { case Command.COMMAND_RECIEVE_SETTINGS: { m_settings = Newtonsoft.Json.JsonConvert.DeserializeObject<ClockSettings>(command.Message); break; } case Command.COMMAND_GET_SETTINGS: { // Send the settings Command settingsCommand = new Command(); settingsCommand.MessageId = Command.COMMAND_RECIEVE_SETTINGS; settingsCommand.Program = GlowCommon.GlowPrograms.Clock; settingsCommand.Message = Newtonsoft.Json.JsonConvert.SerializeObject(m_settings); return settingsCommand; } } return null; }
public Command CommandRecieved(Command command) { return null; }
private void SendUpdatedSettings() { new Task(async () => { Command cmd = new Command(); cmd.Message = Newtonsoft.Json.JsonConvert.SerializeObject(m_settings); cmd.MessageId = Command.COMMAND_RECIEVE_SETTINGS; cmd.Program = GlowCommon.GlowPrograms.GlowControl; await App.GlowBack.ConnectionManager.SendCommand(cmd); }).Start(); }
private void ColorType_SelectionChanged(object sender, SelectionChangedEventArgs e) { // Return if we don't have the settings yet if(m_settings == null) { return; } if((int)ui_colorType.SelectedIndex == (int)m_settings.ColorType) { return; } // Set the value m_settings.ColorType = (ClockSettings.ColorTypes)ui_colorType.SelectedIndex; // Send the command new Task(() => { Command cmd = new Command(); cmd.MessageId = Command.COMMAND_RECIEVE_SETTINGS; cmd.Program = GlowCommon.GlowPrograms.Clock; cmd.Message = Newtonsoft.Json.JsonConvert.SerializeObject(m_settings); App.GlowBack.ConnectionManager.SendCommand(cmd); }).Start(); }
// Updates the settings given a settings command. private void UpdateSettings(Command command) { // Get the settings from the message ManualColorSettings newSettings = Newtonsoft.Json.JsonConvert.DeserializeObject<ManualColorSettings>(command.Message); if(newSettings == null) { return; } // Set the new settings m_settings = newSettings; m_settings.SaveSettings(); // Update the LEDs SetCurrentValues(); }
/// <summary> /// Can be called by the client to send a message. /// </summary> /// <param name="cmd">Message to be sent</param> public async Task<bool> SendMessage(Command cmd) { if(m_mode == CommmandServerMode.Server) { throw new NotImplementedException("Server can't send message right now"); } if(m_socket == null && m_clientDataWriter != null) { throw new Exception("The socket isn't open!"); } lock(m_commandQueue) { // Add the command to the queue m_commandQueue.Enqueue(cmd); // Check to see if we should send if (m_isSendingCommand) { // If we are already sending just add the command to be sent later. return true; } else { // If we are not sending we need to send. m_isSendingCommand = true; } } while (true) { // Grab the next command to send. Command currentCommand = null; lock (m_commandQueue) { if (m_commandQueue.Count == 0) { m_isSendingCommand = false; return true; } currentCommand = m_commandQueue.Dequeue(); } // Try to send it. try { await InternalSendMessage(currentCommand, m_clientDataWriter); } catch (Exception e) { // We failed, tell the consumer. System.Diagnostics.Debug.WriteLine("Send Message Failed: " + e.Message); m_socket = null; m_clientDataWriter = null; m_listener.OnDisconnected(); return false; } } }
/// <summary> /// Sends new settings to the server /// </summary> private void SendNewSettings() { // Kick off a task to send the message. new Task(async () => { Command cmd = new Command(); cmd.MessageId = Command.COMMAND_RECIEVE_SETTINGS; cmd.Program = GlowCommon.GlowPrograms.RandomColor; cmd.Message = JsonConvert.SerializeObject(m_settings); await App.GlowBack.ConnectionManager.SendCommand(cmd); }).Start(); // Update the UI UpdateRandomColorSettingsUi(); }
/// <summary> /// Called by the consumer when they want to send a message to the other side. /// </summary> /// <param name="cmd"></param> /// <returns></returns> public async Task<bool> SendCommand(Command cmd) { lock (objectLock) { if(!IsConnected || m_commandServer == null) { return false; } } // Send the message return await m_commandServer.SendMessage(cmd); }
// Updates the settings given a settings command. private void UpdateSettings(Command command) { // Get the settings from the message WeatherSettings newSettings = Newtonsoft.Json.JsonConvert.DeserializeObject<WeatherSettings>(command.Message); if (newSettings == null) { return; } // Update the location if needed if(m_settings.CurrentLocation.State != newSettings.CurrentLocation.State || m_settings.CurrentLocation.City != newSettings.CurrentLocation.City) { m_settings.CurrentLocation = newSettings.CurrentLocation; UpdateWeather(); } }
private void QueryCurrentState() { new Task(async () => { Command cmd = new Command(); cmd.MessageId = Command.COMMAND_GET_SETTINGS; cmd.Program = GlowCommon.GlowPrograms.GlowControl; await App.GlowBack.ConnectionManager.SendCommand(cmd); }).Start(); }
private Command GetCurrentSettings() { string json = Newtonsoft.Json.JsonConvert.SerializeObject(m_settings); // Make the command Command cmd = new Command(); cmd.Message = json; cmd.MessageId = Command.COMMAND_RECIEVE_SETTINGS; cmd.Program = GlowPrograms.GlowControl; return cmd; }
private void GetCurrentClockSettings() { new Task(() => { Command cmd = new Command(); cmd.MessageId = Command.COMMAND_GET_SETTINGS; cmd.Program = GlowCommon.GlowPrograms.Clock; App.GlowBack.ConnectionManager.SendCommand(cmd); }).Start(); }
// Updates the settings given a settings command. private void UpdateSettings(Command command) { // Get the settings from the message RandomColorSettings newSettings = Newtonsoft.Json.JsonConvert.DeserializeObject<RandomColorSettings>(command.Message); if (newSettings == null) { return; } // Set the new settings m_settings = newSettings; }
public void OnCommand(Command cmd) { }
private async Task SendNewSettings(WeatherSettings settings) { Command cmd = new Command(); cmd.Program = GlowPrograms.Weather; cmd.MessageId = Command.COMMAND_RECIEVE_SETTINGS; cmd.Message = Newtonsoft.Json.JsonConvert.SerializeObject(settings); try { await App.GlowBack.ConnectionManager.SendCommand(cmd); } catch (Exception e) { Debug.WriteLine("Failed to send weather settings " + e.Message); } }
/// <summary> /// Fired when the server sends a command /// </summary> /// <param name="command"></param> /// <returns></returns> public Command OnCommand(Command command) { if(OnCommandRecieved != null) { OnCommandRecieved(command); } return null; }
private async Task InternalSendMessage(Command cmd, DataWriter writer) { if(cmd == null) { return; } if(cmd.Program == GlowPrograms.None) { throw new Exception("The program can't be none!"); } // Serialize the cmd string cmdJson = Newtonsoft.Json.JsonConvert.SerializeObject(cmd); UInt32 stringSize = writer.MeasureString(cmdJson); writer.WriteUInt32(stringSize); writer.WriteString(cmdJson); await writer.StoreAsync(); }
public Command CommandRecieved(Command command) { // Ignore return null; }
/// <summary> /// Called by the connection manager when we get a command /// </summary> /// <param name="command"></param> private void ConnectionManager_OnCommandRecieved(Command command) { if(!m_paneControls.ContainsKey(command.Program)) { return; } // Send the command m_paneControls[command.Program].OnCommand(command); }