Commit a0fe2af8 authored by José Soares's avatar José Soares

Finished cleaning project (still need to complete to-do's). Replaced weights...

Finished cleaning project (still need to complete to-do's). Replaced weights table with 1 row in the peers table.
parent 5df2fe1e
......@@ -10,8 +10,8 @@ interface CManagerInterface {
double [] getCentrality(in String [] peerList);
//ContextualManagerSimilarity
//int [] getI(TYPE, NODE);
//double [] getI(TYPE, NODE);
//List<String> getAll(in);
//"A.C"
//"A.C.I"
}
\ No newline at end of file
......@@ -48,9 +48,11 @@ public class ContextualManagerReceive implements WifiP2pListener.TxtRecordAvaila
@Override
public void onTxtRecordAvailable(String fullDomainName, Map<String, String> txtRecordMap, WifiP2pDevice srcDevice) {
Log.d("teste", "Trying to receive");
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", "received A : " + A + "and C: " + C);
String hashSrcDeviceBSSID = MacSecurity.MD5hash(srcDevice.deviceAddress);
//if it's the first time we see this peer we save it
......
......@@ -5,7 +5,10 @@ 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.modals.ContextualManagerWeight;
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.WifiP2pTxtRecord;
......@@ -44,23 +47,25 @@ public class ContextualManagerSend {
public void run() {
double A;
double C;
Log.d("teste", "trying to send");
ContextualManagerDataSource dataSource = new ContextualManagerDataSource(mContext);
dataSource.openDB(true);
if (!dataSource.isTableEmpty(ContextualManagerSQLiteHelper.TABLE_WEIGHTS)) {
if (dataSource.hasPeer(MacSecurity.MD5hash("self"), ContextualManagerService.checkWeek("peers"))) {
ContextualManagerWeight weight = dataSource.getWeight();
A = weight.getA();
C = weight.getC();
ContextualManagerAP self = dataSource.getPeer(MacSecurity.MD5hash("self"), ContextualManagerService.checkWeek("peers"));
A = self.getAvailability();
C = self.getCentrality();
WifiP2pTxtRecord.setRecord(mContext, Identity.AVAILABILITY, String.valueOf(A));
WifiP2pTxtRecord.setRecord(mContext, Identity.CENTRALITY, String.valueOf(C));
Log.d("teste", "sent A " + A + " and C " + C);
//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); //5 em 5 min
}, 0, 1*60*1000); //todo every 5 min
}
}
......@@ -325,10 +325,9 @@ public class ContextualManagerDataSource {
* Function registerNewResourceUsage
* Register a new Resource Usage in the application. It creates a new record on the ResourceUsage table, with the information passed as PhysicalResourceUSage.
* @param resUsg resource usage
* @param tableName name of the table on the database
* @return the row ID of the newly inserted row, or -1 if an error occurred.
*/
public long registerNewResourceUsage(ContextualManagerPhysicalUsage resUsg, String tableName) {
public long registerNewResourceUsage(ContextualManagerPhysicalUsage resUsg) {
ContentValues values = new ContentValues();
values.put(ContextualManagerSQLiteHelper.COLUMN_TYPE_OF_RESOURCE, resUsg.getResourceType().toString());
StringBuilder arrayToDatabase = new StringBuilder();
......@@ -340,17 +339,16 @@ public class ContextualManagerDataSource {
}
values.put(ContextualManagerSQLiteHelper.COLUMN_AVERAGE_USAGE_HOUR, arrayToDatabase.toString());
values.put(ContextualManagerSQLiteHelper.COLUMN_DAYOFTHEWEEK, String.valueOf(resUsg.getDayOfTheWeek()));
return db.insert(tableName, null, values);
return db.insert(ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE, null, values);
}
/**
* Function registerNewAppsUsage
* Register a new Apps Usage in the application. It creates a new record on the AppsUsage table, with the information passed as ContextualManagerAppUsage.
* @param appUsg resource usage
* @param tableName name of the table on the database
* @return the row ID of the newly inserted row, or -1 if an error occurred.
*/
public long registerNewAppUsage(ContextualManagerAppUsage appUsg, String tableName){
public long registerNewAppUsage(ContextualManagerAppUsage appUsg){
ContentValues values = new ContentValues();
values.put(ContextualManagerSQLiteHelper.COLUMN_APP_NAME, appUsg.getAppName());
values.put(ContextualManagerSQLiteHelper.COLUMN_APP_CATEGORY, appUsg.getAppCategory());
......@@ -363,7 +361,7 @@ public class ContextualManagerDataSource {
}
values.put(ContextualManagerSQLiteHelper.COLUMN_AVERAGE_USAGE_HOUR, arrayToDatabase.toString());
values.put(ContextualManagerSQLiteHelper.COLUMN_DAYOFTHEWEEK, appUsg.getDayOfTheWeek());
return db.insertOrThrow(tableName, null, values);
return db.insertOrThrow(ContextualManagerSQLiteHelper.TABLE_APPS_USAGE, null, values);
}
/**
......@@ -436,10 +434,9 @@ public class ContextualManagerDataSource {
* Update a resource usage already registered by the application.
* This modifies the corresponding averageUsage and day of the week in the resource usage table.
* @param pru physical resource usage.
* @param tableName name of the table on the database
* @return true, if successful.
*/
public boolean updateResourceUsage(ContextualManagerPhysicalUsage pru, String tableName){
public boolean updateResourceUsage(ContextualManagerPhysicalUsage pru){
String identifier = ContextualManagerSQLiteHelper.COLUMN_TYPE_OF_RESOURCE + "='" + pru.getResourceType() + "'" +" COLLATE NOCASE ";
ContentValues values = new ContentValues();
StringBuilder arrayToDatabase = new StringBuilder();
......@@ -453,20 +450,18 @@ public class ContextualManagerDataSource {
values.put(ContextualManagerSQLiteHelper.COLUMN_AVERAGE_USAGE_HOUR, arrayToDatabase.toString());
values.put(ContextualManagerSQLiteHelper.COLUMN_DAYOFTHEWEEK, String.valueOf(pru.getDayOfTheWeek()));
int rows = db.update(tableName, values, identifier, null);
int rows = db.update(ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE, values, identifier, null);
return rows != 0 ? true : false;
}
/**
* TODO get the parameter "tablename" out -> unnecessary
* Function updateAppUsage
* Update an app usage already registered by the application.
* @param appUsg resource usage.
* @param tableName name of the table on the database
* @return true, if successful.
*/
public boolean updateAppUsage(ContextualManagerAppUsage appUsg, String tableName){
public boolean updateAppUsage(ContextualManagerAppUsage appUsg){
String identifier = ContextualManagerSQLiteHelper.COLUMN_APP_NAME + "='" + appUsg.getAppName() + "'" +" COLLATE NOCASE ";
ContentValues values = new ContentValues();
StringBuilder arrayToDatabase = new StringBuilder();
......@@ -479,7 +474,7 @@ public class ContextualManagerDataSource {
values.put(ContextualManagerSQLiteHelper.COLUMN_AVERAGE_USAGE_HOUR, arrayToDatabase.toString());
values.put(ContextualManagerSQLiteHelper.COLUMN_APP_CATEGORY, appUsg.getAppCategory());
values.put(ContextualManagerSQLiteHelper.COLUMN_DAYOFTHEWEEK, String.valueOf(appUsg.getDayOfTheWeek()));
int rows = db.update(tableName, values, identifier, null);
int rows = db.update(ContextualManagerSQLiteHelper.TABLE_APPS_USAGE, values, identifier, null);
return rows != 0 ? true : false;
}
......@@ -547,9 +542,9 @@ public class ContextualManagerDataSource {
* @param tableName the name of the table
* @return pru the physical resource usage
*/
public ContextualManagerPhysicalUsage getResourceUsage(String type, String tableName){
public ContextualManagerPhysicalUsage getResourceUsage(String type){
ContextualManagerPhysicalUsage pru = null;
Cursor cursor = db.query(tableName, allColumnsResourceUsage, ContextualManagerSQLiteHelper.COLUMN_TYPE_OF_RESOURCE + "='" + type + "'"+ " COLLATE NOCASE ", null, null, null, null);
Cursor cursor = db.query(ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE, allColumnsResourceUsage, ContextualManagerSQLiteHelper.COLUMN_TYPE_OF_RESOURCE + "='" + type + "'"+ " COLLATE NOCASE ", null, null, null, null);
if (cursor.moveToFirst()){
pru = cursorResourceUsage(cursor);
}
......@@ -562,7 +557,6 @@ public class ContextualManagerDataSource {
*/
public ContextualManagerWeight getWeight (){
ContextualManagerWeight weight = null;
//Cursor cursor = db.query(tableName, allColumnsWeight, ContextualManagerSQLiteHelper.COLUMN_DATETIME + "='" + dateTime + "'"+ " COLLATE NOCASE ", null, null, null, null);
// query to get last row of the weight's table
String selectQuery = "SELECT * FROM " + ContextualManagerSQLiteHelper.TABLE_WEIGHTS + " ORDER BY " + ContextualManagerSQLiteHelper.COLUMN_DATETIME + " DESC LIMIT 1";
Cursor cursor = db.rawQuery(selectQuery, null);
......@@ -706,10 +700,6 @@ public class ContextualManagerDataSource {
return (DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM " + tableName + " WHERE " + ContextualManagerSQLiteHelper.COLUMN_BSSID + " = '" + bssid + "'"+ " COLLATE NOCASE ", null) == 0)? false : true;
}
public boolean hasWeight(int dayOfTheWeek) {
return (DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM " + ContextualManagerSQLiteHelper.TABLE_WEIGHTS + " WHERE " + ContextualManagerSQLiteHelper.COLUMN_DAYOFTHEWEEK + " = '" + dayOfTheWeek + "'", null) == 0)? false : true;
}
/**
* Function getBestAP
* Checks the APs registered by the application and available in the List of ScanResult, and the return the one with the highest rank.
......@@ -894,7 +884,6 @@ public class ContextualManagerDataSource {
String query = "Select * from " + tableName + " where " + columnName + " = '" + fieldValue + "'";
Cursor cursor = db.rawQuery(query, null);
//Log.d("RESOURCE", "" + cursor.getCount());
if(cursor.getCount() <= 0){
cursor.close();
......
......@@ -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());
Log.d("teste", "A calculated: " + A.toString()); // todo delete log
return A;
}
......
......@@ -59,7 +59,7 @@ public class ContextualManagerCentrality {
peerList = dataSource.getAllPeers(checkWeek("peers"));
for (ContextualManagerAP peer : peerList) {
//Todo only if peer is connected (A->1)
//Todo only if peer is connected (future)
numEncounters = peer.getNumEncounters();
avgEncDur = peer.getAvgEncounterDuration();
centrality += numEncounters*avgEncDur/distance;
......
......@@ -36,18 +36,18 @@ import com.senception.contextualmanager.wifi.p2p.WifiP2pListenerManager;
@SuppressLint("NewApi")
public class ContextualManagerWifiP2P extends BroadcastReceiver implements WifiP2pListener.TxtRecordAvailable {
//public static final String TAG = "WIFIP2P ---->";
public static final String TAG = ContextualManagerWifiP2P.class.getSimpleName();
private static WifiP2pManager manager;
private static Channel channel;
private ContextualManagerService service;
ArrayList<ContextualManagerAP> peersList = new ArrayList<ContextualManagerAP>();
ArrayList<ContextualManagerAP> arrayPersence = new ArrayList<ContextualManagerAP>();
public String peerName;
//WifiDataExchangeRead wifiDataExchange;
static Context mContext = null;
@SuppressWarnings("static-access")
public ContextualManagerWifiP2P(WifiP2pManager manager, Channel channel,
/**
* Constructor
*/
public ContextualManagerWifiP2P(WifiP2pManager manager, Channel channel,
final ContextualManagerService service) {
super();
this.manager = manager;
......@@ -67,16 +67,7 @@ public class ContextualManagerWifiP2P extends BroadcastReceiver implements WifiP
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
/*if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
//TODO
} else {
//TODO
}
//Log.d(TAG, "P2P state changed - " + state);
} else */if (manager != null) {
Log.d("teste", "making a scan");
if (manager != null) {
manager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
@Override
......@@ -85,7 +76,6 @@ public class ContextualManagerWifiP2P extends BroadcastReceiver implements WifiP
for(WifiP2pDevice dev : peers.getDeviceList()){
ContextualManagerAP peerfound = new ContextualManagerAP();
peerfound.setSSID(dev.deviceName);
Log.d("teste", "peerfound: " + dev.deviceName);
peerfound.setBSSID(dev.deviceAddress);
......@@ -95,17 +85,9 @@ public class ContextualManagerWifiP2P extends BroadcastReceiver implements WifiP
//wifiDataExchange = new WifiDataExchangeRead(mContext, peersList);
service.notifyOnPeersFound(peersList);
service.discoveredPeers(peersList);
//Log.d(TAG,String.format("PeerListListener: %d peers available, updating device list", peers.getDeviceList().size()));
}
});
}
//Log.d(TAG, "P2P peers changed");
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
}
}
/**
......
......@@ -13,6 +13,8 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
......@@ -26,6 +28,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.modals.ContextualManagerAP;
import com.senception.contextualmanager.modals.ContextualManagerWeight;
import com.senception.contextualmanager.modals.ContextualManagerAppUsage;
import com.senception.contextualmanager.modals.ContextualManagerPhysicalUsage;
......@@ -34,6 +37,7 @@ import com.senception.contextualmanager.physical_usage.ContextualManagerCPU;
import com.senception.contextualmanager.physical_usage.ContextualManagerMemory;
import com.senception.contextualmanager.physical_usage.ContextualManagerPhysicalResourceType;
import com.senception.contextualmanager.physical_usage.ContextualManagerStorage;
import com.senception.contextualmanager.security.MacSecurity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
......@@ -43,6 +47,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
......@@ -172,7 +177,7 @@ public class ContextualManagerCaptureService extends Service {
*/
private void initializeAppsTable(ContextualManagerAppUsage app) {
if (!dataSource.rowExists(ContextualManagerSQLiteHelper.TABLE_APPS_USAGE, app.getAppName(), ContextualManagerSQLiteHelper.COLUMN_APP_NAME)) {
dataSource.registerNewAppUsage(app, ContextualManagerSQLiteHelper.TABLE_APPS_USAGE);
dataSource.registerNewAppUsage(app);
}
}
......@@ -182,7 +187,7 @@ public class ContextualManagerCaptureService extends Service {
*/
private void initializeResourceTable(ContextualManagerPhysicalUsage pru) {
if (!dataSource.rowExists(ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE, pru.getResourceType().toString(), ContextualManagerSQLiteHelper.COLUMN_TYPE_OF_RESOURCE)) {
dataSource.registerNewResourceUsage(pru, ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE);
dataSource.registerNewResourceUsage(pru);
}
}
......@@ -223,7 +228,7 @@ public class ContextualManagerCaptureService extends Service {
midnight.set(Calendar.HOUR_OF_DAY, 0);
/*
* Todo
* Todo to start at midnight
* To use after tests: (to start allways at midnight)
* midnight.add(Calendar.DAY_OF_MONTH, 1);
*/
......@@ -270,31 +275,38 @@ public class ContextualManagerCaptureService extends Service {
rList.add(ContextualManagerAvailability.calculateR(energy.getUsagePerHour(), cpu.getUsagePerHour(), memory.getUsagePerHour(), storage.getUsagePerHour()));
// Calculates the A - availability (sum of all Rs) every hour (for tests: min)
availability = ContextualManagerAvailability.calculateA(rList);
int currentMinute = currentTime.get(Calendar.MINUTE); //todo change to hourly
double A = availability.get(currentMinute);
/*Centrality Calculation:*/
double C = ContextualManagerCentrality.calculateC(dataSource);
/* Saves A and C into the database */
int dayOfTheWeek = day.get(Calendar.DAY_OF_WEEK);
int currentMinute = currentTime.get(Calendar.MINUTE); //todo change to hourly
ContextualManagerAP mySelf = new ContextualManagerAP();
mySelf.setSSID("self");
mySelf.setBSSID(MacSecurity.MD5hash("self"));
mySelf.setAvailability(A);
mySelf.setCentrality(C);
if(!dataSource.hasPeer(mySelf.getBSSID(), ContextualManagerService.checkWeek("peers"))) {
dataSource.registerNewPeers(mySelf, ContextualManagerService.checkWeek("peers"));
}
else {
dataSource.updatePeer(mySelf, ContextualManagerService.checkWeek("peers"));
}
/*
ContextualManagerWeight weight = new ContextualManagerWeight(dayOfTheWeek);
double A = availability.get(currentMinute);
weight.setA(A);
weight.setC(C);
weight.updateDateTime();
weight.setDayOfTheWeek(newDayOfTheWeek);
//todo check functionality of weight table
if (!dataSource.hasWeight(newDayOfTheWeek)){
if (dataSource.isTableEmpty(ContextualManagerSQLiteHelper.TABLE_WEIGHTS)){
dataSource.registerWeight(weight);
}
else {
//boolean updated =
dataSource.updateWeight(weight);
/*if(!updated){
dataSource.registerWeight(weight);
}*/
}
}*/
/* Captures the apps usage */
captureAppsUsage();
......@@ -304,19 +316,19 @@ public class ContextualManagerCaptureService extends Service {
/*Saves the 4 physical resource usage into the database*/
energy.setDayOfTheWeek(String.valueOf(newDayOfTheWeek));
dataSource.updateResourceUsage(energy, ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE);
dataSource.updateResourceUsage(energy);
cpu.setDayOfTheWeek(String.valueOf(newDayOfTheWeek));
dataSource.updateResourceUsage(cpu, ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE);
dataSource.updateResourceUsage(cpu);
memory.setDayOfTheWeek(String.valueOf(newDayOfTheWeek));
dataSource.updateResourceUsage(memory, ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE);
dataSource.updateResourceUsage(memory);
storage.setDayOfTheWeek(String.valueOf(newDayOfTheWeek));
dataSource.updateResourceUsage(storage, ContextualManagerSQLiteHelper.TABLE_RESOURCE_USAGE);
dataSource.updateResourceUsage(storage);
/*Saves the apps usage into the database*/
for (ContextualManagerAppUsage app: apps){
boolean updated = dataSource.updateAppUsage(app, ContextualManagerSQLiteHelper.TABLE_APPS_USAGE);
boolean updated = dataSource.updateAppUsage(app);
if (!updated){
dataSource.registerNewAppUsage(app, ContextualManagerSQLiteHelper.TABLE_APPS_USAGE);
dataSource.registerNewAppUsage(app);
}
}
}
......@@ -438,7 +450,7 @@ public class ContextualManagerCaptureService extends Service {
}
/**
* TODO
* TODO category
* Updates the category of each app in the apps list if there is internet connection
* and the respective app has a category affiliated in google play store -- To-complete
* https://stackoverflow.com/questions/10710442/how-to-get-category-for-each-app-on-device-on-android
......
......@@ -35,6 +35,7 @@ import java.util.Date;
*/
public class ContextualManagerInterfaceService extends Service {
private static final String TAG = ContextualManagerInterfaceService.class.getSimpleName();
private static ContextualManagerDataSource dataSource;
final CManagerInterface.Stub mBinder = new CManagerInterface.Stub(){
......@@ -44,7 +45,7 @@ public class ContextualManagerInterfaceService extends Service {
double [] availability = new double [peerList.length];
for (int i = 0; i < peerList.length; i++){
//peerList[i] == peerId
if(dataSource.hasPeer(peerList[i], ContextualManagerService.checkWeek("peers"))){
ContextualManagerAP peer = dataSource.getPeer(peerList[i], ContextualManagerService.checkWeek("peers"));
availability[i] = peer.getAvailability();
......@@ -72,13 +73,6 @@ public class ContextualManagerInterfaceService extends Service {
}
};
public class LocalBinder extends Binder {
public ContextualManagerInterfaceService getService(){
return ContextualManagerInterfaceService.this;
}
}
@Override
public void onCreate() {
......@@ -94,10 +88,6 @@ public class ContextualManagerInterfaceService extends Service {
return mBinder;
}
public void stopForeGround(){
stopForeground(true);
}
@Override
public void onDestroy() {
super.onDestroy();
......
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