Changeset 44 in tmcsimulator for trunk/src/tmcsim/cadsimulator/stillimagecontrol/ATMSCommunicator.java
- Timestamp:
- 06/23/2016 06:30:20 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tmcsim/cadsimulator/stillimagecontrol/ATMSCommunicator.java
r2 r44 7 7 8 8 /** 9 * ATMSCommunicator handles communication between the CAD Simulator and the 10 * ATMS Server. The funcationality provided includes querying the current time11 * on the ATMS server and replacing images on the ATMS to model12 * traffic flow changes. The ATMSCommunicator uses the plink.exe external13 * application to establish an SSH communication with the ATMS server, which is14 * used to execute commands remotely.15 * 9 * ATMSCommunicator handles communication between the CAD Simulator and the ATMS 10 * Server. The funcationality provided includes querying the current time on the 11 * ATMS server and replacing images on the ATMS to model traffic flow changes. 12 * The ATMSCommunicator uses the plink.exe external application to establish an 13 * SSH communication with the ATMS server, which is used to execute commands 14 * remotely. 15 * 16 16 * @author Matthew Cechini 17 17 * @version 18 18 */ 19 public class ATMSCommunicator { 20 21 /** Connection user name. */ 19 public class ATMSCommunicator 20 { 21 22 /** 23 * Connection user name. 24 */ 22 25 protected String username; 23 24 /** Connection password. */ 26 /** 27 * Connection password. 28 */ 25 29 protected String password; 26 27 /** ATMS Server Host name. */ 30 /** 31 * ATMS Server Host name. 32 */ 28 33 protected String viewerHost; 29 30 /** Absolute directory path for images. */ 34 /** 35 * Absolute directory path for images. 36 */ 31 37 protected String image_dir; 32 33 /** Base plink command string. */ 38 /** 39 * Base plink command string. 40 */ 34 41 protected String plinkBaseCMD; 35 36 /** Constructor. */ 37 public ATMSCommunicator(String host, String user, String pwd, String dir) { 42 protected String remoteShellCmd; 43 44 /** 45 * Constructor. 46 */ 47 public ATMSCommunicator(String host, String user, String pwd, String dir) 48 { 38 49 viewerHost = host; 39 username = user; 40 password = pwd; 41 image_dir = dir; 42 43 plinkBaseCMD = "plink -l " + username + " -pw " + password + " " + viewerHost + " "; 50 username = user; 51 password = pwd; 52 image_dir = dir; 53 String osname = System.getProperty("os.name"); 54 if (osname.startsWith("Windows")) 55 { 56 remoteShellCmd = "plink -l " + username + " -pw " + password + " " + viewerHost + " \"date\""; 57 } 58 else 59 { 60 remoteShellCmd = "date"; //sshpass -p " + password + " " + "ssh -t -l " + username + viewerHost + " date &"; 61 } 44 62 } 45 63 46 64 /** 47 * Get the current ATMS server time as the number of seconds since Jan 1, 1970. 48 * 65 * Get the current ATMS server time as the number of seconds since Jan 1, 66 * 1970. 67 * 49 68 * @return Current time in seconds. 50 69 * @throws RemoteException If there is an exception in RMI communication. 51 */ 52 public long getCurrentTime() throws Exception { 53 70 */ 71 public long getCurrentTime() throws Exception 72 { 73 54 74 Calendar currentCal = Calendar.getInstance(); 55 75 56 Process timeProc = Runtime.getRuntime().exec( plinkBaseCMD + " \"date\"");76 Process timeProc = Runtime.getRuntime().exec(remoteShellCmd); 57 77 timeProc.waitFor(); 58 59 if(timeProc.exitValue() == 0) { 78 // System.out.println("timeProc exit = " + timeProc.exitValue()); 79 // byte[] procBytes = new byte[timeProc.getInputStream().available()]; 80 // timeProc.getInputStream().read(procBytes); 81 // String returnString = new String(procBytes); 82 // System.out.println("timeProc returned: " + returnString); 83 84 if (timeProc.exitValue() == 0) 85 { 60 86 String tempToken = null; 61 87 byte[] dateBytes = new byte[timeProc.getInputStream().available()]; 62 88 timeProc.getInputStream().read(dateBytes); 63 89 64 90 StringTokenizer spaceTok = new StringTokenizer(new String(dateBytes), " "); 65 while(spaceTok.hasMoreTokens()) { 91 while (spaceTok.hasMoreTokens()) 92 { 66 93 tempToken = spaceTok.nextToken(); 67 68 if(tempToken.indexOf(":") != -1) { 94 95 if (tempToken.indexOf(":") != -1) 96 { 69 97 StringTokenizer colonTok = new StringTokenizer(new String(tempToken), ":"); 70 98 71 99 currentCal.set(Calendar.HOUR, Integer.parseInt(colonTok.nextToken())); 72 100 currentCal.set(Calendar.MINUTE, Integer.parseInt(colonTok.nextToken())); 73 101 currentCal.set(Calendar.SECOND, Integer.parseInt(colonTok.nextToken())); 74 75 System.out.println("Time retreieved from ATRMS server: " +76 DateFormat.getDateTimeInstance().format(currentCal.getTime()));77 102 103 System.out.println("Time retreieved from ATRMS server: " 104 + DateFormat.getDateTimeInstance().format(currentCal.getTime())); 105 78 106 } 79 107 } 80 } 81 82 return currentCal.getTimeInMillis(); 108 } 109 110 return currentCal.getTimeInMillis(); 83 111 84 112 } 85 113 86 114 /** 87 * Show a new image for an ATMS camera. The ATMS camera files are named88 * <ATMS_Camera_ID>.xpm. If a camera file exists, delete it. Then copy89 * the parameter file name to <ATMS_Camera_ID>.xpm. If the camera ID or90 * new file does not exist, throw an exception.91 * 115 * Show a new image for an ATMS camera. The ATMS camera files are named 116 * <ATMS_Camera_ID>.xpm. If a camera file exists, delete it. Then copy the 117 * parameter file name to <ATMS_Camera_ID>.xpm. If the camera ID or new file 118 * does not exist, throw an exception. 119 * 92 120 * @param ATMS_cameraID ATMS indexed camera ID. 93 121 * @param fileName Filename to show. 94 122 * @throws RemoteException If there is an exception in RMI communication. 95 */ 96 public void showImage(Integer ATMS_cameraID, String fileName) throws RemoteException { 97 123 */ 124 public void showImage(Integer ATMS_cameraID, String fileName) throws RemoteException 125 { 126 98 127 System.out.println("Showing CameraID " + ATMS_cameraID + ", with filename: " + fileName); 99 128 /* 100 Process imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\""); 101 imageProc.waitFor(); 102 103 if(imageProc.exitValue() != 0) { 104 throw new Exception(image_dir + "/cctvImage" + ATMS_cameraID + ".xpm does not exist"); 105 } 106 107 imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/" + fileName + ".xpm\""); 108 imageProc.waitFor(); 129 Process imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\""); 130 imageProc.waitFor(); 109 131 110 if(imageProc.exitValue() != 0) { 111 throw new Exception(image_dir + "/" + fileName + ".xpm does not exist"); 112 } 113 114 imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"rm " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\""); 115 imageProc.waitFor(); 116 117 imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"cp " + image_dir + "/" + fileName + ".xpm" + 118 " " + image_dir + "/" + "cctvImage" + ATMS_cameraID + ".xpm\""); 119 imageProc.waitFor(); 120 */ 121 122 } 132 if(imageProc.exitValue() != 0) { 133 throw new Exception(image_dir + "/cctvImage" + ATMS_cameraID + ".xpm does not exist"); 134 } 135 136 imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/" + fileName + ".xpm\""); 137 imageProc.waitFor(); 138 139 if(imageProc.exitValue() != 0) { 140 throw new Exception(image_dir + "/" + fileName + ".xpm does not exist"); 141 } 142 143 imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"rm " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\""); 144 imageProc.waitFor(); 145 146 imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"cp " + image_dir + "/" + fileName + ".xpm" + 147 " " + image_dir + "/" + "cctvImage" + ATMS_cameraID + ".xpm\""); 148 imageProc.waitFor(); 149 */ 150 151 } 123 152 }
Note: See TracChangeset
for help on using the changeset viewer.
