public DeviceConfig Post(string deviceId, [FromBody] DeviceConfig deviceConfig) { using (HydroContext context = new HydroContext()) { var config = context.DeviceConfig.SingleOrDefault(x => x.COMPort == deviceId); if (config != null) { config.FriendlyName = deviceConfig.FriendlyName; config.DosingConfig = deviceConfig.DosingConfig; } else { config = new DeviceConfig() { COMPort = deviceId, DosingConfig = deviceConfig.DosingConfig, FriendlyName = deviceConfig.FriendlyName }; context.DeviceConfig.Add(config); } context.SaveChanges(); return(config); } }
public List <DeviceData> GetHistorical() { using (HydroContext context = new HydroContext()) { return(context.DeviceData.OrderByDescending(x => x.DateCreated).Take(10).ToList()); } }
public Dictionary <string, DeviceData> GetMinuteHistorical(int count = 5) { using (HydroContext context = new HydroContext()) { var startMinute = DateTime.Now.AddMinutes(-count); Dictionary <string, DeviceData> data = new Dictionary <string, DeviceData>(); for (int i = 0; i < count; i++) { DateTime key = startMinute.AddMinutes(i); data[key.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")] = context.DeviceData.Where(x => key <= x.DateCreated && x.DateCreated <= key.AddSeconds(59)).Count() > 0 ? new DeviceData() { DateCreated = key, PHValue = context.DeviceData.Where(x => key <= x.DateCreated && x.DateCreated <= key.AddSeconds(59)).Average(x => x.PHValue), WaterTemperature = context.DeviceData.Where(x => key <= x.DateCreated && x.DateCreated <= key.AddSeconds(59)).Average(x => x.WaterTemperature), Temperature = context.DeviceData.Where(x => key <= x.DateCreated && x.DateCreated <= key.AddSeconds(59)).Average(x => x.Temperature), Humidity = context.DeviceData.Where(x => key <= x.DateCreated && x.DateCreated <= key.AddSeconds(59)).Average(x => x.Humidity), } : null; } return(data); } }
private void StartReader() { var cancellationTokenSource = new CancellationTokenSource(); var updateTask = Repeat.Interval(TimeSpan.FromSeconds(1), () => { DeviceData data = GetCurrentSerialDeviceData(); this.Data.Add(data); this.Data = this.Data.OrderByDescending(x => x.DateCreated).Take(10).ToList(); }, cancellationTokenSource.Token); var saveTask = Repeat.Interval(TimeSpan.FromMinutes(1), () => { if (this.Data.Count > 0) { using (HydroContext context = new HydroContext()) { context.DeviceData.Add(this.Data.First()); context.SaveChanges(); } } }, cancellationTokenSource.Token); }
public DeviceInfo Get(string deviceId) { using (HydroContext context = new HydroContext()) { return(new DeviceInfo() { DeviceConfig = context.DeviceConfig.SingleOrDefault(x => x.COMPort == deviceId), DeviceData = _deviceManager.GetDeviceReaders().FirstOrDefault(x => x.GetDeviceId() == deviceId).GetLoggedSerialDeviceData().FirstOrDefault() }); } }
public List <DeviceInfo> Get() { using (HydroContext context = new HydroContext()) { return(_deviceManager.GetDeviceReaders().Select(x => new DeviceInfo() { DeviceConfig = context.DeviceConfig.SingleOrDefault(), DeviceData = x.GetLoggedSerialDeviceData().FirstOrDefault() }).ToList()); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, HydroContext context) { context.Database.EnsureCreated(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); if (!env.IsDevelopment()) { app.UseSpaStaticFiles(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { // To learn more about options for serving an Angular SPA from ASP.NET Core, // see https://go.microsoft.com/fwlink/?linkid=864501 spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript: "start"); } }); }