Commit 8a87583ed2e6b285b80bb494f9e3fa62669866f0

Authored by thomas godart
1 parent 05fe0560

Minor changes in jsp files

.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23.xml
... ... @@ -9,7 +9,7 @@
9 9 </database>
10 10 <schema id="3" parent="2" name="public">
11 11 <ObjectId>2200</ObjectId>
12   - <IntrospectionTimestamp>2017-06-07.19:30:21.978</IntrospectionTimestamp>
  12 + <IntrospectionTimestamp>2017-06-08.14:53:07.452</IntrospectionTimestamp>
13 13 <IntrospectionStateNumber>17257</IntrospectionStateNumber>
14 14 <Current>1</Current>
15 15 <Visible>1</Visible>
... ...
.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata
No preview for this file type
.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata.values.at
No preview for this file type
src/main/java/com/lo53_mobile_localization/core/servlet/CalibrationServlet.java
1 1 package com.lo53_mobile_localization.core.servlet;
2 2  
  3 +import com.lo53_mobile_localization.core.entity.AccessPoint;
3 4 import com.lo53_mobile_localization.core.entity.Location;
  5 +import com.lo53_mobile_localization.core.entity.Rssi;
  6 +import com.lo53_mobile_localization.core.service.AccessPointService;
4 7 import com.lo53_mobile_localization.core.service.LocationService;
  8 +import com.lo53_mobile_localization.core.service.RSSIService;
5 9 import com.lo53_mobile_localization.core.servlet.util.Param;
6 10  
7 11 import javax.servlet.ServletException;
... ... @@ -15,6 +19,7 @@ import java.io.IOException;
15 19 import java.net.DatagramPacket;
16 20 import java.net.DatagramSocket;
17 21 import java.net.InetAddress;
  22 +import java.net.SocketException;
18 23 import java.nio.charset.StandardCharsets;
19 24  
20 25 /**
... ... @@ -65,7 +70,7 @@ public class CalibrationServlet extends HttpServlet {
65 70 InetAddress address = InetAddress.getByAddress(Param.AP_IPS[i]);
66 71 socket = new DatagramSocket(Param.AP_COMM_PORT);
67 72  
68   - String msg = buildRssiRequestMessage(Param.AP_IPS[i], Param.AP_COMM_PORT);
  73 + String msg = buildRssiRequestMessage(Param.AP_IPS[i], Param.AP_COMM_PORT, macAddr);
69 74  
70 75 DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.length(), address, Param.AP_COMM_PORT);
71 76 socket.send(packet);
... ... @@ -78,12 +83,68 @@ public class CalibrationServlet extends HttpServlet {
78 83 if (socket != null) socket.close();
79 84 }
80 85 }else{
81   - System.out.println("MAC Address not found !");
  86 + System.out.println("Unknown mac address");
82 87 }
83 88 }else{
84   - System.out.println("Coordinates not found");
  89 + System.out.println("Wrong coordinates");
85 90 }
86   - request.getRequestDispatcher(Param.PATH_CALIBRATION).forward(request, response);
  91 +
  92 + long startTime = System.currentTimeMillis(); //fetch starting time
  93 + DatagramSocket socket = null;
  94 + try {
  95 + socket = new DatagramSocket(Param.DATAG_LISTENING_PORT);
  96 + while (false || (System.currentTimeMillis() - startTime) < 500) {
  97 +
  98 + byte[] receiveData = new byte[1024];
  99 + DatagramPacket packet = new DatagramPacket(receiveData, receiveData.length);
  100 + socket.receive(packet);
  101 + String receivedData = new String(packet.getData());
  102 +
  103 + //msg = "ap:ap:ap:ap:ap:ap" , "xx:xx:xx:xx:xx:xx" , "val" , "nbSamples"
  104 + String[] data = receivedData.split(",");
  105 +
  106 + String apMacAddress = data[0];
  107 + String deviceMac = data[1];
  108 + double value = Double.parseDouble(data[2]);
  109 + int nbSamples = Integer.parseInt(data[3]);
  110 +
  111 + if(nbSamples > 4){
  112 +
  113 + LocationService locationService = new LocationService();
  114 + AccessPointService apService = new AccessPointService();
  115 +
  116 + Location location = new Location();
  117 + location.setX(x);
  118 + location.setY(y);
  119 +
  120 + locationService.storeEntity(location);
  121 +
  122 + AccessPoint ap = apService.getSingleAccessPointFromMacAdress(apMacAddress);
  123 + if(ap == null){
  124 + ap = new AccessPoint();
  125 + ap.setMac_address(apMacAddress);
  126 + apService.storeEntity(ap);
  127 + }
  128 +
  129 + Rssi rssi = new Rssi();
  130 + rssi.setLocation(location);
  131 + rssi.setAccessPoint(ap);
  132 + rssi.setOccurences(nbSamples);
  133 + rssi.setValue(value);
  134 +
  135 + RSSIService rssiService = new RSSIService();
  136 +
  137 + rssiService.storeEntity(rssi);
  138 + }
  139 + }
  140 + } catch (SocketException e) {
  141 + System.out.println("[Calibration] SocketException");
  142 + } catch (IOException e) {
  143 + System.out.println("[Calibration] IOException");
  144 + }finally{
  145 + if (socket != null ) socket.close();
  146 + }
  147 + request.getRequestDispatcher(Param.PATH_HOME).forward(request, response);
87 148 }
88 149  
89 150 private String getMacAddressFromArpTable(String ip) throws IOException {
... ... @@ -105,19 +166,18 @@ public class CalibrationServlet extends HttpServlet {
105 166 return macAddr;
106 167 }
107 168  
108   - private String buildRssiRequestMessage(byte [] apIp, int port){
  169 + private String buildRssiRequestMessage(byte [] apIp, int port, String mac){
109 170  
110 171 String request = "http://";
111 172  
112 173 String ip = new String(apIp, StandardCharsets.UTF_8);
113 174  
114   - String macaddress = (String) getServletContext().getAttribute("macaddress");
115 175 String map_id = "1";
116 176 String get = "/get";
117 177 String macReq = "macs=";
118 178  
119 179 request += ip + ":" + port + get + "?";
120   - request += macReq + macaddress;
  180 + request += macReq + mac;
121 181  
122 182 System.out.println(request);
123 183  
... ...
src/main/java/com/lo53_mobile_localization/core/servlet/PositionServlet.java
1 1 package com.lo53_mobile_localization.core.servlet;
2 2  
  3 +import com.lo53_mobile_localization.core.entity.Location;
3 4 import com.lo53_mobile_localization.core.entity.Rssi;
  5 +import com.lo53_mobile_localization.core.service.AccessPointService;
  6 +import com.lo53_mobile_localization.core.service.LocationService;
4 7 import com.lo53_mobile_localization.core.service.RSSIService;
5 8 import com.lo53_mobile_localization.core.servlet.util.Param;
6 9  
... ... @@ -11,6 +14,7 @@ import java.io.IOException;
11 14 import java.net.DatagramPacket;
12 15 import java.net.DatagramSocket;
13 16 import java.net.InetAddress;
  17 +import java.net.SocketException;
14 18 import java.nio.charset.StandardCharsets;
15 19 import java.util.List;
16 20  
... ... @@ -26,7 +30,7 @@ import javax.servlet.http.HttpServletResponse;
26 30 urlPatterns = {"/position"}
27 31 )public class PositionServlet extends HttpServlet {
28 32  
29   - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  33 + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
30 34  
31 35 String requestIp = request.getRemoteAddr();
32 36 System.out.println("Request from ip : " + requestIp);
... ... @@ -51,35 +55,73 @@ import javax.servlet.http.HttpServletResponse;
51 55 }
52 56 socket.close();
53 57 } catch (IOException iox) {
54   - System.out.println("[Positioning Servlet:Sending GET message to APs] IOException");
55   - } finally {
56   - if (socket != null) socket.close();
  58 + System.out.println("Position Servlet : error when sending IOException");
57 59 }
58 60  
59   - // Sleep during 500ms
60 61 try {
61   - Thread.sleep(500);
62   - } catch (InterruptedException e1) {
63   - e1.printStackTrace();
64   - }
  62 + socket = new DatagramSocket(Param.DATAG_LISTENING_PORT);
65 63  
66   - RSSIService rssiService = new RSSIService();
  64 + long startTime = System.currentTimeMillis(); //fetch starting time
67 65  
68   - List<Rssi> rssis = rssiService.getRssiWithMacAdress(macAddr);
  66 + while (false || (System.currentTimeMillis() - startTime) < 500) {
69 67  
70   - int nbRes = 0;
71   - for (Rssi r : rssis) {
72   - nbRes++;
73   - }
  68 + byte[] receiveData = new byte[1024];
  69 + DatagramPacket packet = new DatagramPacket(receiveData, receiveData.length);
  70 + socket.receive(packet);
  71 + String receivedData = new String(packet.getData());
  72 +
  73 + //msg = "ap:ap:ap:ap:ap:ap" , "xx:xx:xx:xx:xx:xx" , "val" , "nbSamples"
  74 + String[] data = receivedData.split(",");
  75 +
  76 + String apMacAddress = data[0];
  77 + String deviceMac = data[1];
  78 + double value = Double.parseDouble(data[2]);
  79 + int nbSamples = Integer.parseInt(data[3]);
  80 +
  81 + if(nbSamples > 4){
  82 +
  83 + //int nbRes = 0;
  84 + //for (Rssi r : rssis) {
  85 + // nbRes++;
  86 + //}
  87 +
  88 + LocationService locationService = new LocationService();
  89 + AccessPointService apService = new AccessPointService();
  90 + RSSIService rssiService = new RSSIService();
74 91  
  92 + List<Rssi> rssis = rssiService.getRssiWithMacAdress(macAddr);
75 93  
  94 + Location loc = computeMinimumDistance();
  95 +
  96 + request.setAttribute("xLoc", loc.getX());
  97 + request.setAttribute("yLoc", loc.getY());
  98 +
  99 + request.getRequestDispatcher(Param.PATH_POSITION).forward(request,response);
  100 + }
  101 + }
  102 + } catch (SocketException e) {
  103 + System.out.println("[Calibration] SocketException");
  104 + } catch (IOException e) {
  105 + System.out.println("[Calibration] IOException");
  106 + }finally{
  107 + if (socket != null ) socket.close();
  108 + }
76 109 }
  110 + request.setAttribute("xLoc", 40);
  111 + request.setAttribute("yLoc", 40);
  112 +
  113 + request.getRequestDispatcher(Param.PATH_POSITION).forward(request,response);
77 114 }
78 115  
79 116  
80   - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  117 + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  118 +
  119 + response.setContentType(Param.CONTENT_TYPE);
  120 + request.getRequestDispatcher(Param.PATH_POSITION).forward(request,response);
  121 +
81 122 }
82 123  
  124 +
83 125 private String getMacAdressFromArpTable(String ip) throws IOException {
84 126  
85 127 BufferedReader in = new BufferedReader(new FileReader("/proc/net/arp"));
... ... @@ -119,4 +161,51 @@ import javax.servlet.http.HttpServletResponse;
119 161 return request;
120 162 }
121 163  
  164 + private Location computeMinimumDistance(){
  165 +
  166 + Location location = new Location();
  167 + location.setX(25);
  168 + location.setY(25);
  169 +
  170 + return location;
  171 + }
  172 +
  173 + public double rssi_distance( List<Rssi> rss1, List<Rssi> rss2) {
  174 + double distance = 0.0;
  175 + for (Rssi val:rss1) {
  176 + for (Rssi val2 : rss2) {
  177 + if (val.getAccessPoint().getMac_address() == val2.getAccessPoint().getMac_address()) {
  178 + if(!val.equals(rss2.get(rss2.size()-1)))
  179 + distance += (val.getValue() - val2.getValue()) * (val.getValue() - val2.getValue());
  180 + else
  181 + distance += (val.getValue() + 95.0) * (val.getValue() + 95.0);
  182 +
  183 + }
  184 + }
  185 + }
  186 + for (Rssi val:rss2) {
  187 + for (Rssi val2 : rss1) {
  188 + if (val.getAccessPoint().getMac_address() == val2.getAccessPoint().getMac_address()) {
  189 + if(val.equals(rss1.get(rss1.size()-1)))
  190 + distance += (val.getValue() + 95.0) * (val.getValue() + 95.0);
  191 + }
  192 + }
  193 + }
  194 + return Math.sqrt(distance);
  195 + }
  196 +
  197 + /*Location closest_rssi( List<Fingerprint> fingerprints, List<Rssi> mes_rssi) {
  198 + double minimum_dist;
  199 + Location minimum_dist_location = new Location();
  200 + minimum_dist = Double.POSITIVE_INFINITY;
  201 + Fingerprint fingerprint = new Fingerprint();
  202 + for (Fingerprint finger:fingerprints) {
  203 + if (minimum_dist > rssi_distance(finger.getRSSI(),mes_rssi)){
  204 + minimum_dist = rssi_distance(finger.getRSSI(),mes_rssi);
  205 + minimum_dist_location = fingerprint.getLocation();
  206 + }
  207 + }
  208 + return minimum_dist_location;
  209 + }*/
  210 +
122 211 }
123 212 \ No newline at end of file
... ...
src/main/java/com/lo53_mobile_localization/core/servlet/PreferencesServlet.java
... ... @@ -22,7 +22,6 @@ public class PreferencesServlet extends HttpServlet {
22 22  
23 23 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
24 24 response.setContentType(Param.CONTENT_TYPE);
25   - request.setAttribute("message", "HELLO THERE");
26 25 request.getRequestDispatcher(Param.PATH_PREFERENCES).forward(request,response);
27 26  
28 27 }
... ... @@ -31,7 +30,7 @@ public class PreferencesServlet extends HttpServlet {
31 30  
32 31 response.setContentType(Param.CONTENT_TYPE);
33 32  
34   - String macAddress = request.getParameter(Param.AP_MAC_ADDRESS);
  33 + //String macAddress = request.getParameter(Param.AP_MAC_ADDRESS);
35 34 String ip = request.getParameter(Param.ATTRIBUTE_IP);
36 35 String port = request.getParameter(Param.ATTRIBUTE_PORT);
37 36  
... ... @@ -40,12 +39,9 @@ public class PreferencesServlet extends HttpServlet {
40 39 + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
41 40 + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
42 41  
43   - String MAC_PATTERN = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
  42 + // String MAC_PATTERN = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
44 43  
45   - String PORT_PATTERN = "[0-9]{4}";
46   -
47   - Pattern patternMac = Pattern.compile(MAC_PATTERN);
48   - Matcher matchMac = patternMac.matcher(macAddress);
  44 + String PORT_PATTERN = "[0-6][0-9]{3}";
49 45  
50 46 Pattern patternIP = Pattern.compile(IP_PATTERN);
51 47 Matcher matchIp = patternIP.matcher(ip);
... ... @@ -53,16 +49,15 @@ public class PreferencesServlet extends HttpServlet {
53 49 Pattern patternPort = Pattern.compile(PORT_PATTERN);
54 50 Matcher matchPort = patternPort.matcher(port);
55 51  
56   - if (!matchIp.find() || !matchMac.find() || !matchPort.find()) {
  52 + if (!matchIp.find() || !matchPort.find()) {
57 53 System.out.println("ERROR IN SETTINGS");
58 54 request.setAttribute("message", "ERROR IN ONE OF THE FIELDS");
59 55 request.getRequestDispatcher(Param.PATH_PREFERENCES).forward(request, response);
60 56  
61 57 } else {
62 58  
63   - System.out.println("MAC ADDRESS = " + macAddress + " IP = " + ip + " PORT = " + port);
  59 + System.out.println(" IP = " + ip + " PORT = " + port);
64 60  
65   - getServletContext().setAttribute("macaddress", macAddress);
66 61 getServletContext().setAttribute("ip", ip);
67 62 getServletContext().setAttribute("port", port);
68 63  
... ...
src/test/java/com/lo53_mobile_localization/core/service/RssiServiceTest.java
... ... @@ -82,7 +82,7 @@ public class RssiServiceTest extends HibernateTestHelper{
82 82  
83 83 service.updateEntity(rssi.getId(), rssi);
84 84  
85   - Rssi storedClient = (Rssi) service.getEntity(rssi.getId());
  85 + Rssi storedClient = service.getEntity(rssi.getId());
86 86  
87 87 assertEquals((int)rssi.getValue(), (int)storedClient.getValue());
88 88 }
... ...