private static void TestRepository()
        {
            ILoggingRepository repo = Core.Factory.GetILoggingRepository();

            var dateTimeStart = new DateTime(2011, 10, 4, 0, 3, 12);
            var user = repo.GetUserByApiKey("bWFpbEBwZXRlcmdlcnJpdHNlbi5ubA");

            Random rand = new Random();

            var e1Offset = rand.NextDouble();
            var e2Offset = rand.NextDouble();
            var gasOffset = rand.NextDouble();

            for (int i = 0; i < 100; i++) {
                var logEntry = new LogEntry();
                logEntry.Timestamp = dateTimeStart.AddMinutes(90 * i);
                logEntry.UserId = user.UserId;
                logEntry.E1 = Convert.ToDecimal(e1Offset);
                logEntry.E2 = Convert.ToDecimal(e2Offset);
                logEntry.GasMeasurementMoment = logEntry.Timestamp;
                logEntry.GasMeasurementValue = Convert.ToDecimal(gasOffset);

                repo.AddEntry(logEntry);

                e1Offset += rand.NextDouble();
                e2Offset += rand.NextDouble();
                gasOffset += rand.NextDouble();

                if (i % 600 == 0)
                    Debug.WriteLine(string.Format("{0} %", i / 600));
            }
        }
        //// POST api/logentries
        public void Post([FromBody]PostEntry value)
        {
            if (ModelState.IsValid) {
                try {
                    var user = repo.GetUserByApiKey(value.ApiKey);
                    if (user == null)
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("Invalid API Key: {0}", value.ApiKey)) });

                    DateTime gasMeasurementMoment = value.Timestamp;

                    var hubContext = GlobalHost.ConnectionManager.GetHubContext<UsageHub>();
                    hubContext.Clients.Group(value.ApiKey).newCurrentUsage(value.Timestamp, value.CurrentUsage, value.CurrentRetour);

                    if (!DateTime.TryParseExact("20" + value.GasMeasurementMoment, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out gasMeasurementMoment))
                        gasMeasurementMoment = value.Timestamp;

                    var logEntry = new LogEntry() {
                        Timestamp = value.Timestamp,
                        E1 = value.E1,
                        E2 = value.E2,
                        E1Retour = value.E1Retour,
                        E2Retour = value.E2Retour,
                        CurrentTariff = value.CurrentTariff,
                        CurrentUsage = value.CurrentUsage,
                        CurrentRetour = value.CurrentRetour,
                        GasMeasurementMoment = gasMeasurementMoment,
                        GasMeasurementValue = value.GasMeasurementValue,
                        PvCounter = Convert.ToDecimal(value.PvProductionCounter) / 1000,
                        UserId = user.UserId
                    };

                    repo.AddEntry(logEntry);

                    return;
                } catch (Exception ex) {
                    repo = Core.Factory.ResetILoggingRepository();

                    new LogEvent(ex.Message).Raise();

                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
            } else {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Invalid data") });
            }
        }
        public Model.Usage GetUsage(LogEntry logEntry)
        {
            var baseTimestamp = GetUsageTimestamp(logEntry.Timestamp);

            Usage usage = null;
            bool updateUsage = false;

            if (lastHourlyUsage.ContainsKey(logEntry.UserId) && lastHourlyUsage[logEntry.UserId].Timestamp == baseTimestamp) {
                usage = lastHourlyUsage[logEntry.UserId];
            }

            if (usage == null) {
                updateUsage = true;
                usage = context.Usages.FirstOrDefault(x => x.UserId == logEntry.UserId  && x.Timestamp == baseTimestamp);
            }

            if (usage == null) {
                usage = new Model.Usage() { Timestamp = baseTimestamp, UserId = logEntry.UserId };

                var prevUsage = context.Usages.Where(x => x.UserId == logEntry.UserId && x.Timestamp < baseTimestamp).OrderByDescending(x => x.Timestamp).FirstOrDefault();
                if (prevUsage != null) {
                    usage.E1Start = prevUsage.E1Current;
                    usage.E2Start = prevUsage.E2Current;
                    usage.E1RetourStart = prevUsage.E1RetourCurrent;
                    usage.E2RetourStart = prevUsage.E2RetourCurrent;
                    usage.PvProductionStart = prevUsage.PvProductionCurrent;
                } else {
                    usage.E1Start = logEntry.E1;
                    usage.E2Start = logEntry.E2;
                    usage.E1RetourStart = logEntry.E1Retour;
                    usage.E2RetourStart = logEntry.E2Retour;
                    usage.PvProductionStart = 0;
                }

                context.Usages.Add(usage);
            }

            if (updateUsage) {
                lastHourlyUsage[logEntry.UserId] = usage;
            }

            return usage;
        }
        private Model.Usage GetGasUsage(LogEntry logEntry)
        {
            var baseTimestamp = GetUsageTimestamp(logEntry.GasMeasurementMoment).AddHours(-1);

            Usage usage = null;

            if (lastHourlyUsage.ContainsKey(logEntry.UserId) && lastHourlyUsage[logEntry.UserId].Timestamp == baseTimestamp) {
                usage = lastHourlyUsage[logEntry.UserId];
            }

            if (usage == null)
                usage = context.Usages.FirstOrDefault(x => x.UserId == logEntry.UserId && x.Timestamp == baseTimestamp);

            if (usage != null && usage.GasStart == 0) {
                var prevUsage = context.Usages.Where(x => x.UserId == logEntry.UserId && x.UsageId < usage.UsageId).OrderByDescending(x => x.UsageId).FirstOrDefault();
                if (prevUsage != null && prevUsage.GasCurrent > 0)
                    usage.GasStart = prevUsage.GasCurrent;
                else
                    usage.GasStart = context.Usages.Where(x => x.UserId == logEntry.UserId && x.UsageId < usage.UsageId).Max(x => x.GasCurrent);
            }

            return usage;
        }