private SyncDto CreateLock(string orderID, List <string> procedures, string userName, string userID, string domain, string site, string ip) { Order order = _dbContext.Set <Order>().Where(p => p.UniqueID == orderID).FirstOrDefault(); Patient patient = _dbContext.Set <Patient>().Where(p => p.UniqueID == order.PatientID).FirstOrDefault(); string rpguids = ""; string ownerIP = ip; foreach (string procedureID in procedures) { rpguids += procedureID + "&" + userID + "&" + ownerIP + "|"; } if (rpguids.EndsWith("|")) { rpguids = rpguids.Trim("|".ToCharArray()); } SyncDto syncDto = new SyncDto(); syncDto.OrderID = orderID; //syncDto.SyncType = (int)LockType.Register; syncDto.SyncType = (int)LockType.Register; syncDto.Owner = userID; syncDto.OwnerIP = ownerIP; syncDto.CreateTime = DateTime.Now; syncDto.ModuleID = "0400"; syncDto.PatientName = patient.LocalName; syncDto.PatientID = patient.PatientNo; syncDto.AccNo = order.AccNo; syncDto.Counter = 1; syncDto.ProcedureIDs = rpguids; syncDto.Domain = domain; return(syncDto); }
private void AddLock(SyncDto syncDto) { Sync sync = Mapper.Map <SyncDto, Sync>(syncDto); _dbContext.Set <Sync>().Add(sync); _dbContext.SaveChanges(); }
public HttpResponseMessage UploadReport(long timestamp) { DicServerContext ctx = new DicServerContext(); SyncDto sync = new SyncDto(); DateTime lastSync = DateHandlers.UnixToDateTime(timestamp); sync.UsbVendors = new List <UsbVendorDto>(); foreach (UsbVendor vendor in ctx.UsbVendors.Where(v => v.ModifiedWhen > lastSync)) { sync.UsbVendors.Add(new UsbVendorDto { VendorId = (ushort)vendor.VendorId, Vendor = vendor.Vendor }); } sync.UsbProducts = new List <UsbProductDto>(); foreach (UsbProduct product in ctx.UsbProducts.Where(p => p.ModifiedWhen > lastSync)) { sync.UsbProducts.Add(new UsbProductDto { Id = product.Id, Product = product.Product, ProductId = (ushort)product.ProductId, VendorId = (ushort)product.Vendor.VendorId }); } sync.Offsets = new List <CdOffsetDto>(); foreach (CompactDiscOffset offset in ctx.CdOffsets.Where(o => o.ModifiedWhen > lastSync)) { sync.Offsets.Add(new CdOffsetDto(offset, offset.Id)); } sync.Devices = new List <DeviceDto>(); foreach (Device device in ctx.Devices.Where(d => d.ModifiedWhen > lastSync).ToList()) { sync.Devices.Add(new DeviceDto(JsonConvert.DeserializeObject <DeviceReportV2>(JsonConvert.SerializeObject(device, Formatting.None, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })), device.Id, device.OptimalMultipleSectorsRead)); } JsonSerializer js = JsonSerializer.Create(); StringWriter sw = new StringWriter(); js.Serialize(sw, sync); return(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(sw.ToString(), Encoding.UTF8, "application/json") }); }
public void DeleteLockByUserIDTest() { var userName = RandomHelper.GenerateString(); var userID = RandomHelper.GenerateString(); var domain = RandomHelper.GenerateString(); var site = RandomHelper.GenerateString(); var ip = RandomHelper.GenerateString(); var result = _reportLockService.AddLockByReportID(_reportID, userName, userID, domain, site, ip); Assert.AreEqual(true, result); _reportLockService.DeleteLockByUserID(LockType.Register, userID); SyncDto syncDto = _reportLockService.GetLock(_orderID, LockType.Register); Assert.IsNull(syncDto); }
public void GetLockTest() { var userName = RandomHelper.GenerateString(); var userID = RandomHelper.GenerateString(); var domain = RandomHelper.GenerateString(); var site = RandomHelper.GenerateString(); var ip = RandomHelper.GenerateString(); var result = _reportLockService.AddLockByReportID(_reportID, userName, userID, domain, site, ip); Assert.AreEqual(true, result); SyncDto syncDto = _reportLockService.GetLock(_orderID, LockType.Register); Assert.IsNotNull(syncDto); Assert.AreEqual(true, syncDto.ProcedureIDs.Contains(_procedureID)); }
private void UpdateLock(SyncDto syncDto) { var sync = _dbContext.Set <Sync>().Where(p => p.OrderID == syncDto.OrderID && p.SyncType == syncDto.SyncType).FirstOrDefault(); //new data sync.Owner = syncDto.Owner; sync.OwnerIP = syncDto.OwnerIP; sync.CreateTime = syncDto.CreateTime; sync.ModuleID = syncDto.ModuleID; sync.PatientName = syncDto.PatientName; sync.AccNo = syncDto.AccNo; sync.Counter = syncDto.Counter; sync.ProcedureIDs = syncDto.ProcedureIDs; sync.Domain = syncDto.Domain; _dbContext.SaveChanges(); }
public bool AddLockByProcedureID(string procedureID, string userName, string userID, string domain, string site, string ip) { Procedure procedure = _dbContext.Set <Procedure>().Where(p => p.UniqueID == procedureID).FirstOrDefault(); var result = GetLock(procedure.OrderID, LockType.Register); if (result == null) { List <String> procedureList = new List <string>(); procedureList.Add(procedure.UniqueID); SyncDto syncDto = CreateLock(procedure.OrderID, procedureList, userName, userID, domain, site, ip); AddLock(syncDto); result = GetLock(procedure.OrderID, LockType.Register); if (result != null) { return(true); } } //owner has the lock else if (result.ProcedureIDs.Contains(procedureID + "&" + userID)) { return(true); } // no the lock else if (!result.ProcedureIDs.Contains(procedureID)) { string ownerIP = ip; if (result.ProcedureIDs == "") { result.ProcedureIDs += procedureID + "&" + userID + "&" + ownerIP; } else { result.ProcedureIDs += "|" + procedureID + "&" + userID + "&" + ownerIP; } //add lock UpdateLock(result); return(true); } return(false); }
public static void UpdateMasterDatabase(bool create) { DicContext mctx = DicContext.Create(Settings.Settings.MasterDbPath); mctx.Database.Migrate(); mctx.SaveChanges(); try { long lastUpdate = 0; DateTime latest = DateTime.MinValue; if (!create) { List <DateTime> latestAll = new List <DateTime>(); if (mctx.UsbVendors.Any()) { latestAll.Add(mctx.UsbVendors.Max(v => v.ModifiedWhen)); } if (mctx.UsbProducts.Any()) { latestAll.Add(mctx.UsbProducts.Max(p => p.ModifiedWhen)); } if (mctx.CdOffsets.Any()) { latestAll.Add(mctx.CdOffsets.Max(o => o.ModifiedWhen)); } if (mctx.Devices.Any()) { latestAll.Add(mctx.Devices.Max(d => d.LastSynchronized)); } if (latestAll.Any()) { latest = latestAll.Max(t => t); lastUpdate = (latest.ToFileTimeUtc() - new DateTime(1970, 1, 1).ToFileTimeUtc()) / 10000000; } } if (lastUpdate == 0) { create = true; DicConsole.WriteLine("Creating master database"); } else { DicConsole.WriteLine("Updating master database"); DicConsole.WriteLine("Last update: {0}", latest); } DateTime updateStart = DateTime.UtcNow; WebRequest request = WebRequest.Create($"https://www.discimagechef.app/api/update?timestamp={lastUpdate}"); ((HttpWebRequest)request).UserAgent = $"DiscImageChef {typeof(Version).Assembly.GetName().Version}"; request.Method = "GET"; request.ContentType = "application/json"; WebResponse response = request.GetResponse(); if (((HttpWebResponse)response).StatusCode != HttpStatusCode.OK) { DicConsole.ErrorWriteLine("Error {0} when trying to get updated entities.", ((HttpWebResponse)response).StatusCode); return; } Stream data = response.GetResponseStream(); StreamReader reader = new StreamReader(data ?? throw new InvalidOperationException()); SyncDto sync = JsonConvert.DeserializeObject <SyncDto>(reader.ReadToEnd()); if (create) { DicConsole.WriteLine("Adding USB vendors"); foreach (UsbVendorDto vendor in sync.UsbVendors) { mctx.UsbVendors.Add(new UsbVendor(vendor.VendorId, vendor.Vendor)); } DicConsole.WriteLine("Added {0} usb vendors", sync.UsbVendors.Count); DicConsole.WriteLine("Adding USB products"); foreach (UsbProductDto product in sync.UsbProducts) { mctx.UsbProducts.Add(new UsbProduct(product.VendorId, product.ProductId, product.Product)); } DicConsole.WriteLine("Added {0} usb products", sync.UsbProducts.Count); DicConsole.WriteLine("Adding CompactDisc read offsets"); foreach (CdOffsetDto offset in sync.Offsets) { mctx.CdOffsets.Add(new CdOffset(offset) { Id = offset.Id }); } DicConsole.WriteLine("Added {0} CompactDisc read offsets", sync.Offsets.Count); DicConsole.WriteLine("Adding known devices"); foreach (DeviceDto device in sync.Devices) { mctx.Devices.Add(new Device(device) { Id = device.Id }); } DicConsole.WriteLine("Added {0} known devices", sync.Devices.Count); } else { long addedVendors = 0; long addedProducts = 0; long addedOffsets = 0; long addedDevices = 0; long modifiedVendors = 0; long modifiedProducts = 0; long modifiedOffsets = 0; long modifiedDevices = 0; DicConsole.WriteLine("Updating USB vendors"); foreach (UsbVendorDto vendor in sync.UsbVendors) { UsbVendor existing = mctx.UsbVendors.FirstOrDefault(v => v.Id == vendor.VendorId); if (existing != null) { modifiedVendors++; existing.Vendor = vendor.Vendor; existing.ModifiedWhen = updateStart; mctx.UsbVendors.Update(existing); } else { addedVendors++; mctx.UsbVendors.Add(new UsbVendor(vendor.VendorId, vendor.Vendor)); } } DicConsole.WriteLine("Added {0} USB vendors", addedVendors); DicConsole.WriteLine("Modified {0} USB vendors", modifiedVendors); DicConsole.WriteLine("Updating USB products"); foreach (UsbProductDto product in sync.UsbProducts) { UsbProduct existing = mctx.UsbProducts.FirstOrDefault(p => p.VendorId == product.VendorId && p.ProductId == product.ProductId); if (existing != null) { modifiedProducts++; existing.Product = product.Product; existing.ModifiedWhen = updateStart; mctx.UsbProducts.Update(existing); } else { addedProducts++; mctx.UsbProducts.Add(new UsbProduct(product.VendorId, product.ProductId, product.Product)); } } DicConsole.WriteLine("Added {0} USB products", addedProducts); DicConsole.WriteLine("Modified {0} USB products", modifiedProducts); DicConsole.WriteLine("Updating CompactDisc read offsets"); foreach (CdOffsetDto offset in sync.Offsets) { CdOffset existing = mctx.CdOffsets.FirstOrDefault(o => o.Id == offset.Id); if (existing != null) { modifiedOffsets++; existing.Agreement = offset.Agreement; existing.Manufacturer = offset.Manufacturer; existing.Model = offset.Model; existing.Submissions = offset.Submissions; existing.Offset = offset.Offset; existing.ModifiedWhen = updateStart; mctx.CdOffsets.Update(existing); } else { addedOffsets++; mctx.CdOffsets.Add(new CdOffset(offset) { Id = offset.Id }); } } DicConsole.WriteLine("Added {0} CompactDisc read offsets", addedOffsets); DicConsole.WriteLine("Modified {0} CompactDisc read offsets", modifiedOffsets); DicConsole.WriteLine("Updating known devices"); foreach (DeviceDto device in sync.Devices) { Device existing = mctx.Devices.FirstOrDefault(d => d.Id == device.Id); if (existing != null) { modifiedDevices++; mctx.Remove(existing); existing = new Device(device) { Id = device.Id, OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead }; mctx.Devices.Add(existing); } else { addedDevices++; mctx.Devices.Add(new Device(device) { Id = device.Id, OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead }); } } DicConsole.WriteLine("Added {0} known devices", addedDevices); DicConsole.WriteLine("Modified {0} known devices", modifiedDevices); } } catch (Exception ex) { DicConsole.ErrorWriteLine("Exception {0} when updating database.", ex); } finally { DicConsole.WriteLine("Saving changes..."); mctx.SaveChanges(); } }