Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public JT808_0x8105 Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            JT808_0x8105 jT808_0x8105 = new JT808_0x8105
            {
                CommandWord              = reader.ReadByte(),
                CommandParameters        = new List <ICommandParameter>(),
                CustomCommandParameters  = new List <ICommandParameter>(),
                UnknownCommandParameters = new Dictionary <int, byte[]>()
            };

            if (jT808_0x8105.CommandWord == 1 || jT808_0x8105.CommandWord == 2)
            {
                int remain = reader.ReadCurrentRemainContentLength();
                if (remain > 0)
                {
                    var           commandValueBuffer = reader.ReadArray(remain);
                    List <byte[]> commandParameters  = new List <byte[]>();
                    while (true)
                    {
                        var index = commandValueBuffer.IndexOf(CommandParameterSeparatorValue);
                        if (commandValueBuffer.Length <= 0)
                        {
                            break;
                        }
                        if (index == 0)
                        {
                            commandParameters.Add(null);
                            commandValueBuffer = commandValueBuffer.Slice(index + 1);
                        }
                        else
                        {
                            var value = commandValueBuffer.Slice(0, index);
                            commandParameters.Add(value.ToArray());
                            commandValueBuffer = commandValueBuffer.Slice(index + 1);
                        }
                    }
                    for (int i = 0; i < commandParameters.Count; i++)
                    {
                        //如果大于13个命令参数,说明有自定义命令参数再里面
                        var cmd = commandParameters[i];
                        if (i >= 13)
                        {
                            //读取自定义的命令参数
                            if (config.JT808_0x8105_Cusotm_Factory.Map.TryGetValue(i, out Type type))
                            {
                                var commandParameter = (ICommandParameter)Activator.CreateInstance(type);
                                commandParameter.ToValue(cmd);
                                jT808_0x8105.CustomCommandParameters.Add(commandParameter);
                            }
                            else
                            {
                                jT808_0x8105.UnknownCommandParameters.Add(i, cmd);
                            }
                        }
                        else
                        {
                            //读取标准的命令参数
                            ICommandParameter commandParameter = JT808_0x8105_CommandParameterExtensions.CreateCommandParameter(i);
                            if (commandParameter != null)
                            {
                                commandParameter.ToValue(cmd);
                                jT808_0x8105.CommandParameters.Add(commandParameter);
                            }
                        }
                    }
                }
            }
            return(jT808_0x8105);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="writer"></param>
        /// <param name="config"></param>
        public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config)
        {
            JT808_0x8105 jT808_0x8105 = new JT808_0x8105
            {
                CommandWord              = reader.ReadByte(),
                CommandParameters        = new List <ICommandParameter>(),
                CustomCommandParameters  = new List <ICommandParameter>(),
                UnknownCommandParameters = new Dictionary <int, byte[]>()
            };

            writer.WriteNumber($"[{ jT808_0x8105.CommandWord.ReadNumber()}]命令字", jT808_0x8105.CommandWord);
            if (jT808_0x8105.CommandWord == 1 || jT808_0x8105.CommandWord == 2)
            {
                int remain = reader.ReadCurrentRemainContentLength();
                if (remain > 0)
                {
                    var           commandValueBuffer = reader.ReadArray(remain);
                    List <byte[]> commandParameters  = new List <byte[]>();
                    while (true)
                    {
                        var index = commandValueBuffer.IndexOf(CommandParameterSeparatorValue);
                        if (commandValueBuffer.Length <= 0)
                        {
                            break;
                        }
                        if (index == 0)
                        {
                            commandParameters.Add(null);
                            commandValueBuffer = commandValueBuffer.Slice(index + 1);
                        }
                        else
                        {
                            var value = commandValueBuffer.Slice(0, index);
                            commandParameters.Add(value.ToArray());
                            commandValueBuffer = commandValueBuffer.Slice(index + 1);
                        }
                    }
                    writer.WriteStartObject("命令参数对象");
                    for (int i = 0; i < commandParameters.Count; i++)
                    {
                        //如果大于13个命令参数,说明有自定义命令参数再里面
                        var cmd = commandParameters[i];
                        if (i >= 13)
                        {
                            //读取自定义的命令参数
                            if (config.JT808_0x8105_Cusotm_Factory.Map.TryGetValue(i, out Type type))
                            {
                                var commandParameter = (ICommandParameter)Activator.CreateInstance(type);
                                commandParameter.ToValue(cmd);
                                writer.WriteString($"[{cmd.ToHexString()}]{commandParameter.CommandName}", commandParameter.ToValueString());
                            }
                            else
                            {
                                writer.WriteString($"[{cmd.ToHexString()}]未知命令参数{i}", "");
                            }
                        }
                        else
                        {
                            //读取标准的命令参数
                            ICommandParameter commandParameter = JT808_0x8105_CommandParameterExtensions.CreateCommandParameter(i);
                            if (commandParameter != null)
                            {
                                commandParameter.ToValue(cmd);
                                writer.WriteString($"[{cmd?.ToHexString()}]{commandParameter.CommandName}", commandParameter.ToValueString());
                            }
                        }
                    }
                    writer.WriteEndObject();
                }
            }
        }