From 05fe05604dabec18427ced96052dc006420c0d55 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 8 Jun 2017 10:30:11 +0200 Subject: [PATCH] Web interface AccessPointListener beginning First servlets management --- .gitignore | 1 + .idea/artifacts/LO53_mobile_localization_war_exploded.xml | 1 + .idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23.xml | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------ .idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata | Bin 65536 -> 0 bytes .idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata.values.at | Bin 304 -> 0 bytes LO53_mobile_localization.iml | 50 ++++++++------------------------------------------ pom.xml | 5 +++++ src/main/java/com/lo53_mobile_localization/core/entity/Rssi.java | 20 +++++++++----------- src/main/java/com/lo53_mobile_localization/core/entity/RssiID.java | 17 +++++++++++++++++ src/main/java/com/lo53_mobile_localization/core/repository/RSSIDAO.java | 132 ++++-------------------------------------------------------------------------------------------------------------------------------- src/main/java/com/lo53_mobile_localization/core/service/AccessPointService.java | 36 ++++++++++++++++++++++++++++++++++++ src/main/java/com/lo53_mobile_localization/core/service/LocationService.java | 21 +++++++++++++++++++++ src/main/java/com/lo53_mobile_localization/core/service/RSSIService.java | 48 +++++++++++++++++++++++++++++++++++++++++------- src/main/java/com/lo53_mobile_localization/core/servlet/CalibrationServlet.java | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- src/main/java/com/lo53_mobile_localization/core/servlet/PositionServlet.java | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/lo53_mobile_localization/core/servlet/PreferencesServlet.java | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/lo53_mobile_localization/core/servlet/util/Param.java | 36 ++++++++++++++++++++++++++++++++++++ src/test/java/com/lo53_mobile_localization/core/service/RssiServiceTest.java | 41 ++++++++++++++++++++++++++--------------- 18 files changed, 551 insertions(+), 277 deletions(-) create mode 100644 .gitignore create mode 100644 src/main/java/com/lo53_mobile_localization/core/servlet/PositionServlet.java create mode 100644 src/main/java/com/lo53_mobile_localization/core/servlet/PreferencesServlet.java create mode 100644 src/main/java/com/lo53_mobile_localization/core/servlet/util/Param.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bfa6a22 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +# Created by .ignore support plugin (hsz.mobi) diff --git a/.idea/artifacts/LO53_mobile_localization_war_exploded.xml b/.idea/artifacts/LO53_mobile_localization_war_exploded.xml index 8b5dcfb..0ca494a 100644 --- a/.idea/artifacts/LO53_mobile_localization_war_exploded.xml +++ b/.idea/artifacts/LO53_mobile_localization_war_exploded.xml @@ -33,6 +33,7 @@ + diff --git a/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23.xml b/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23.xml index 6693114..b37a075 100644 --- a/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23.xml +++ b/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23.xml @@ -9,8 +9,8 @@ 2200 - 2017-05-31.07:36:16.132 - 11286 + 2017-06-07.19:30:21.978 + 17257 1 1 @@ -30,65 +30,65 @@ 12113 - 35521 - 11277 + 47042 + 17248 1 - 35523 - 11285 + 47044 + 17256
- 35528 - 11286 + 47049 + 17257
- 35533 - 11284 + 47054 + 17255
- 35541 - 11286 + 47062 + 17257
1 integer|0 1 - 11278 + 17249 2 varchar(18)|0 - 11278 + 17249 3 integer|0 - 11278 + 17249 - 35526 + 47047 id 1 - 35546 + 47067 mac_address 1 - 35527 + 47048 id accesspoint_pkey 1 - 35547 + 47068 mac_address uk_58em6b5lhouv812y1d05nwv9w - 35548 + 47069 location_id_id location location_pkey @@ -98,38 +98,38 @@ 1 integer|0 1 - 11279 + 17250 2 double precision|0 1 - 11279 + 17250 3 double precision|0 1 - 11279 + 17250 4 integer|0 - 11279 + 17250 - 35531 + 47052 id 1 - 35532 + 47053 id location_pkey 1 - 35553 + 47074 map_id map map_pkey @@ -139,109 +139,101 @@ 1 integer|0 1 - 11280 + 17251 2 bytea|0 - 11280 + 17251 3 varchar(100)|0 - 11280 + 17251 4 double precision|0 1 - 11280 + 17251 5 double precision|0 1 - 11280 + 17251 6 integer|0 1 - 11280 + 17251 7 integer|0 1 - 11280 + 17251 - 35539 + 47060 id 1 - 35540 + 47061 id map_pkey 1 - + 1 integer|0 1 - 11281 + 17252 - + 2 integer|0 1 - 11281 + 17252 - + 3 - integer|0 - 1 - 11281 - - - 4 double precision|0 1 - 11281 + 17252 - - 5 + + 4 integer|0 - 11281 + 17252 - - 6 + + 5 integer|0 - 11281 + 17252 - - 35544 - accesspointid -locationid + + 47065 + id 1 - - 35545 - accesspointid -locationid + + 47066 + id rssi_pkey 1 - - 35558 + + 47079 accesspoint_id accesspoint accesspoint_pkey id - - 35563 + + 47084 location_id location location_pkey diff --git a/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata b/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata index 615afc4..a3df136 100644 Binary files a/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata and b/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata differ diff --git a/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata.values.at b/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata.values.at index 1086822..2f3b6c6 100644 Binary files a/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata.values.at and b/.idea/dataSources/767b098c-c5bc-42ee-b3bc-d02e521ecc23/_metadata_/metadata.values.at differ diff --git a/LO53_mobile_localization.iml b/LO53_mobile_localization.iml index b150b64..df97bf1 100644 --- a/LO53_mobile_localization.iml +++ b/LO53_mobile_localization.iml @@ -1,17 +1,23 @@ - + - + + + + + + + @@ -22,44 +28,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index cbf63c4..76fb269 100644 --- a/pom.xml +++ b/pom.xml @@ -73,6 +73,11 @@ 4.12 test + + taglibs + standard + 1.1.2 + diff --git a/src/main/java/com/lo53_mobile_localization/core/entity/Rssi.java b/src/main/java/com/lo53_mobile_localization/core/entity/Rssi.java index 02f03d5..d8fe0ea 100644 --- a/src/main/java/com/lo53_mobile_localization/core/entity/Rssi.java +++ b/src/main/java/com/lo53_mobile_localization/core/entity/Rssi.java @@ -8,18 +8,15 @@ import java.io.Serializable; */ @Entity -public class Rssi implements Serializable { +public class Rssi implements Serializable, IEntity { - @EmbeddedId - private RssiID id; + @Id + @GeneratedValue + private int id; - @MapsId("locationId") - @JoinColumn(name = "location_id") @OneToOne private Location location; - @MapsId("accessPointId") - @JoinColumn(name = "accessPoint_id") @OneToOne private AccessPoint accessPoint; @@ -43,10 +40,6 @@ public class Rssi implements Serializable { this.occurences = occurences; } - public RssiID getId() { - return id; - } - public Location getLocation() { return location; } @@ -62,4 +55,9 @@ public class Rssi implements Serializable { public void setAccessPoint(AccessPoint accessPoint) { this.accessPoint = accessPoint; } + + @Override + public int getId() { + return id; + } } diff --git a/src/main/java/com/lo53_mobile_localization/core/entity/RssiID.java b/src/main/java/com/lo53_mobile_localization/core/entity/RssiID.java index 5473651..f966c54 100644 --- a/src/main/java/com/lo53_mobile_localization/core/entity/RssiID.java +++ b/src/main/java/com/lo53_mobile_localization/core/entity/RssiID.java @@ -3,6 +3,7 @@ package com.lo53_mobile_localization.core.entity; import javax.persistence.Column; import javax.persistence.Embeddable; import java.io.Serializable; +import java.util.Objects; /** * Created by Thomas on 18/05/2017. @@ -11,8 +12,10 @@ import java.io.Serializable; @Embeddable public class RssiID implements Serializable { + @Column(name = "location_id") private int locationId; + @Column(name = "accessPoint_id") private int accessPointId; @@ -31,4 +34,18 @@ public class RssiID implements Serializable { public void setAccessPointId(int accessPointId) { this.accessPointId = accessPointId; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof RssiID)) return false; + RssiID that = (RssiID) o; + return Objects.equals(getLocationId(), that.getLocationId()) && + Objects.equals(getAccessPointId(), that.getAccessPointId()); + } + + @Override + public int hashCode() { + return Objects.hash(getAccessPointId(), getLocationId()); + } } diff --git a/src/main/java/com/lo53_mobile_localization/core/repository/RSSIDAO.java b/src/main/java/com/lo53_mobile_localization/core/repository/RSSIDAO.java index 3f1efc8..bf91ae9 100644 --- a/src/main/java/com/lo53_mobile_localization/core/repository/RSSIDAO.java +++ b/src/main/java/com/lo53_mobile_localization/core/repository/RSSIDAO.java @@ -1,5 +1,7 @@ package com.lo53_mobile_localization.core.repository; +import com.lo53_mobile_localization.core.entity.AccessPoint; +import com.lo53_mobile_localization.core.entity.Location; import com.lo53_mobile_localization.core.entity.Rssi; import com.lo53_mobile_localization.core.entity.RssiID; import com.lo53_mobile_localization.core.util.HibernateUtil; @@ -21,136 +23,10 @@ import java.util.List; /** * Created by Thomas on 10/05/2017. */ -public class RSSIDAO { - - protected Logger LOGGER; - protected EntityManagerFactory emf = Persistence.createEntityManagerFactory("rssi"); - protected EntityManager em = emf.createEntityManager(); +public class RSSIDAO extends DefaultDAO{ public RSSIDAO() { super(); + entityType = Rssi.class; } - - /** - * To insert an entity into the database - * @param entity - */ - public void insertEntity(Rssi entity) { - Session session = HibernateUtil.getSession(); - EntityTransaction entr = em.getTransaction(); - try { - entr.begin(); - session.persist(entity); - entr.commit(); - } catch(Exception e) { - LOGGER.error("error in insertEntity: " + e); - if(entr != null) - entr.rollback(); - throw e; - } - } - - /** - * To update an entity into the database - * @param id - * @param entity - */ - public void updateEntity(RssiID id, Rssi entity) throws Exception { - - Rssi storedEntity = getEntity(id); - - try { - BeanUtils.copyProperties(storedEntity, entity); - } catch (IllegalAccessException|InvocationTargetException e) { - LOGGER.error("error in updateEntity: " + e); - throw e; - } - - Session session = HibernateUtil.getSession(); - Transaction tr = session.getTransaction(); - try { - tr.begin(); - session.update(entity); - tr.commit(); - } catch(Exception e) { - LOGGER.error("error in updateEntity: " + e); - if(tr != null) - tr.rollback(); - throw e; - } - } - - /** - * To remove an entity into the database - * @param id - */ - public void removeEntity(RssiID id) throws Exception { - - Rssi entity = getEntity(id); - - Session session = HibernateUtil.getSession(); - Transaction tr = session.getTransaction(); - try { - tr.begin(); - session.delete(entity); - tr.commit(); - } catch(Exception e) { - LOGGER.error("error in removeEntity: " + e); - if(tr != null) - tr.rollback(); - throw e; - } - } - - /** - * To get an entity from its id and class - * @param id - * @return - */ - @SuppressWarnings(value="unchecked") - public Rssi getEntity(RssiID id) throws Exception { - Rssi entity = null; - - Session session = HibernateUtil.getSession(); - Transaction tr = session.getTransaction(); - try { - tr.begin(); - String hql = "From Rssi r where r.id.locationId = :locationId AND r.id.accessPointId = :accessPointId"; - - - - String strLocId = Integer.toString(id.getLocationId()); - String strApId = Integer.toString(id.getAccessPointId()); - Query query = HibernateUtil.getSession().createQuery(hql); - query.setParameter("locationId",strApId); - query.setParameter("accessPointId", strApId); - entity = (Rssi)query.getSingleResult(); - tr.commit(); - } catch(Exception e) { - LOGGER.error("error in getEntity: " + e); - if(tr != null) - tr.rollback(); - throw e; - } - return entity; - } - - /** - * To get all entities - * @return - */ - public List getEntities() throws Exception { - List listEntities = null; - - Session session = HibernateUtil.getSession(); - try { - Query query = session.createQuery("from Rssi"); - listEntities = query.list(); - }catch (HibernateException e) { - LOGGER.error("error in getEntities: " + e); - throw e; - } - return listEntities; - } - } diff --git a/src/main/java/com/lo53_mobile_localization/core/service/AccessPointService.java b/src/main/java/com/lo53_mobile_localization/core/service/AccessPointService.java index bb41ebc..ceb24e3 100644 --- a/src/main/java/com/lo53_mobile_localization/core/service/AccessPointService.java +++ b/src/main/java/com/lo53_mobile_localization/core/service/AccessPointService.java @@ -2,7 +2,12 @@ package com.lo53_mobile_localization.core.service; import com.lo53_mobile_localization.core.entity.AccessPoint; import com.lo53_mobile_localization.core.entity.IEntity; +import com.lo53_mobile_localization.core.entity.Location; +import com.lo53_mobile_localization.core.entity.Rssi; import com.lo53_mobile_localization.core.repository.AccessPointDAO; +import com.lo53_mobile_localization.core.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.query.Query; import java.util.List; @@ -37,4 +42,35 @@ public class AccessPointService implements IService{ public List getEntities() throws Exception { return this.accessPointDAO.getEntities(); } + + public AccessPoint getSingleAccessPointFromMacAdress(String mac){ + + Session session = HibernateUtil.getSession(); + session.beginTransaction(); + + String hql= "from " + AccessPoint.class.getCanonicalName() + " ap where ap.mac_address = :mac"; + + Query query = session.createQuery(hql); + query.setParameter("mac", mac); + + AccessPoint result = (AccessPoint) query.getSingleResult(); + + return result; + } + + public List getAccessPointFromMacAdress(String mac){ + + Session session = HibernateUtil.getSession(); + session.beginTransaction(); + + String hql= "from " + AccessPoint.class.getCanonicalName() + " ap where ap.mac_address = :mac"; + + Query query = session.createQuery(hql); + query.setParameter("mac", mac); + + List result = query.list(); + + return result; + } + } diff --git a/src/main/java/com/lo53_mobile_localization/core/service/LocationService.java b/src/main/java/com/lo53_mobile_localization/core/service/LocationService.java index ee04574..dc4542f 100644 --- a/src/main/java/com/lo53_mobile_localization/core/service/LocationService.java +++ b/src/main/java/com/lo53_mobile_localization/core/service/LocationService.java @@ -1,8 +1,13 @@ package com.lo53_mobile_localization.core.service; +import com.lo53_mobile_localization.core.entity.AccessPoint; import com.lo53_mobile_localization.core.entity.IEntity; import com.lo53_mobile_localization.core.entity.Location; +import com.lo53_mobile_localization.core.entity.Rssi; import com.lo53_mobile_localization.core.repository.LocationDAO; +import com.lo53_mobile_localization.core.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.query.Query; import java.util.List; @@ -37,4 +42,20 @@ public class LocationService implements IService { public List getEntities() throws Exception { return this.locationDAO.getEntities(); } + + public Location getLocationFromCoordinates(double x, double y){ + + Session session = HibernateUtil.getSession(); + session.beginTransaction(); + + String hql= "from " + Location.class.getCanonicalName() + " l where l.x = :x and l.y = :y"; + + Query query = session.createQuery(hql); + query.setParameter("x", x); + query.setParameter("y",y); + + Location result = (Location) query.getSingleResult(); + + return result; + } } diff --git a/src/main/java/com/lo53_mobile_localization/core/service/RSSIService.java b/src/main/java/com/lo53_mobile_localization/core/service/RSSIService.java index 28ee969..018a40a 100644 --- a/src/main/java/com/lo53_mobile_localization/core/service/RSSIService.java +++ b/src/main/java/com/lo53_mobile_localization/core/service/RSSIService.java @@ -1,9 +1,10 @@ package com.lo53_mobile_localization.core.service; -import com.lo53_mobile_localization.core.entity.IEntity; -import com.lo53_mobile_localization.core.entity.Rssi; -import com.lo53_mobile_localization.core.entity.RssiID; +import com.lo53_mobile_localization.core.entity.*; import com.lo53_mobile_localization.core.repository.RSSIDAO; +import com.lo53_mobile_localization.core.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.query.Query; import java.util.List; @@ -19,23 +20,56 @@ public class RSSIService { this.rssi_recordDAO = new RSSIDAO(); } - public void storeEntity(Rssi entity) { + public void storeEntity(IEntity entity) { this.rssi_recordDAO.insertEntity((Rssi) entity); } - public void updateEntity(RssiID id, Rssi entity) throws Exception { + public void updateEntity(int id, IEntity entity) throws Exception { this.rssi_recordDAO.updateEntity(id, (Rssi) entity); } - public void removeEntity(RssiID id) throws Exception { + public void removeEntity(int id) throws Exception { this.rssi_recordDAO.removeEntity(id); } - public Rssi getEntity(RssiID id) throws Exception { + public Rssi getEntity(int id) throws Exception { return this.rssi_recordDAO.getEntity(id); } public List getEntities() throws Exception { return this.rssi_recordDAO.getEntities(); } + + public Rssi getRssiWithLocationAndAP(Location location, AccessPoint ap){ + + Session session = HibernateUtil.getSession(); + session.beginTransaction(); + + String hql= "from " + Rssi.class.getCanonicalName() + " r where r.location.id = :loc and r.accessPoint.id = :accesspoint"; + + Query query = session.createQuery(hql); + query.setParameter("loc", location.getId()); + query.setParameter("accesspoint",ap.getId()); + + Rssi result = (Rssi)query.getSingleResult(); + + return result; + } + + public List getRssiWithMacAdress(String mac){ + + Session session = HibernateUtil.getSession(); + session.beginTransaction(); + + String hql= "from " + Rssi.class.getCanonicalName() + " r where r.accessPoint.mac_address = :macAddress"; + + Query query = session.createQuery(hql); + query.setParameter("macAddress", mac); + + List result = query.list(); + + return result; + } + + } diff --git a/src/main/java/com/lo53_mobile_localization/core/servlet/CalibrationServlet.java b/src/main/java/com/lo53_mobile_localization/core/servlet/CalibrationServlet.java index 2932782..23d28c7 100644 --- a/src/main/java/com/lo53_mobile_localization/core/servlet/CalibrationServlet.java +++ b/src/main/java/com/lo53_mobile_localization/core/servlet/CalibrationServlet.java @@ -1,42 +1,127 @@ package com.lo53_mobile_localization.core.servlet; +import com.lo53_mobile_localization.core.entity.Location; +import com.lo53_mobile_localization.core.service.LocationService; +import com.lo53_mobile_localization.core.servlet.util.Param; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.nio.charset.StandardCharsets; /** * Created by Thomas on 18/05/2017. */ +@WebServlet( + name = "CalibrationServlet", + urlPatterns = {"/calibration"} +) public class CalibrationServlet extends HttpServlet { + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + + response.setContentType(Param.CONTENT_TYPE); + + request.getRequestDispatcher(Param.PATH_CALIBRATION).forward(request, response); + + + } + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + + String apRequestIp = request.getRemoteAddr(); + System.out.println("Request from " + apRequestIp + " received"); + + int x = -1; + int y = -1; + try{ + x = Integer.parseInt(request.getParameter("positionX")); + y = Integer.parseInt(request.getParameter("positionY")); + + System.out.println("x = " + x + " y = " + y); + }catch (NumberFormatException nfe){ + System.out.println("Wrong parameters"); + } + + if (x != -1 && y != -1){ + //String macAddr = getMacAddressFromArpTable(apRequestIp); + String macAddr = "12:12:12:12:12:12"; + if (macAddr != null){ + System.out.println("MAC Address found : "+macAddr); + DatagramSocket socket = null; + try{ + for (int i = 0 ; i < Param.AP_IPS.length ; i++){ + + InetAddress address = InetAddress.getByAddress(Param.AP_IPS[i]); + socket = new DatagramSocket(Param.AP_COMM_PORT); + + String msg = buildRssiRequestMessage(Param.AP_IPS[i], Param.AP_COMM_PORT); + + DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.length(), address, Param.AP_COMM_PORT); + socket.send(packet); + System.out.println("Sending "+msg+" to AP number " + i); + } + socket.close(); + } catch (IOException iox){ + System.out.println(iox+" [Calibration Servlet] IOException"); + }finally{ + if (socket != null) socket.close(); + } + }else{ + System.out.println("MAC Address not found !"); + } + }else{ + System.out.println("Coordinates not found"); + } + request.getRequestDispatcher(Param.PATH_CALIBRATION).forward(request, response); + } + + private String getMacAddressFromArpTable(String ip) throws IOException { - private String getMacAdressFromArpTable(String ip) throws IOException { - // Open the ARP table too look up for this IP BufferedReader in = new BufferedReader(new FileReader("/proc/net/arp")); String strLine; String macAddr = null; - // Read File Line By Line while ((strLine = in.readLine()) != null) { - // Find the matching IP System.out.println(strLine); if (strLine.contains(ip)) { - // Split the line String[] table = strLine.split(" "); - // Retrieve the MAC Address - //macAddr = table[24]; for (int i = 0; i < table.length; i++) { if (table[i].length() == 17) macAddr = table[i]; - //System.out.println("Table["+i+"] : "+table[i]); } } } return macAddr; } + private String buildRssiRequestMessage(byte [] apIp, int port){ + + String request = "http://"; + + String ip = new String(apIp, StandardCharsets.UTF_8); + + String macaddress = (String) getServletContext().getAttribute("macaddress"); + String map_id = "1"; + String get = "/get"; + String macReq = "macs="; + + request += ip + ":" + port + get + "?"; + request += macReq + macaddress; + + System.out.println(request); + + return request; + } + } diff --git a/src/main/java/com/lo53_mobile_localization/core/servlet/PositionServlet.java b/src/main/java/com/lo53_mobile_localization/core/servlet/PositionServlet.java new file mode 100644 index 0000000..3f57c4d --- /dev/null +++ b/src/main/java/com/lo53_mobile_localization/core/servlet/PositionServlet.java @@ -0,0 +1,122 @@ +package com.lo53_mobile_localization.core.servlet; + +import com.lo53_mobile_localization.core.entity.Rssi; +import com.lo53_mobile_localization.core.service.RSSIService; +import com.lo53_mobile_localization.core.servlet.util.Param; + +import java.io.BufferedReader; + +import java.io.FileReader; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +@WebServlet( + name = "PositionServlet", + urlPatterns = {"/position"} +)public class PositionServlet extends HttpServlet { + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + String requestIp = request.getRemoteAddr(); + System.out.println("Request from ip : " + requestIp); + + //String macAddr = getMacAdressFromArpTable(requestIp); + String macAddr = "12:12:12:12:12:12"; + + if (macAddr != null) { + + // Send Get request to all APs + DatagramSocket socket = null; + try { + for (int i = 0; i < Param.AP_IPS.length; i++) { + InetAddress address = InetAddress.getByAddress(Param.AP_IPS[i]); + socket = new DatagramSocket(Param.AP_COMM_PORT); + + String msg = buildMessage(macAddr, Param.AP_IPS[i]); + + DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.length(), address, Param.AP_COMM_PORT); + socket.send(packet); + System.out.println("Sending " + msg + " to AP number " + i); + } + socket.close(); + } catch (IOException iox) { + System.out.println("[Positioning Servlet:Sending GET message to APs] IOException"); + } finally { + if (socket != null) socket.close(); + } + + // Sleep during 500ms + try { + Thread.sleep(500); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + + RSSIService rssiService = new RSSIService(); + + List rssis = rssiService.getRssiWithMacAdress(macAddr); + + int nbRes = 0; + for (Rssi r : rssis) { + nbRes++; + } + + + } + } + + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + } + + private String getMacAdressFromArpTable(String ip) throws IOException { + + BufferedReader in = new BufferedReader(new FileReader("/proc/net/arp")); + String strLine; + String macAddr = null; + + while ((strLine = in.readLine()) != null) { + System.out.println(strLine); + if (strLine.contains(ip)) { + String[] table = strLine.split(" "); + //macAddr = table[24]; + for (int i = 0; i < table.length; i++) { + if (table[i].length() == 17) + macAddr = table[i]; + } + } + } + return macAddr; + } + + private String buildMessage(String mac, byte[] apIp){ + + //String msg = http://:/get?macs=XX:XX:XX:XX:XX + + String request = "http://"; + + String ip = new String(apIp, StandardCharsets.UTF_8); + int port = Param.AP_COMM_PORT; + String map_id = "1"; + String positionServlet = "/position"; + + request += ip + ":" + port + positionServlet + "?"; + request += "macs=" + mac; + + System.out.println(request); + + return request; + } + +} \ No newline at end of file diff --git a/src/main/java/com/lo53_mobile_localization/core/servlet/PreferencesServlet.java b/src/main/java/com/lo53_mobile_localization/core/servlet/PreferencesServlet.java new file mode 100644 index 0000000..f743fef --- /dev/null +++ b/src/main/java/com/lo53_mobile_localization/core/servlet/PreferencesServlet.java @@ -0,0 +1,73 @@ +package com.lo53_mobile_localization.core.servlet; + +import com.lo53_mobile_localization.core.servlet.util.Param; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created by Thomas on 03/06/2017. + */ +@WebServlet( + name = "PreferencesServlet", + urlPatterns = {"/preferences"} +) +public class PreferencesServlet extends HttpServlet { + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + response.setContentType(Param.CONTENT_TYPE); + request.setAttribute("message", "HELLO THERE"); + request.getRequestDispatcher(Param.PATH_PREFERENCES).forward(request,response); + + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + + response.setContentType(Param.CONTENT_TYPE); + + String macAddress = request.getParameter(Param.AP_MAC_ADDRESS); + String ip = request.getParameter(Param.ATTRIBUTE_IP); + String port = request.getParameter(Param.ATTRIBUTE_PORT); + + String IP_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; + + String MAC_PATTERN = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"; + + String PORT_PATTERN = "[0-9]{4}"; + + Pattern patternMac = Pattern.compile(MAC_PATTERN); + Matcher matchMac = patternMac.matcher(macAddress); + + Pattern patternIP = Pattern.compile(IP_PATTERN); + Matcher matchIp = patternIP.matcher(ip); + + Pattern patternPort = Pattern.compile(PORT_PATTERN); + Matcher matchPort = patternPort.matcher(port); + + if (!matchIp.find() || !matchMac.find() || !matchPort.find()) { + System.out.println("ERROR IN SETTINGS"); + request.setAttribute("message", "ERROR IN ONE OF THE FIELDS"); + request.getRequestDispatcher(Param.PATH_PREFERENCES).forward(request, response); + + } else { + + System.out.println("MAC ADDRESS = " + macAddress + " IP = " + ip + " PORT = " + port); + + getServletContext().setAttribute("macaddress", macAddress); + getServletContext().setAttribute("ip", ip); + getServletContext().setAttribute("port", port); + + request.getRequestDispatcher(Param.PATH_HOME).forward(request, response); + } + } + +} diff --git a/src/main/java/com/lo53_mobile_localization/core/servlet/util/Param.java b/src/main/java/com/lo53_mobile_localization/core/servlet/util/Param.java new file mode 100644 index 0000000..e0b02e8 --- /dev/null +++ b/src/main/java/com/lo53_mobile_localization/core/servlet/util/Param.java @@ -0,0 +1,36 @@ +package com.lo53_mobile_localization.core.servlet.util; + +/** + * Created by Thomas on 03/06/2017. + */ +public class Param{ + + + public static final String ATTRIBUTE_IP = "ip"; + public static final String ATTRIBUTE_PORT = "port"; + + public static final String AP_MAC_ADDRESS = "macaddress"; + + public static final String PATH_PREFERENCES= "/jsp/preferences.jsp"; + public static final String PATH_POSITION= "/jsp/position.jsp"; + public static final String PATH_CALIBRATION="/jsp/calibration.jsp"; + + public static final String PATH_HOME = "/index.jsp"; + + public static final String CONTENT_TYPE = "text/html;charset=UTF-8"; + + public static final int AP_COMM_PORT = 7777; + + public static final byte[][] AP_IPS = { + {(byte)192,(byte)168,(byte)1,(byte)1}, + {(byte)192,(byte)168,(byte)1,(byte)2}, + {(byte)192,(byte)168,(byte)1,(byte)3} + }; + + public static final int DATAG_LISTENING_PORT = 8080; + + public static final String CALIBRATION = "calibration"; + public static final String POSITION = "position"; + + +} diff --git a/src/test/java/com/lo53_mobile_localization/core/service/RssiServiceTest.java b/src/test/java/com/lo53_mobile_localization/core/service/RssiServiceTest.java index 833d927..4c110c6 100644 --- a/src/test/java/com/lo53_mobile_localization/core/service/RssiServiceTest.java +++ b/src/test/java/com/lo53_mobile_localization/core/service/RssiServiceTest.java @@ -2,9 +2,6 @@ package com.lo53_mobile_localization.core.service; import com.lo53_mobile_localization.core.entity.*; import com.lo53_mobile_localization.core.helpers.HibernateTestHelper; -import com.lo53_mobile_localization.core.repository.AccessPointDAO; -import com.lo53_mobile_localization.core.repository.LocationDAO; -import com.lo53_mobile_localization.core.repository.RSSIDAO; import org.junit.Before; import org.junit.Test; @@ -18,8 +15,8 @@ import static org.junit.Assert.assertTrue; */ public class RssiServiceTest extends HibernateTestHelper{ - Rssi item = null; - RSSIService service; + private Rssi item = null; + private RSSIService service; @Before public void setUp() throws Exception { @@ -31,19 +28,18 @@ public class RssiServiceTest extends HibernateTestHelper{ Location loc = new Location(); AccessPoint ap = new AccessPoint(); - LocationDAO locationDAO = new LocationDAO(); - AccessPointDAO accessPointDAO = new AccessPointDAO(); + LocationService locationService = new LocationService(); + AccessPointService apService = new AccessPointService(); - locationDAO.insertEntity(loc); - accessPointDAO.insertEntity(ap); + locationService.storeEntity(loc); + apService.storeEntity(ap); - rssi.getId().setAccessPointId(ap.getId()); - rssi.getId().setLocationId(loc.getId()); + rssi.setAccessPoint(ap); + rssi.setLocation(loc); rssi.setValue(10); rssi.setOccurences(6); - RSSIDAO rssiDao = new RSSIDAO(); - rssiDao.insertEntity(rssi); + service.storeEntity(rssi); item = rssi; } @@ -62,8 +58,14 @@ public class RssiServiceTest extends HibernateTestHelper{ Location loc = new Location(); AccessPoint ap = new AccessPoint(); - testingRssi.getId().setAccessPointId(ap.getId()); - testingRssi.getId().setLocationId(loc.getId()); + AccessPointService apService = new AccessPointService(); + LocationService locationService = new LocationService(); + + locationService.storeEntity(loc); + apService.storeEntity(ap); + + testingRssi.setAccessPoint(ap); + testingRssi.setLocation(loc); service.storeEntity(testingRssi); @@ -116,5 +118,14 @@ public class RssiServiceTest extends HibernateTestHelper{ assertTrue(storedItems.containsAll(addedItems)); } + @Test + public void getRssiWithLocationAndAP(){ + + Rssi testRssi = service.getRssiWithLocationAndAP(item.getLocation(), item.getAccessPoint()); + + assertEquals(item, testRssi); + + } + } -- libgit2 0.21.4