Commit 0d53d056 authored by José Soares's avatar José Soares

Cleanned up the project until reaching the modals package

parent 0f9bbfba
package com.senception.contextualmanager.aidl;
//import com.senception.cmumobile.inference.MyObject;
/*
* This aidl file contains all the methods that contextual manager
* provides to clients.
......@@ -14,8 +12,6 @@ interface CManagerInterface {
//ContextualManagerSimilarity
//int [] getI(TYPE, NODE);
// List<MyObject> getAll(in int[] peerList);
//List<String> getAll(in);
//"A.C"
}
......@@ -3,16 +3,14 @@ package com.senception.contextualmanager.communication;
import android.content.Context;
import android.net.wifi.p2p.WifiP2pDevice;
import android.util.Log;
import com.senception.contextualmanager.databases.ContextualManagerDataSource;
import com.senception.contextualmanager.databases.ContextualManagerSQLiteHelper;
import com.senception.contextualmanager.modals.ContextualManagerAP;
import com.senception.contextualmanager.security.MacSecurity;
import com.senception.contextualmanager.services.ContextualManagerService;
import com.senception.contextualmanager.wifi.p2p.Identity;
import com.senception.contextualmanager.wifi.p2p.WifiP2pListener;
import com.senception.contextualmanager.wifi.p2p.WifiP2pListenerManager;
import com.senception.contextualmanager.wifi.p2p.WifiP2pTxtRecord;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
......@@ -20,65 +18,43 @@ import java.util.Map;
/**
* Copyright (C) 2016 Senception Lda
* Author(s): Igor dos Santos - [email protected] *
* José Soares - [email protected]
* Update to Contextual Manager 2017
* @author Igor dos Santos
* @author José Soares
* @version 0.1
* @author Igor dos Santos - [email protected]
* @author José Soares - [email protected]
* @version 0.2
*
* @file Contains ContextualManagerReceive. This class is used
* to receive information from contextual managers (A,C)
*/
public class ContextualManagerReceive implements WifiP2pListener.TxtRecordAvailable {
private static SimpleDateFormat dataFormat = new SimpleDateFormat("dd-MM-yyyy 'at' HH:mm:ss");
private final String TAG = ContextualManagerReceive.class.getSimpleName();
private ContextualManagerDataSource dataSource;
private Context mContext;
/**
* Constructs a contectualManagerReceive that will be attempting to
* receive the availability and centrality from other peers that are
* also running the Contextual Manager.
* @param context
*/
public ContextualManagerReceive(Context context) {
mContext = context;
dataSource = new ContextualManagerDataSource(mContext);
dataSource.openDB(true);
WifiP2pListenerManager.registerListener(this);
/*Log.d("Communication", "A TENTAR ENVIAR:");
WifiP2pTxtRecord.setRecord(mContext, Identity.AVAILABILITY, "40.5");
WifiP2pTxtRecord.setRecord(mContext, Identity.CENTRALITY, "49.7");
WifiP2pTxtRecord.setRecord(mContext, Identity.SIMILARITY, "410");*/
}
@Override
public void onTxtRecordAvailable(String fullDomainName, Map<String, String> txtRecordMap, WifiP2pDevice srcDevice) {
/* fullDomainName: uuidcm (cm - service on identify)
*
* txtRecordMap:
* Availability --> 4716456285
* Centrality --> -1
* Similarity --> 10
*
* srcDevice.deviceName: SAMSUNG NEO
*/
Log.d("teste", "MAKING A SCAN ON TXTRECORD:");
Log.i("teste", fullDomainName + " " + txtRecordMap + " " + srcDevice.deviceName);
if(txtRecordMap != null && txtRecordMap.size() != 0) {
Log.d("teste", "ENCONTREI PEER:");
double A = Double.parseDouble(txtRecordMap.get(Identity.AVAILABILITY));
Log.d("teste", "A recebido: " + A);
double C = Double.parseDouble(txtRecordMap.get(Identity.CENTRALITY));
Log.d("teste", "C recebido: " + C);
Log.i(TAG, fullDomainName + " " + txtRecordMap + " " + srcDevice.deviceName);
//deviceAddress - Mac (BSSID) | deviceName - Device name (SSID)
String hashSrcDeviceBSSID = MacSecurity.MD5hash(srcDevice.deviceAddress);
//if it's the first time we see this peer
if (!dataSource.hasPeer(hashSrcDeviceBSSID, checkWeek("peers"))) {
//if it's the first time we see this peer we save it
if (!dataSource.hasPeer(hashSrcDeviceBSSID, ContextualManagerService.checkWeek("peers"))) {
ContextualManagerAP peer = new ContextualManagerAP();
peer.setSSID(srcDevice.deviceName);
peer.setBSSID(hashSrcDeviceBSSID);
......@@ -88,62 +64,22 @@ public class ContextualManagerReceive implements WifiP2pListener.TxtRecordAvaila
peer.setCentrality(C);
peer.setNumEncounters(1);
peer.setStartEncounter((int)(System.currentTimeMillis()/1000)); //time in seconds System.currentTimeMillis()/1000
dataSource.registerNewPeers(peer, checkWeek("peers"));
Log.d("teste", "SAVED PEER");
dataSource.registerNewPeers(peer, ContextualManagerService.checkWeek("peers"));
} else {
ContextualManagerAP peer = dataSource.getPeer(hashSrcDeviceBSSID, checkWeek("peers"));
//if we've already seen this peer and it is disconnected then it is again connected and we increade numEnc
ContextualManagerAP peer = dataSource.getPeer(hashSrcDeviceBSSID, ContextualManagerService.checkWeek("peers"));
peer.setSSID(srcDevice.deviceName);
peer.setBSSID(hashSrcDeviceBSSID);
peer.setAvailability(A);
peer.setCentrality(C);
//TODO peer.setLatitude(latitude);
//TODO peer.setLongitude(longitude);
dataSource.updatePeer(peer, checkWeek("peers"));
Log.d("teste", "UPDATED PEER");
dataSource.updatePeer(peer, ContextualManagerService.checkWeek("peers"));
}
//txtRecordMap.values();
}
else{
Log.d("teste", "The txt Record was null or empty");
Log.d(TAG, "The txt Record was null or empty");
}
}
/**
* Funtion checkWeek
* Check if it is peer or an ap, and what day
* @param peerON value to check if is peer or not
* @return tableName current AP or PEERS
*/
public static String checkWeek(String peerON){
SimpleDateFormat sdf = new SimpleDateFormat("EEE");
Date date = new Date();
String dw = sdf.format(date);
String tableName = "";
if(dw.equalsIgnoreCase("Mon") || dw.equalsIgnoreCase("Seg")){
tableName = (peerON.equalsIgnoreCase("peers")) ? ContextualManagerSQLiteHelper.TABLE_MONDAY_PEERS : ContextualManagerSQLiteHelper.TABLE_MONDAY;
}
else if(dw.equalsIgnoreCase("Tue") || dw.equalsIgnoreCase("Ter")){
tableName = (peerON.equalsIgnoreCase("peers")) ? ContextualManagerSQLiteHelper.TABLE_TUESDAY_PEERS : ContextualManagerSQLiteHelper.TABLE_TUESDAY;
}
else if(dw.equalsIgnoreCase("Wed") || dw.equalsIgnoreCase("Qua")){
tableName =(peerON.equalsIgnoreCase("peers")) ? ContextualManagerSQLiteHelper.TABLE_WEDNESDAY_PEERS : ContextualManagerSQLiteHelper.TABLE_WEDNESDAY;
}
else if(dw.equalsIgnoreCase("Thu") || dw.equalsIgnoreCase("Qui")){
tableName = (peerON.equalsIgnoreCase("peers")) ? ContextualManagerSQLiteHelper.TABLE_THURSDAY_PEERS : ContextualManagerSQLiteHelper.TABLE_THURSDAY;
}
else if(dw.equalsIgnoreCase("Fri") || dw.equalsIgnoreCase("Sex")){
tableName = (peerON.equalsIgnoreCase("peers")) ? ContextualManagerSQLiteHelper.TABLE_FRIDAY_PEERS : ContextualManagerSQLiteHelper.TABLE_FRIDAY;
}
else if(dw.equalsIgnoreCase("Sat") || dw.equalsIgnoreCase("Sáb")){
tableName = (peerON.equalsIgnoreCase("peers")) ? ContextualManagerSQLiteHelper.TABLE_SATURDAY_PEERS : ContextualManagerSQLiteHelper.TABLE_SATURDAY;
}
else if(dw.equalsIgnoreCase("Sun") || dw.equalsIgnoreCase("Dom")){
tableName = (peerON.equalsIgnoreCase("peers")) ? ContextualManagerSQLiteHelper.TABLE_SUNDAY_PEERS : ContextualManagerSQLiteHelper.TABLE_SUNDAY;
}
return tableName;
}
}
......@@ -16,12 +16,10 @@ import java.util.TimerTask;
/**
* Copyright (C) 2016 Senception Lda
* Author(s): Igor dos Santos - [email protected] *
* José Soares - [email protected]ption.com
* Update to Contextual Manager 2017
* @author Igor dos Santos
* @author José Soares
* @version 0.1
* @author Igor dos Santos - [email protected]
* @author José Soares - [email protected]
* @version 0.2
*
* @file Contains ContextualManagerSend. This class is used
* to send information to other contextual managers (A,C)
......@@ -31,13 +29,19 @@ public class ContextualManagerSend {
private final String TAG = ContextualManagerSend.class.getSimpleName();
private Context mContext;
/**
* /**
* Constructs a contectualManagerSend that will be attempting to
* send the availability and centrality to other peers that are
* also running the Contextual Manager.
* @param context
*/
public ContextualManagerSend(Context context) {
mContext = context;
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Log.d("Communication", "A TENTAR ENVIAR:");
double A;
double C;
......@@ -46,27 +50,17 @@ public class ContextualManagerSend {
if (!dataSource.isTableEmpty(ContextualManagerSQLiteHelper.TABLE_WEIGHTS)) {
ContextualManagerWeight weight = dataSource.getWeight();
A = weight.getA();
C = weight.getC();
Log.d("Communication", "A: " + A); // A with value 1.3872E7 means 1.3872 * 10^7, cientific notation
Log.d("Communication", "C: " + C);
//TODO Change A and C from int to double if needed
// TODO if txtRecord = {} we shouldn't send it
WifiP2pTxtRecord.setRecord(mContext, Identity.AVAILABILITY, String.valueOf(A));
WifiP2pTxtRecord.setRecord(mContext, Identity.CENTRALITY, String.valueOf(C));
WifiP2pTxtRecord.setRecord(mContext, Identity.SIMILARITY, "10");
//Todo WifiP2pTxtRecord.setRecord(mContext, Identity.SIMILARITY, "0");
}
else{
Log.d("Communication", "A TABELA AINDA ESTA VAZIA");
Log.d(TAG, "Table is still empty so we can't send the availability and centrality.");
}
/*
WifiP2pTxtRecord.setRecord(mContext, Identity.AVAILABILITY, "20.5");
WifiP2pTxtRecord.setRecord(mContext, Identity.CENTRALITY, "9.7");
WifiP2pTxtRecord.setRecord(mContext, Identity.SIMILARITY, "10");*/
}
}, 0, 1*60000); //5 em 5 min
}, 0, 1*60*1000); //5 em 5 min
}
}
......@@ -16,12 +16,12 @@ import java.util.Calendar;
*
* @file Contains ContextualManagerAvailability. Class that handles the inference of the availability.
*/
//(A) Usage/Availability
public class ContextualManagerAvailability {
//Formulas:
// 1) U(i) = Σ r(i) / T
// 2) R(i) = B(i) * B(i) * CPU(i) * MEM(i) * STORAGE(i), r(i) ⊂ [0,1]
/*Formulas:
* 1) A(i) = Σ r(i) / T
* 2) R(i) = B(i) * B(i) * CPU(i) * MEM(i) * STORAGE(i), r(i) ⊂ [0,1]
*/
/**
* Calculates a number that measures if a device is viable to conduct communication with in a given time.
......
......@@ -37,6 +37,14 @@ public class ContextualManagerCentrality {
A(j): adjancency
d(i,j) ∈ [0,100]
*/
/**
* Method that calculates the centrality of a device, using
* it's peer's average encounter duration and the number of encounters.
* (Eigenvector)
* @param dataSource
* @return centrality - the centrality of the device
*/
public static double calculateC(ContextualManagerDataSource dataSource){
//double degree;
int numEncounters;
......
......@@ -12,7 +12,7 @@ package com.senception.contextualmanager.inference;
* @file Contains ContextualManagerSimilarity. Class that handles the inference of the similarity.
*
*/
// (I) ContextualManagerSimilarity
// TODO
public class ContextualManagerSimilarity {
}
package com.senception.contextualmanager.inference;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by Senception on 06/03/2018.
*/
public class MyObject implements Parcelable{
private int test;
public MyObject(int i){
test = i;
}
protected MyObject(Parcel in) {
test = in.readInt();
}
public static final Creator<MyObject> CREATOR = new Creator<MyObject>() {
@Override
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);
}
@Override
public MyObject[] newArray(int size) {
return new MyObject[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(test);
}
}
......@@ -15,9 +15,9 @@ package com.senception.contextualmanager.modals;
public class ContextualManagerAP {
private int id;
private String BSSID; //mac address of the WAP (wireless access point)
private String SSID; //Name of the WAP
private double attractiveness; //What i belive is the centrality
private String BSSID; //Mac address of the WAP (wireless access point)
private double attractiveness; //TODO since it's not used in this vertion, delete.
private String dateTime;
private String DayOfWeek;
private double latitude;
......@@ -30,181 +30,256 @@ public class ContextualManagerAP {
private double avgEncounterDuration;
private int isConnected; //0 - not connected | 1 - connected
//TODO Javadoc for all gets and sets
/*The gets and sets for an ap */
public int getIsConnected() {
return isConnected;
/**
*PerSense AP Constructor
*/
public ContextualManagerAP(){
super();
}
public void setIsConnected(int isConnected) {
this.isConnected = isConnected;
/**
*Get the id of this AP
*@return the id
*/
public int getId() {
return id;
}
/**
*Set the id of this AP
*@param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
*Get the bssid of this AP
*@return the bssid
*/
public String getBSSID() {
return BSSID;
}
/**
*Set the bssid of this AP
*@param bSSID the bssid to set
*/
public void setBSSID(String bSSID) {
BSSID = bSSID;
}
/**
*Get the ssid of this AP
*@return the SSID
*/
public String getSSID() {
return SSID;
}
/**
*Set the ssid of this AP
*@param sSID the ssid to set
*/
public void setSSID(String sSID) {
SSID = sSID;
}
/**
*Get the day of week of this AP
*@return the dayofweek
*/
public String getDayOfWeek() {
return DayOfWeek;
}
/**
*Set the day of the week of this AP
*@param dayOfWeek the day of the week to set
*/
public void setDayOfWeek(String dayOfWeek) {
DayOfWeek = dayOfWeek;
}
/**
*Get the attractiveness of this AP
*@return the attractiveness
*/
public double getAttractiveness() {
return attractiveness;
}
/**
*Set the attractiveness of this AP
*@param attractiveness the attractiveness to set
*/
public void setAttractiveness(double attractiveness) {
this.attractiveness = attractiveness;
}
/**
*Get the date and time of this AP
*@return the dateTime
*/
public String getDateTime() {
return dateTime;
}
/**
*Set the date and time of this AP
*@param dateTime the date and time to set
*/
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
/**
*Get the latitude of this AP
*@return the latitude
*/
public double getLatitude() {
return latitude;
}
/**
*Set the latitude of this AP
*@param latitude the latitude to set
*/
public void setLatitude(double latitude) {
this.latitude = latitude;
}
/**
*Gets the longitude of this AP
*@return the longitude
*/
public double getLongitude() {
return longitude;
}
/**
*Set the longitude of this AP
*@param longitude the longitude to set
*/
public void setLongitude(double longitude) {
this.longitude = longitude;
}
/* ------ The gets and sets for a peer ------ */ //Todo separate aps from peers
/**
* Get the availability of this peer.
* @return the availability
*/
public double getAvailability() {
return Availability;
}
/**
* Set the availability of this peer.
* @param availability the availability to set
*/
public void setAvailability(double availability) {
Availability = availability;
}
/**
* Gets the centrality of this peer.
* @return the centrality
*/
public double getCentrality() {
return Centrality;
}
/**
* Set the centrality of this peer.
* @param centrality the centrality to set.
*/
public void setCentrality(double centrality) {
Centrality = centrality;
}
//TODO finish the javadoc
/**
* Get the number of encounters of this peer.
* @return the numEncounters
*/
public int getNumEncounters() {
return numEncounters;
}
/**
* Set the number of encounters of this peer.
* @param numEncounters the number of encounters to set.
*/
public void setNumEncounters(int numEncounters) {
this.numEncounters = numEncounters;
}
/**
*
* @return
*/
public int getStartEncounter() {
return startEncounter;
}
/**
*
* @param startTime
*/
public void setStartEncounter(int startTime) {
this.startEncounter = startTime;
}
/**
*
* @return
*/
public int getEndEncounter() {
return endEncounter;
}
/**
*
* @param endTime
*/
public void setEndEncounter(int endTime) {
this.endEncounter = endTime;
}
/**
*
* @return
*/
public double getAvgEncounterDuration() {
return avgEncounterDuration;
}
/**
*
* @param avgEncounterDuration
*/
public void setAvgEncounterDuration(double avgEncounterDuration) {
this.avgEncounterDuration = avgEncounterDuration;
}
public double getAvailability() {
return Availability;
}
public void setAvailability(double availability) {
Availability = availability;
}
public double getCentrality() {
return Centrality;
}
public void setCentrality(double centrality) {
Centrality = centrality;
}
public int getStartEncounter() {
return startEncounter;
}
public void setStartEncounter(int startTime) {
this.startEncounter = startTime;
}
public int getEndEncounter() {
return endEncounter;
}
public void setEndEncounter(int endTime) {
this.endEncounter = endTime;
}
public int getNumEncounters() {
return numEncounters;
}
public void setNumEncounters(int numEncounters) {
this.numEncounters = numEncounters;
}
/**
*Get the id of this AP
*@return the id
*/
public int getId() {
return id;
}
/**
*Set the id of this AP
*@param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
*Get the day of week of this AP
*@return the dayofweek
*/
public String getDayOfWeek() {
return DayOfWeek;
}
/**
*Set the day of the week of this AP
*@param dayOfWeek the day of the week to set
*/
public void setDayOfWeek(String dayOfWeek) {
DayOfWeek = dayOfWeek;
}
/**
*Get the latitude of this AP
<