| 1 | package tmcsim.cadsimulator.stillimagecontrol; |
|---|
| 2 | |
|---|
| 3 | import java.rmi.RemoteException; |
|---|
| 4 | import java.text.DateFormat; |
|---|
| 5 | import java.util.Calendar; |
|---|
| 6 | import java.util.StringTokenizer; |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * ATMSCommunicator handles communication between the CAD Simulator and the |
|---|
| 10 | * ATMS Server. The funcationality provided includes querying the current time |
|---|
| 11 | * on the ATMS server and replacing images on the ATMS to model |
|---|
| 12 | * traffic flow changes. The ATMSCommunicator uses the plink.exe external |
|---|
| 13 | * application to establish an SSH communication with the ATMS server, which is |
|---|
| 14 | * used to execute commands remotely. |
|---|
| 15 | * |
|---|
| 16 | * @author Matthew Cechini |
|---|
| 17 | * @version |
|---|
| 18 | */ |
|---|
| 19 | public class ATMSCommunicator { |
|---|
| 20 | |
|---|
| 21 | /** Connection user name. */ |
|---|
| 22 | protected String username; |
|---|
| 23 | |
|---|
| 24 | /** Connection password. */ |
|---|
| 25 | protected String password; |
|---|
| 26 | |
|---|
| 27 | /** ATMS Server Host name. */ |
|---|
| 28 | protected String viewerHost; |
|---|
| 29 | |
|---|
| 30 | /** Absolute directory path for images. */ |
|---|
| 31 | protected String image_dir; |
|---|
| 32 | |
|---|
| 33 | /** Base plink command string. */ |
|---|
| 34 | protected String plinkBaseCMD; |
|---|
| 35 | |
|---|
| 36 | /** Constructor. */ |
|---|
| 37 | public ATMSCommunicator(String host, String user, String pwd, String dir) { |
|---|
| 38 | viewerHost = host; |
|---|
| 39 | username = user; |
|---|
| 40 | password = pwd; |
|---|
| 41 | image_dir = dir; |
|---|
| 42 | |
|---|
| 43 | plinkBaseCMD = "plink -l " + username + " -pw " + password + " " + viewerHost + " "; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Get the current ATMS server time as the number of seconds since Jan 1, 1970. |
|---|
| 48 | * |
|---|
| 49 | * @return Current time in seconds. |
|---|
| 50 | * @throws RemoteException If there is an exception in RMI communication. |
|---|
| 51 | */ |
|---|
| 52 | public long getCurrentTime() throws Exception { |
|---|
| 53 | |
|---|
| 54 | Calendar currentCal = Calendar.getInstance(); |
|---|
| 55 | |
|---|
| 56 | Process timeProc = Runtime.getRuntime().exec(plinkBaseCMD + " \"date\""); |
|---|
| 57 | timeProc.waitFor(); |
|---|
| 58 | |
|---|
| 59 | if(timeProc.exitValue() == 0) { |
|---|
| 60 | String tempToken = null; |
|---|
| 61 | byte[] dateBytes = new byte[timeProc.getInputStream().available()]; |
|---|
| 62 | timeProc.getInputStream().read(dateBytes); |
|---|
| 63 | |
|---|
| 64 | StringTokenizer spaceTok = new StringTokenizer(new String(dateBytes), " "); |
|---|
| 65 | while(spaceTok.hasMoreTokens()) { |
|---|
| 66 | tempToken = spaceTok.nextToken(); |
|---|
| 67 | |
|---|
| 68 | if(tempToken.indexOf(":") != -1) { |
|---|
| 69 | StringTokenizer colonTok = new StringTokenizer(new String(tempToken), ":"); |
|---|
| 70 | |
|---|
| 71 | currentCal.set(Calendar.HOUR, Integer.parseInt(colonTok.nextToken())); |
|---|
| 72 | currentCal.set(Calendar.MINUTE, Integer.parseInt(colonTok.nextToken())); |
|---|
| 73 | 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 | |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | return currentCal.getTimeInMillis(); |
|---|
| 83 | |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Show a new image for an ATMS camera. The ATMS camera files are named |
|---|
| 88 | * <ATMS_Camera_ID>.xpm. If a camera file exists, delete it. Then copy |
|---|
| 89 | * the parameter file name to <ATMS_Camera_ID>.xpm. If the camera ID or |
|---|
| 90 | * new file does not exist, throw an exception. |
|---|
| 91 | * |
|---|
| 92 | * @param ATMS_cameraID ATMS indexed camera ID. |
|---|
| 93 | * @param fileName Filename to show. |
|---|
| 94 | * @throws RemoteException If there is an exception in RMI communication. |
|---|
| 95 | */ |
|---|
| 96 | public void showImage(Integer ATMS_cameraID, String fileName) throws RemoteException { |
|---|
| 97 | |
|---|
| 98 | System.out.println("Showing CameraID " + ATMS_cameraID + ", with filename: " + fileName); |
|---|
| 99 | /* |
|---|
| 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(); |
|---|
| 109 | |
|---|
| 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 | } |
|---|
| 123 | } |
|---|