void AddCommand(InternalRobotMessage internalRobotMessage)
 {
     lock (commandsLock)
     {
         var command = new RobotCommand(internalRobotMessage.commandDescription, internalRobotMessage.message);
         commands.Add(command);
     }
 }
    void AddMessage(InternalRobotMessage message)
    {
        lock (chatMessagesLock)
        {
            Constants.roboStuff.chatMessages.Add(message);
        }

        TrimChatMessages();
    }
    //Message package is assembled from string
    public void NewMessage(string message)
    {
        Tokenizer tokenizer = new Tokenizer(message, ' ');

        //message type is given from the broadcast source,
        //The broadcast source must contain a particular key / string pair for it to be picked up here.
        var messageType = tokenizer.GetToken();

        //UnityEngine.Debug.Log (messageType);


        if (messageType == "chat")
        {
            var user        = tokenizer.GetToken();
            var chatMessage = tokenizer.GetString();

            //Basic chat message generation, user + message
            AddMessage(new InternalRobotMessage(user, chatMessage));

            //command message generation, user + command, commandDescription, commandID
        }
        else if (messageType == "command")
        {
            var isFromChat             = tokenizer.GetToken() == "from_chat";
            var user                   = tokenizer.GetToken();
            var commandId              = Convert.ToInt32(tokenizer.GetToken());
            var commandDescriptionSize = Convert.ToInt32(tokenizer.GetToken());
            var commandDescription     = tokenizer.GetString(commandDescriptionSize);
            var command                = tokenizer.GetString();

            var internalRobotMessage = new InternalRobotMessage(user, command, commandDescription, commandId);

            AddCommand(internalRobotMessage);

            //Admin messages are hidden from the GUI
            if (isFromChat)
            {
                AddMessage(internalRobotMessage);
            }

            //handles command execution state for GUI
        }
        else if (messageType == "command_begin")
        {
            var commandId = Convert.ToInt32(tokenizer.GetToken());

            SetCommandIsExecuting(commandId, true);
        }
        else if (messageType == "command_end")
        {
            var commandId = Convert.ToInt32(tokenizer.GetToken());

            SetCommandIsExecuting(commandId, false);

            //Hook for setting a global variable, this is a secure message type that should only come from an admin
        }
        else if (messageType == "variable")
        {
            var variable = tokenizer.GetToken();
            var value    = tokenizer.GetString();

            SetVariable(variable, value);

            //This message type is a hack for Skynet for generating new message types.
        }
        else if (messageType == "parse")
        {
            //UnityEngine.Debug.Log ("parsing");
            var from           = tokenizer.GetToken();
            var privilegeLevel = Convert.ToInt32(tokenizer.GetToken());
            var platform       = tokenizer.GetToken();
            var msg            = tokenizer.GetString();
            //UnityEngine.Debug.Log (from);
            //UnityEngine.Debug.Log (privilegeLevel);
            //UnityEngine.Debug.Log (platform);
            //UnityEngine.Debug.Log (msg);
            //UnityEngine.Debug.Log ("done parsing");
        }

        //Not exactly what this does, this is a Ryan thing. - Jill
        else if (messageType == "run")
        {
            //UnityEngine.Debug.Log ("run");
            var json = tokenizer.GetString();
            //Debug.Log(json);
            var packet = JSON.Parse(json);
            // packet["type"] = motor, led, sendCommandMessage
            // packet["from"] = user that typed it
            // packet["text"] = what they typed
            // packet["i"] = which led
            // packet["r"] = red
            // packet["g"] = green
            // packet["b"] = blue
            //UnityEngine.Debug.Log(packet["type"]);
            //UnityEngine.Debug.Log(packet["text"]);
            //UnityEngine.Debug.Log ("done run");
        }
        else if (messageType == "hello")
        {
        }

        else
        {
            //If no message types are found...
            throw new Exception();
        }
    }