private object CreateDataModel(TemperatureData data, DateTime now)
 {
     return data == null
         ? null
         : new
         {
             d = data.CurrentDate,
             t = data.Temperature,
             h = data.Humidity,
             dd = data.CurrentDate.ToShortTimeString(),
             ddd = data.CurrentDate < now.AddDays(-1) ? data.CurrentDate.ToString("M") : null,
             dt = FormatTemperature(data.Temperature),
             dh = data.Humidity + "%"
         };
 }
        public void MicroclimateDataReceived(int channel, decimal temperature, int humidity)
        {
            var now = DateTime.Now;

            Logger.Debug("microclimate data received: c={0}, t={1}, h={2}", channel, temperature, humidity);

            using (ISession session = Context.OpenSession())
            {
                var sensors = session
                    .Query<TemperatureSensor>()
                    .Where(s => s.Channel == channel)
                    .ToList();

                Logger.Debug("{0} sensors was found", sensors.Count);

                foreach (var sensor in sensors)
                {
                    var intTemperature = Convert.ToInt32(temperature);

                    sensor.CurrentTemperature = intTemperature;
                    sensor.CurrentHumidity = humidity;
                    sensor.Timestamp = now;

                    var data = new TemperatureData
                    {
                        Id = Guid.NewGuid(),
                        CurrentDate = now,
                        Temperature = intTemperature,
                        Humidity = humidity,
                        Sensor = sensor
                    };

                    session.Save(sensor);
                    session.Save(data);
                }

                session.Flush();
            }
        }