Beispiel #1
0
 public List <LedValue> GetAllLeds()
 {
     using (var context = new PiHomeContext())
     {
         return(context.Led.AsNoTracking().Select(x => new LedValue
         {
             Id = x.Id, ModuleId = x.ModuleId, X = x.Position.X, Y = x.Position.Y, Index = x.Index,
             Color = new Color()
         }).ToList());
     }
 }
Beispiel #2
0
        public void LogData(int configId, int value)
        {
            using (var context = new PiHomeContext())
            {
                var config = context.LogConfiguration.SingleOrDefault(x => x.Id == configId);

                if (config != null)
                {
                    LogData(config, value);
                }
            }
        }
Beispiel #3
0
 public List <Log> GetLogs(int moduleId, int featureId, DateTime from, DateTime to)
 {
     using (var context = new PiHomeContext())
     {
         return(context.LogConfiguration
                .Where(x => x.ModuleId == moduleId && x.FeatureId == featureId)
                .SelectMany(x => x.Log)
                .Where(x => x.Time > from && x.Time < to)
                .AsNoTracking()
                .OrderBy(x => x.Time)
                .ToList());
     }
 }
Beispiel #4
0
        public void LogData(int moduleId, int featureId, double value)
        {
            using (var context = new PiHomeContext())
            {
                var config = context.LogConfiguration.Include(x => x.Feature)
                             .SingleOrDefault(x => x.FeatureId == featureId && x.ModuleId == moduleId);

                if (config != null)
                {
                    LogData(config, (int)Math.Round(value * config.Feature.LogFactor));
                }
            }
        }
Beispiel #5
0
 public string GetPresetToActivate()
 {
     using (var context = new PiHomeContext())
     {
         var pa = context.PresetActivation.Include(x => x.Preset)
                  .FirstOrDefault(x => x.NextActivationTime < DateTime.UtcNow);
         if (pa != null)
         {
             CalculateNextActivation(ref pa);
             context.SaveChanges();
             return(pa.Preset.Name);
         }
         return(null);
     }
 }
Beispiel #6
0
 public Dictionary <Module, LedValue[]> GetPreset(string name)
 {
     using (var context = new PiHomeContext())
     {
         return(context.LedPreset.AsNoTracking().Where(x => x.Name == name).SelectMany(x => x.LedPresetValues).Select(x => new { Value = new LedValue
                                                                                                                                 {
                                                                                                                                     Color = new Color(x.Color),
                                                                                                                                     ModuleId = x.Led.ModuleId,
                                                                                                                                     Index = x.Led.Index,
                                                                                                                                     X = x.Led.Position.X,
                                                                                                                                     Y = x.Led.Position.Y,
                                                                                                                                     Id = x.LedId
                                                                                                                                 }, Module = x.Led.Module }).AsEnumerable().GroupBy(x => x.Module).ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToArray()));
     }
 }
Beispiel #7
0
 public void AddFeature(int moduleId, int featureId, TimeSpan interval)
 {
     using (var context = new PiHomeContext())
     {
         var module = context.Module.Single(x => x.Id == moduleId);
         module.FeatureIds = module.FeatureIds.Union(new[] { featureId }).ToArray();
         module.LogConfiguration.Add(new LogConfiguration
         {
             FeatureId = featureId,
             Interval  = interval,
             NextPoll  = DateTime.UtcNow
         });
         context.SaveChanges();
     }
 }
Beispiel #8
0
 public ModuleDto GetModule(int id)
 {
     using (var context = new PiHomeContext())
     {
         var module = context.Module.Include(x => x.Led).AsNoTracking().Single(x => x.Id == id);
         return(new ModuleDto
         {
             Name = module.Name, FeatureIds = module.FeatureIds,
             Leds = module.Led.Select(x => new LedDto {
                 Index = x.Index, X = x.Position.X, Y = x.Position.Y
             })
                    .ToArray()
         });
     }
 }
Beispiel #9
0
 private void LogData(LogConfiguration config, int value)
 {
     using (var context = new PiHomeContext())
     {
         var log = new Log
         {
             LogConfigurationId = config.Id,
             Time  = DateTime.UtcNow,
             Value = value
         };
         config.NextPoll = DateTime.UtcNow.Add(config.Interval);
         context.Add(log);
         context.SaveChanges();
     }
 }
Beispiel #10
0
 public LedValue[] ToLedValues(IEnumerable <LedPresetDto> dtoValues)
 {
     using (var context = new PiHomeContext())
     {
         var modules    = context.Module.AsNoTracking().ToDictionary(x => x.Name, x => x.Id);
         var ledsFromDb = context.Led.AsTracking().GroupBy(x => x.ModuleId).ToDictionary(x => x.Key, x => x.ToDictionary(y => y.Index, y => y.Id));
         return(dtoValues.Select(x =>
         {
             var mId = modules[x.ModuleName];
             return new LedValue
             {
                 Color = x.Color, ModuleId = mId, Id = ledsFromDb[mId][x.Led.Index], Index = x.Led.Index,
                 X = x.Led.X, Y = x.Led.Y
             };
         }).ToArray());
     }
 }
Beispiel #11
0
 public PresetDto GetPresetDto(string name)
 {
     using (var context = new PiHomeContext())
     {
         return(context.LedPreset.AsNoTracking().Where(x => x.Name == name).Include(x => x.LedPresetValues).ThenInclude(x => x.Led).Select(x => new PresetDto
         {
             Name = x.Name, LastChangeDate = x.ChangeDate,
             LedValues = x.LedPresetValues.Select(y => new LedPresetDto
             {
                 ModuleName = y.Led.Module.Name,
                 Led = new LedDto {
                     X = y.Led.Position.X, Index = y.Led.Index, Y = y.Led.Position.Y
                 },
                 Color = new Color(y.Color)
             }).ToArray()
         }).SingleOrDefault());
     }
 }
Beispiel #12
0
        public void AddLedValues(IEnumerable <LedValue> values)
        {
            var leds = values.ToArray();

            using (var context = new PiHomeContext())
            {
                foreach (var ledValue in leds)
                {
                    context.Led.Add(new Led
                    {
                        ModuleId = ledValue.ModuleId,
                        Index    = ledValue.Index,
                        Position = new NpgsqlPoint(ledValue.X, ledValue.Y)
                    });
                }
                context.SaveChanges();
            }
        }
Beispiel #13
0
        public void DeleteOldLogs(int moduleId)
        {
            using (var context = new PiHomeContext())
            {
                var configs = context.LogConfiguration.Where(x => x.ModuleId == moduleId && x.RetensionTime != null)
                              .AsNoTracking();
                var logsToRem = new List <Log>();
                foreach (var config in configs)
                {
                    var data = DateTime.UtcNow.Subtract(config.RetensionTime.Value);
                    logsToRem.AddRange(context.Log.Where(x => x.LogConfigurationId == config.Id && x.Time < data));
                }

                if (logsToRem.Any())
                {
                    context.Log.RemoveRange(logsToRem);
                    context.SaveChanges();
                }
            }
        }
Beispiel #14
0
 public Module UpsertModule(ModuleDto mod, IPAddress ip)
 {
     using (var context = new PiHomeContext())
     {
         var module       = context.Module.SingleOrDefault(x => x.Name == mod.Name);
         var existingLeds = new Dictionary <int, Led>();
         if (module == null)
         {
             module = new Module
             {
                 Name       = mod.Name,
                 FeatureIds = mod.FeatureIds,
                 Ip         = ip
             };
             context.Add(module);
         }
         else
         {
             module.FeatureIds = mod.FeatureIds;
             module.Ip         = ip;
             existingLeds      = context.Led.Where(x => x.ModuleId == module.Id).ToDictionary(x => x.Index, x => x);
         }
         context.Led.RemoveRange(existingLeds.Where(x => mod.Leds.All(y => y.Index != x.Key)).Select(x => x.Value));
         foreach (var ledValue in mod.Leds)
         {
             if (!existingLeds.TryGetValue(ledValue.Index, out var led))
             {
                 led = new Led {
                     Index = ledValue.Index, Module = module
                 };
                 context.Led.Add(led);
             }
             led.Position = new NpgsqlPoint(ledValue.X, ledValue.Y);
         }
         context.SaveChanges();
         return(module);
     }
 }
Beispiel #15
0
 public void UpdatePresetActivation(PresetActivation presetActivation, string presetName)
 {
     using (var context = new PiHomeContext())
     {
         var preset     = context.LedPreset.Single(x => x.Name == presetName);
         var activation = context.PresetActivation.SingleOrDefault(x => x.Id == presetActivation.Id);
         if (activation == null)
         {
             presetActivation.Preset = preset;
             CalculateNextActivation(ref presetActivation);
             context.PresetActivation.Add(presetActivation);
         }
         else
         {
             activation.Preset         = preset;
             activation.ActivationTime = presetActivation.ActivationTime;
             activation.Active         = presetActivation.Active;
             activation.DaysOfWeek     = presetActivation.DaysOfWeek;
             CalculateNextActivation(ref activation);
         }
         context.SaveChanges();
     }
 }