Advertisement
aviksaha

avik_java_oracle

Aug 6th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.73 KB | None | 0 0
  1. //package JavaApplication12;
  2.  
  3. import java.sql.SQLException;
  4. import java.text.DecimalFormat;
  5. import java.io.BufferedInputStream;
  6. import java.io.BufferedOutputStream;
  7. import java.io.BufferedReader;
  8. import java.io.BufferedWriter;
  9. import java.io.DataInputStream;
  10. import java.io.DataOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStreamWriter;
  14. import java.io.PrintWriter;
  15. import java.net.ServerSocket;
  16. import java.net.Socket;
  17. import java.sql.Connection;
  18. import java.sql.DriverManager;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.text.DateFormat;
  22. import java.text.ParseException;
  23. import java.text.SimpleDateFormat;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26.  
  27. /**
  28. * A server program which accepts requests from clients to
  29. * capitalize strings. When clients connect, a new thread is
  30. * started to handle an interactive dialog in which the client
  31. * sends in a string and the server thread sends back the
  32. * capitalized version of the string.
  33. *
  34. * The program is runs in an infinite loop, so shutdown in platform
  35. * dependent. If you ran it from a console window with the "java"
  36. * interpreter, Ctrl+C generally will shut it down.
  37. */
  38. public class JavaApplication12 {
  39. Connection conn = null;
  40. /**
  41. * Application method to run the server runs in an infinite loop
  42. * listening on port 9898. When a connection is requested, it
  43. * spawns a new thread to do the servicing and immediately returns
  44. * to listening. The server keeps a unique client number for each
  45. * client that connects just to show interesting logging
  46. * messages. It is certainly not necessary to do this.
  47. */
  48. public static void main(String[] args) throws Exception {
  49. System.out.println("The capitalization server is running.");
  50. int clientNumber = 0;
  51. ServerSocket listener = new ServerSocket(3000);
  52.  
  53.  
  54.  
  55. Class.forName("oracle.jdbc.OracleDriver");
  56.  
  57. try {
  58. while (true) {
  59. new Capitalizer(listener.accept(), clientNumber++).start();
  60. }
  61. } finally {
  62. listener.close();
  63. }
  64. }
  65.  
  66. /**
  67. * A private thread to handle capitalization requests on a particular
  68. * socket. The client terminates the dialogue by sending a single line
  69. * containing only a period.
  70. */
  71. private static class Capitalizer extends Thread {
  72. private Socket socket;
  73. private int clientNumber;
  74. private BufferedReader in;
  75. private BufferedWriter out;
  76. private PrintWriter pw;
  77. public DataOutputStream dout;
  78. public DataInputStream din;
  79. public Connection conn = null;
  80. //static DataOutputStream dout;
  81. //private DataOutputStream dout;
  82.  
  83.  
  84. public Capitalizer(Socket socket, int clientNumber) throws SQLException {
  85. this.socket = socket;
  86. this.clientNumber = clientNumber;
  87. conn = DriverManager.getConnection("jdbc:oracle:thin:@203.188.246.141:1521:orcl","vehicle","mononsoft");
  88. // TODO code application logic here
  89. System.out.println("Successfully Connected");
  90.  
  91. log("New connection with client# " + clientNumber + " at " + socket);
  92. }
  93.  
  94. /**
  95. * Services this thread's client by first sending the
  96. * client a welcome message then repeatedly reading strings
  97. * and sending back the capitalized version of the string.
  98. */
  99. public void run() {
  100. try {
  101.  
  102. // Decorate the streams so we can send characters
  103. // and not just bytes. Ensure output is flushed
  104. // after every newline.
  105. // in = new BufferedReader(
  106. //new InputStreamReader(socket.getInputStream()));
  107. ////out = new BufferedWriter(
  108. //new OutputStreamWriter(socket.getOutputStream()));
  109.  
  110. //dout = new DataOutputStream(socket.getOutputStream());
  111.  
  112. //pw = new PrintWriter(socket.getOutputStream());
  113.  
  114. din = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
  115. dout = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
  116.  
  117. // Send a welcome message to the client.
  118. //out.println("Hello, you are client #" + clientNumber + ".");
  119. //out.println("Enter a line with only a period to quit\n");
  120.  
  121. // Get messages from the client, line by line; return them
  122. // capitalized
  123. //out.write("**,imei:864180035993786,100");
  124.  
  125.  
  126. //String clientData= in.readUTF();
  127.  
  128.  
  129.  
  130. while (true) {
  131.  
  132. byte[] byteData = null;
  133. try {
  134. byteData = receiveData(din);
  135. } catch (Exception ex) {
  136. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  137. }
  138. String clientRequestMessage = new String(byteData).trim();
  139. String clientData = doProcess(clientRequestMessage);
  140. if(clientData==null){
  141. String [] check = clientRequestMessage.split(":");
  142. String latitude=null, longitude=null;
  143. //imei:864180035993786,sensor alarm,180808114518,,L,,,5265,,2a9c,,,,,0,0,0.00%,,;
  144. log("final Message : "+clientRequestMessage);
  145. String [] arr = clientRequestMessage.split(",");
  146. //int i=0;
  147. if( !"imei".equals(check[0])){
  148.  
  149. System.out.println("Unauthorized value supplied by device");
  150. break;
  151. }
  152. else if("L".equals(arr[4])){
  153. latitude="0";
  154. longitude="0";
  155. }
  156. else{
  157. latitude=String.valueOf(degree_to_decimal(arr[7],arr[8]));
  158. longitude=String.valueOf(degree_to_decimal(arr[9],arr[10]));
  159. }
  160. //for(String a : arr)
  161. //System.out.println(a);
  162. String [] arr1 = arr[0].split(":");
  163. //To display Latitute and Longtitude
  164. System.out.println("Latitude : "+latitude);
  165. System.out.println("Longitude : "+longitude);
  166. //end to display Latitude and Longditude
  167.  
  168. // concat date
  169. char[] datearr = arr[2].toCharArray();
  170. String date= datearr[5] + "/" + datearr[2] + datearr[3] + "/20"+datearr[0]+datearr[1];
  171. String datew= datearr[4] + date;
  172. //concat date end
  173. // for(int i=0;i<datearr.length;i++){
  174. // System.out.println(arr[2]+" "+datearr[i]+" "+datew);
  175. // }
  176. // find maximum value to insert into Column "ID"
  177.  
  178. String Query1="select MAX(ID) as insert_id from GPS_INFO_HISTORY";
  179. PreparedStatement ps1 = conn.prepareStatement(Query1);
  180. ResultSet rs=ps1.executeQuery();
  181. int total=0;
  182. while (rs.next()) {
  183. total=rs.getInt("insert_id"); // store maximum value into total
  184. }
  185.  
  186. // insert all value insert into GPS_INFO_HISTORY table
  187. 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(?, ?, ?,?,?, ?, ?,?,?,?,?,?,?,?,?,?, ?,?,?, ?, ?,?,?,?,?,?,?,?,?,?,?,?)";
  188. PreparedStatement ps = conn.prepareStatement(Query);
  189. //------ (ID, SN_IMEI_ID, TRACKER_ID,R_DATETIME, L_DATETIME,LATITUDE,LONGITUDE,SPEED, ALTITUDE, AZIMUTH, HDOP, TRACKER_STATE, GPS_VALID, ALARM_ID)
  190.  
  191. // set date format datew variable
  192. DateFormat formatter;
  193. java.util.Date dob;
  194. formatter = new SimpleDateFormat("dd/MM/yy");
  195. dob = formatter.parse(datew);
  196. //datew formation complete
  197.  
  198. //Date date1=new SimpleDateFormat("dd/MM/yyyy").parse("02/15/2012");
  199. ps.setInt(1, total+1); //ID
  200. ps.setString(2, arr1[1]); //SN_IMEI_ID
  201. ps.setString(3, arr1[1]); //TRACKER_ID
  202. ps.setDate(4, new java.sql.Date(dob.getTime())); //R_DATETIME
  203. ps.setDate(5, new java.sql.Date(dob.getTime())); //L_DATETIME
  204. ps.setString(6, latitude); //LATITUDE
  205. ps.setString(7, longitude); //LONGITUDE
  206. if(arr[11]==null||"".equals(arr[11])){
  207. ps.setFloat(8, 0);
  208. }else{
  209. ps.setFloat(8, Float.valueOf(arr[11]));
  210. } //SPEED
  211. ps.setInt(9, 0); //ALTITUDE
  212. ps.setInt(10, 0); //AZIMUTH
  213. ps.setFloat(11, 0); //HDOP
  214. ps.setString(12, "1"); //GPS_VALID
  215. ps.setString(13, "1"); //TRACKER_STATE
  216. ps.setFloat(14, 0); //VOLTAGE1
  217. ps.setFloat(15, 0); //VOLTAGE2
  218. ps.setInt(16, 123); //ps.setInt(16, Integer.parseInt(arr[1])); //ALARM_ID
  219. ps.setString(17, null); //BASE_ID
  220. ps.setInt(18, 0); //SATELLITE_NUMBER
  221. ps.setInt(19, 0); //GSM_SIGNAL
  222. ps.setInt(20, 0); //JOURNEY
  223. ps.setInt(21, 0); //RUN_TIME
  224. ps.setFloat(22, 0); //VOLTAGE3
  225. ps.setFloat(23, 0); //VOLTAGE4
  226. ps.setFloat(24, 0); //VOLTAGE5
  227. ps.setFloat(25, 0); //VOLTAGE6
  228. ps.setFloat(26, 0); //VOLTAGE7
  229. ps.setFloat(27, 0); //VOLTAGE8
  230. ps.setString(28, null); //RFID
  231. ps.setInt(29, 0); //GEOFENCE_ALARM_ID
  232. ps.setString(30, null); //LOCATION
  233. ps.setInt(31, 1); //PROTOCOL_VER
  234. ps.setString(32, null); //PROTOCOL_DATA
  235. int c = ps.executeUpdate();
  236. if(c==1){
  237. System.out.println("successfully inserted");
  238. }
  239.  
  240.  
  241. break;
  242. }
  243. else{
  244. sendData(dout, clientData.getBytes());
  245. }
  246.  
  247. }
  248.  
  249. } catch (ParseException ex) {
  250. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  251. } catch (SQLException ex) {
  252. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  253. } catch (IOException e) {
  254. log("Error handling client# " + clientNumber + ": " + e);
  255. } finally {
  256. try {
  257. //din.flush();
  258. dout.flush();
  259. din.close();
  260. try {
  261. conn.close();
  262. } catch (SQLException ex) {
  263. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  264. }
  265. //out.flush();
  266. //out.close();
  267. //pw.flush();
  268. //pw.close();
  269. socket.close();
  270. } catch (IOException ex) {
  271. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  272. }
  273. log("Connection with client# " + clientNumber + " closed");
  274. }
  275. }
  276.  
  277. /**
  278. * Logs a simple message. In this case we just write the
  279. * message to the server applications standard output.
  280. */
  281. private void log(String message) {
  282. System.out.println(message);
  283. }
  284. public static String doProcess(String message)
  285. {
  286. System.out.println("Processing the Client Request...");
  287. // if (message.equals("CLIENT_REQUEST:SEND_SYSTEM_TIME"))
  288. // {
  289. // Date date = new Date(System.currentTimeMillis());
  290. // return date.toString();
  291. // }
  292. // else
  293. // {
  294. // return "SERVER-ERROR:INVALID_REQUEST";
  295. // }
  296. if(message.length()==26){
  297. return "LOAD";
  298. }
  299. else if(message.length()==16){
  300. return "ON";
  301. }
  302. else{
  303. return null;
  304. }
  305. }
  306.  
  307. /**
  308. * Method receives the Client Request
  309. */
  310. public static byte[] receiveData(DataInputStream din) throws Exception
  311. {
  312. try
  313. {
  314. byte[] inputData = new byte[1024];
  315. din.read(inputData);
  316. return inputData;
  317. }
  318. catch (Exception exception)
  319. {
  320. throw exception;
  321. }
  322. }
  323.  
  324. /**
  325. * Method used to Send Response to Client
  326. */
  327. public static synchronized void sendData(DataOutputStream dout, byte[] byteData)
  328. {
  329. if (byteData == null)
  330. {
  331. return;
  332. }
  333. try
  334. {
  335. dout.write(byteData);
  336. dout.flush();
  337. }
  338. catch (Exception exception)
  339. {
  340. }
  341. }
  342. public float degree_to_decimal(String coordinates_in_degrees, String direction){
  343. DecimalFormat df = new DecimalFormat("#0.######");
  344. int degrees = (int)(Float.valueOf(coordinates_in_degrees) / 100);
  345. double minutes = Double.valueOf(coordinates_in_degrees) - Double.valueOf(degrees * 100);
  346. double seconds = minutes / 60.0;
  347. double coordinates_in_decimal = degrees + seconds;
  348.  
  349. if ((direction .equals("S")) || (direction.equals("W"))) {
  350. coordinates_in_decimal = coordinates_in_decimal * (-1);
  351. }
  352. String ret = df.format(coordinates_in_decimal);
  353. return Float.valueOf(ret);
  354. //return (float) minutes;
  355. }
  356. }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement