Search in sources :

Example 1 with Temperature

use of com.savonia.thesis.db.entity.Temperature in project Thesis by bajnax.

the class LeConnectedDeviceActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_le_connected_device);
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        isServiceRunning = savedInstanceState.getBoolean(CONNECTION_STATE);
        hasReceivedServices = savedInstanceState.getBoolean(RECEIVED_SERVICES);
    } else {
        isServiceRunning = false;
        hasReceivedServices = false;
    }
    deviceStatus = (TextView) findViewById(R.id.deviceStatus);
    // spinner, its neighbouring textView, graph and connectivity to the SaMi cloud
    // won't be shown if another device is connected
    spinner = (ProgressBar) findViewById(R.id.spinner);
    lookUpText = (TextView) findViewById(R.id.lookUpTextView);
    spinner.setVisibility(View.GONE);
    lookUpText.setVisibility(View.GONE);
    toolBar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolBar);
    // setting up the graph's vertical labels
    sensorsGraph = (GraphView) findViewById(R.id.graph);
    sensorsGraph.setTitle("Current sensor\'s data");
    sensorsGraph.setTitleColor(R.color.colorPrimaryDark);
    sensorsGraph.getGridLabelRenderer().setVerticalAxisTitle("Value");
    sensorsGraph.getGridLabelRenderer().setHorizontalAxisTitle("Time");
    // enabling horizontal zooming and scrolling
    sensorsGraph.getViewport().setScalable(true);
    sensorsGraph.getGridLabelRenderer().setLabelVerticalWidth(50);
    sensorsGraph.getGridLabelRenderer().setLabelHorizontalHeight(50);
    sensorsGraph.getViewport().setYAxisBoundsManual(true);
    sensorsGraph.getViewport().setMinY(0);
    sensorsGraph.getViewport().setMaxY(40);
    // TODO: make the date labels on the X axis to be shown properly
    // set date label formatter
    SimpleDateFormat mDateFormatter = new SimpleDateFormat("MM-dd HH:mm:ss");
    sensorsGraph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(LeConnectedDeviceActivity.this, mDateFormatter));
    // only 2 because of the space
    sensorsGraph.getGridLabelRenderer().setNumHorizontalLabels(2);
    Calendar calendar = Calendar.getInstance();
    long t1 = calendar.getTimeInMillis();
    long t2 = calendar.getTimeInMillis() + 15000;
    sensorsGraph.getViewport().setXAxisBoundsManual(true);
    sensorsGraph.getViewport().setMinX((double) t1);
    sensorsGraph.getViewport().setMaxX(((double) t2));
    // as we use dates as labels, the human rounding to nice readable numbers
    // is not necessary
    sensorsGraph.getGridLabelRenderer().setHumanRounding(false);
    temperatureSeries = new PointsGraphSeries<>();
    sensorsGraph.addSeries(temperatureSeries);
    expListView = (ExpandableListView) findViewById(R.id.expandableListView);
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager != null)
        mBluetoothAdapter = bluetoothManager.getAdapter();
    else
        finish();
    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    deviceAddress = intent.getStringExtra("deviceAddress");
    if (deviceAddress == null) {
        finish();
    }
    // setting up the progress bar, in case the BLE Shield is connected
    if (deviceAddress.equals(GattAttributesSample.DEVICE_ADDRESS)) {
        Drawable progressDrawable = spinner.getIndeterminateDrawable().mutate();
        progressDrawable.setColorFilter(getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
        spinner.setProgressDrawable(progressDrawable);
        spinner.setVisibility(View.VISIBLE);
        lookUpText.setVisibility(View.VISIBLE);
    }
    // Starting service if it is not running yet and binding to it afterwards
    Intent gattServiceIntent = new Intent(getApplicationContext(), BluetoothLowEnergyService.class);
    // the service will be created only once
    getApplicationContext().startService(gattServiceIntent);
    // service clients are able to bind to it at any time
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    registerReceiver(mGattUpdateReceiver, GattUpdateIntentFilter());
    /* if (mBluetoothLEService != null && !isServiceRunning) {
            final boolean result = mBluetoothLEService.connect(deviceAddress);
            Toast.makeText(LeConnectedDeviceActivity.this,
                    "Connect request result: " + result, Toast.LENGTH_SHORT).show();
        }*/
    expListView.setVisibility(View.GONE);
    sensorsGraph.setVisibility(View.GONE);
    final SensorsDataViewModel sensorsDataViewModel = ViewModelProviders.of(this).get(SensorsDataViewModel.class);
    sensorsDataViewModel.getTemperatures().observe(this, new Observer<List<Temperature>>() {

        @Override
        public void onChanged(@Nullable final List<Temperature> temperatures) {
            if (temperatures == null) {
                Toast.makeText(LeConnectedDeviceActivity.this, "No data received", Toast.LENGTH_SHORT).show();
            } else if (temperatures.size() > 0) {
                if (temperatures.size() == 1) {
                    // set manual x bounds to have nice steps
                    // sensorsGraph.getViewport().setXAxisBoundsManual(true);
                    sensorsGraph.getViewport().setMinX((double) temperatures.get(0).getTimestamp());
                    sensorsGraph.getViewport().setMaxX((double) sensorsGraph.getViewport().getMinX(false) + 15000);
                    Log.d(TAG, "Initial timestamp, MinLabelX: " + mDateFormatter.format((double) temperatures.get(0).getTimestamp()));
                    Log.d(TAG, "Final timestamp, MaxLabelX: " + mDateFormatter.format(((double) sensorsGraph.getViewport().getMinX(false) + 15000)));
                }
                /*//scales programmatically
                    sensorsGraph.getViewport().setMaxX((double)temperatures.get(temperatures.size()-1).getTimestamp());*/
                displayTemperature(temperatures.get(temperatures.size() - 1));
            }
        }
    });
}
Also used : Temperature(com.savonia.thesis.db.entity.Temperature) BluetoothManager(android.bluetooth.BluetoothManager) Calendar(java.util.Calendar) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) List(java.util.List) ArrayList(java.util.ArrayList) DateAsXAxisLabelFormatter(com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with Temperature

use of com.savonia.thesis.db.entity.Temperature in project Thesis by bajnax.

the class BluetoothLowEnergyService method broadcastUpdate.

// broadcasting characteristic's data to LeConnectedDeviceActivity
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);
    /*// For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for(byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" +
                    stringBuilder.toString());*/
    final byte[] data = characteristic.getValue();
    String value;
    if (data != null && data.length > 0) {
        value = new String(data);
        intent.putExtra(EXTRA_DATA, value);
        // retrieving the double value from the temperature notification
        if (value.charAt(0) == 't' && !value.contains("g")) {
            try {
                StringBuilder sb = new StringBuilder(value);
                value = sb.substring(2);
                value = value.trim();
                double tempValue = Double.parseDouble(value);
                Temperature temperature = new Temperature(tempValue);
                CentralRepository.getInstance(SensorsValuesDatabase.getDatabase(getApplicationContext())).insertTemperature(temperature);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (value.charAt(0) == 'g' && !value.contains("t")) {
            // retrieving the double value from the gas notification
            try {
                StringBuilder sb = new StringBuilder(value);
                value = sb.substring(2);
                value = value.trim();
                double gasValue = Double.parseDouble(value);
                Gas gas = new Gas(gasValue);
                CentralRepository.getInstance(SensorsValuesDatabase.getDatabase(getApplicationContext())).insertGas(gas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    sendBroadcast(intent);
}
Also used : Temperature(com.savonia.thesis.db.entity.Temperature) Gas(com.savonia.thesis.db.entity.Gas) Intent(android.content.Intent)

Aggregations

Intent (android.content.Intent)2 Temperature (com.savonia.thesis.db.entity.Temperature)2 BluetoothManager (android.bluetooth.BluetoothManager)1 Drawable (android.graphics.drawable.Drawable)1 DateAsXAxisLabelFormatter (com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter)1 Gas (com.savonia.thesis.db.entity.Gas)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 List (java.util.List)1