Beispiel #1
0
        public void Run()
        {
            InitializeSubscriptionClient();

            var entitiesContext = new EntitiesContext();
            ITemperatureAggregateService temperatureAggregateService = new TemperatureAggregateService(
                new EntityRepository<TemperatureAggregate>(entitiesContext), new EntityRepository<Brew>(entitiesContext));

            while (true)
            {
                Trace.WriteLine("Receiving message...", "Info");
                var brokeredMessage = _subscriptionClient.Receive();
                if (brokeredMessage != null)
                {
                    Trace.WriteLine("Received message.", "Info");
                    Trace.WriteLine(
                        string.Format("Message id: {0} - Message enqueued: {1}", brokeredMessage.MessageId,
                                      brokeredMessage.EnqueuedTimeUtc), "Verbose");

                    SensorReading sensorReading = null;

                    // We support two message types: a serialized SensorReading and one that only sends us
                    // message properties (for netmf support). Let's see what we have...
                    if (brokeredMessage.Properties.Any() && brokeredMessage.Properties.ContainsKey("SensorId"))
                    {
                        sensorReading = new SensorReading
                                            {
                                                SensorId = (string)brokeredMessage.Properties["SensorId"],
                                                When = new DateTime(Convert.ToInt64(brokeredMessage.Properties["When"]), DateTimeKind.Utc),
                                                Value = Convert.ToDouble(brokeredMessage.Properties["Value"], CultureInfo.CreateSpecificCulture("en-US"))
                                            };
                        if (sensorReading.When.Year > (DateTime.UtcNow.Year + 1) || sensorReading.When.Year < DateTime.UtcNow.Year)
                        {
                            sensorReading.When = brokeredMessage.EnqueuedTimeUtc;
                        }
                    }
                    else
                    {
                        sensorReading = brokeredMessage.GetBody<SensorReading>();
                    }

                    Trace.WriteLine(string.Format("Message id: {0} - Sensor: {1} - Sensor reading: {2}", brokeredMessage.MessageId, sensorReading.SensorId, sensorReading.Value), "Verbose");

                    Trace.WriteLine("Processing message...", "Info");
                    temperatureAggregateService.AggregateData(sensorReading.SensorId, sensorReading.When, sensorReading.Value);

                    brokeredMessage.Complete();
                    Trace.WriteLine("Processed message.", "Info");
                }
            }
        }
Beispiel #2
0
 public EntityRepository(EntitiesContext entities)
 {
     this._entities = entities;
 }