forked from community/device-mgt-plugins
Increased the battery data retrieving sequence. Completed network data reading implementation. The data publishing interval is changed to 1000ms.revert-dabc3590
parent
49ee7c2986
commit
e0afb8d205
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.iot.android.sense.event.streams.battery;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import org.wso2.carbon.iot.android.sense.util.SenseDataHolder;
|
||||
|
||||
public class BatteryReaderService extends IntentService {
|
||||
|
||||
private Context context;
|
||||
|
||||
public BatteryReaderService() {
|
||||
super("BatteryReaderService");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
|
||||
intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
|
||||
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
|
||||
Intent intent1 = registerReceiver(null, intentFilter);
|
||||
|
||||
Log.i("Battery Data", String.valueOf(intent1.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)));
|
||||
if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())) {
|
||||
SenseDataHolder.getBatteryDataHolder().add(new BatteryData(BatteryData.State.OK));
|
||||
} else if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) {
|
||||
SenseDataHolder.getBatteryDataHolder().add(new BatteryData(BatteryData.State.LOW));
|
||||
} else {
|
||||
SenseDataHolder.getBatteryDataHolder().add(new BatteryData(intent1));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.iot.android.sense.event.streams.data;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.TrafficStats;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
import org.wso2.carbon.iot.android.sense.util.SenseDataHolder;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class to read data sent and received by the device.
|
||||
*/
|
||||
public class NetworkDataReader extends AsyncTask<Void, Void, Long> {
|
||||
|
||||
private NetworkData networkData;
|
||||
private Context context;
|
||||
private Handler mHandler = new Handler();
|
||||
private long mStartRX = 0;
|
||||
private long mStartTX = 0;
|
||||
private final String WIFI = "WIFI";
|
||||
private final String MOBILE = "MOBILE";
|
||||
private String connectionType;
|
||||
|
||||
public NetworkDataReader(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Long doInBackground(Void... voids) {
|
||||
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
networkData = new NetworkData();
|
||||
|
||||
if (getConnectionType(connectivityManager, ConnectivityManager.TYPE_WIFI)) {
|
||||
connectionType = WIFI;
|
||||
} else if (getConnectionType(connectivityManager, ConnectivityManager.TYPE_MOBILE)) {
|
||||
connectionType = MOBILE;
|
||||
}
|
||||
|
||||
mStartRX = TrafficStats.getTotalRxBytes();
|
||||
mStartTX = TrafficStats.getTotalTxBytes();
|
||||
if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
|
||||
Log.e("ERROR", "Not connected.");
|
||||
} else {
|
||||
mHandler.postDelayed(mRunnable, 10000);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect data sent and received with in 10 second time frames.
|
||||
*/
|
||||
private final Runnable mRunnable = new Runnable() {
|
||||
public void run() {
|
||||
long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
|
||||
long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
|
||||
Log.i("Usage: ", String.valueOf(rxBytes) + " " + String.valueOf(txBytes) + " " + System.currentTimeMillis());
|
||||
networkData.setType(connectionType);
|
||||
networkData.setTimeStamp(new Date().getTime());
|
||||
networkData.setDataSent(txBytes);
|
||||
networkData.setDataReceived(rxBytes);
|
||||
SenseDataHolder.getNetworkDataHolder().add(networkData);
|
||||
mHandler.postDelayed(mRunnable, 10000);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the type of the connection currently have.
|
||||
*/
|
||||
private boolean getConnectionType(ConnectivityManager manager, Integer type) {
|
||||
NetworkInfo networkInfo = manager.getNetworkInfo(type);
|
||||
return networkInfo.isConnected();
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.iot.android.sense.event.streams.data;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import org.wso2.carbon.iot.android.sense.util.SenseDataHolder;
|
||||
|
||||
public class NetworkDataReceiver extends BroadcastReceiver{
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
long sent = android.net.TrafficStats.getTotalTxBytes();
|
||||
long received = android.net.TrafficStats.getTotalRxBytes();
|
||||
Log.d("NetworkData :", "Received: " + sent + " Received : " + received);
|
||||
|
||||
NetworkData networkData = new NetworkData(received, sent);
|
||||
|
||||
SenseDataHolder.getNetworkDataHolder().add(networkData);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue