Example #1
0
        internal override void UpdateFromResponse(TesiraResponse response)
        {
#if DEBUG
            Debug.WriteSuccess(ControlBlock.InstanceTag + " Channel " + ChannelNumber,
                "Received {0} response for {1}: {2}", response.CommandType, response.AttributeCode,
                response.TryParseResponse().ToString());
#endif
            if (response.CommandType == TesiraCommand.Get)
            {
                switch (response.AttributeCode)
                {
                    case TesiraAttributeCode.MinLevel:
                        MinLevel = response.TryParseResponse()["value"].Value<double>();
                        break;
                    case TesiraAttributeCode.MaxLevel:
                        MaxLevel = response.TryParseResponse()["value"].Value<double>();
                        break;
                    case TesiraAttributeCode.Mute:
                        _mute = response.TryParseResponse()["value"].Value<bool>();
                        break;
                    case TesiraAttributeCode.Level:
                        _level = response.TryParseResponse()["value"].Value<double>();
                        LevelString = _level.ToString("F1") + " dB";
                        break;
                    case TesiraAttributeCode.Gain:
                        _level = response.TryParseResponse()["value"].Value<double>();
                        LevelString = _level.ToString("F1") + " dB";
                        break;
                }
            }
        }
        protected override void ReceivedResponse(TesiraResponse response)
        {
            if (response.OtherCommandElements.Any())
            {
                try
                {
                    var channel = uint.Parse(response.OtherCommandElements.First());
#if DEBUG
                    Debug.WriteInfo("Response for channel " + channel);
#endif
                    if (Channels.ContainsKey(channel))
                    {
                        Channels[channel].UpdateFromResponse(response);
                        return;
                    }
                }
                catch (Exception e)
                {
                    CloudLog.Exception(e, "Should be response with index");
                }
            }

            if (response.CommandType != TesiraCommand.Get || response.OtherCommandElements.Any())
            {
                return;
            }

            var json = response.TryParseResponse();

            if (json == null)
            {
                CloudLog.Error("{0} could not parse {1} value from json message \"{2}\"", GetType().Name,
                               response.AttributeCode, response.Message);
                return;
            }

            try
            {
                switch (response.AttributeCode)
                {
                case TesiraAttributeCode.NumChannels:
                    var numberOfChannels = json["value"].Value <uint>();
                    for (uint i = 1; i <= numberOfChannels; i++)
                    {
                        if (!Channels.ContainsKey(i))
                        {
                            Channels[i] = CreateChannel(i);
                        }
                    }
                    OnInitialized();
                    break;

                default:
                    UpdateAttribute(response.AttributeCode, json);
                    break;
                }
            }
            catch (Exception e)
            {
                CloudLog.Error("{0} could not parse {1} value from json \"{2}\", {3}", GetType().Name,
                               response.AttributeCode, json.ToString(), e.Message);
            }
        }