Commit 06bf8835d34e1324db88079fca02dab451d60a50
1 parent
026c5284
Ajout servlet calibration request
Showing
7 changed files
with
126 additions
and
13 deletions
Show diff stats
src/main/java/core/dao/HibernateDao.java
... | ... | @@ -163,6 +163,20 @@ public class HibernateDao { |
163 | 163 | return reply; |
164 | 164 | } |
165 | 165 | |
166 | + | |
167 | + public int saveLocation(Location location) { | |
168 | + TransactionCallBack callBack = execTransactionProcess((session)->{ | |
169 | + TransactionCallBack<Integer> reply = new TransactionCallBack<Integer>(); | |
170 | + Integer id = (Integer) session.save(location); | |
171 | + if(id!=null) | |
172 | + reply.results.add(id); | |
173 | + return reply; | |
174 | + }); | |
175 | + | |
176 | + if(callBack!=null && callBack.results.size()>0) | |
177 | + return (Integer)callBack.results.get(0); | |
178 | + return -1; | |
179 | + } | |
166 | 180 | public Location getLocation(int Location) { |
167 | 181 | TransactionCallBack callBack = execTransactionProcess((session)->{ |
168 | 182 | TransactionCallBack reply = new TransactionCallBack<Location>(); |
... | ... | @@ -178,6 +192,7 @@ public class HibernateDao { |
178 | 192 | return (callBack.getResults().isEmpty()?null:(Location)callBack.getResults().get(0)); |
179 | 193 | } |
180 | 194 | |
195 | + | |
181 | 196 | private interface ITransactionProcess{ |
182 | 197 | TransactionCallBack exec(Session tr); |
183 | 198 | } | ... | ... |
src/main/java/core/repository/Location.java
... | ... | @@ -19,7 +19,13 @@ public class Location implements Serializable { |
19 | 19 | |
20 | 20 | public Location () { |
21 | 21 | } |
22 | - | |
22 | + | |
23 | + public Location(double posx, double posy, Map map) { | |
24 | + this.x = posx; | |
25 | + this.y = posy; | |
26 | + this.map = map; | |
27 | + } | |
28 | + | |
23 | 29 | public Integer getId(){return id;} |
24 | 30 | |
25 | 31 | public void setId(Integer id){this.id = id;} | ... | ... |
src/main/java/core/service/CalibrationService.java
... | ... | @@ -29,4 +29,7 @@ public class CalibrationService { |
29 | 29 | return dao.saveAccessPoint(ap); |
30 | 30 | } |
31 | 31 | public List<AccessPoint> getAllAccessPoints(){return dao.getAccessPoint();} |
32 | + public int registerLocation(double posx, double posy, int mapId){ | |
33 | + return dao.saveLocation(new Location(posx, posy, dao.getMap(mapId))); | |
34 | + } | |
32 | 35 | } | ... | ... |
1 | +package servlet; | |
2 | + | |
3 | +import core.service.CalibrationService; | |
4 | + | |
5 | +import javax.servlet.ServletException; | |
6 | +import javax.servlet.annotation.WebServlet; | |
7 | +import javax.servlet.http.HttpServlet; | |
8 | +import javax.servlet.http.HttpServletRequest; | |
9 | +import javax.servlet.http.HttpServletResponse; | |
10 | +import java.io.IOException; | |
11 | + | |
12 | +/** | |
13 | + * Created by Guillaume on 07/06/2017. | |
14 | + */ | |
15 | +public class APCalibrationServlet extends HttpServlet { | |
16 | + | |
17 | + @Override | |
18 | + public void doGet(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) | |
19 | + throws ServletException, IOException { | |
20 | + execute(servletRequest, servletResponse); | |
21 | + } | |
22 | + | |
23 | + @Override | |
24 | + public void doPost(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) | |
25 | + throws ServletException, IOException { | |
26 | + execute(servletRequest, servletResponse); | |
27 | + } | |
28 | + | |
29 | + private void execute(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) throws ServletException, IOException { | |
30 | + | |
31 | + try { | |
32 | + int locId = Integer.parseInt(servletRequest.getParameter("LOCATION_ID")); | |
33 | + String apMacAddr = servletRequest.getParameter("AP_MAC_ADDRESS"); | |
34 | + double val = Double.parseDouble(servletRequest.getParameter("VAL")); | |
35 | + | |
36 | + new CalibrationService().addCalibrationData(locId, apMacAddr, val); | |
37 | + }catch(Exception e){ | |
38 | + e.printStackTrace(); | |
39 | + try{ | |
40 | + servletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); | |
41 | + }catch(IllegalStateException ex){ | |
42 | + ex.printStackTrace(); | |
43 | + } | |
44 | + } | |
45 | + } | |
46 | +} | ... | ... |
src/main/java/servlet/CalibrationServlet.java
... | ... | @@ -7,21 +7,36 @@ import javax.servlet.http.HttpServlet; |
7 | 7 | import javax.servlet.http.HttpServletRequest; |
8 | 8 | import javax.servlet.http.HttpServletResponse; |
9 | 9 | import java.io.IOException; |
10 | +import java.net.DatagramPacket; | |
11 | +import java.net.DatagramSocket; | |
12 | +import java.net.Inet4Address; | |
13 | +import java.net.InetAddress; | |
14 | +import java.util.Random; | |
10 | 15 | |
11 | 16 | /** |
12 | 17 | * Created by Guillaume on 15/05/2017. |
13 | 18 | */ |
14 | 19 | public class CalibrationServlet extends HttpServlet{ |
15 | - @Override | |
16 | - public void service (final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) | |
20 | + | |
21 | + public void execute (final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) | |
17 | 22 | throws ServletException, IOException { |
18 | 23 | |
19 | 24 | try { |
20 | - int locId = Integer.parseInt(servletRequest.getParameter("LOCATION_ID")); | |
21 | - String apMacAddr = servletRequest.getParameter("AP_MAC_ADDRESS"); | |
22 | - double val = Double.parseDouble(servletRequest.getParameter("VAL")); | |
25 | + String clientMacAddr = servletRequest.getParameter("CLIENT_MAC_ADDR"); | |
26 | + int mapId = Integer.parseInt(servletRequest.getParameter("MAP_ID")); | |
27 | + double posx = Double.parseDouble(servletRequest.getParameter("X")); | |
28 | + double posy = Double.parseDouble(servletRequest.getParameter("Y")); | |
23 | 29 | |
24 | - new CalibrationService().addCalibrationData(locId, apMacAddr, val); | |
30 | + Integer locationId = new CalibrationService().registerLocation(posx, posy, mapId); | |
31 | + if(locationId!=-1){ | |
32 | + multicastCalibrationRequest(clientMacAddr, locationId); | |
33 | + }else{ | |
34 | + try{ | |
35 | + servletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); | |
36 | + }catch(IllegalStateException ex){ | |
37 | + ex.printStackTrace(); | |
38 | + } | |
39 | + } | |
25 | 40 | }catch(Exception e){ |
26 | 41 | e.printStackTrace(); |
27 | 42 | try{ |
... | ... | @@ -35,12 +50,29 @@ public class CalibrationServlet extends HttpServlet{ |
35 | 50 | @Override |
36 | 51 | public void doGet(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) |
37 | 52 | throws ServletException, IOException { |
38 | - service(servletRequest, servletResponse); | |
53 | + execute(servletRequest, servletResponse); | |
39 | 54 | } |
40 | 55 | |
41 | 56 | @Override |
42 | 57 | public void doPost(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) |
43 | 58 | throws ServletException, IOException { |
44 | - service(servletRequest, servletResponse); | |
59 | + execute(servletRequest, servletResponse); | |
60 | + } | |
61 | + | |
62 | + private void multicastCalibrationRequest(String clientMacAddress, int locationId){ | |
63 | + try (DatagramSocket socket = new DatagramSocket(4445)){ | |
64 | + | |
65 | + byte[] buf = new byte[256]; | |
66 | + | |
67 | + String dString = "CALIB=" + clientMacAddress + ";SERV=" + Inet4Address.getLocalHost().getHostAddress()+";LOC_ID="+locationId+";"; | |
68 | + buf = dString.getBytes(); | |
69 | + | |
70 | + // send it | |
71 | + InetAddress group = InetAddress.getByName("230.0.0.1"); | |
72 | + DatagramPacket packet = new DatagramPacket(buf, buf.length, group, 4446); | |
73 | + socket.send(packet); | |
74 | + } catch (IOException e) { | |
75 | + e.printStackTrace(); | |
76 | + } | |
45 | 77 | } |
46 | 78 | } | ... | ... |
src/main/java/servlet/PositioningServlet.java
... | ... | @@ -24,10 +24,10 @@ public class PositioningServlet extends HttpServlet{ |
24 | 24 | throws ServletException, IOException { |
25 | 25 | |
26 | 26 | |
27 | - String clientIpAddr = servletRequest.getParameter("CLIENT_IP"); | |
27 | + String clientMacAddr = servletRequest.getParameter("CLIENT_MAC_ADDR"); | |
28 | 28 | PositioningService posServ = new PositioningService(); |
29 | 29 | |
30 | - multicastPositioningRequest(posServ.getMacAddr(clientIpAddr)); | |
30 | + multicastPositioningRequest(clientMacAddr); | |
31 | 31 | |
32 | 32 | |
33 | 33 | Location loc = null; |
... | ... | @@ -38,7 +38,7 @@ public class PositioningServlet extends HttpServlet{ |
38 | 38 | e.printStackTrace(); |
39 | 39 | } |
40 | 40 | |
41 | - loc = posServ.getLocation(clientIpAddr); | |
41 | + loc = posServ.getLocation(clientMacAddr); | |
42 | 42 | }while(loc==null); |
43 | 43 | |
44 | 44 | servletResponse.setContentType("text/plain"); | ... | ... |
src/main/webapp/WEB-INF/web.xml
... | ... | @@ -13,7 +13,18 @@ |
13 | 13 | |
14 | 14 | <servlet-mapping> |
15 | 15 | <servlet-name>calibrationServlet</servlet-name> |
16 | - <url-pattern>/calibration</url-pattern> | |
16 | + <url-pattern>/calibration/request</url-pattern> | |
17 | + </servlet-mapping> | |
18 | + | |
19 | + <!-- Calibration servlet --> | |
20 | + <servlet> | |
21 | + <servlet-name>apCalibrationServlet</servlet-name> | |
22 | + <servlet-class>classes.APCalibrationServlet</servlet-class> | |
23 | + </servlet> | |
24 | + | |
25 | + <servlet-mapping> | |
26 | + <servlet-name>apCalibrationServlet</servlet-name> | |
27 | + <url-pattern>/ap/calibration</url-pattern> | |
17 | 28 | </servlet-mapping> |
18 | 29 | |
19 | 30 | <!-- Positioning request servlet --> | ... | ... |