Skip to content

Commit

Permalink
Updates to DataLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpMaster committed Jul 1, 2016
1 parent 28c5b70 commit 7056f83
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class BluetoothLeService extends Service {

private final static String TAG = BluetoothLeService.class.getSimpleName();
private static final boolean autoConnect = true;
private static final boolean DEBUG = true;
private static final boolean DEBUG = false;

private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
Expand All @@ -58,7 +58,6 @@ public static IntentFilter makeBluetoothUpdateIntentFilter() {
intentFilter.addAction(Constants.ACTION_BLUETOOTH_DATA_AVAILABLE);
intentFilter.addAction(Constants.ACTION_WHEEL_DATA_AVAILABLE);
intentFilter.addAction(Constants.ACTION_REQUEST_SERIAL_DATA);
intentFilter.addAction(Constants.ACTION_PEBBLE_SERVICE_STARTED);
return intentFilter;
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/cooper/wheellog/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class Constants {
public static final String ACTION_BLUETOOTH_DATA_AVAILABLE = "com.cooper.wheellog.bluetoothDataAvailable";
public static final String ACTION_WHEEL_DATA_AVAILABLE = "com.cooper.wheellog.wheelDataAvailable";
public static final String ACTION_REQUEST_SERIAL_DATA = "com.cooper.wheellog.requestSerialData";
public static final String ACTION_PEBBLE_SERVICE_STARTED = "com.cooper.wheellog.pebbleServiceStarted";
public static final String ACTION_LOGGING_SERVICE_STARTED = "com.cooper.wheellog.loggingServiceStarted";
// public static final String ACTION_PEBBLE_SERVICE_STARTED = "com.cooper.wheellog.pebbleServiceStarted";
// public static final String ACTION_LOGGING_SERVICE_STARTED = "com.cooper.wheellog.loggingServiceStarted";

public static final UUID PEBBLE_APP_UUID = UUID.fromString("185c8ae9-7e72-451a-a1c7-8f1e81df9a3d");

Expand Down
59 changes: 33 additions & 26 deletions app/src/main/java/com/cooper/wheellog/DataLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,23 @@ public IBinder onBind(Intent intent) {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
instance = this;
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.UK);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
registerReceiver(mBluetoothUpdateReceiver, BluetoothLeService.makeBluetoothUpdateIntentFilter());

File root = android.os.Environment.getExternalStorageDirectory();
//LOGI("\nExternal file system root: "+root);
if (isExternalStorageReadable() && isExternalStorageWritable()) {

checkExternalMedia();
File dir = getDownloadsStorageDir();

File dir = new File (root.getAbsolutePath() + "/Download");
dir.mkdirs();
file = new File(dir, "wheelLog.txt");
fileExists = file.exists();
SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
String fileName = sdFormatter.format(new Date()) + ".csv";

Log.d(TAG, "DataLogger Started");
file = new File(dir, fileName);
fileExists = file.exists();

Log.d(TAG, "DataLogger Started");
return START_STICKY;
}
stopSelf();
return START_STICKY;
}

Expand All @@ -81,24 +84,27 @@ public void onDestroy() {
Log.d(TAG, "DataLogger stopped");
}

private void checkExternalMedia(){
boolean mExternalStorageAvailable;
boolean mExternalStorageWritable;
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}

if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWritable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWritable = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWritable = false;
public File getDownloadsStorageDir() {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "wheelLog");
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
LOGI("\n\nExternal Media: readable="
+mExternalStorageAvailable+" writable="+mExternalStorageWritable);
return file;
}

private void writeToSDFile(){
Expand All @@ -109,14 +115,15 @@ private void writeToSDFile(){
if (!fileExists)
{
fileExists = true;
pw.println("date,speed,voltage,current,distance,fan_status");
pw.println("date,speed,voltage,current,battery_level,distance,temperature,fan_status");
}

pw.println(String.format("%s,%f,%f,%f,%f,%d",
pw.println(String.format(Locale.US, "%s,%f,%f,%f,%d,%f,%d,%d",
sdf.format(new Date()),
Wheel.getInstance().getSpeedDouble(),
Wheel.getInstance().getVoltageDouble(),
Wheel.getInstance().getCurrentDouble(),
Wheel.getInstance().getBatteryLevel(),
Wheel.getInstance().getCurrentDistanceDouble(),
Wheel.getInstance().getTemperature(),
Wheel.getInstance().getFanStatus()
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/cooper/wheellog/DeviceListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static class ViewHolder {

public DeviceListAdapter(AppCompatActivity appCompatActivity) {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mLeDevices = new ArrayList<>();
mInflator = appCompatActivity.getLayoutInflater();
}

Expand All @@ -36,9 +36,9 @@ public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}

public void clear() {
mLeDevices.clear();
}
// public void clear() {
// mLeDevices.clear();
// }

@Override
public int getCount() {
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/com/cooper/wheellog/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.widget.TextView;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends Activity {

private static final int DEVICE_SCAN_REQUEST = 10;
Expand Down Expand Up @@ -87,14 +89,14 @@ public void onReceive(Context context, Intent intent) {
} else if (Constants.ACTION_WHEEL_DATA_AVAILABLE.equals(action)) {
textViewSpeed.setText(String.format("%s KPH", Wheel.getInstance().getSpeedDouble()));
textViewVoltage.setText(String.format("%sV", Wheel.getInstance().getVoltageDouble()));
textViewTemperature.setText(String.format("%d°C", Wheel.getInstance().getTemperature()));
textViewTemperature.setText(String.format(Locale.US, "%d°C", Wheel.getInstance().getTemperature()));
textViewCurrent.setText(String.format("%sW", Wheel.getInstance().getCurrentDouble()));
textViewBattery.setText(String.format("%d%%", Wheel.getInstance().getBatteryLevel()));
textViewBattery.setText(String.format(Locale.US, "%d%%", Wheel.getInstance().getBatteryLevel()));
textViewFanStatus.setText(Wheel.getInstance().getFanStatus() == 0 ? "Off" : "On");
textViewMaxSpeed.setText(String.format("%s KPH", Wheel.getInstance().getMaxSpeedDouble()));
textViewCurrentDistance.setText(String.format("%s KM", Wheel.getInstance().getCurrentDistanceDouble()));
textViewTotalDistance.setText(String.format("%s KM", Wheel.getInstance().getTotalDistanceDouble()));
textViewVersion.setText(String.format("%d", Wheel.getInstance().getVersion()));
textViewVersion.setText(String.format(Locale.US, "%d", Wheel.getInstance().getVersion()));
textViewName.setText(Wheel.getInstance().getName());
textViewType.setText(Wheel.getInstance().getType());
textViewSerial.setText(Wheel.getInstance().getSerial());
Expand Down Expand Up @@ -130,10 +132,12 @@ private void setConnectionState(int connectionState) {
mBluetoothLeService.writeBluetoothGattCharacteristic(data);
break;
case BluetoothLeService.STATE_CONNECTING:
Log.d(TAG, "Bluetooth connecting");
buttonScan.setEnabled(false);
buttonConnect.setText(R.string.waiting_for_device);
break;
case BluetoothLeService.STATE_DISCONNECTED:
Log.d(TAG, "Bluetooth disconnected");
buttonScan.setEnabled(true);
buttonConnect.setText(R.string.connect);
break;
Expand Down
11 changes: 3 additions & 8 deletions app/src/main/java/com/cooper/wheellog/ScanActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
Expand Down Expand Up @@ -143,11 +142,7 @@ public void run() {

private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
return result == PackageManager.PERMISSION_GRANTED;
}

private void requestPermission(){
Expand All @@ -159,7 +154,7 @@ private void requestPermission(){
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Expand Down
33 changes: 15 additions & 18 deletions app/src/main/java/com/cooper/wheellog/Wheel.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.cooper.wheellog;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class Wheel {
private static int speed;
private static long totalDistance;
private static int current;
private static int temperature;
private static int currentMode;
// private static int currentMode;
private static int battery;
private static int voltage;
private static long currentDistance;
private static int currentTime;
private static int maxSpeed;
private static int fanStatus;
private static int connectionState = BluetoothLeService.STATE_DISCONNECTED;
private static int wheelConnectionState = BluetoothLeService.STATE_DISCONNECTED;
private static String mDeviceNameString;
private static String mUnicycleType;
private static int mVersion;
Expand All @@ -34,10 +35,10 @@ public static Wheel getInstance() {
public int getTemperature() { return temperature; }
public int getBatteryLevel() { return battery; }
public int getFanStatus() { return fanStatus; }
public int getConnectionState() { return connectionState; }
public int getMaxSpeed() { return maxSpeed; }
public int getConnectionState() { return wheelConnectionState; }
// public int getMaxSpeed() { return maxSpeed; }
public int getVersion() { return mVersion; }
public int getCurrentTime() { return currentTime; };
// public int getCurrentTime() { return currentTime; };

public String getName() { return mDeviceNameString; }
public String getType() { return mUnicycleType; }
Expand All @@ -48,7 +49,7 @@ public String getCurrentTimeString() {
TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(currentTime));
long seconds = TimeUnit.SECONDS.toSeconds(currentTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(currentTime));
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
return String.format(Locale.US, "%02d:%02d:%02d", hours, minutes, seconds);
}

public double getSpeedDouble() { return (double) speed / 10.0F; }
Expand All @@ -58,7 +59,7 @@ public String getCurrentTimeString() {
public double getCurrentDistanceDouble() { return (double) currentDistance / 1000.0F; }
public double getTotalDistanceDouble() { return (double) totalDistance / 1000.0F; }

public void setConnectionState(boolean connected) { connectionState = connected ? 1 : 0; }
public void setConnectionState(boolean connected) { wheelConnectionState = connected ? 1 : 0; }

private int byteArrayInt2(byte low, byte high) {
return (low & 255) + ((high & 255) * 256);
Expand All @@ -81,10 +82,10 @@ public int decodeResponse(byte[] data) {
totalDistance = byteArrayInt4(data[6], data[7], data[8], data[9]);
current = byteArrayInt2(data[10], data[11]);
temperature = byteArrayInt2(data[12], data[13]) / 100;
currentMode = -1;
if ((data[15] & 255) == 224) {
currentMode = data[14];
}
// currentMode = -1;
// if ((data[15] & 255) == 224) {
// currentMode = data[14];
// }

if (voltage < 500) {
battery = 10;
Expand Down Expand Up @@ -116,17 +117,13 @@ public int decodeResponse(byte[] data) {
}
try {
mVersion = Integer.parseInt(ss[ss.length - 1]);
} catch (Exception e) {
} catch (Exception ignored) {
}
return Constants.REQUEST_SERIAL_DATA;
} else if ((data[16] & 255) == 179) {
byte[] sndata = new byte[18];
for (int i = 0; i < 14; i++) {
sndata[i] = data[i + 2];
}
for (int i = 14; i < 17; i++) {
sndata[i] = data[i + 3];
}
System.arraycopy(data, 2, sndata, 0, 14);
System.arraycopy(data, 17, sndata, 14, 3);
sndata[17] = (byte) 0;
mUnicycleSN = new String(sndata);
}
Expand Down

0 comments on commit 7056f83

Please sign in to comment.