Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package JavaApplication12;
- import java.sql.SQLException;
- import java.text.DecimalFormat;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * A server program which accepts requests from clients to
- * capitalize strings. When clients connect, a new thread is
- * started to handle an interactive dialog in which the client
- * sends in a string and the server thread sends back the
- * capitalized version of the string.
- *
- * The program is runs in an infinite loop, so shutdown in platform
- * dependent. If you ran it from a console window with the "java"
- * interpreter, Ctrl+C generally will shut it down.
- */
- public class JavaApplication12 {
- Connection conn = null;
- /**
- * Application method to run the server runs in an infinite loop
- * listening on port 9898. When a connection is requested, it
- * spawns a new thread to do the servicing and immediately returns
- * to listening. The server keeps a unique client number for each
- * client that connects just to show interesting logging
- * messages. It is certainly not necessary to do this.
- */
- public static void main(String[] args) throws Exception {
- System.out.println("The capitalization server is running.");
- int clientNumber = 0;
- ServerSocket listener = new ServerSocket(3000);
- Class.forName("oracle.jdbc.OracleDriver");
- try {
- while (true) {
- new Capitalizer(listener.accept(), clientNumber++).start();
- }
- } finally {
- listener.close();
- }
- }
- /**
- * A private thread to handle capitalization requests on a particular
- * socket. The client terminates the dialogue by sending a single line
- * containing only a period.
- */
- private static class Capitalizer extends Thread {
- private Socket socket;
- private int clientNumber;
- private BufferedReader in;
- private BufferedWriter out;
- private PrintWriter pw;
- public DataOutputStream dout;
- public DataInputStream din;
- public Connection conn = null;
- //static DataOutputStream dout;
- //private DataOutputStream dout;
- public Capitalizer(Socket socket, int clientNumber) throws SQLException {
- this.socket = socket;
- this.clientNumber = clientNumber;
- conn = DriverManager.getConnection("jdbc:oracle:thin:@203.188.246.141:1521:orcl","vehicle","mononsoft");
- // TODO code application logic here
- System.out.println("Successfully Connected");
- log("New connection with client# " + clientNumber + " at " + socket);
- }
- /**
- * Services this thread's client by first sending the
- * client a welcome message then repeatedly reading strings
- * and sending back the capitalized version of the string.
- */
- public void run() {
- try {
- // Decorate the streams so we can send characters
- // and not just bytes. Ensure output is flushed
- // after every newline.
- // in = new BufferedReader(
- //new InputStreamReader(socket.getInputStream()));
- ////out = new BufferedWriter(
- //new OutputStreamWriter(socket.getOutputStream()));
- //dout = new DataOutputStream(socket.getOutputStream());
- //pw = new PrintWriter(socket.getOutputStream());
- din = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
- dout = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
- // Send a welcome message to the client.
- //out.println("Hello, you are client #" + clientNumber + ".");
- //out.println("Enter a line with only a period to quit\n");
- // Get messages from the client, line by line; return them
- // capitalized
- //out.write("**,imei:864180035993786,100");
- //String clientData= in.readUTF();
- while (true) {
- byte[] byteData = null;
- try {
- byteData = receiveData(din);
- } catch (Exception ex) {
- Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
- }
- String clientRequestMessage = new String(byteData).trim();
- String clientData = doProcess(clientRequestMessage);
- if(clientData==null){
- String [] check = clientRequestMessage.split(":");
- String latitude=null, longitude=null;
- //imei:864180035993786,sensor alarm,180808114518,,L,,,5265,,2a9c,,,,,0,0,0.00%,,;
- log("final Message : "+clientRequestMessage);
- String [] arr = clientRequestMessage.split(",");
- //int i=0;
- if( !"imei".equals(check[0])){
- System.out.println("Unauthorized value supplied by device");
- break;
- }
- else if("L".equals(arr[4])){
- latitude="0";
- longitude="0";
- }
- else{
- latitude=String.valueOf(degree_to_decimal(arr[7],arr[8]));
- longitude=String.valueOf(degree_to_decimal(arr[9],arr[10]));
- }
- //for(String a : arr)
- //System.out.println(a);
- String [] arr1 = arr[0].split(":");
- //To display Latitute and Longtitude
- System.out.println("Latitude : "+latitude);
- System.out.println("Longitude : "+longitude);
- //end to display Latitude and Longditude
- // concat date
- char[] datearr = arr[2].toCharArray();
- String date= datearr[5] + "/" + datearr[2] + datearr[3] + "/20"+datearr[0]+datearr[1];
- String datew= datearr[4] + date;
- //concat date end
- // for(int i=0;i<datearr.length;i++){
- // System.out.println(arr[2]+" "+datearr[i]+" "+datew);
- // }
- // find maximum value to insert into Column "ID"
- String Query1="select MAX(ID) as insert_id from GPS_INFO_HISTORY";
- PreparedStatement ps1 = conn.prepareStatement(Query1);
- ResultSet rs=ps1.executeQuery();
- int total=0;
- while (rs.next()) {
- total=rs.getInt("insert_id"); // store maximum value into total
- }
- // insert all value insert into GPS_INFO_HISTORY table
- String Query = "Insert into GPS_INFO_HISTORY(ID, SN_IMEI_ID, TRACKER_ID,R_DATETIME, L_DATETIME,LATITUDE,LONGITUDE,SPEED, ALTITUDE, AZIMUTH, HDOP, GPS_VALID, TRACKER_STATE, VOLTAGE1, VOLTAGE2, ALARM_ID, BASE_ID, SATELLITE_NUMBER, GSM_SIGNAL, JOURNEY, RUN_TIME, VOLTAGE3, VOLTAGE4, VOLTAGE5, VOLTAGE6, VOLTAGE7, VOLTAGE8, RFID, GEOFENCE_ALARM_ID, LOCATION, PROTOCOL_VER, PROTOCOL_DATA) values(?, ?, ?,?,?, ?, ?,?,?,?,?,?,?,?,?,?, ?,?,?, ?, ?,?,?,?,?,?,?,?,?,?,?,?)";
- PreparedStatement ps = conn.prepareStatement(Query);
- //------ (ID, SN_IMEI_ID, TRACKER_ID,R_DATETIME, L_DATETIME,LATITUDE,LONGITUDE,SPEED, ALTITUDE, AZIMUTH, HDOP, TRACKER_STATE, GPS_VALID, ALARM_ID)
- // set date format datew variable
- DateFormat formatter;
- java.util.Date dob;
- formatter = new SimpleDateFormat("dd/MM/yy");
- dob = formatter.parse(datew);
- //datew formation complete
- //Date date1=new SimpleDateFormat("dd/MM/yyyy").parse("02/15/2012");
- ps.setInt(1, total+1); //ID
- ps.setString(2, arr1[1]); //SN_IMEI_ID
- ps.setString(3, arr1[1]); //TRACKER_ID
- ps.setDate(4, new java.sql.Date(dob.getTime())); //R_DATETIME
- ps.setDate(5, new java.sql.Date(dob.getTime())); //L_DATETIME
- ps.setString(6, latitude); //LATITUDE
- ps.setString(7, longitude); //LONGITUDE
- if(arr[11]==null||"".equals(arr[11])){
- ps.setFloat(8, 0);
- }else{
- ps.setFloat(8, Float.valueOf(arr[11]));
- } //SPEED
- ps.setInt(9, 0); //ALTITUDE
- ps.setInt(10, 0); //AZIMUTH
- ps.setFloat(11, 0); //HDOP
- ps.setString(12, "1"); //GPS_VALID
- ps.setString(13, "1"); //TRACKER_STATE
- ps.setFloat(14, 0); //VOLTAGE1
- ps.setFloat(15, 0); //VOLTAGE2
- ps.setInt(16, 123); //ps.setInt(16, Integer.parseInt(arr[1])); //ALARM_ID
- ps.setString(17, null); //BASE_ID
- ps.setInt(18, 0); //SATELLITE_NUMBER
- ps.setInt(19, 0); //GSM_SIGNAL
- ps.setInt(20, 0); //JOURNEY
- ps.setInt(21, 0); //RUN_TIME
- ps.setFloat(22, 0); //VOLTAGE3
- ps.setFloat(23, 0); //VOLTAGE4
- ps.setFloat(24, 0); //VOLTAGE5
- ps.setFloat(25, 0); //VOLTAGE6
- ps.setFloat(26, 0); //VOLTAGE7
- ps.setFloat(27, 0); //VOLTAGE8
- ps.setString(28, null); //RFID
- ps.setInt(29, 0); //GEOFENCE_ALARM_ID
- ps.setString(30, null); //LOCATION
- ps.setInt(31, 1); //PROTOCOL_VER
- ps.setString(32, null); //PROTOCOL_DATA
- int c = ps.executeUpdate();
- if(c==1){
- System.out.println("successfully inserted");
- }
- break;
- }
- else{
- sendData(dout, clientData.getBytes());
- }
- }
- } catch (ParseException ex) {
- Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException e) {
- log("Error handling client# " + clientNumber + ": " + e);
- } finally {
- try {
- //din.flush();
- dout.flush();
- din.close();
- try {
- conn.close();
- } catch (SQLException ex) {
- Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
- }
- //out.flush();
- //out.close();
- //pw.flush();
- //pw.close();
- socket.close();
- } catch (IOException ex) {
- Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
- }
- log("Connection with client# " + clientNumber + " closed");
- }
- }
- /**
- * Logs a simple message. In this case we just write the
- * message to the server applications standard output.
- */
- private void log(String message) {
- System.out.println(message);
- }
- public static String doProcess(String message)
- {
- System.out.println("Processing the Client Request...");
- // if (message.equals("CLIENT_REQUEST:SEND_SYSTEM_TIME"))
- // {
- // Date date = new Date(System.currentTimeMillis());
- // return date.toString();
- // }
- // else
- // {
- // return "SERVER-ERROR:INVALID_REQUEST";
- // }
- if(message.length()==26){
- return "LOAD";
- }
- else if(message.length()==16){
- return "ON";
- }
- else{
- return null;
- }
- }
- /**
- * Method receives the Client Request
- */
- public static byte[] receiveData(DataInputStream din) throws Exception
- {
- try
- {
- byte[] inputData = new byte[1024];
- din.read(inputData);
- return inputData;
- }
- catch (Exception exception)
- {
- throw exception;
- }
- }
- /**
- * Method used to Send Response to Client
- */
- public static synchronized void sendData(DataOutputStream dout, byte[] byteData)
- {
- if (byteData == null)
- {
- return;
- }
- try
- {
- dout.write(byteData);
- dout.flush();
- }
- catch (Exception exception)
- {
- }
- }
- public float degree_to_decimal(String coordinates_in_degrees, String direction){
- DecimalFormat df = new DecimalFormat("#0.######");
- int degrees = (int)(Float.valueOf(coordinates_in_degrees) / 100);
- double minutes = Double.valueOf(coordinates_in_degrees) - Double.valueOf(degrees * 100);
- double seconds = minutes / 60.0;
- double coordinates_in_decimal = degrees + seconds;
- if ((direction .equals("S")) || (direction.equals("W"))) {
- coordinates_in_decimal = coordinates_in_decimal * (-1);
- }
- String ret = df.format(coordinates_in_decimal);
- return Float.valueOf(ret);
- //return (float) minutes;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement