public static User GetUserRefreshIfNecessary(string username, string caseid) { try { APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne <User>("Users", "[username] = '" + username + "'"); if (!bm.Success) { throw new Exception("Unable to retrieve user details for case: " + username); } var user = (User)bm.Data; if (DateTime.Now > user.expiry_date.Value.AddMinutes(-30)) { RefreshToken(user.refresh_token, caseid); bm = APetaPoco.PpRetrieveOne <User>("Users", "[username] = '" + username + "'"); if (!bm.Success) { throw new Exception("Unable to retrieve UPDATED user details for case: " + username); } user = (User)bm.Data; } return(user); } catch (Exception ex) { string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name; LOG.Write(errorString); LOG.Write(ex.Message); LOG.InsertEvent(errorString, "API", ex.Message, caseid); return(null); } finally { GC.Collect(); } }
static void Debug() { //AUP.UploadZip("44186", "47777", @"d:\temp\1.3.12.2.1107.5.1.4.65115.3000001603240736279390001264491.zip", "henryknipe", "20160411091821_henryknipe", "1.3.12.2.1107.5.8.9.13.26.65.26.126.218.77882191"); APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne <DbCase>("Cases", "[case_id] = '20160607180814_frank'"); var pcase = (DbCase)bm.Data; bm = APetaPoco.PpRetrieveList <DbStudy>("Studies", "[case_id] = '20160607180814_frank'"); pcase.study_list = (List <DbStudy>)bm.Data; foreach (var st in pcase.study_list) { bm = APetaPoco.PpRetrieveList <DbSeries>("Series", "[case_id] = '20160607180814_frank' AND [study_uid] = '" + st.study_uid + "'"); st.series = (List <DbSeries>)bm.Data; //ADCM.DownloadStudy(st); LOG.Write("Converting DCM to PNG..."); AIMG.MultiFrameProcess(st); bool convertComplete = AIMG.ConvertDcmToPng(st); //if (!convertComplete) //{ // ACASE.ClearCaseFiles(pcase); // throw new Exception("Unable to convert study to PNG"); //} LOG.Write("Completed image conversion"); LOG.Write("Optimizing PNG's for study..."); AIMG.OptiPng(st); LOG.Write("Completed optimization."); //AIMG.ZipSeries(st); } //ACASE.ProcessCase(pcase); }
public static void RefreshToken(string refreshToken, string caseid) { try { var api = ACFG.GetSiteApiDetails(); string responseFromServer = null; try { WebRequest request = WebRequest.Create(api.oauth_url + "token?client_id=" + api.site_id + "&client_secret=" + api.site_secret + "&refresh_token=" + refreshToken + "&grant_type=refresh_token"); request.Method = "POST"; WebResponse response = request.GetResponse(); var dataStream = response.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(dataStream); responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); } catch (WebException ex) { using (var stream = ex.Response.GetResponseStream()) using (var reader = new System.IO.StreamReader(stream)) { string errorResponse = reader.ReadToEnd(); LOG.InsertEvent("Unable to upload zip", "API", errorResponse, caseid); } GC.Collect(); return; } var tokenResp = JsonConvert.DeserializeObject <TokenResponse>(responseFromServer); if (tokenResp == null || tokenResp == default(TokenResponse)) { throw new Exception("Unable to retrieve valid token response"); } APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne <User>("Users", "[refresh_token] = '" + refreshToken + "'"); if (bm.Success) { var user = (User)bm.Data; user.access_token = tokenResp.access_token; user.refresh_token = tokenResp.refresh_token; user.expiry_date = DateTime.Now.AddSeconds(tokenResp.expires_in - 10); bm = APetaPoco.PpUpdate(user); if (!bm.Success) { throw new Exception("Unable to update user details with new token"); } } } catch (Exception ex) { string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name; LOG.Write(errorString); LOG.Write(ex.Message); LOG.InsertEvent(errorString, "API", ex.Message, caseid); } finally { GC.Collect(); } }
public static Node GetDebugNode() { APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne <Node>("PACS", "[LocalAe] = 'ANDYPACS3'"); if (bm.Success) { return((Node)bm.Data); } else { return(null); } }
public static Node GetSelectedNode() { APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne <Node>("PACS", "[Selected] = 1"); if (bm.Success) { return((Node)bm.Data); } else { return(null); } }
private static DbStudy GetStudy(string condition) { APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne <DbStudy>("Studies", condition); if (!bm.Success) { throw new Exception("Unable to get Study based on condition: " + condition); } var dbs = (DbStudy)bm.Data; bm = APetaPoco.PpRetrieveList <DbSeries>("Series", condition); if (bm.Success) { var series = (List <DbSeries>)bm.Data; dbs.series = series; } return(dbs); }
public static bool ConvertDcmToPng(DbStudy study) { try { string dcmPath = ADCM.GetStoreString(); string studyPath = Path.Combine(dcmPath, study.study_uid); if (!Directory.Exists(studyPath)) { throw new Exception("Study path not found"); } var allSeriesPaths = Directory.GetDirectories(studyPath); if (allSeriesPaths.Length < 1) { throw new Exception("No series subdirectories"); } foreach (var s in allSeriesPaths) { var dcmFiles = Directory.GetFiles(s, "*.dcm"); if (dcmFiles.Length < 1) { throw new Exception("No DCM files inside series path: " + s); } DicomFile tempdcm = new DicomFile(dcmFiles[0]); tempdcm.Load(); var seriesName = tempdcm.DataSet[DicomTags.SeriesDescription].GetString(0, null); var seriesUID = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1); if (string.IsNullOrEmpty(seriesName)) { seriesName = "Unamed_Series"; } APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne <DbSeries>("Series", "[case_id] = '" + study.case_id + "' AND [series_uid] = '" + seriesUID + "'"); DbSeries dbseries = null; if (bm.Success) { dbseries = (DbSeries)bm.Data; } string outputPath = Path.Combine(dcmPath, "OUTPUT", study.case_id, study.study_uid, seriesUID); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } //int fileCount = 0; for (int k = 0; k < dcmFiles.Length; k++) { DicomFile dcmFile = new DicomFile(dcmFiles[k]); dcmFile.Load(); int fileCount = 0; var windowWidth = dcmFile.DataSet[DicomTags.WindowWidth].ToString(); if (!string.IsNullOrEmpty(windowWidth)) { var tempSplitString = windowWidth.Split('\\'); windowWidth = tempSplitString[0]; } var windowCenter = dcmFile.DataSet[DicomTags.WindowCenter].ToString(); if (!string.IsNullOrEmpty(windowCenter)) { var tempSplitString = windowCenter.Split('\\'); windowCenter = tempSplitString[0]; } var ww = dcmFile.DataSet[DicomTags.WindowWidth].GetFloat32(0, 0); var wc = dcmFile.DataSet[DicomTags.WindowCenter].GetFloat32(0, 0); if (ww == 0 && !string.IsNullOrEmpty(windowWidth)) { if (windowWidth.Contains(".")) { var tempSplitString = windowWidth.Split('.'); ww = int.Parse(tempSplitString[0]); } else { ww = int.Parse(windowWidth); } } if (wc == 0 && !string.IsNullOrEmpty(windowCenter)) { if (windowCenter.Contains(".")) { var tempSplitString = windowCenter.Split('.'); wc = int.Parse(tempSplitString[0]); } else { wc = int.Parse(windowCenter); } } LocalSopDataSource localds = new LocalSopDataSource(dcmFile); if (!localds.IsImage) { continue; } ImageSop sop = new ImageSop(localds); int frameCount = sop.Frames.Count; var fileName = dcmFile.DataSet[DicomTags.InstanceNumber].GetInt16(0, 0); if (frameCount > 1) { for (int j = 1; j <= frameCount; j++) { GC.Collect(); Frame f = sop.Frames[j]; var jpgPath = Path.Combine(outputPath, fileName + "." + j + ".png"); Bitmap bmp = null; if (string.IsNullOrEmpty(windowWidth) || string.IsNullOrEmpty(windowCenter)) { bmp = DrawDefaultFrame(f); } else { bmp = DrawLutFrame(f, ww, wc); } if (bmp != null) { if (dbseries != null && dbseries.crop_h != null && dbseries.crop_w != null && dbseries.crop_x != null && dbseries.crop_y != null) { bmp = Crop(bmp, dbseries.crop_x.Value, dbseries.crop_y.Value, dbseries.crop_w.Value, dbseries.crop_h.Value); } SaveImage(bmp, jpgPath); } fileCount += 1; GC.Collect(); } } else { GC.Collect(); var jpgPath = Path.Combine(outputPath, fileName + ".png"); Frame f = sop.Frames[1]; Bitmap bmp = null; if (string.IsNullOrEmpty(windowWidth) || string.IsNullOrEmpty(windowCenter)) { bmp = DrawDefaultFrame(f); } else { bmp = DrawLutFrame(f, ww, wc); } if (bmp != null) { if (dbseries != null && dbseries.crop_h != null && dbseries.crop_w != null && dbseries.crop_x != null && dbseries.crop_y != null) { bmp = Crop(bmp, dbseries.crop_x.Value, dbseries.crop_y.Value, dbseries.crop_w.Value, dbseries.crop_h.Value); } SaveImage(bmp, jpgPath); } fileCount += 1; GC.Collect(); } } } LOG.InsertEvent("Successfully converted study from DCM to PNG", "IMG", null, study.case_id, study.study_uid); return(true); } catch (Exception ex) { string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name; LOG.Write(errorString); LOG.Write(ex.Message); LOG.InsertEvent(errorString, "IMG", ex.Message, study.case_id, study.study_uid); return(false); } }
public static BoolMessage CheckIfOnlySelected(Node node) { var rbm = new BoolMessage(); rbm.Data = null; try { APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne <Node>("PACS", "[Selected] = 1"); Node currentSelectedNode = null; if (bm.Success) { currentSelectedNode = (Node)bm.Data; } else { return(bm); } if (currentSelectedNode == null || currentSelectedNode == default(Node)) { if (node.Selected) { rbm.Success = true; } else { throw new Exception("There's no selected node in the database"); } } else { if (node.Selected) { if (node.Id == currentSelectedNode.Id) { rbm.Success = true; } else { currentSelectedNode.Selected = false; bm = APetaPoco.PpUpdate(currentSelectedNode); if (bm.Success) { rbm.Success = true; } else { return(bm); } } } else { if (currentSelectedNode.Id != node.Id) { rbm.Success = true; } else { throw new Exception("Can't unselect the only selected node in database"); } } } } catch (Exception ex) { rbm.Success = false; rbm.Message = ex.Message; } return(rbm); }