public void AddUpdateSessionToDevice(SerializerSession session) { using (CgmContext cgmContext = new CgmContext()) { Device dev = cgmContext.Device.FirstOrDefault(e => e.SerialNumber == session.SessionDevice.Device.SerialNumber); if (dev == null) { dev = new Device(); dev.Name = session.SessionDevice.Device.ModelNumber; dev.SerialNumber = session.SessionDevice.Device.SerialNumber; dev.SerialNumberFull = session.SessionDevice.Device.SerialNumberFull; cgmContext.Device.Add(dev); } if (session.SessionCommunicationParameters.LinkMac != null) { dev.LinkMac = BitConverter.ToString(session.SessionCommunicationParameters.LinkMac); } if (session.SessionCommunicationParameters.PumpMac != null) { dev.PumpMac = BitConverter.ToString(session.SessionCommunicationParameters.PumpMac); } if (session.SessionCommunicationParameters.LinkKey != null) { dev.LinkKey = BitConverter.ToString(session.SessionCommunicationParameters.LinkKey); } dev.RadioChannel = session.SessionCommunicationParameters.RadioChannel.ToString(); cgmContext.SaveChanges(); } }
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(); } } } } }
public void GetOrSetSessionAndSettings(SerializerSession session) { if (!string.IsNullOrEmpty(session.SessionDevice.Device.SerialNumber)) { using (CgmContext cgmContext = new CgmContext()) { Device device = cgmContext.Device.FirstOrDefault(e => e.SerialNumber == session.SessionDevice.Device.SerialNumber); if (device != null) { if (!string.IsNullOrEmpty(device.LinkMac)) { session.SessionCommunicationParameters.LinkMac = device.LinkMac.GetBytes(); } if (!string.IsNullOrEmpty(device.LinkKey)) { session.SessionCommunicationParameters.LinkKey = device.LinkKey.GetBytes(); } if (!string.IsNullOrEmpty(device.PumpMac)) { session.SessionCommunicationParameters.PumpMac = device.PumpMac.GetBytes(); } session.SessionCommunicationParameters.RadioChannel = byte.Parse(device.RadioChannel); session.SessionDevice.Device.ModelNumber = device.Name; session.SessionDevice.Device.SerialNumber = device.SerialNumber; session.SessionDevice.Device.SerialNumberFull = device.SerialNumberFull; } else { //Insert the device Device dev = new Device(); dev.Name = session.SessionDevice.Device.ModelNumber; dev.SerialNumber = session.SessionDevice.Device.SerialNumber; dev.SerialNumberFull = session.SessionDevice.Device.SerialNumberFull; cgmContext.Device.Add(dev); cgmContext.SaveChanges(); } } } }
public void Sync(List <History> histories, int datatype) { using (CgmContext cgmContext = new CgmContext()) { var query = cgmContext.History.Where(e => e.HistoryDataType == datatype).ToList(); var SyncQuery = from comp in histories join entry in query on comp.Key equals entry.Key select comp; var missingHistory = histories.Except(SyncQuery.ToList()).ToList(); if (missingHistory.Count > 0) { //check for double keys. should not happen..... but hey... var groups = missingHistory.GroupBy(e => e.Key) .Select(group => new { Metric = group.Key, Count = group.Count() }); var errors = groups.Where(e => e.Count > 1).ToList(); foreach (var error in errors) { var all = missingHistory.Where(e => e.Key == error.Metric).ToList(); //keep the first for (int i = 1; i < all.Count; i++) { missingHistory.Remove(all[i]); } } cgmContext.History.AddRange(missingHistory); cgmContext.SaveChanges(); } //keep only the last 5 days (ca. 5*400 =2000 events ish) Delete(2000, datatype); } }
public void AddKeys(List <string> keys) { List <HistoryStatus> statusList = new List <HistoryStatus>(); keys.ForEach(e => statusList.Add(new HistoryStatus() { Key = e, Status = 0 })); using (CgmContext ctx = new CgmContext()) { try { ctx.HistoryStatus.AddRange(statusList); ctx.SaveChanges(); } catch (Exception ex) { throw; } } }