コード例 #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject item             = JObject.Load(reader);
            var     cidPropertyValue = item["cid"].Value <string>();

            if (string.IsNullOrEmpty(cidPropertyValue))
            {
                throw new JsonReaderException("Undefined mac command identifier");
            }

            if (Enum.TryParse <CidEnum>(cidPropertyValue, true, out var macCommandType))
            {
                switch (macCommandType)
                {
                case CidEnum.DevStatusCmd:
                {
                    var cmd = new DevStatusRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case CidEnum.DutyCycleCmd:
                {
                    var cmd = new DutyCycleRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case CidEnum.NewChannelCmd:
                {
                    var cmd = new NewChannelRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case CidEnum.RXParamCmd:
                {
                    var cmd = new RXParamSetupRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case CidEnum.RXTimingCmd:
                {
                    var cmd = new RXTimingSetupRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }
                }
            }

            throw new JsonReaderException($"Unkown MAC command identifier: {cidPropertyValue}");
        }
コード例 #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (serializer is null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            var item             = JObject.Load(reader);
            var cidPropertyValue = item["cid"].Value <string>();

            if (string.IsNullOrEmpty(cidPropertyValue))
            {
                throw new JsonReaderException("Undefined mac command identifier");
            }

            if (Enum.TryParse <Cid>(cidPropertyValue, true, out var macCommandType))
            {
                switch (macCommandType)
                {
                case Cid.DevStatusCmd:
                {
                    var cmd = new DevStatusRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case Cid.DutyCycleCmd:
                {
                    var cmd = new DutyCycleRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case Cid.NewChannelCmd:
                {
                    var cmd = new NewChannelRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case Cid.RXParamCmd:
                {
                    var cmd = new RXParamSetupRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case Cid.RXTimingCmd:
                {
                    var cmd = new RXTimingSetupRequest();
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case Cid.Zero:
                case Cid.One:
                case Cid.LinkCheckCmd:
                case Cid.LinkADRCmd:
                {
                    GetValue("dataRate", out var datarate);
                    GetValue("txPower", out var txpower);
                    GetValue("chMask", out var chmask);
                    GetValue("chMaskCntl", out var chmaskcntl);
                    GetValue("nbRep", out var nbrep);

                    void GetValue(string propertyName, out JToken value)
                    {
                        if (!item.TryGetValue(propertyName, StringComparison.OrdinalIgnoreCase, out value))
                        {
                            throw new JsonReaderException($"Property '{propertyName}' is missing");
                        }
                    }

                    var cmd = new LinkADRRequest((ushort)datarate, (ushort)txpower, (ushort)chmask, (byte)chmaskcntl, (byte)nbrep);
                    serializer.Populate(item.CreateReader(), cmd);
                    return(cmd);
                }

                case Cid.TxParamSetupCmd:
                default:
                    throw new JsonReaderException($"Unhandled command identifier: {macCommandType}");
                }
            }

            throw new JsonReaderException($"Unknown MAC command identifier: {cidPropertyValue}");
        }