source: tmcsimulator/trunk/src/tmcsim/cadsimulator/stillimagecontrol/ATMSCommunicator.java @ 44

Revision 44, 5.2 KB checked in by jdalbey, 10 years ago (diff)

MultiFile? commit: Add CADViewer Interface and change CADSimulator to Observer Pattern.

Line 
1package tmcsim.cadsimulator.stillimagecontrol;
2
3import java.rmi.RemoteException;
4import java.text.DateFormat;
5import java.util.Calendar;
6import java.util.StringTokenizer;
7
8/**
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 * @author Matthew Cechini
17 * @version
18 */
19public class ATMSCommunicator
20{
21
22    /**
23     * Connection user name.
24     */
25    protected String username;
26    /**
27     * Connection password.
28     */
29    protected String password;
30    /**
31     * ATMS Server Host name.
32     */
33    protected String viewerHost;
34    /**
35     * Absolute directory path for images.
36     */
37    protected String image_dir;
38    /**
39     * Base plink command string.
40     */
41    protected String plinkBaseCMD;
42    protected String remoteShellCmd;
43
44    /**
45     * Constructor.
46     */
47    public ATMSCommunicator(String host, String user, String pwd, String dir)
48    {
49        viewerHost = host;
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        }
62    }
63
64    /**
65     * Get the current ATMS server time as the number of seconds since Jan 1,
66     * 1970.
67     *
68     * @return Current time in seconds.
69     * @throws RemoteException If there is an exception in RMI communication.
70     */
71    public long getCurrentTime() throws Exception
72    {
73
74        Calendar currentCal = Calendar.getInstance();
75
76        Process timeProc = Runtime.getRuntime().exec(remoteShellCmd);
77        timeProc.waitFor();
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        {
86            String tempToken = null;
87            byte[] dateBytes = new byte[timeProc.getInputStream().available()];
88            timeProc.getInputStream().read(dateBytes);
89
90            StringTokenizer spaceTok = new StringTokenizer(new String(dateBytes), " ");
91            while (spaceTok.hasMoreTokens())
92            {
93                tempToken = spaceTok.nextToken();
94
95                if (tempToken.indexOf(":") != -1)
96                {
97                    StringTokenizer colonTok = new StringTokenizer(new String(tempToken), ":");
98
99                    currentCal.set(Calendar.HOUR, Integer.parseInt(colonTok.nextToken()));
100                    currentCal.set(Calendar.MINUTE, Integer.parseInt(colonTok.nextToken()));
101                    currentCal.set(Calendar.SECOND, Integer.parseInt(colonTok.nextToken()));
102
103                    System.out.println("Time retreieved from ATRMS server: "
104                            + DateFormat.getDateTimeInstance().format(currentCal.getTime()));
105
106                }
107            }
108        }
109
110        return currentCal.getTimeInMillis();
111
112    }
113
114    /**
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     *
120     * @param ATMS_cameraID ATMS indexed camera ID.
121     * @param fileName Filename to show.
122     * @throws RemoteException If there is an exception in RMI communication.
123     */
124    public void showImage(Integer ATMS_cameraID, String fileName) throws RemoteException
125    {
126
127        System.out.println("Showing CameraID " + ATMS_cameraID + ", with filename: " + fileName);
128        /*
129         Process imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\"");
130         imageProc.waitFor();
131
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    }
152}
Note: See TracBrowser for help on using the repository browser.