Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.74 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.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.OutputStreamWriter;
  15. import java.io.PrintWriter;
  16. import java.net.HttpURLConnection;
  17. import java.net.MalformedURLException;
  18. import java.net.ServerSocket;
  19. import java.net.Socket;
  20. import java.net.URL;
  21. import java.sql.Connection;
  22. import java.sql.DriverManager;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.text.DateFormat;
  26. import java.text.ParseException;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Date;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31.  
  32. import org.json.simple.JSONArray;
  33. import org.json.simple.JSONObject;
  34. import org.json.simple.parser.JSONParser;
  35.  
  36. /**
  37. * A server program which accepts requests from clients to
  38. * capitalize strings. When clients connect, a new thread is
  39. * started to handle an interactive dialog in which the client
  40. * sends in a string and the server thread sends back the
  41. * capitalized version of the string.
  42. *
  43. * The program is runs in an infinite loop, so shutdown in platform
  44. * dependent. If you ran it from a console window with the "java"
  45. * interpreter, Ctrl+C generally will shut it down.
  46. */
  47. public class JavaApplication12 {
  48.  
  49. Connection conn = null;
  50.  
  51.  
  52. /**
  53. * Application method to run the server runs in an infinite loop
  54. * listening on port 9898. When a connection is requested, it
  55. * spawns a new thread to do the servicing and immediately returns
  56. * to listening. The server keeps a unique client number for each
  57. * client that connects just to show interesting logging
  58. * messages. It is certainly not necessary to do this.
  59. */
  60. public static void main(String[] args) throws Exception {
  61. System.out.println("The capitalization server is running.");
  62.  
  63. int clientNumber = 0;
  64. ServerSocket listener = new ServerSocket(3000);
  65.  
  66. Class.forName("oracle.jdbc.OracleDriver");
  67.  
  68.  
  69.  
  70.  
  71.  
  72. try {
  73. while (true) {
  74. new Capitalizer(listener.accept(), clientNumber++).start();
  75. }
  76. } finally {
  77. listener.close();
  78.  
  79.  
  80. }
  81. }
  82.  
  83. /**
  84. * A private thread to handle capitalization requests on a particular
  85. * socket. The client terminates the dialogue by sending a single line
  86. * containing only a period.
  87. */
  88. private static class Capitalizer extends Thread {
  89.  
  90.  
  91. private Socket socket;
  92. private int clientNumber;
  93. private BufferedReader in;
  94. private BufferedWriter out;
  95. private PrintWriter pw;
  96. public DataOutputStream dout;
  97. public DataInputStream din;
  98. public Connection conn = null;
  99. //static DataOutputStream dout;
  100. //private DataOutputStream dout;
  101.  
  102. //static DataOutputStream dout;
  103. //private DataOutputStream dout;
  104.  
  105.  
  106. public Capitalizer(Socket socket, int clientNumber) throws SQLException {
  107. this.socket = socket;
  108. this.clientNumber = clientNumber;
  109. conn = DriverManager.getConnection("jdbc:oracle:thin:@203.188.246.141:1521:orcl","vehicle","mononsoft");
  110.  
  111. // TODO code application logic here
  112. System.out.println("Successfully Connected");
  113.  
  114. log("New connection with client# " + clientNumber + " at " + socket);
  115. }
  116.  
  117. /**
  118. * Services this thread's client by first sending the
  119. * client a welcome message then repeatedly reading strings
  120. * and sending back the capitalized version of the string.
  121. */
  122. public void run() {
  123. try {
  124.  
  125. // Decorate the streams so we can send characters
  126. // and not just bytes. Ensure output is flushed
  127. // after every newline.
  128. // in = new BufferedReader(
  129. //new InputStreamReader(socket.getInputStream()));
  130. ////out = new BufferedWriter(
  131. //new OutputStreamWriter(socket.getOutputStream()));
  132.  
  133. //dout = new DataOutputStream(socket.getOutputStream());
  134.  
  135. //pw = new PrintWriter(socket.getOutputStream());
  136.  
  137. din = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
  138. dout = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
  139.  
  140. // Send a welcome message to the client.
  141. //out.println("Hello, you are client #" + clientNumber + ".");
  142. //out.println("Enter a line with only a period to quit\n");
  143.  
  144. // Get messages from the client, line by line; return them
  145. // capitalized
  146. //out.write("**,imei:864180035993786,100");
  147.  
  148.  
  149. //String clientData= in.readUTF();
  150.  
  151.  
  152.  
  153. while (true) {
  154.  
  155. byte[] byteData = null;
  156. String location=null;
  157. try {
  158. byteData = receiveData(din);
  159. } catch (Exception ex) {
  160. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  161. }
  162. String clientRequestMessage = new String(byteData).trim();
  163. String clientData = doProcess(clientRequestMessage);
  164. if(clientData==null){
  165. String [] check = clientRequestMessage.split(":");
  166.  
  167. log("final Message : "+clientRequestMessage+" String Length : "+clientRequestMessage.length());
  168. String [] arr = clientRequestMessage.split(",");
  169. //int i=0;
  170. if( !"imei".equals(check[0])||"L".equals(arr[4])){ //||"L".equals(arr[4])
  171.  
  172. System.out.println("Unauthorized value supplied by device");
  173. break;
  174. }
  175. //for(String a : arr)
  176. //System.out.println(a);
  177. String [] arr1 = arr[0].split(":");
  178.  
  179.  
  180.  
  181. //To display Latitute and Longtitude
  182. System.out.println("Latitude : "+degree_to_decimal(arr[7],arr[8]));
  183. System.out.println("Longitude : "+degree_to_decimal(arr[9],arr[10]));
  184. try {
  185. location = getAddressByGpsCoordinates(String.valueOf(degree_to_decimal(arr[9],arr[10])),String.valueOf(degree_to_decimal(arr[7],arr[8])));
  186. if ("".equals(location) || location==null){
  187. location="No location";
  188. }
  189. //end to display Latitude and Longditude
  190. } catch (MalformedURLException ex) {
  191. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  192. } catch (org.json.simple.parser.ParseException ex) {
  193. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  194. }
  195.  
  196. // concat date
  197. char[] datearr = arr[2].toCharArray();
  198. String date= datearr[5] + "/" + datearr[2] + datearr[3] + "/20"+datearr[0]+datearr[1]+" "+datearr[6]+datearr[7]+":"+ datearr[8]+datearr[9]+":"+datearr[10]+datearr[11];
  199. String datew= datearr[4] + date;
  200. System.out.println("datew : "+datew);
  201. //concat date end
  202. // for(int i=0;i<datearr.length;i++){
  203. // System.out.println(arr[2]+" "+datearr[i]+" "+datew);
  204. // }
  205. // find maximum value to insert into Column "ID"
  206. DateFormat formatter;
  207. java.util.Date dob;
  208. formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
  209. dob = formatter.parse(datew);
  210. System.out.println("String Date : "+dob);
  211.  
  212. String Query3="select GEO_FENCE_CORDS, INOUT, ID from (select GEO_FENCE_CORDS, INOUT, to_char(EXPIRE_DATE,'yyyy-mm-dd') as sdf, ID from GEOFENCE_SETUP where VEHICLE_IMEI_NUMBER ='"+arr1[1]+"' and STATUS= '1') where sdf>='"+new java.sql.Date(dob.getTime())+"' "; //and EXPIRE_DATE >= '"+dob.getTime()+"'
  213. System.out.println("date "+new java.sql.Date(dob.getTime()));
  214. System.out.println(Query3);
  215. PreparedStatement ps13 = conn.prepareStatement(Query3);
  216. ResultSet rs3=ps13.executeQuery();
  217. String cords=null;
  218. String inout=null;
  219. int geo_id;
  220. //int k=0;
  221. while (rs3.next()) {
  222. cords = rs3.getString("GEO_FENCE_CORDS");
  223. // store maximum value into total
  224. inout= rs3.getString("INOUT");
  225. geo_id=rs3.getInt("ID");
  226.  
  227. //geofencetesting check2= new geofencetesting();
  228. checkgeo(cords,location,arr1[1],String.valueOf(degree_to_decimal(arr[7],arr[8])),String.valueOf(degree_to_decimal(arr[9],arr[10])),inout,geo_id,datew);
  229.  
  230. }
  231. alarminsert(arr1[1],location,clientRequestMessage,datew);
  232. //System.out.println("special"+k);
  233.  
  234.  
  235.  
  236. //String[] cordsamount = cords.split(",");
  237.  
  238.  
  239. String Query1="select MAX(ID) as insert_id from GPS_INFO_HISTORY";
  240. PreparedStatement ps1 = conn.prepareStatement(Query1);
  241. ResultSet rs=ps1.executeQuery();
  242. int total=0;
  243. while (rs.next()) {
  244. total = rs.getInt("insert_id"); // store maximum value into total
  245. }
  246. if("1".equals(arr[14])){
  247. String speed="0";
  248. if(arr[11]==null||"".equals(arr[11])){
  249.  
  250. }else{
  251. speed= arr[11];
  252. }
  253.  
  254. // insert all value insert into GPS_INFO_HISTORY table
  255. String Query = "Insert into GPS_INFO_HISTORY(ID, SN_IMEI_ID, TRACKER_ID, L_DATETIME, R_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"
  256. + "('"+Integer.toString(total+1)+"', "
  257. + "'"+arr1[1]+"', "
  258. + "'"+arr1[1]+"', "
  259. + "TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS'), "
  260. + "TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS'), "
  261. + "'"+String.valueOf(degree_to_decimal(arr[7],arr[8]))+"', "
  262. + "'"+String.valueOf(degree_to_decimal(arr[9],arr[10]))+"', "
  263. + "'"+Float.valueOf(speed)+"', "
  264. + "'"+0+"', "
  265. + "'"+0+"', "
  266. + "'"+0+"', "
  267. + "'"+1+"', "
  268. + "'"+1+"', "
  269. + "'"+0+"', "
  270. + "'"+0+"', "
  271. + "'"+123+"', "
  272. + null+", "
  273. + "'"+0+"', "
  274. + "'"+0+"', "
  275. + "'"+0+"', "
  276. + "'"+0+"', "
  277. + "'"+0+"', "
  278. + "'"+0+"', "
  279. + "'"+0+"', "
  280. + "'"+0+"', "
  281. + "'"+0+"', "
  282. + "'"+0+"', "
  283. + null+", "
  284. + "'"+0+"', "
  285. + "'"+location+"', "
  286. + "'"+1+"', "
  287. + null+")";
  288. PreparedStatement ps = conn.prepareStatement(Query);
  289. //------ (ID, SN_IMEI_ID, TRACKER_ID,R_DATETIME, L_DATETIME,LATITUDE,LONGITUDE,SPEED, ALTITUDE, AZIMUTH, HDOP, TRACKER_STATE, GPS_VALID, ALARM_ID)
  290.  
  291. // set date format datew variable
  292.  
  293. //datew formation complete
  294.  
  295. //Date date1=new SimpleDateFormat("dd/MM/yyyy").parse("02/15/2012");
  296. // ps.setInt(1, total+1); //ID
  297. // ps.setString(2, arr1[1]); //SN_IMEI_ID
  298. // ps.setString(3, arr1[1]); //TRACKER_ID
  299. // ps.setDate(4, new java.sql.Date(dob.getTime())); //R_DATETIME
  300. // ps.setDate(5, new java.sql.Date(dob.getTime())); //L_DATETIME
  301. // ps.setString(6, String.valueOf(degree_to_decimal(arr[7],arr[8]))); //LATITUDE
  302. // ps.setString(7, String.valueOf(degree_to_decimal(arr[9],arr[10]))); //LONGITUDE
  303. // if(arr[11]==null||"".equals(arr[11])){
  304. // ps.setFloat(8, 0);
  305. // }else{
  306. // ps.setFloat(8, Float.valueOf(arr[11]));
  307. // } //SPEED
  308. // ps.setInt(9, 0); //ALTITUDE
  309. // ps.setInt(10, 0); //AZIMUTH
  310. // ps.setFloat(11, 0); //HDOP
  311. // ps.setString(12, "1"); //GPS_VALID
  312. // ps.setString(13, "1"); //TRACKER_STATE
  313. // ps.setFloat(14, 0); //VOLTAGE1
  314. // ps.setFloat(15, 0); //VOLTAGE2
  315. // ps.setInt(16, 123); //ps.setInt(16, Integer.parseInt(arr[1])); //ALARM_ID
  316. // ps.setString(17, null); //BASE_ID
  317. // ps.setInt(18, 0); //SATELLITE_NUMBER
  318. // ps.setInt(19, 0); //GSM_SIGNAL
  319. // ps.setInt(20, 0); //JOURNEY
  320. // ps.setInt(21, 0); //RUN_TIME
  321. // ps.setFloat(22, 0); //VOLTAGE3
  322. // ps.setFloat(23, 0); //VOLTAGE4
  323. // ps.setFloat(24, 0); //VOLTAGE5
  324. // ps.setFloat(25, 0); //VOLTAGE6
  325. // ps.setFloat(26, 0); //VOLTAGE7
  326. // ps.setFloat(27, 0); //VOLTAGE8
  327. // ps.setString(28, null); //RFID
  328. // ps.setInt(29, 0); //GEOFENCE_ALARM_ID
  329. // ps.setString(30, location); //LOCATION
  330. // ps.setInt(31, 1); //PROTOCOL_VER
  331. // ps.setString(32, null); //PROTOCOL_DATA
  332. int c = ps.executeUpdate();
  333. if(c==1){
  334. System.out.println("successfully inserted");
  335. // String queryi="update GPS_INFO_HISTORY SET L_DATETIME=TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS') , R_DATETIME=TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS') WHERE ID= (SELECT MAX(ID) FROM GPS_INFO_HISTORY where SN_IMEI_ID='"+arr1[1]+"')";
  336. // PreparedStatement ps1i = conn.prepareStatement(queryi);
  337. // String queryii="update GPS_INFO_HISTORY_CURRENT SET L_DATETIME=TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS') , R_DATETIME=TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS') where SN_IMEI_ID="+arr1[1]+"and L_DATETIME<=TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS') and R_DATETIME<=TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS')";
  338. // PreparedStatement ps1ii = conn.prepareStatement(queryii);
  339. // int d = ps1i.executeUpdate();
  340. // if(d==1){
  341. // System.out.println("successfully inserted");
  342. // }
  343. // ps1ii.executeUpdate();
  344. }
  345. // imei:865011030518469,tracker,181223162404,,F,102404.00,A,2344.75462,N,09023.81367,E,0.391,0;
  346. }
  347. String Query_insert = "Insert into GPS_INFO_HISTORY_ALL(IMEI, DATE_TIME, LOCATION, ENGINE_STATUS, SPEED) values('"+arr1[1]+"',TO_DATE('"+datew+"', 'DD/MM/YYYY HH24:MI:SS'),'"+location+"', '"+arr[14]+"','"+Float.valueOf(arr[11])+"')";
  348. PreparedStatement psss = conn.prepareStatement(Query_insert);
  349. psss.executeUpdate();
  350. break;
  351. }
  352. else{
  353. sendData(dout, clientData.getBytes());
  354. }
  355.  
  356. }
  357.  
  358. } catch (ParseException ex) {
  359. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  360. } catch (SQLException ex) {
  361. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  362. } catch (IOException e) {
  363. log("Error handling client# " + clientNumber + ": " + e);
  364. } finally {
  365. try {
  366. //din.flush();
  367. dout.flush();
  368. din.close();
  369. try {
  370. conn.close();
  371. } catch (SQLException ex) {
  372. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  373. }
  374. //out.flush();
  375. //out.close();
  376. //pw.flush();
  377. //pw.close();
  378. socket.close();
  379. } catch (IOException ex) {
  380. Logger.getLogger(JavaApplication12.class.getName()).log(Level.SEVERE, null, ex);
  381. }
  382. log("Connection with client# " + clientNumber + " closed");
  383. }
  384. }
  385.  
  386. /**
  387. * Logs a simple message. In this case we just write the
  388. * message to the server applications standard output.
  389. */
  390. private void log(String message) {
  391. System.out.println(message);
  392. }
  393.  
  394. public String doProcess(String message) throws SQLException
  395. {
  396. String return_val= null;
  397. System.out.println("Processing the Client Request..."+message);
  398. // if (message.equals("CLIENT_REQUEST:SEND_SYSTEM_TIME"))
  399. // {
  400. // Date date = new Date(System.currentTimeMillis());
  401. // return date.toString();
  402. // }
  403. // else
  404. // {
  405. // return "SERVER-ERROR:INVALID_REQUEST";
  406. // }
  407.  
  408. if(message.length()==26){
  409. return "LOAD";
  410. }
  411. else if(message.length()==16){
  412. return "ON";
  413. }
  414. else{
  415. String [] arr = message.split(",");
  416.  
  417.  
  418. if(arr.length>1){
  419. if("tracker".equals(arr[1])){
  420.  
  421. }
  422. else{
  423. String [] arr1 = arr[0].split(":");
  424. if(arr1.length==2){
  425. if("imei".equals(arr1[0])&& "1".equals(arr[14])){
  426. return_val=command_check(arr1[1]);
  427. }
  428. }
  429. }
  430. }
  431.  
  432. //imei:865011030518469,tracker,,,L,,,5265,,2a9b,,,;
  433. return return_val;
  434. }
  435. }
  436.  
  437. public String command_check(String imei) throws SQLException{
  438. String return_val= null;
  439. //String return_val= "**,imei:"+imei+",101,10s";
  440. String Query1="select count(*) as count_command from COMMANDS where EXECUTE_STATUS='0' and VEHICLE_IMEI='"+imei+"'";
  441. PreparedStatement psl1 = conn.prepareStatement(Query1);
  442. ResultSet rs1=psl1.executeQuery();
  443. int total=0;
  444. while (rs1.next()) {
  445. total = rs1.getInt("count_command"); // store maximum value into total
  446. }
  447. if(total>0){
  448.  
  449. String Query2="select * from COMMANDS where EXECUTE_STATUS='0' and VEHICLE_IMEI='"+imei+"' and ROWNUM=1";
  450. PreparedStatement ps12 = conn.prepareStatement(Query2);
  451. ResultSet rs2=ps12.executeQuery();
  452.  
  453. while (rs2.next()) {
  454. int command_type = rs2.getInt("COMMAND_ID");
  455. if(command_type==1){
  456. return_val="**,imei:"+imei+",109";
  457. }
  458. else if(command_type==2){
  459. return_val="**,imei:"+imei+",107,"+rs2.getInt("SPEED_LIMIT");
  460. }
  461.  
  462. String Query = "update COMMANDS SET EXECUTE_STATUS=? WHERE ID=?";
  463. PreparedStatement ps = conn.prepareStatement(Query);
  464. ps.setInt(2, rs2.getInt("ID")); //TRACKER_ID
  465. ps.setInt(1, 1);
  466. ps.executeUpdate();
  467.  
  468.  
  469. }
  470. }
  471. return return_val;
  472. }
  473. /**
  474. * Method receives the Client Request
  475. */
  476. public static byte[] receiveData(DataInputStream din) throws Exception
  477. {
  478. try
  479. {
  480. byte[] inputData = new byte[1024];
  481. din.read(inputData);
  482. return inputData;
  483. }
  484. catch (Exception exception)
  485. {
  486. throw exception;
  487. }
  488. }
  489. public static boolean isInteger(String s) {
  490. boolean a= true;
  491. try {
  492. Integer.parseInt(s);
  493. } catch(NumberFormatException e) {
  494. a= false;
  495. } catch(NullPointerException e) {
  496. a= false;
  497. }
  498. // only got here if we didn't return false
  499. return a;
  500. }
  501. public static String getAddressByGpsCoordinates(String lng, String lat)
  502. throws MalformedURLException, IOException, org.json.simple.parser.ParseException {
  503.  
  504. URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng="
  505. + lat + "," + lng + "&key=AIzaSyCXx3ubAosxwUQH4i4gMo6j89RaUbdISz0");
  506. //'https://maps.googleapis.com/maps/api/geocode/json?latlng=23.73929,90.375586&key=AIzaSyCXx3ubAosxwUQH4i4gMo6j89RaUbdISz0'
  507. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  508. String formattedAddress = "";
  509.  
  510. try {
  511. InputStream in = url.openStream();
  512.  
  513. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  514. String result, line = reader.readLine();
  515. result = line;
  516. System.out.println("hiii"+result);
  517. while ((line = reader.readLine()) != null) {
  518. result += line;
  519. }
  520.  
  521. JSONParser parser = new JSONParser();
  522. JSONObject rsp = (JSONObject) parser.parse(result);
  523.  
  524. if (rsp.containsKey("results")) {
  525. JSONArray matches = (JSONArray) rsp.get("results");
  526. JSONObject data = (JSONObject) matches.get(0); //TODO: check if idx=0 exists
  527. formattedAddress = (String) data.get("formatted_address");
  528. System.out.println("hi");
  529. }
  530. else{
  531. System.out.println("none");
  532. }
  533.  
  534. return "";
  535. } finally {
  536. urlConnection.disconnect();
  537. return formattedAddress;
  538. }
  539. }
  540.  
  541. /**
  542. * Method used to Send Response to Client
  543. */
  544. public static synchronized void sendData(DataOutputStream dout, byte[] byteData)
  545. {
  546. if (byteData == null)
  547. {
  548. return;
  549. }
  550. try
  551. {
  552. dout.write(byteData);
  553. dout.flush();
  554. }
  555. catch (Exception exception)
  556. {
  557. }
  558. }
  559. public float degree_to_decimal(String coordinates_in_degrees, String direction){
  560. DecimalFormat df = new DecimalFormat("#0.######");
  561. int degrees = (int)(Float.valueOf(coordinates_in_degrees) / 100);
  562. double minutes = Double.valueOf(coordinates_in_degrees) - Double.valueOf(degrees * 100);
  563. double seconds = minutes / 60.0;
  564. double coordinates_in_decimal = degrees + seconds;
  565.  
  566. if ((direction .equals("S")) || (direction.equals("W"))) {
  567. coordinates_in_decimal = coordinates_in_decimal * (-1);
  568. }
  569. String ret = df.format(coordinates_in_decimal);
  570. return Float.valueOf(ret);
  571. //return (float) minutes;
  572. }
  573.  
  574. public void alarminsert(String imei, String location, String s, String date_exact) throws SQLException{
  575. String alarm_id=null;
  576. String [] arr = s.split(",");
  577. if("help me".equals(arr[1])){
  578. alarm_id="006";
  579. }
  580. else if("low battery".equals(arr[1])){
  581. alarm_id="007";
  582. }
  583. else if("ac alarm".equals(arr[1])){
  584. alarm_id="009";
  585. }
  586. else if("speed".equals(arr[1])){
  587. alarm_id="003";
  588. }
  589. else if("acc on".equals(arr[1])||"1".equals(arr[14])){
  590.  
  591.  
  592. String Query_OF="select count(*) as count_extra from GPS_INFO_HISTORY_CURRENT WHERE SN_IMEI_ID = '"+imei+"' and L_DATETIME<=TO_DATE('"+date_exact+"','DD-MM-YYYY HH24:MI:SS')";
  593. PreparedStatement ps_OF = conn.prepareStatement(Query_OF);
  594. ResultSet rs_OF=ps_OF.executeQuery();
  595. int total_OF=0;
  596. while (rs_OF.next()) {
  597. total_OF = rs_OF.getInt("count_extra"); // store maximum value into total
  598. }
  599. if(total_OF>0){
  600. alarm_id="001";
  601. String Query1="select count(*) as insert_id from ONOFF WHERE VEHICLE_IMEI = '"+imei+"'";
  602. PreparedStatement ps1 = conn.prepareStatement(Query1);
  603. ResultSet rs=ps1.executeQuery();
  604. int total=0;
  605. while (rs.next()) {
  606. total = rs.getInt("insert_id"); // store maximum value into total
  607. }
  608. if(total>0){
  609. String Query = "update ONOFF SET ONOFF=? WHERE VEHICLE_IMEI=? ";
  610. PreparedStatement ps = conn.prepareStatement(Query);
  611. //SN_IMEI_ID
  612. ps.setString(2, imei); //TRACKER_ID
  613. ps.setInt(1, 1);
  614. ps.executeUpdate();
  615.  
  616.  
  617. }
  618.  
  619. String Queryyy = "SELECT ALARM_ID from ALARMS WHERE EXACT_TIME=TO_DATE((select MAX(TO_CHAR(EXACT_TIME,'DD-MM-YYYY HH24:MI:SS')) TIMEA from ALARMS WHERE (ALARM_ID = '001' OR ALARM_ID = '002') AND VEHICLE_IMEI='"+imei+"'),'DD-MM-YYYY HH24:MI:SS') and VEHICLE_IMEI='"+imei+"' AND (ALARM_ID = '001' OR ALARM_ID = '002')";
  620. PreparedStatement psss = conn.prepareStatement(Queryyy);
  621. ResultSet rsss=psss.executeQuery();
  622. String alarmid=null;
  623. while (rsss.next()) {
  624. alarmid = rsss.getString("ALARM_ID");
  625. if("002".equals(alarmid)){
  626. alarm_id="001";
  627. }
  628. else{
  629. alarm_id=null;
  630. }
  631. }
  632. }
  633.  
  634. }
  635. else if("acc off".equals(arr[1])||"0".equals(arr[14])){
  636.  
  637. String Query_OF="select count(*) as count_extra from GPS_INFO_HISTORY_CURRENT WHERE SN_IMEI_ID = '"+imei+"' and L_DATETIME<=TO_DATE('"+date_exact+"','DD-MM-YYYY HH24:MI:SS')";
  638. PreparedStatement ps_OF = conn.prepareStatement(Query_OF);
  639. ResultSet rs_OF=ps_OF.executeQuery();
  640. int total_OF=0;
  641. while (rs_OF.next()) {
  642. total_OF = rs_OF.getInt("count_extra"); // store maximum value into total
  643. }
  644. if(total_OF>0){
  645. alarm_id="002";
  646. String Query1="select count(*) as insert_id from ONOFF WHERE VEHICLE_IMEI = '"+imei+"'";
  647. PreparedStatement ps1 = conn.prepareStatement(Query1);
  648. ResultSet rs=ps1.executeQuery();
  649. int total=0;
  650. while (rs.next()) {
  651. total = rs.getInt("insert_id"); // store maximum value into total
  652. }
  653. if(total>0){
  654. String Query = "update ONOFF SET ONOFF=? WHERE VEHICLE_IMEI=? ";
  655. PreparedStatement ps = conn.prepareStatement(Query);
  656. ps.setString(2, imei); //TRACKER_ID
  657. ps.setInt(1, 0);
  658. ps.executeUpdate();
  659.  
  660.  
  661. }
  662. else{
  663. String Query = "insert into ONOFF (ONOFF,VEHICLE_IMEI) values (?,?)";
  664. PreparedStatement ps = conn.prepareStatement(Query);
  665. //SN_IMEI_ID
  666. ps.setString(2, imei); //TRACKER_ID
  667. ps.setInt(1, 0);
  668. ps.executeUpdate();
  669.  
  670. }
  671.  
  672. String Queryyy = "SELECT ALARM_ID from ALARMS WHERE EXACT_TIME=TO_DATE((select MAX(TO_CHAR(EXACT_TIME,'DD-MM-YYYY HH24:MI:SS')) TIMEA from ALARMS WHERE (ALARM_ID = '001' OR ALARM_ID = '002') AND VEHICLE_IMEI='"+imei+"'),'DD-MM-YYYY HH24:MI:SS') and VEHICLE_IMEI='"+imei+"' AND (ALARM_ID = '001' OR ALARM_ID = '002')";
  673. PreparedStatement psss = conn.prepareStatement(Queryyy);
  674. ResultSet rsss=psss.executeQuery();
  675. String alarmid=null;
  676. int a=0;
  677. while (rsss.next()) {
  678. alarmid = rsss.getString("ALARM_ID");
  679. if("001".equals(alarmid)){
  680. alarm_id="002";
  681. }
  682. else{
  683. alarm_id=null;
  684. }
  685.  
  686. }
  687. }
  688.  
  689.  
  690.  
  691. }
  692. else if("door alarm".equals(arr[1])){
  693. alarm_id="011";
  694. }
  695. else if("accident alarm".equals(arr[1])){
  696. alarm_id="013";
  697. }
  698.  
  699. if(alarm_id!=null){
  700. String Query = "Insert into ALARMS(ALARM_ID, VEHICLE_IMEI, LOCATION, EXACT_TIME, SEEN_STATUS) values('"+alarm_id+"', '"+imei+"', '"+location+"', TO_DATE('"+date_exact+"', 'DD/MM/YYYY HH24:MI:SS'), '0')";
  701.  
  702. PreparedStatement ps = conn.prepareStatement(Query);
  703.  
  704.  
  705. // //System.out.println(dateFormat.format(date));
  706. // ps.setString(1, alarm_id); //ID
  707. // ps.setString(2, imei); //SN_IMEI_ID
  708. // ps.setString(3, location);
  709. // ps.setString(4, location);
  710. // //TRACKER_ID
  711. // //ps.setDate(4, new java.sql.Date("2018-08-06") ); //R_DATETIME
  712. // ps.setInt(5, 0);
  713.  
  714. int c = ps.executeUpdate();
  715. if(c==1){
  716. System.out.println("successfully alarm inserted");
  717. }
  718. }
  719.  
  720.  
  721. }
  722.  
  723.  
  724.  
  725.  
  726. public void checkgeo(String strArray, String location, String imei, String lat, String lang, String inout,int geo_id, String date_exact ) throws SQLException {
  727.  
  728. String[]strArraynew = strArray.split(",");
  729. System.out.println(strArray +" "+ strArraynew.length );
  730. int side= strArraynew.length;
  731. double x[]= new double [side];
  732. double y[]= new double[side];
  733. double lengthtopoint[]=new double [side];
  734. double sidelength[]=new double [side];
  735. double angles[]=new double [side];
  736. double xcheck=Double.parseDouble(lat);
  737. double ycheck=Double.parseDouble(lang);
  738.  
  739. int m=0;
  740. for(int i=0;i<side;i=i+2)
  741. {
  742. //System.out.println("Enter x for vertex "+i);
  743. x[m]=Double.parseDouble(strArraynew[i]);
  744. System.out.println("Enter x "+m+" for vertex "+x[m]);
  745. y[m]=Double.parseDouble(strArraynew[i+1]);
  746. System.out.println("Enter y "+m+" for vertex "+y[m]);
  747. lengthtopoint[m]=Math.sqrt(((x[m]-xcheck)*(x[m]-xcheck))+((y[m]-ycheck)*(y[m]-ycheck)));
  748. System.out.println("length of line joining given check point and vertex "+m+" is " +lengthtopoint[m]);
  749. m++;
  750. }
  751. side=side/2;
  752. for(int k=0;k<side-1;k++)
  753. {
  754. sidelength[k]=Math.sqrt(((x[k+1]-x[k])*(x[k+1]-x[k]))+((y[k+1]-y[k])*(y[k+1]-y[k])));
  755. //System.out.println("length of side "+k +(k+1)+ " is " +sidelength[k]);
  756. }
  757. sidelength[side-1]=Math.sqrt(((x[0]-x[side-1])*(x[0]-x[side-1]))+((y[0]-y[side-1])*(y[0]-y[side-1])));
  758. //System.out.println("length of side "+(side-1)+"0 is " +sidelength[side-1]);
  759.  
  760. /*for(int w=0;w<side-1;w++)
  761. {
  762. System.out.println("length is " +sidelength[w]);
  763. }*/
  764. for(int l=0;l<side-1;l++)
  765. {
  766. angles[l] =((180/(Math.PI)))*Math.acos(((lengthtopoint[l]*lengthtopoint[l])+(lengthtopoint[l+1]*lengthtopoint[l+1])-(sidelength[l]*sidelength[l]))/(2*lengthtopoint[l]*lengthtopoint[l+1]));
  767. //System.out.println("Angle= " +angles[l]);
  768. }
  769.  
  770. angles[side-1] =((180/(Math.PI)))*Math.acos(((lengthtopoint[side-1]*lengthtopoint[side-1])+(lengthtopoint[0]*lengthtopoint[0])-(sidelength[side-1]*sidelength[side-1]))/(2*lengthtopoint[side-1]*lengthtopoint[0]));
  771. //System.out.println("Angle= " +angles[side-1]);
  772. int returnnum;
  773. double sum=0;
  774. for(int z=0;z<side;z++)
  775. {
  776. sum=sum+angles[z];
  777. }
  778. System.out.println("sum "+sum);
  779. if (sum==360)
  780. {
  781.  
  782. returnnum=0;
  783. }
  784. else if(sum<360)
  785.  
  786. {
  787.  
  788. returnnum=1;
  789. }
  790. else{
  791. returnnum=2;
  792.  
  793. }
  794. if("OUT".equals(inout) && returnnum==1){
  795. System.out.println(" The point"+(xcheck)+","+(ycheck)+"lies outside the polygon");
  796. String Query="select count(*) as countval from ALARMS where ALARM_ID='004' AND VEHICLE_IMEI='"+imei+"' AND SEEN_STATUS='0' AND GEOFENCE_ID = '"+geo_id+"' ";
  797. PreparedStatement ps1 = conn.prepareStatement(Query);
  798. ResultSet rs=ps1.executeQuery();
  799. int total=0;
  800. while (rs.next()) {
  801. total = rs.getInt("countval"); // store maximum value into total
  802. }
  803.  
  804.  
  805. if(total==0){
  806. Query = "Insert into ALARMS(ALARM_ID, VEHICLE_IMEI, LOCATION, SEEN_STATUS, GEOFENCE_ID, EXACT_TIME) values('004', '"+imei+"', '"+location+"', 0, '"+String.valueOf(geo_id)+"', TO_DATE('"+date_exact+"', 'DD/MM/YYYY HH24:MI:SS'))";
  807.  
  808. PreparedStatement ps = conn.prepareStatement(Query);
  809.  
  810.  
  811. // //System.out.println(dateFormat.format(date));
  812. // ps.setString(1, "004"); //ID
  813. // ps.setString(2, imei); //SN_IMEI_ID
  814. // ps.setString(3, location); //TRACKER_ID
  815. // //ps.setDate(4, new java.sql.Date("2018-08-06") ); //R_DATETIME
  816. // ps.setInt(4, 0);
  817. // ps.setString(5, String.valueOf(geo_id));
  818.  
  819. int c = ps.executeUpdate();
  820. if(c==1){
  821. System.out.println("successfully alarm inserted");
  822. }
  823. }
  824.  
  825. }
  826. else if("IN".equals(inout)&& returnnum==0){
  827.  
  828. System.out.println(" The point"+(xcheck)+","+(ycheck) +"lies inside polygon ");
  829.  
  830. String Query="select count(*) as countval from ALARMS where ALARM_ID='005' AND VEHICLE_IMEI='"+imei+"' AND SEEN_STATUS='0' AND GEOFENCE_ID = '"+geo_id+"' ";
  831. PreparedStatement ps1 = conn.prepareStatement(Query);
  832. ResultSet rs=ps1.executeQuery();
  833. int total=0;
  834. while (rs.next()) {
  835. total = rs.getInt("countval"); // store maximum value into total
  836. }
  837.  
  838.  
  839. if(total==0){
  840. Query = "Insert into ALARMS(ALARM_ID, VEHICLE_IMEI, LOCATION, SEEN_STATUS, GEOFENCE_ID, EXACT_TIME) values('005', '"+imei+"', '"+location+"', 0, '"+String.valueOf(geo_id)+"', TO_DATE('"+date_exact+"', 'DD/MM/YYYY HH24:MI:SS'))";
  841.  
  842. PreparedStatement ps = conn.prepareStatement(Query);
  843.  
  844.  
  845. // //System.out.println(dateFormat.format(date));
  846. // ps.setString(1, "005"); //ID
  847. // ps.setString(2, imei); //SN_IMEI_ID
  848. // ps.setString(3, location); //TRACKER_ID
  849. // //ps.setDate(4, new java.sql.Date("2018-08-06") ); //R_DATETIME
  850. // ps.setInt(4, 0);
  851. // ps.setString(5, String.valueOf(geo_id));
  852.  
  853. int c = ps.executeUpdate();
  854. if(c==1){
  855. System.out.println("successfully alarm inserted");
  856. }
  857. }
  858. }
  859. //return returnnum;
  860.  
  861. }
  862. //end function;
  863.  
  864.  
  865. }
  866. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement