import java.io.File; //Library for handling files import java.io.FileWriter; //Library for writing to files import java.io.IOException; //Library for handling exceptions import java.io.FileOutputStream; //Library for handling file output streams, in order to create images import java.util.List; //Library for Lists import java.util.ArrayList; //Library for ArrayLists public class userApp { //Request codes that change in each 2-hour session public static String echoRequestCode = "E4246"; public static String imageRequestCode = "M7435"; public static String imageRequestCodeError = "G8606"; public static String gpsRequestCode = "P2698"; public static String ackCode = "Q7959"; public static String nackCode = "R4816"; public static void main(String[] param) { (new userApp()).demo(); } //Initializes the modem public Modem initModem(int speed, int timeout){ Modem modem; modem=new Modem(); modem.setSpeed(speed); modem.setTimeout(timeout); modem.open("ithaki"); return modem; } //Receives what Ithaki sends without sending a request code. Prints the packet on the terminal if printRx is true. Packet comes as a string. public String listen(Modem modem, String delimiter, boolean printRx){ int k; String rxmessage =""; System.out.println("Sent request for message."); //Endless loop for listening for message for (;;) { try { //Read a byte from the modem k=modem.read(); if (k==-1){ System.out.println("Connection closed."); break; } //System.out.println((char)k); //System.out.print((char)k+" <"+k+"> "); rxmessage = rxmessage + (char)k; //System.out.println(rxmessage); //Break endless loop by catching break sequence. if (rxmessage.indexOf(delimiter)>-1){ if(printRx==true){System.out.println(rxmessage);} System.out.println("End of listen message."); break; } } catch (Exception x) { break; } } return rxmessage; } //Sends a request code and receives the answer. Prints the packet on the terminal if printRx is true. public String sendAndListen(Modem modem, String txmessage, String delimiter, boolean printRx){ int k; String rxmessage = ""; //We create a different variable for the message we will send to Ithaki. The reason for differentiating is because for some reason the terminal couldn't print the //txmessage if we sent it along with the txmessage. String txmessageToSend = txmessage + "\r"; modem.write(txmessageToSend.getBytes()); String toPrint = "Sent " + txmessage + " request code."; System.out.println(toPrint); rxmessage = listen(modem, delimiter, printRx); return rxmessage; } //Creates a file. public void createFile(String fileName){ try { File myObj = new File(fileName); if (myObj.createNewFile()) { System.out.println("File created: " + myObj.getName()); } else { System.out.println("File already exists."); } } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } //Writes a string to a file. public void writeToFile(String fileName, String toWrite){ try { //The true argument is needed in order to append to the file and not overwrite it. FileWriter myWriter = new FileWriter(fileName, true); myWriter.write(toWrite); myWriter.close(); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } //Appends a string to a file. public void appendToFile(String fileName, String toWrite){ try{ FileWriter myWriter = new FileWriter(fileName, true); myWriter.append(toWrite); myWriter.flush(); myWriter.close(); } catch(IOException e){ System.out.println("An error occured."); e.printStackTrace(); } } //Receives a lot of echo packets and calculates each packet's response time in ms. Writes the results in a txt and a csv file. public void makeEchoPacketsList(Modem modem, int packetsNum){ long time1; long timePassed; createFile("echopackets.txt"); writeToFile("echopackets.txt", "Echo packets received: \n\n"); createFile("echopackets.csv"); appendToFile("echopackets.csv", "Response time (ms)"); appendToFile("echopackets.csv", "\n"); for(int i=0; i intList = new ArrayList(); ArrayList byteList = new ArrayList(); int k; boolean foundEndDelimiter = false; int lastValue = 0; String jpegFileName = ""; //String txtFileName = ""; if(requestCode==imageRequestCode){ jpegFileName = "imageNoError.jpeg"; //txtFileName = "imageNoError.txt"; } else if(requestCode==imageRequestCodeError){ jpegFileName = "imageWithError.jpeg"; //txtFileName = "imageWithError.txt"; } else{ jpegFileName = "gpsImage.jpeg"; //txtFileName = "gpsImage.txt"; } String txmessageToSend = requestCode + "\r"; modem.write(txmessageToSend.getBytes()); System.out.println("Sent request for image."); String toPrint = "Sent " + requestCode + " request code."; System.out.println(toPrint); for(;;){ try{ k = modem.read(); if (k==-1){ System.out.println("Connection closed."); break; } intList.add(k); byteList.add((byte)k); if(lastValue==255 && k==217){foundEndDelimiter=true;} if(foundEndDelimiter){ System.out.println("End of listen message."); break; } else{ lastValue = k; } } catch(Exception x){ break; } } //Now intList stores the image in an ArrayList form. We will now write that into a txt file for checking (optional). // createFile(txtFileName); // writeToFile(txtFileName, "Int array of image:/n/n"); // for(int i=0; i getGPSlines(String rxmessage){ ArrayList result = new ArrayList(); //temp string will store each packet every time. String temp = ""; char c = ' '; int i = 0; while(temp != "STOP ITHAKI GPS TRACKING"){ c = rxmessage.charAt(i); while(c != '\n'){ temp += c; i++; c = rxmessage.charAt(i); } //When line seperator is found, empty temp in order to store the next packet. if(c == '\n'){ result.add(temp); temp = ""; i++; } if(i>=rxmessage.length()){ break; } } return result; } //Parses GPS packet (single line). Produces an int array containing time, latitude and longitude. public int[] parseGPSpacket(String gpsPacket){ String strResult = ""; int result[] = {0, 0, 0, 0, 0, 0, 0}; //index1 is where the time starts, index2 is where the latitude starts, index3 is where longitude starts. int index1 = 0; int index2 = 0; int index3 = 0; //numOfCommas will store the number of commas we've found so far. int numOfCommasFound = 0; int i = 0; char c = gpsPacket.charAt(i); while(c!= '\n'){ if(c == ','){ switch(numOfCommasFound){ //If we haven't found any commas yet, then this comma is the first and denotes the start of the time information. case 0: index1 = i+1; break; //If we've only found one comma, then this comma is the second and denotes the start of the latitude information. case 1: index2 = i+1; break; //If we've found two commas, then this comma is the third but nothing starts here, so we skip it. case 2: break; //If we've found three commas, then this comma is the fourth and denotes the start of the longitude information. //However there's always a 0 in the start, so we skip that too. case 3: index3 = i+2; break; } numOfCommasFound++; } i++; if(i>=gpsPacket.length()){ break; } c = gpsPacket.charAt(i); } //Now that we've found the indexes of the information we need, we fill the result array with that info. //This could be done more neatly. String temp = ""; temp += gpsPacket.charAt(index1); temp += gpsPacket.charAt(index1+1); result[0] = Integer.valueOf(temp); temp = ""; temp += gpsPacket.charAt(index1+2); temp += gpsPacket.charAt(index1+3); result[1] = Integer.valueOf(temp); temp = ""; temp += gpsPacket.charAt(index1+4); temp += gpsPacket.charAt(index1+5); result[2] = Integer.valueOf(temp); temp = ""; temp += gpsPacket.charAt(index2); temp += gpsPacket.charAt(index2+1); temp += gpsPacket.charAt(index2+2); temp += gpsPacket.charAt(index2+3); result[3] = Integer.valueOf(temp); temp = ""; temp += gpsPacket.charAt(index2+5); temp += gpsPacket.charAt(index2+6); temp += gpsPacket.charAt(index2+7); temp += gpsPacket.charAt(index2+8); result[4] = Integer.valueOf(temp); temp = ""; temp += gpsPacket.charAt(index3); temp += gpsPacket.charAt(index3+1); temp += gpsPacket.charAt(index3+2); temp += gpsPacket.charAt(index3+3); result[5] = Integer.valueOf(temp); temp = ""; temp += gpsPacket.charAt(index3+5); temp += gpsPacket.charAt(index3+6); temp += gpsPacket.charAt(index3+7); temp += gpsPacket.charAt(index3+8); result[6] = Integer.valueOf(temp); return result; } //Creates a T=... code out of a gpsPacket string. public String createTcode(String gpsPacket){ int[] intArr = parseGPSpacket(gpsPacket); String t1 = String.valueOf(intArr[3]); t1 += String.valueOf(Math.round(0.006*intArr[4])); String t2 = String.valueOf(intArr[5]); t2 += String.valueOf(Math.round(0.006*intArr[6])); return "T="+t2+t1; } //Creates a GPS image with traces we get. public void createGPSimage(Modem modem, String rCode, int numTraces){ String rxmessage = sendAndListen(modem, gpsRequestCode+rCode, "STOP ITHAKI GPS TRACKING\r\n", false); ArrayList strArray = getGPSlines(rxmessage); //Get the number of packets we want out of R code. String temp = ""; temp += rCode.charAt(7); temp += rCode.charAt(8); int numOfPackets = Integer.valueOf(temp); //Ask for at least 13 packets in order to be able to find at least 4 that are at least 4 seconds apart from each other. //This was found empirically. if(numOfPackets < 13){ System.out.println("Please request at least 13 GPS packets in order for them to have at least 4 seconds difference."); return; } //We pick some of the GPS packets. We try to split the numOfPackets we have in almost equal parts, in order for them to be as much away from each other (in seconds) as possible. ArrayList gpsPackets = new ArrayList(); gpsPackets.add(strArray.get(1)); //First GPS packet is always the first one (stored in line 1, since line 0 has start delimiter). for(int i=1; i'){ startIndex = i+2; break; } } for(int i=0; i<3; i++){ fcsString += arqPacket.charAt(startIndex + i); } int fcs; fcs = Integer.parseInt(fcsString); return fcs; } //Takes arqPacket string and finds the XOR result of its characters. public int xorResult(String arqPacket){ String xSeq = ""; int startIndex = 0; //Find the beginning of the X sequence. for (int i=0; i xArr = new ArrayList(); for (int i=0; i