Commit 5efd1cc3 authored by José Soares's avatar José Soares

Bug found: trying to parse a null when receiving A and C

parent dbe1b634
......@@ -30,5 +30,4 @@ dependencies {
compile files('libs/opencsv-3.9.jar')
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'org.jsoup:jsoup:1.8.3'
}
......@@ -12,6 +12,7 @@ interface CManagerInterface {
//Map getC(in String [] peerList); //String [] to ArrayList<String>
Map getAvailability(in List<String> peerList);
Map getCentrality(in List<String> peerList);
Map getSimilarity(in List<String> peerList);
//ContextualManagerSimilarity
//double [] getI(TYPE, NODE);
......
......@@ -101,8 +101,6 @@ public class ContextualManagerMainActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.cmumobile_ma_layout);
backupDB();
FragmentManager manager = getFragmentManager();
//Asks user for permission to get usage stats
......@@ -213,7 +211,7 @@ public class ContextualManagerMainActivity extends Activity {
@Override
protected void onDestroy() {
backupDB(); //todo eliminate the backup function
backupDB(this); //todo eliminate the backup function
super.onDestroy();
}
......@@ -222,13 +220,13 @@ public class ContextualManagerMainActivity extends Activity {
* database tables this method creates a backup in a place that the database is visible.
* (main folder of the android device)
*/
private void backupDB() {
public static void backupDB(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String DB_PATH = this.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
String DB_PATH = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
String currentDBPath = "contextualmanager.db";
String backupDBPath = "contextualmanagerbackup.db";
File currentDB = new File(DB_PATH, currentDBPath);
......
......@@ -3,6 +3,8 @@ package com.senception.contextualmanager.communication;
import android.content.Context;
import android.net.wifi.p2p.WifiP2pDevice;
import android.util.Log;
import com.senception.contextualmanager.activities.ContextualManagerMainActivity;
import com.senception.contextualmanager.databases.ContextualManagerDataSource;
import com.senception.contextualmanager.modals.ContextualManagerAP;
import com.senception.contextualmanager.security.MacSecurity;
......@@ -47,8 +49,21 @@ public class ContextualManagerReceive implements WifiP2pListener.TxtRecordAvaila
public void onTxtRecordAvailable(String fullDomainName, Map<String, String> txtRecordMap, WifiP2pDevice srcDevice) {
if(txtRecordMap != null && txtRecordMap.size() != 0) {
double A = Double.parseDouble(txtRecordMap.get(Identity.AVAILABILITY));
double C = Double.parseDouble(txtRecordMap.get(Identity.CENTRALITY));
Log.d("teste", "txtRecord: " + txtRecordMap.toString());
String a = txtRecordMap.get(Identity.AVAILABILITY);
String c = txtRecordMap.get(Identity.CENTRALITY);
Log.d("teste", "a: " + a + " c : " + c);
double A = 0;
double C = 0;
//if(a != null && c != null ) {
A = Double.parseDouble(a);
C = Double.parseDouble(txtRecordMap.get(Identity.CENTRALITY));
//else{
Log.d("teste", "Device Name: " + srcDevice.deviceName);
//double I = Double.parseDouble(txtRecordMap.get(Identity.SIMILARITY));
String hashSrcDeviceBSSID = MacSecurity.md5Hash(srcDevice.deviceAddress);
//if it's the first time we see this peer we save it
......@@ -60,6 +75,7 @@ public class ContextualManagerReceive implements WifiP2pListener.TxtRecordAvaila
//TODO peer.setLongitude(longitude);
peer.setAvailability(A);
peer.setCentrality(C);
//peer.setSimilarity(I);
peer.setNumEncounters(1);
peer.setStartEncounter((int)(System.currentTimeMillis()/1000)); //time in seconds System.currentTimeMillis()/1000
dataSource.registerNewPeers(peer, ContextualManagerService.checkWeek("peers"));
......@@ -69,10 +85,12 @@ public class ContextualManagerReceive implements WifiP2pListener.TxtRecordAvaila
peer.setBSSID(hashSrcDeviceBSSID);
peer.setAvailability(A);
peer.setCentrality(C);
//peer.setSimilarity(I);
//TODO peer.setLatitude(latitude);
//TODO peer.setLongitude(longitude);
dataSource.updatePeer(peer, ContextualManagerService.checkWeek("peers"));
}
ContextualManagerMainActivity.backupDB(mContext);
}
else{
Log.d(TAG, "The txt Record was null or empty");
......
......@@ -41,25 +41,30 @@ public class ContextualManagerSend {
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
double A;
double C;
ContextualManagerDataSource dataSource = new ContextualManagerDataSource(mContext);
dataSource.openDB(true);
if (dataSource.hasPeer(MacSecurity.md5Hash("self"), ContextualManagerService.checkWeek("peers"))) {
ContextualManagerAP self = dataSource.getPeer(MacSecurity.md5Hash("self"), ContextualManagerService.checkWeek("peers"));
A = self.getAvailability();
C = self.getCentrality();
double A = self.getAvailability();
double C = self.getCentrality();
Log.d("teste", "A: " + A + " C: " + C);
//double I = self.getSimilarity();
WifiP2pTxtRecord.setRecord(mContext, Identity.AVAILABILITY, String.valueOf(A));
WifiP2pTxtRecord.setRecord(mContext, Identity.CENTRALITY, String.valueOf(C));
String AToSend = String.valueOf(A);
String CToSend = String.valueOf(C);
Log.d("teste", "AToSend :" + AToSend + "CToSend: " + CToSend );
//String IToSend = String.valueOf(I); //Todo: Find bug - Find out when can A,C or I be null
WifiP2pTxtRecord.setRecord(mContext, Identity.AVAILABILITY, AToSend);
WifiP2pTxtRecord.setRecord(mContext, Identity.CENTRALITY, CToSend);
//WifiP2pTxtRecord.setRecord(mContext, Identity.SIMILARITY, IToSend);
Log.d(TAG, "Sent A, C and I.");
//Todo WifiP2pTxtRecord.setRecord(mContext, Identity.SIMILARITY, "0");
}
else{
Log.d(TAG, "Table is still empty so we can't send the availability and centrality.");
}
}
}, 0, 1*60*1000); //todo every 5 min
}, 0, 10*1000); //todo every 5 min
}
}
......@@ -140,6 +140,7 @@ public class ContextualManagerDataSource {
ContextualManagerSQLiteHelper.COLUMN_LONGITUDE,
ContextualManagerSQLiteHelper.COLUMN_AVAILABILITY,
ContextualManagerSQLiteHelper.COLUMN_CENTRALITY,
ContextualManagerSQLiteHelper.COLUMN_SIMILARITY,
ContextualManagerSQLiteHelper.COLUMN_NUM_ENCOUNTERS,
ContextualManagerSQLiteHelper.COLUMN_START_ENCOUNTER,
ContextualManagerSQLiteHelper.COLUMN_END_ENCOUNTER,
......@@ -225,12 +226,12 @@ public class ContextualManagerDataSource {
ap.setLongitude(cursor.getDouble(4)); // longitude
ap.setAvailability(cursor.getDouble(5)); // a
ap.setCentrality(cursor.getDouble(6)); // c
ap.setNumEncounters(cursor.getInt(7)); // num enc
ap.setStartEncounter(cursor.getInt(8)); // start enc
ap.setEndEncounter(cursor.getInt(9)); // end enc
ap.setAvgEncounterDuration(cursor.getDouble(10)); // avg encounter duration
ap.setIsConnected(cursor.getInt(11)); // is connected
ap.setSimilarity(cursor.getDouble(7)); // i
ap.setNumEncounters(cursor.getInt(8)); // num enc
ap.setStartEncounter(cursor.getInt(9)); // start enc
ap.setEndEncounter(cursor.getInt(10)); // end enc
ap.setAvgEncounterDuration(cursor.getDouble(11)); // avg encounter duration
ap.setIsConnected(cursor.getInt(12)); // is connected
return ap;
}
......@@ -312,6 +313,7 @@ public class ContextualManagerDataSource {
values.put(ContextualManagerSQLiteHelper.COLUMN_LONGITUDE, ap.getLongitude());
values.put(ContextualManagerSQLiteHelper.COLUMN_AVAILABILITY, ap.getAvailability());
values.put(ContextualManagerSQLiteHelper.COLUMN_CENTRALITY, ap.getCentrality());
values.put(ContextualManagerSQLiteHelper.COLUMN_SIMILARITY, ap.getSimilarity());
values.put(ContextualManagerSQLiteHelper.COLUMN_NUM_ENCOUNTERS, ap.getNumEncounters());
values.put(ContextualManagerSQLiteHelper.COLUMN_START_ENCOUNTER, ap.getStartEncounter());
values.put(ContextualManagerSQLiteHelper.COLUMN_END_ENCOUNTER, ap.getEndEncounter());
......@@ -419,6 +421,7 @@ public class ContextualManagerDataSource {
values.put(ContextualManagerSQLiteHelper.COLUMN_LONGITUDE, ap.getLongitude());
values.put(ContextualManagerSQLiteHelper.COLUMN_AVAILABILITY, ap.getAvailability());
values.put(ContextualManagerSQLiteHelper.COLUMN_CENTRALITY, ap.getCentrality());
values.put(ContextualManagerSQLiteHelper.COLUMN_SIMILARITY, ap.getSimilarity());
values.put(ContextualManagerSQLiteHelper.COLUMN_NUM_ENCOUNTERS, ap.getNumEncounters());
values.put(ContextualManagerSQLiteHelper.COLUMN_START_ENCOUNTER, ap.getStartEncounter());
values.put(ContextualManagerSQLiteHelper.COLUMN_END_ENCOUNTER, ap.getEndEncounter());
......@@ -539,7 +542,6 @@ public class ContextualManagerDataSource {
* Function getResourceUsage
* Gets a ContextualManagerPhysicalUsage saved in the database.
* @param type the physical resource usage type
* @param tableName the name of the table
* @return pru the physical resource usage
*/
public ContextualManagerPhysicalUsage getResourceUsage(String type){
......
......@@ -89,8 +89,9 @@ public class ContextualManagerSQLiteHelper extends SQLiteOpenHelper {
public static final String COLUMN_APP_CATEGORY = "appcategory";
//WEIGHTS
public static final String COLUMN_AVAILABILITY = "a"; //Affinity network level of a node (measures node's centrality/popularity).
public static final String COLUMN_CENTRALITY = "c"; // Internal Usage weight of a node (measures the availability of the node)
public static final String COLUMN_AVAILABILITY = "a"; // Internal Usage weight of a node (measures the availability of the node)
public static final String COLUMN_CENTRALITY = "c"; // Affinity network level of a node (measures node's centrality/popularity).
public static final String COLUMN_SIMILARITY = "i"; // Similarity of a node (measures the node's similarity).
/* Measures the (eigenvector) similarity between the selected resource of node I and j.
For instance, I can provide a measure of battery similarity over time between nodes.
......@@ -190,6 +191,7 @@ public class ContextualManagerSQLiteHelper extends SQLiteOpenHelper {
+ COLUMN_LONGITUDE + " integer, "
+ COLUMN_AVAILABILITY + " text, "
+ COLUMN_CENTRALITY + " text, "
+ COLUMN_SIMILARITY + " text, "
+ COLUMN_NUM_ENCOUNTERS + " integer, "
+ COLUMN_START_ENCOUNTER + " integer, "
+ COLUMN_END_ENCOUNTER + " integer, "
......@@ -206,6 +208,7 @@ public class ContextualManagerSQLiteHelper extends SQLiteOpenHelper {
+ COLUMN_LONGITUDE + " integer, "
+ COLUMN_AVAILABILITY + " text, "
+ COLUMN_CENTRALITY + " text, "
+ COLUMN_SIMILARITY + " text, "
+ COLUMN_NUM_ENCOUNTERS + " integer, "
+ COLUMN_START_ENCOUNTER + " integer, "
+ COLUMN_END_ENCOUNTER + " integer, "
......@@ -222,6 +225,7 @@ public class ContextualManagerSQLiteHelper extends SQLiteOpenHelper {
+ COLUMN_LONGITUDE + " integer, "
+ COLUMN_AVAILABILITY + " text, "
+ COLUMN_CENTRALITY + " text, "
+ COLUMN_SIMILARITY + " text, "
+ COLUMN_NUM_ENCOUNTERS + " integer, "
+ COLUMN_START_ENCOUNTER + " integer, "
+ COLUMN_END_ENCOUNTER + " integer, "
......@@ -238,6 +242,7 @@ public class ContextualManagerSQLiteHelper extends SQLiteOpenHelper {
+ COLUMN_LONGITUDE + " integer, "
+ COLUMN_AVAILABILITY + " text, "
+ COLUMN_CENTRALITY + " text, "
+ COLUMN_SIMILARITY + " text, "
+ COLUMN_NUM_ENCOUNTERS + " integer, "
+ COLUMN_START_ENCOUNTER + " integer, "
+ COLUMN_END_ENCOUNTER + " integer, "
......@@ -254,6 +259,7 @@ public class ContextualManagerSQLiteHelper extends SQLiteOpenHelper {
+ COLUMN_LONGITUDE + " integer, "
+ COLUMN_AVAILABILITY + " text, "
+ COLUMN_CENTRALITY + " text, "
+ COLUMN_SIMILARITY + " text, "
+ COLUMN_NUM_ENCOUNTERS + " integer, "
+ COLUMN_START_ENCOUNTER + " integer, "
+ COLUMN_END_ENCOUNTER + " integer, "
......@@ -270,6 +276,7 @@ public class ContextualManagerSQLiteHelper extends SQLiteOpenHelper {
+ COLUMN_LONGITUDE + " integer, "
+ COLUMN_AVAILABILITY + " text, "
+ COLUMN_CENTRALITY + " text, "
+ COLUMN_SIMILARITY + " text, "
+ COLUMN_NUM_ENCOUNTERS + " integer, "
+ COLUMN_START_ENCOUNTER + " integer, "
+ COLUMN_END_ENCOUNTER + " integer, "
......@@ -286,6 +293,7 @@ public class ContextualManagerSQLiteHelper extends SQLiteOpenHelper {
+ COLUMN_LONGITUDE + " integer, "
+ COLUMN_AVAILABILITY + " text, "
+ COLUMN_CENTRALITY + " text, "
+ COLUMN_SIMILARITY + " text, "
+ COLUMN_NUM_ENCOUNTERS + " integer, "
+ COLUMN_START_ENCOUNTER + " integer, "
+ COLUMN_END_ENCOUNTER + " integer, "
......
......@@ -42,7 +42,7 @@ public class ContextualManagerAvailability {
A = sumArrays(A, rList.get(i)); //A = R0 //A = A + R2 + R3 --> R1+R2
}
}
Log.d("teste", "A calculated: " + A.toString()); // todo delete log
return A;
}
......@@ -72,7 +72,7 @@ public class ContextualManagerAvailability {
}
//30+30 = 60 --> 60/currentHour
else{
res.add((usagePerHour1.get(i) + usagePerHour2.get(i)) / System.currentTimeMillis()/1000); //todo optimize without divide for the hour in the sumArrays method
res.add((usagePerHour1.get(i) + usagePerHour2.get(i)) / System.currentTimeMillis()/1000); //todo optimize without dividing for the hour in the sumArrays method
}
}
return res;
......@@ -92,7 +92,6 @@ public class ContextualManagerAvailability {
ArrayList<Integer> e2 = multiplyArrays(e, e); // e square
ArrayList<Integer> e2Cpu = multiplyArrays(e2, cpu); // e2 * cpu
ArrayList<Integer> memStor = multiplyArrays(mem, storage); // mem * storage
ArrayList<Integer> r = multiplyArrays(e2Cpu, memStor);
return r;
}
......
package com.senception.contextualmanager.inference;
import com.senception.contextualmanager.databases.ContextualManagerDataSource;
import com.senception.contextualmanager.modals.ContextualManagerAP;
import java.util.ArrayList;
import static com.senception.contextualmanager.services.ContextualManagerService.checkWeek;
/**
* Copyright (C) 2016 Senception Lda
* Author(s): Igor dos Santos - [email protected] *
......@@ -14,5 +21,21 @@ package com.senception.contextualmanager.inference;
*/
// TODO I: similarity
public class ContextualManagerSimilarity {
//I = numEnc*AvgDuration
public static double calculateI(ContextualManagerDataSource dataSource) {
double similarity = 0;
//get peer list
ArrayList<ContextualManagerAP> peerList;
if(!dataSource.isTableEmpty(checkWeek("peers"))) {
peerList = dataSource.getAllPeers(checkWeek("peers"));
for (ContextualManagerAP peer : peerList) {
double numEncounters = peer.getNumEncounters();
double avgEncDur = peer.getAvgEncounterDuration();
similarity = numEncounters*avgEncDur;
}
}
return similarity;
}
}
......@@ -24,6 +24,7 @@ public class ContextualManagerAP {
private double longitude;
private double Availability;
private double Centrality;
private double Similarity;
private int startEncounter;
private int endEncounter;
private int numEncounters;
......@@ -200,6 +201,20 @@ public class ContextualManagerAP {
Centrality = centrality;
}
/**
* Gets the similarity of this peer.
* @return the similarity
*/
public double getSimilarity(){return this.Similarity;}
/**
* Set the similarity of this peer.
* @rparam similarity the similarity to set.
*/
public void setSimilarity(double similarity){
this.Similarity = similarity;
}
/**
* Get the number of encounters of this peer.
* @return the numEncounters
......
......@@ -25,6 +25,7 @@ import com.senception.contextualmanager.databases.ContextualManagerDataSource;
import com.senception.contextualmanager.databases.ContextualManagerSQLiteHelper;
import com.senception.contextualmanager.inference.ContextualManagerCentrality;
import com.senception.contextualmanager.inference.ContextualManagerAvailability;
import com.senception.contextualmanager.inference.ContextualManagerSimilarity;
import com.senception.contextualmanager.modals.ContextualManagerAP;
import com.senception.contextualmanager.modals.ContextualManagerAppUsage;
import com.senception.contextualmanager.modals.ContextualManagerPhysicalUsage;
......@@ -87,7 +88,7 @@ public class ContextualManagerCaptureService extends Service {
super.onCreate();
dataSource = new ContextualManagerDataSource(this);
dataSource.openDB(true);
backupDB();
ContextualManagerMainActivity.backupDB(this);
/*
* Initializes the physical resource usage table in the DB
......@@ -268,12 +269,16 @@ public class ContextualManagerCaptureService extends Service {
/*Centrality Calculation:*/
double C = ContextualManagerCentrality.calculateC(dataSource);
/*Similarity Calculation*/
double I = ContextualManagerSimilarity.calculateI(dataSource);
/* Saves A and C into the database */
ContextualManagerAP mySelf = new ContextualManagerAP();
mySelf.setSSID("self");
mySelf.setBSSID(MacSecurity.md5Hash("self"));
mySelf.setAvailability(A);
mySelf.setCentrality(C);
mySelf.setSimilarity(I);
if(!dataSource.hasPeer(mySelf.getBSSID(), ContextualManagerService.checkWeek("peers"))) {
dataSource.registerNewPeers(mySelf, ContextualManagerService.checkWeek("peers"));
}
......@@ -297,7 +302,7 @@ public class ContextualManagerCaptureService extends Service {
/* Captures the apps usage */
captureAppsUsage();
ContextualManagerMainActivity.backupDB(context);
}
else{ // if daily: Saves the usage percentage into the database
......@@ -404,38 +409,6 @@ public class ContextualManagerCaptureService extends Service {
return usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, start, end);
}
/**
* Since the database on the device is only visible through root, to check the
* database tables this method creates a backup in a place that the database is visible.
* (main folder of the android device)
*/
private void backupDB() {
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String DB_PATH = this.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
//String currentDBPath = "cmumobile.db";
String currentDBPath = "contextualmanager.db";
String backupDBPath = "contextualmanagerbackup.db";
File currentDB = new File(DB_PATH, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Log.d(TAG, "BACKUP DONE");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* TODO category
* Updates the category of each app in the apps list if there is internet connection
......
......@@ -120,6 +120,22 @@ public class ContextualManagerInterfaceService extends Service {
}
return hashMapCentrality;
}
@Override
public Map getSimilarity(List<String> peerList){
HashMap<String, Double> hashMapSimilarity = new HashMap<>();
for (int i = 0; i < peerList.size(); i++){
//peerList[i] == peerId
if(dataSource.hasPeer(String.valueOf(peerList.get(i)), ContextualManagerService.checkWeek("peers"))){
ContextualManagerAP peer = dataSource.getPeer(String.valueOf(peerList.get(i)), ContextualManagerService.checkWeek("peers"));
hashMapSimilarity.put(peerList.get(i), peer.getSimilarity());
}
else
hashMapSimilarity.put(peerList.get(i), null); //if the peer id given was not found on the db then we can't provide it's centrality
}
return hashMapSimilarity;
}
};
@Override
......
......@@ -253,6 +253,7 @@ public class ContextualManagerService extends Service{
ap.setLongitude(longitude);
ap.setAvailability(0.0);
ap.setCentrality(0.0);
ap.setSimilarity(0.0);
ap.setNumEncounters(1);
ap.setStartEncounter((int)(System.currentTimeMillis()/1000)); //time in seconds System.currentTimeMillis()/1000
ap.setEndEncounter((int)(System.currentTimeMillis()/1000));
......
......@@ -5,6 +5,7 @@
<string name="background">A correr em background...</string>
<string name="getInfo">A Obter Informação, Por Favor Aguarde...</string>
<string name="stopservice">Parar o Serviço</string>
<string name="options">Options</string>
<string name="wifiOff">Wifi Desligado</string>
<string name="wifiOn">Wifi Ligado</string>
<string name="wifiDown">Falha na conexão Wifi</string>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment