コード例 #1
0
        private RoomConfig ParseRoomConfigNode(XmlReader roomReader)
        {
            string     RoomId     = roomReader[SettingsConst.RoomId_PropertyName];
            RoomConfig roomConfig = new RoomConfig(RoomId);

            roomConfig.EventLeewaySeconds = this.EventLeewaySeconds;

            roomReader.ReadToDescendant(SettingsConst.RoomTitle_XmlElementName);

            do
            {
                if (SettingsConst.Sensors_XmlElementName.Equals(roomReader.LocalName))
                {
                    ParseSensorsNode(roomReader, roomConfig);
                    roomReader.ReadEndElement();
                }
                else
                {
                    parseRoomConfigTag(roomConfig, roomReader.LocalName, roomReader.ReadElementContentAsString());
                }
                roomReader.Read();
            } while(!SettingsConst.RoomConfigSections_XmlElementName.Equals(roomReader.LocalName) && roomReader.ReadState != ReadState.EndOfFile);

            return(roomConfig);
        }
コード例 #2
0
        private void parseRoomConfigTag(RoomConfig roomConfig, string TagName, string TagValue)
        {
            var propertyInfo = typeof(RoomConfig).GetRuntimeProperty(TagName);

            if (propertyInfo != null)
            {
                propertyInfo.SetValue(roomConfig, TagValue);
            }
        }
コード例 #3
0
 private void ParseSensorsNode(XmlReader roomReader, RoomConfig roomConfig)
 {
     if (roomReader.ReadToDescendant(SettingsConst.Sensor_XmlElementName))
     {
         do
         {
             RoomSensor sensor = new RoomSensor();
             sensor.DeviceId  = roomReader[SettingsConst.DeviceId_PropertyName];
             sensor.Telemetry = roomReader[SettingsConst.Telemetry_XmlAttributeName];
             roomConfig.RoomSensors.Add(sensor);
         } while (roomReader.ReadToNextSibling(roomReader.LocalName));
     }
 }
コード例 #4
0
ファイル: LevelXmlConfig.cs プロジェクト: Kryndex/SmartHive
        public async void Load()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    string configUrl   = config.GetPropertyValue(SettingsConst.LevelConfigUrl_PropertyName);
                    Stream xmlInStream = await client.GetStreamAsync(configUrl);

                    using (XmlReader reader = XmlReader.Create(xmlInStream))
                    {
                        reader.ReadStartElement();
                        while (reader.ReadToFollowing(SettingsConst.RoomConfigSections_XmlElementName))
                        {
                            RoomConfig room = ParseRoomConfigNode(reader);
                            // If this room related to our level - store config in memory
                            if (this.LevelId.Equals(room.ServiceBusTopic))
                            {
                                this.LevelRooms.Add(room.Location, room);
                                if (string.IsNullOrEmpty(this.ServiceBusNamespace))
                                {
                                    this.SasKey                 = room.SasKey;
                                    this.SasKeyName             = room.SasKeyName;
                                    this.ServiceBusNamespace    = room.ServiceBusNamespace;
                                    this.ServiceBusSubscription = room.ServiceBusSubscription;
                                    this.ServiceBusTopic        = room.ServiceBusTopic;
                                }
                            }
                        }
                    }
                    // Inform listeners we sucessfuly load settings
                    if (this.OnSettingsLoaded != null)
                    {
                        this.isLoaded = true;
                        this.OnSettingsLoaded.Invoke(this, true);
                    }
                }
            }
            catch (Exception ex)
            {
                // Inform listeners about an error
                if (this.OnSettingsLoaded != null)
                {
                    this.OnSettingsLoaded.Invoke(this, false);
                    this.isLoaded = false;
                }
            }
        }
コード例 #5
0
        public async void Load()
        {
            string     configUrl = config.GetPropertyValue(SettingsConst.LevelConfigUrl_PropertyName);
            HttpClient client    = new HttpClient();

            try
            {
                client.Timeout = new TimeSpan(0, 5, 0); // TODO: Add settings

                await client.GetStreamAsync(configUrl).ContinueWith(task =>
                {
                    if (task.Status != TaskStatus.RanToCompletion)
                    {
                        ReportAnError(new HttpRequestException(String.Format("Error Http request {0} task status {1}", configUrl, task.Status)));
                    }
                    else
                    {
                        var xmlInStream = task.Result;
                        if (xmlInStream != null)
                        {
                            using (XmlReader reader = XmlReader.Create(xmlInStream))
                            {
                                reader.ReadStartElement();
                                while (reader.ReadToFollowing(SettingsConst.RoomConfigSections_XmlElementName))
                                {
                                    RoomConfig room = ParseRoomConfigNode(reader);
                                    // If this room related to our level - store config in memory
                                    if (this.LevelId.Equals(room.ServiceBusTopic))
                                    {
                                        this.LevelRooms.Add(room.Location, room);
                                        if (string.IsNullOrEmpty(this.ServiceBusNamespace))
                                        {
                                            this.SasKey                 = room.SasKey;
                                            this.SasKeyName             = room.SasKeyName;
                                            this.ServiceBusNamespace    = room.ServiceBusNamespace;
                                            this.ServiceBusSubscription = room.ServiceBusSubscription;
                                            this.ServiceBusTopic        = room.ServiceBusTopic;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            ReportAnError(new HttpRequestException("Http req. " + configUrl + " return an error error: " + task.Status));
                        }
                        // Inform listeners we sucessfuly load settings
                        if (this.OnSettingsLoaded != null)
                        {
                            this.isLoaded = true;
                            this.OnSettingsLoaded.Invoke(this, true);
                        }
                    }
                });
            }catch (Exception ex)
            {
                ReportAnError(ex);
            }
            finally
            {
                client.Dispose();
            }
        }