Пример #1
0
 public void OnCommand(Command cmd)
 {
     switch (cmd.MessageId)
     {
         case Command.COMMAND_RECIEVE_SETTINGS:
             ProcessSettingsRecieved(cmd.Message);
             break;
     }
 }
Пример #2
0
 // 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;
 }
Пример #3
0
 /// <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();
 }
Пример #4
0
 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;
 }
Пример #5
0
 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;
 }
Пример #6
0
        /// <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;
            }
        }
Пример #7
0
        // 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);
        }
Пример #8
0
 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;
 }
Пример #9
0
 public Command CommandRecieved(Command command)
 {
     return null;
 }
Пример #10
0
 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();
 }
Пример #11
0
        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();
        }
Пример #12
0
        // 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();
        }
Пример #13
0
        /// <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;
                }
            }
        }
Пример #14
0
        /// <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();
        }
Пример #15
0
        /// <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);
        }
Пример #16
0
        // 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();
            }
        }
Пример #17
0
 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();
 }
Пример #18
0
        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;
        }
Пример #19
0
 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();            
 }
Пример #20
0
        // 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;
        }
Пример #21
0
        public void OnCommand(Command cmd)
        {

        }
Пример #22
0
        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);
            }
        }
Пример #23
0
 /// <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;
 }
Пример #24
0
        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();
        }
Пример #25
0
 public Command CommandRecieved(Command command)
 {
     // Ignore
     return null;
 }
Пример #26
0
        /// <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);
        }