예제 #1
0
        static SerializedCommands Serialize(Message[] messages, Dictionary <int, CommandInfo> infos)
        {
            //validate
            if (messages == null || messages.Length == 0)
            {
                return(new SerializedCommands());
            }

            //serialize the command and add to the string
            StringBuilder sb          = new StringBuilder();
            bool          fastCommand = false;

            foreach (var message in messages)
            {
                CommandInfo commandInfo = null;
                if (infos.TryGetValue(message.ID, out commandInfo))
                {
                    if (commandInfo.FastCommand)
                    {
                        fastCommand = true;
                    }
                    bool paramOmitted = false;
                    int? servoCount   = (commandInfo.ServoCount != null) ? (int?)commandInfo.ServoCount.GetValue(message) : null;
                    foreach (var propertyInfo in commandInfo.Properties)
                    {
                        //skip param if servo is missing
                        if (propertyInfo.RequiresServoID != null && servoCount != null && propertyInfo.RequiresServoID.Value > servoCount.Value)
                        {
                            continue;
                        }

                        var parm = propertyInfo.Property.GetValue(message);
                        if (parm == null && !propertyInfo.HasDefaultValue)
                        {
                            paramOmitted = true;
                        }
                        else if (paramOmitted)
                        {
                            throw new CommandException($"{propertyInfo.Property.Name} must be omitted when a previous parameter in the command is omitted");
                        }

                        sb.Append(ParamerterConverter.SerializeValue(parm, false, propertyInfo.HasDefaultValue));
                        if (commandInfo.Properties.Last() != propertyInfo)
                        {
                            sb.Append(ParamSeporatorChar);
                        }
                    }
                    sb.Append(EndChar);
                }
            }

            //return the commands
            return(new SerializedCommands()
            {
                Commands = sb.ToString(), FastCommand = fastCommand
            });
        }
예제 #2
0
        static T[] Parse <T>(string data, Dictionary <int, CommandInfo> infos, int?servoCount = null)
        {
            //validate
            if (string.IsNullOrWhiteSpace(data))
            {
                return(null);
            }

            //parse the response
            var result          = new List <T>();
            var responseStrings = data.Split(EndChar);

            foreach (var responseString in responseStrings)
            {
                var parameters = SplitParameters(responseString);

                //ignore a blank command
                if (parameters.Length == 0 || parameters[0].Length == 0)
                {
                    continue;
                }

                //get the type
                int id;
                if (!int.TryParse(parameters[0], out id))
                {
                    continue;
                }
                CommandInfo info = null;
                if (!infos.TryGetValue(id, out info))
                {
                    continue;
                }

                //construct the response
                try
                {
                    var response = (T)Activator.CreateInstance(info.CommandType);
                    for (int i = 1; i < parameters.Length; i++)
                    {
                        if (i < info.Properties.Length)
                        {
                            var propertyInfo = info.Properties[i];

                            //skip param if servo is missing
                            if (propertyInfo.RequiresServoID != null && servoCount != null && propertyInfo.RequiresServoID.Value > servoCount.Value)
                            {
                                continue;
                            }

                            propertyInfo.Property.SetValue(response, ParamerterConverter.ChangeType(parameters[i], info.Properties[i].Property.PropertyType, propertyInfo.HasDefaultValue));
                        }
                    }

                    //add it to the results
                    result.Add(response);
                }
                catch
                {
                    //ignore parsing errors for now
                }
            }

            return(result.ToArray());
        }