예제 #1
0
        private void RequestVera()
        {
            // Generate URI and get HTTP response content
            string strResponse = this.GetWebResponse(this.GetRequestUri(), true);

            // Check response
            if (string.IsNullOrEmpty(strResponse))
            {
                return;
            }

            // Deserialize JSON
            Dictionary <string, object> jsonResponse = this._jsonSerializer.DeserializeObject(strResponse) as Dictionary <string, object>;

            // Clearing datas
            if (this.CurrentLoadTime == 0)
            {
                this.Sections.Clear();
                this.Rooms.Clear();
                this.Categories.Clear();
                this.Scenes.Clear();
                this.Devices.Clear();
            }

            // Load Vera Objects
            this.LoadVeraObjects <Section>(jsonResponse, "sections", this.Sections);
            this.LoadVeraObjects <Room>(jsonResponse, "rooms", this.Rooms);
            this.LoadVeraObjects <Category>(jsonResponse, "categories", this.Categories);
            this.LoadVeraObjects <Scene>(jsonResponse, "scenes", this.Scenes);
            this.LoadVeraObjects <Device>(jsonResponse, "devices", this.Devices, (item) =>
            {
                var categoryId = Convert.ToInt32(item["category"]);
                if (this._deviceTypes.ContainsKey(categoryId))
                {
                    return(Activator.CreateInstance(this._deviceTypes[categoryId]) as Device);
                }
                else
                {
                    return(new Device());
                }
            });

            // Process metadatas
            if (jsonResponse.ContainsKey("full") && (int)jsonResponse["full"] == 1)
            {
                this.Model           = jsonResponse["model"].ToString();
                this.SerialNumber    = jsonResponse["serial_number"].ToString();
                this.TemperatureUnit = jsonResponse["temperature"].ToString();
                this.Version         = jsonResponse["version"].ToString();
            }
            if (jsonResponse.ContainsKey("state"))
            {
                this.CurrentState = StateUtils.GetStateFromCode(Convert.ToInt32(jsonResponse["state"]));;
            }
            if (jsonResponse.ContainsKey("comment"))
            {
                this.CurrentComment = jsonResponse["comment"].ToString();
            }
            if (jsonResponse.ContainsKey("loadtime"))
            {
                this.CurrentLoadTime = Convert.ToInt64(jsonResponse["loadtime"]);
            }
            if (jsonResponse.ContainsKey("dataversion"))
            {
                this.CurrentDataVersion = Convert.ToInt64(jsonResponse["dataversion"]);
            }
            if (jsonResponse.ContainsKey("mode"))
            {
                this.HouseMode = (VeraHouseMode)Convert.ToInt32(jsonResponse["mode"]);
            }

            // Update OK
            this.LastUpdate = DateTime.Now;

            // Raise DataReceived event
            if (this.DataReceived != null)
            {
                this.DataReceived(this, new VeraDataReceivedEventArgs()
                {
                    DataVersion = this.CurrentDataVersion,
                    LoadTime    = this.CurrentLoadTime,
                    Length      = strResponse.Length,
                    RawData     = strResponse
                });
            }
        }