//// 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") }); } }
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)); } } }