private void SaveLastReadHistoryInConfiguration()
        {
            var values = Enum.GetValues(typeof(HistoryDataTypeEnum)).Cast <HistoryDataTypeEnum>();

            using (CgmContext cgmContext = new CgmContext())
            {
                var minimedConfiguration = cgmContext.GetConfiguration <MinimedConfiguration>();

                if (minimedConfiguration == null)
                {
                    minimedConfiguration = new MinimedConfiguration();
                    cgmContext.AddConfiguration <MinimedConfiguration>(minimedConfiguration);
                }

                minimedConfiguration.LastRead = new List <LastPumpRead>();
                foreach (var value in values)
                {
                    int rtc = GetMaxRtc((int)value);
                    if (rtc != 0)
                    {
                        minimedConfiguration.LastRead.Add(new LastPumpRead()
                        {
                            DataType = (int)value, LastRtc = rtc
                        });
                    }
                }

                cgmContext.UpdateConfiguration <MinimedConfiguration>(minimedConfiguration);
            }
        }
 public void SaveConfiguration <T>(T configuration)
 {
     using (CgmContext cgmContext = new CgmContext())
     {
         var config = cgmContext.GetConfiguration <T>();
         if (config != null)
         {
             cgmContext.UpdateConfiguration <T>(configuration);
         }
         else
         {
             cgmContext.AddConfiguration <T>(configuration);
         }
     }
 }
        public void SaveSession(SerializerSession session)
        {
            this.AddUpdateSessionToDevice(session);
            using (CgmContext cgmContext = new CgmContext())
            {
                var settings = cgmContext.GetConfiguration <PumpSettings>();
                if (settings != null)
                {
                    cgmContext.UpdateConfiguration <PumpSettings>(session.PumpSettings);
                }
                else
                {
                    cgmContext.AddConfiguration <PumpSettings>(session.PumpSettings);
                }

                if (session.PumpDataHistory != null)
                {
                    var all = session.PumpDataHistory.PumpEvents.Where(e => e.EventType == EventTypeEnum.PLGM_CONTROLLER_STATE).ToList();
                    all.ForEach(e => session.PumpDataHistory.PumpEvents.Remove(e));

                    if (session.PumpDataHistory.PumpEvents.Count > 0)
                    {
                        this.Sync(session.PumpDataHistory.PumpEvents.Select(e => new History(e)).ToList(), (int)HistoryDataTypeEnum.Pump);
                    }
                    if (session.PumpDataHistory.SensorEvents.Count > 0)
                    {
                        this.Sync(session.PumpDataHistory.SensorEvents.Select(e => new History(e)).ToList(), (int)HistoryDataTypeEnum.Sensor);
                    }

                    SaveLastReadHistoryInConfiguration();
                }

                if (session.Status != null && session.Status.Count > 0 && session.PumpTime.PumpDateTime.HasValue)
                {
                    foreach (var item in session.Status)
                    {
                        DeviceStatus deviceStatus = new DeviceStatus();
                        deviceStatus.DeviceStatusKey   = session.PumpTime.Rtc.GetInt32(0).ToString();
                        deviceStatus.DeviceStatusBytes = item.BytesAsString;
                        if (cgmContext.DeviceStatus.FirstOrDefault(e => e.DeviceStatusKey == deviceStatus.DeviceStatusKey) != null)
                        {
                            cgmContext.DeviceStatus.Add(deviceStatus);
                            cgmContext.SaveChanges();
                        }
                    }
                }
            }
        }