00001
00006 package DisplayGUI;
00007 import org.omg.CORBA.IntHolder;
00008 import ipc.*;
00009
00010
00011 import java.util.*;
00012 import GuiComponents.System.*;
00013
00014 public class ConfigurationInterface implements displayParams, SCTInfo {
00015 private static ConfigurationInterface instance = new ConfigurationInterface();
00016 private int maxWidth=0;
00017 private int midX=0, midY=0;
00018 private int moduleCount=0;
00019 private java.awt.Polygon polygon;
00020
00021 ModuleCell[][][] moduleCells;
00022 double[] radius = {0.,0.,0.,0.,0.,0.};
00023 int[] xPoints = {0,0,0,0};
00024 int[] yPoints = {0,0,0,0};
00025 long fillsn=3000;
00026
00027
00028 private SortedMap[] serialNumberMap = new TreeMap[sctViews];
00029
00030 private Map snMap;
00031 private boolean dcsAvailable;
00032
00033 sctConf.Configuration h;
00034
00035 private ConfigurationInterface() {
00036 Refresh();
00037
00038
00039 }
00040
00041 public static ConfigurationInterface getInstance() {
00042 return instance;
00043 }
00044
00045 public void setWindowSize(int maxWidth, int maxHeight) {
00046 this.maxWidth=maxWidth;
00047 midX=maxWidth/2;
00048 midY=maxHeight/2;
00049 maxWidth*=0.95;
00050 radius[0]=0.35 * (double)maxWidth/2.;
00051 radius[1]=0.52 * (double)maxWidth/2.;
00052 radius[2]= 0.5 * (double)maxWidth/2.;
00053 radius[3]= 0.76* (double)maxWidth/2.;
00054 radius[4]= 0.74 * (double)maxWidth/2.;
00055 radius[5]= maxWidth/2.;
00056 }
00057
00058 public void Refresh() {
00059 for(int i=0;i<sctViews;i++) serialNumberMap[i]=new TreeMap();
00060 moduleCells = new ModuleCell[sctViews][][];
00061 snMap = new HashMap();
00062 dcsAvailable=false;
00063 moduleCount=0;
00064 }
00065
00066 public boolean isConfigured() {
00067 return (moduleCount>0);
00068 }
00069
00070 public void getConfiguration() {
00071 getConfiguration(-1);
00072 }
00073
00074 public void getConfiguration(int moduleGroup) {
00075 h = SystemInterface.getInstance().getConfigurationService();
00076 if(h==null) {
00077 System.err.println("Hardware configuration unavailable - is config_server running?");
00078 return;
00079 }
00080 try {
00081 int[] parts = h.listPartitions();
00082 for(int p=0; p<parts.length; p++) {
00083
00084 int[] crates = h.listCratesInPartition(parts[p]);
00085 for(int c=0; c<crates.length; c++) {
00086
00087 int[] rods = h.listRodsInCrate(parts[p], crates[c]);
00088 for(int r=0; r<rods.length; r++) {
00089
00090
00091
00092
00093 int[] MURs = h.listMURSInRod(parts[p], crates[c], rods[r]);
00094 for(int mur=0; mur<MURs.length; mur++) {
00095
00096 String[] modules = h.listModulesInMUR(parts[p], MURs[mur]);
00097
00098 for(int m=0; m<modules.length; m++) {
00099
00100
00101 IntHolder MUR = new IntHolder(), num = new IntHolder();
00102 h.translateFromSN(modules[m], MUR, num);
00103
00104 IntHolder partition = new IntHolder(), crate = new IntHolder(), rodno=new IntHolder(), channel=new IntHolder() ;
00105 h.translateToROD(MUR.value, num.value, partition, crate, rodno, channel);
00106
00107 SNInfo snInfo = (SNInfo) snMap.get(modules[m]);
00108 if(snInfo==null) snInfo = new SNInfo();
00109 snInfo.set(SNInfo.PARTITION,Integer.toString(partition.value));
00110 snInfo.set(SNInfo.ROD_CRATE,Integer.toString(crate.value));
00111 snInfo.set(SNInfo.ROD_NUMBER,Integer.toString(rodno.value));
00112 snInfo.set(SNInfo.ROD_CHANNEL,Integer.toString(channel.value));
00113 snMap.put(modules[m],snInfo);
00114
00115 try {
00116 moduleGroup = h.getModuleGroup(modules[m]);
00117 }catch(Exception groupE){
00118 System.err.println("SctGUI::ConfigurationInterface - can't retrieve moduleGroup : "+groupE.toString());
00119 moduleGroup=0;
00120 }
00121
00122 IntHolder barrel = new IntHolder(), row = new IntHolder(), number = new IntHolder();
00123 try {
00124 h.translateToBarrel(MUR.value, num.value, barrel, row, number);
00125 defineBarrelModuleGraphicsCell(barrel.value, row.value, number.value, MUR.value, moduleGroup, modules[m]);
00126 } catch(sctConf.ConfigurationException eConfig) {
00127 h.translateToEndcap(MUR.value, num.value, barrel, row, number);
00128 defineEndCapModuleGraphicsCell(barrel.value, row.value, number.value, MUR.value, moduleGroup, modules[m]);
00129 }
00130 try {
00131 h.translateToPowerSupply(MUR.value, num.value,partition,crate,channel);
00132 definePowerSupplyMapping(crate.value,channel.value,modules[m]);
00133 }catch(Exception ee) {}
00134
00135
00136 }
00137 }
00138 }
00139 }
00140 }
00141 } catch(sctConf.ConfigurationException eConfig) {
00142 System.err.println("Configuration exception retrieving configuration " + eConfig.detail);
00143 eConfig.printStackTrace();
00144 } catch(Exception e) {
00145 System.err.println("Non-configuration exception retrieving configuration ..." + e);
00146 e.printStackTrace();
00147 }
00148
00149 writeLookUpTable();
00150
00151
00152 }
00153
00154 public void definePowerSupplyMapping(int crate, int channel, String sn) {
00155
00156 dcsAvailable=true;
00157 SNInfo snInfo = (SNInfo) snMap.get(sn);
00158 if(snInfo==null) snInfo = new SNInfo();
00159
00160 String crateString = Integer.toString(crate);
00161 snInfo.set(SNInfo.DCS_CRATE,crateString);
00162 String channelString = Integer.toString(channel);
00163 snInfo.set(SNInfo.DCS_CHANNEL,channelString);
00164
00165 if(crateString.length()==1) crateString="0"+crateString;
00166 if(channelString.length()==1) channelString="0"+channelString;
00167 snInfo.set(SNInfo.DCS_HEADER,"Crate"+crateString+".Channel"+channelString+".");
00168 snInfo.set(SNInfo.IV_FILE_HEADER,"CRATE"+crateString+"CHAN"+channelString);
00169
00170 snMap.put(sn,snInfo);
00171
00172 }
00173 public String getSNInfo(String sn, int index) {
00174 SNInfo snInfo = (SNInfo) snMap.get(sn);
00175 if(snInfo==null) return "-1";
00176 return snInfo.get(index);
00177 }
00178
00179 public boolean isDCSAvailable() {
00180 return dcsAvailable;
00181 }
00182
00183
00184 public String addModule(int barrel, int row, int number, int MUR, int moduleGroup, String serialNo) {
00185
00186 for(int i=0;i<sctViews;i++) {
00187 if(serialNumberMap[i].containsKey(serialNo)) return "Module "+serialNo+" is already defined in "+SCTViewNames[i];
00188 }
00189
00190 for (Iterator i=serialNumberMap[barrel].entrySet().iterator(); i.hasNext(); ) {
00191 Map.Entry e = (Map.Entry) i.next();
00192 String thisSerialNo = (String)e.getKey();
00193 ModuleCell thisCell = (ModuleCell)e.getValue();
00194 if(thisCell.getRow()==row && thisCell.getModulePosition()==number) return "That location already taken by "+thisCell.getSerialNo();
00195 }
00196 int thisSCTView = barrel;
00197
00198 try {
00199 moduleGroup = h.getModuleGroup(serialNo);
00200 }catch(Exception ee){System.err.println("SctGUI::ConfigurationInterface - can't retrieve moduleGroup : "+ee.toString());}
00201
00202 if(barrel<4) {
00203 thisSCTView+=3;
00204 defineBarrelModuleGraphicsCell(thisSCTView, row, number, MUR, moduleGroup, serialNo);
00205 }
00206 else if(barrel>12) {
00207 thisSCTView-=12;
00208 defineEndCapModuleGraphicsCell(thisSCTView, row, number, MUR, moduleGroup, serialNo);
00209 }
00210 else {
00211 thisSCTView-=3;
00212 thisSCTView*=-1;
00213 defineEndCapModuleGraphicsCell(thisSCTView, row, number, MUR, moduleGroup, serialNo);
00214 }
00215 return null;
00216 }
00217
00218 private void defineBarrelModuleGraphicsCell(int barrel, int row, int number, int MUR, int moduleGroup, String serialNo) {
00219 for(int i=0;i<sctViews;i++) {
00220 if(serialNumberMap[i].containsKey(serialNo)) {
00221 System.out.println("** Module "+serialNo+" defined more than once!");
00222 return;
00223 }
00224 }
00225 if(barrel < 3 || barrel > 6) {
00226 System.out.println("** Module "+serialNo+" reported to be in nonexistant barrel " + barrel + "!");
00227 return;
00228 }
00229
00230 int thisSCTView=barrel-3;
00231 int moduleIndex = number;
00232 moduleIndex+=6;
00233 if(moduleIndex>5) moduleIndex--;
00234 int thisRow=row;
00235 if(moduleCells[thisSCTView]==null) moduleCells[thisSCTView] = new ModuleCell[barrelRows[thisSCTView]][];
00236 if(moduleCells[thisSCTView][thisRow]==null) moduleCells[thisSCTView][thisRow] = new ModuleCell[12];
00237
00238 int xloc=maxWidth/2 + number*boxWidth;
00239
00240 int yloc = yOffSet + (yOffSetCount[thisSCTView] + barrelRows[thisSCTView] - row - 1)*boxHeight;
00241
00242
00243 int[] xPoints = {xloc,xloc,xloc+boxWidth,xloc+boxWidth};
00244 int[] yPoints = {yloc,yloc+boxHeight,yloc+boxHeight,yloc};
00245
00246 moduleCells[thisSCTView][thisRow][moduleIndex] = new ModuleCell(serialNo,thisSCTView,row,number,MUR,moduleGroup,xPoints,yPoints,4);
00247 serialNumberMap[thisSCTView].put(serialNo,moduleCells[thisSCTView][thisRow][moduleIndex]);
00248 moduleCount++;
00249 }
00250
00251 private void defineEndCapModuleGraphicsCell(int disk, int quadrant, int number, int MUR, int moduleGroup, String serialNo) {
00252 for(int i=0;i<sctViews;i++) {
00253 if(serialNumberMap[i].containsKey(serialNo)) {
00254 System.out.println("** Module "+serialNo+" defined more than once!");
00255 return;
00256 }
00257 }
00258
00259
00260 int thisSCTView = (disk<0) ? 3-disk : 12+disk;
00261 if(moduleCells[thisSCTView]==null) moduleCells[thisSCTView] = new ModuleCell[4][];
00262 if(moduleCells[thisSCTView][quadrant]==null) moduleCells[thisSCTView][quadrant] = new ModuleCell[33];
00263
00264 calculatePolygon(thisSCTView,quadrant,number);
00265
00266 moduleCells[thisSCTView][quadrant][number] = new ModuleCell(serialNo,thisSCTView,quadrant,number,MUR,moduleGroup,xPoints,yPoints,4);
00267 serialNumberMap[thisSCTView].put(serialNo,moduleCells[thisSCTView][quadrant][number]);
00268 moduleCount++;
00269 }
00270
00271
00272
00273 public ModuleCell getCell(int sctView, int x, int y) {
00274
00275
00276
00277 if(sctView<4) {
00278
00279 int yIndex = ((yOffSetCount[sctView]+barrelRows[sctView])*boxHeight - y + yOffSet)/boxHeight;
00280 if(yIndex<0 || yIndex>barrelRows[sctView]-1) return null;
00281 int diff = x -maxWidth/2;
00282 int xIndex = diff/boxWidth;
00283 if(diff>0 && xIndex==0) return null;
00284 xIndex+=5;
00285 if(xIndex<0 || xIndex>11) return null;
00286 if(moduleCells[sctView]!=null && moduleCells[sctView][yIndex]!=null) {
00287 if(moduleCells[sctView][yIndex][xIndex]!=null) return moduleCells[sctView][yIndex][xIndex];
00288 }
00289 }
00290 else {
00291
00292
00293 for (Iterator i=serialNumberMap[sctView].entrySet().iterator(); i.hasNext(); ) {
00294 Map.Entry e = (Map.Entry) i.next();
00295 ModuleCell thisCell = (ModuleCell)e.getValue();
00296 if(thisCell.contains(x,y)) return thisCell;
00297 }
00298 }
00299 return null;
00300 }
00301
00302 public Hashtable getSerialNumberMap(int sctView) {
00303 return new Hashtable(serialNumberMap[sctView]);
00304 }
00305 public int getNumberOfModules(int sctView) {
00306 return serialNumberMap[sctView].size();
00307 }
00308 public int getTotalNumberOfModules() {
00309 return moduleCount;
00310 }
00311 public Set getSortedVector(int sctView) {
00312 return serialNumberMap[sctView].keySet();
00313 }
00314 public ModuleCell getModuleCell(int sctView, String serialNo) {
00315 return (ModuleCell)serialNumberMap[sctView].get(serialNo);
00316 }
00317
00318 public void drawSCTViewOutline(java.awt.Graphics2D g2,int selection) {
00319 g2.setColor(java.awt.Color.lightGray);
00320 g2.setFont(displayTextFont);
00321 java.awt.FontMetrics metrics = g2.getFontMetrics();
00322 int rowNo=0;
00323
00324 switch(selection) {
00325 case Barrel3:
00326 case Barrel4:
00327 case Barrel5:
00328 case Barrel6:
00329 int ilow = yOffSetCount[selection];
00330
00331 int ihigh = ilow+barrelRows[selection];
00332 int midX= maxWidth/2;
00333
00334
00335 for(int y=ihigh-1;y>=ilow;y--) {
00336 String rowLabel = Integer.toString(rowNo);
00337 int labelWidth = metrics.stringWidth(rowLabel);
00338 int xLabel = midX - 6*boxWidth - labelWidth -8;
00339
00340 int thisRow = rowNo;
00341 boolean rowUsed = (moduleCells[selection]!=null && moduleCells[selection][thisRow]!=null);
00342 g2.setColor(java.awt.Color.lightGray);
00343 for(int x=-6;x<=6;x++) {
00344 if(x==0) continue;
00345 g2.drawRect(midX+(boxWidth*x),yOffSet+(y*boxHeight), boxWidth, boxHeight);
00346 }
00347 java.awt.Color labelColor = rowUsed ? java.awt.Color.black : java.awt.Color.lightGray;
00348 g2.setColor(labelColor);
00349 g2.drawString(rowLabel,xLabel,yOffSet+y*boxHeight+boxHeight);
00350 rowNo++;
00351 }
00352 g2.setColor(java.awt.Color.lightGray);
00353 g2.drawRect(midX - 6*boxWidth, yOffSet, 13*boxWidth, 56*boxHeight);
00354 break;
00355 case EndCap1a:
00356 case EndCap1c:
00357 case EndCap7a:
00358 case EndCap7c:
00359 case EndCap8a:
00360 case EndCap8c:
00361 for(int quadrant=0;quadrant<4;quadrant++) {
00362 for(int number=13;number<23;number++) {
00363 calculatePolygon(selection,quadrant,number);
00364 if(polygon!=null) {
00365 g2.setColor(java.awt.Color.white);
00366 g2.fill(polygon);
00367 g2.setColor(java.awt.Color.lightGray);
00368 g2.draw(polygon);
00369 }
00370 }
00371 }
00372 for(int quadrant=0;quadrant<4;quadrant++) {
00373 for(int number=0;number<13;number++) {
00374 calculatePolygon(selection,quadrant,number);
00375 if(polygon!=null) {
00376 g2.setColor(java.awt.Color.white);
00377 g2.fill(polygon);
00378 g2.setColor(java.awt.Color.lightGray);
00379 g2.draw(polygon);
00380 }
00381 }
00382 }
00383 break;
00384 case EndCap2a:
00385 case EndCap2c:
00386 case EndCap3a:
00387 case EndCap3c:
00388 case EndCap4a:
00389 case EndCap4c:
00390 case EndCap5a:
00391 case EndCap5c:
00392 case EndCap6a:
00393 case EndCap6c:
00394 for(int quadrant=0;quadrant<4;quadrant++) {
00395 for(int number=13;number<23;number++) {
00396 calculatePolygon(selection,quadrant,number);
00397 if(polygon!=null) {
00398 g2.setColor(java.awt.Color.white);
00399 g2.fill(polygon);
00400 g2.setColor(java.awt.Color.lightGray);
00401 g2.draw(polygon);
00402 }
00403 }
00404 }
00405 for(int quadrant=0;quadrant<4;quadrant++) {
00406 for(int number=0;number<33;number++) {
00407 if(number>=13 && number<23) continue;
00408 calculatePolygon(selection,quadrant,number);
00409 if(polygon!=null) {
00410 g2.setColor(java.awt.Color.white);
00411 g2.fill(polygon);
00412 g2.setColor(java.awt.Color.lightGray);
00413 g2.draw(polygon);
00414 }
00415 }
00416 }
00417 break;
00418 case EndCap9a:
00419 case EndCap9c:
00420 for(int quadrant=0;quadrant<4;quadrant++) {
00421 for(int number=0;number<13;number++) {
00422 calculatePolygon(selection,quadrant,number);
00423 if(polygon!=null) g2.draw(polygon);
00424 }
00425 }
00426 break;
00427 default:
00428
00429 }
00430
00431
00432 }
00433
00434 private void calculatePolygon(int view, int quadrant, int number) {
00435 double thetaOffset=0.;
00436 double theta=0.;
00437 int rindex=0;
00438 int segment=0;
00439 int modules=0;
00440 if(number<13) {
00441 thetaOffset = Math.PI/52.;
00442 theta = Math.PI/26.;
00443 rindex=4;
00444 segment=number;
00445 modules=13;
00446 }
00447 else if(number>=13 && number<23) {
00448 thetaOffset=0.;
00449 theta = Math.PI/20.;
00450 rindex=2;
00451 segment=number-13;
00452 modules=10;
00453 }
00454 else {
00455 thetaOffset=Math.PI/40.;
00456 theta = Math.PI/20.;
00457 rindex=0;
00458 segment=number-23;
00459 modules=10;
00460 }
00461 segment += quadrant*modules;
00462 xPoints[0] = midX+(int)(radius[rindex]*Math.sin(segment*theta+thetaOffset));
00463 xPoints[1] = midX+(int)(radius[rindex]*Math.sin((segment+1)*theta+thetaOffset));
00464 xPoints[2] = midX+(int)(radius[rindex+1]*Math.sin((segment+1)*theta+thetaOffset));
00465 xPoints[3] = midX+(int)(radius[rindex+1]*Math.sin(segment*theta+thetaOffset));
00466 yPoints[0] = midY-(int)(radius[rindex]*Math.cos(segment*theta+thetaOffset));
00467 yPoints[1] = midY-(int)(radius[rindex]*Math.cos((segment+1)*theta+thetaOffset));
00468 yPoints[2] = midY-(int)(radius[rindex+1]*Math.cos((segment+1)*theta+thetaOffset));
00469 yPoints[3] = midY-(int)(radius[rindex+1]*Math.cos(segment*theta+thetaOffset));
00470 polygon = new java.awt.Polygon(xPoints,yPoints,4);
00471
00472 }
00473
00474 public void updateModuleGroups(List cellList) {
00475 for(int i=0;i<cellList.size();i++) {
00476 ModuleCell thisCell = (ModuleCell)cellList.get(i);
00477 try {
00478 h.setModuleGroup(thisCell.getSerialNo(),(short)thisCell.getModuleGroup());
00479 }catch(Exception e){System.err.println("Exception setting modulegroup for module "+thisCell.getSerialNo()+" : "+e.toString());}
00480 }
00481 }
00482 private void writeLookUpTable() {
00483 String directory = Preferences.PreferencesInterface.getInstance().getPreference(Preferences.PreferencesInterface.WEB_LOOKUP_DIR);
00484 if(directory.equals(Preferences.PreferencesInterface.unDefinedString)) return;
00485
00486 Map lookupMap = getLookUpMap(directory);
00487
00488 for(int i=0;i<SCTViewNames.length;i++) {
00489 Set moduleList = getSortedVector(i);
00490 for (java.util.Iterator m = moduleList.iterator(); m.hasNext(); ) {
00491 String thisSN=(String)m.next();
00492 ModuleCell thisCell = getModuleCell(i,thisSN);
00493 String desc = SCTViewNames[i]+", Row "+thisCell.getRow()+", Position "+thisCell.getPosition();
00494 lookupMap.put(thisSN,desc);
00495 }
00496 }
00497 saveLookUpMap(directory,lookupMap);
00498 lookupMap=null;
00499
00500 }
00501 private Map getLookUpMap(String directory) {
00502 Map thisMap = new HashMap();
00503 java.util.regex.Pattern p = java.util.regex.Pattern.compile("(\\d{14})=(.*)");
00504 java.io.File lookupFile = new java.io.File(directory,"moduleMap.txt");
00505 try {
00506 if(lookupFile.exists()) {
00507 java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(lookupFile));
00508 String line;
00509 while((line=in.readLine())!=null) {
00510 java.util.regex.Matcher matcher = p.matcher(line);
00511 if(matcher.matches()) thisMap.put(line.substring(matcher.start(1),matcher.end(1)),line.substring(matcher.start(2),matcher.end(2)));
00512 }
00513 in.close();
00514 }
00515 }catch(Exception e){System.err.println("SctGUI::ConfigurationInterface - Exception reading in "+lookupFile.getName()+" - "+e.toString());}
00516 return thisMap;
00517 }
00518 private void saveLookUpMap(String directory,Map thisMap) {
00519 java.io.File lookupFile = new java.io.File(directory,"moduleMap.txt");
00520 int lineCount=0;
00521 try {
00522 java.io.BufferedWriter out = new java.io.BufferedWriter(new java.io.FileWriter(lookupFile));
00523 String line;
00524 for (Iterator i=thisMap.entrySet().iterator(); i.hasNext(); ) {
00525 Map.Entry e = (Map.Entry) i.next();
00526 out.write((String)e.getKey()+"="+(String)e.getValue());
00527 out.newLine();
00528 lineCount++;
00529 }
00530 out.close();
00531
00532 }catch(Exception e){System.err.println("SctGUI::ConfigurationInterface - Exception writing to "+lookupFile.getName()+" - "+e.toString());}
00533 }
00534
00535
00536
00537
00538
00539
00540
00541
00542 }