Search in sources :

Example 1 with GBException

use of nodomain.freeyourgadget.gadgetbridge.GBException in project Gadgetbridge by Freeyourgadget.

the class AbstractDeviceCoordinator method deleteDevice.

@Override
public void deleteDevice(final GBDevice gbDevice) throws GBException {
    LOG.info("will try to delete device: " + gbDevice.getName());
    if (gbDevice.isConnected() || gbDevice.isConnecting()) {
        GBApplication.deviceService().disconnect();
    }
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        DaoSession session = dbHandler.getDaoSession();
        Device device = DBHelper.findDevice(gbDevice, session);
        if (device != null) {
            deleteDevice(gbDevice, device, session);
            QueryBuilder<?> qb = session.getDeviceAttributesDao().queryBuilder();
            qb.where(DeviceAttributesDao.Properties.DeviceId.eq(device.getId())).buildDelete().executeDeleteWithoutDetachingEntities();
            session.getDeviceDao().delete(device);
        } else {
            LOG.info("device to delete not found in db: " + gbDevice);
        }
    } catch (Exception e) {
        throw new GBException("Error deleting device: " + e.getMessage(), e);
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) BluetoothDevice(android.bluetooth.BluetoothDevice) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Example 2 with GBException

use of nodomain.freeyourgadget.gadgetbridge.GBException in project Gadgetbridge by Freeyourgadget.

the class DeviceSupportFactory method createClassNameDeviceSupport.

private DeviceSupport createClassNameDeviceSupport(GBDevice device) throws GBException {
    String className = device.getAddress();
    try {
        Class<?> deviceSupportClass = Class.forName(className);
        Constructor<?> constructor = deviceSupportClass.getConstructor();
        DeviceSupport support = (DeviceSupport) constructor.newInstance();
        // has to create the device itself
        support.setContext(device, null, mContext);
        return support;
    } catch (ClassNotFoundException e) {
        // not a class, or not known at least
        return null;
    } catch (Exception e) {
        throw new GBException("Error creating DeviceSupport instance for " + className, e);
    }
}
Also used : GBException(nodomain.freeyourgadget.gadgetbridge.GBException) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Example 3 with GBException

use of nodomain.freeyourgadget.gadgetbridge.GBException in project Gadgetbridge by Freeyourgadget.

the class DeviceSupportFactory method createTCPDeviceSupport.

private DeviceSupport createTCPDeviceSupport(GBDevice gbDevice) throws GBException {
    try {
        DeviceSupport deviceSupport = new ServiceDeviceSupport(new PebbleSupport(), EnumSet.of(ServiceDeviceSupport.Flags.BUSY_CHECKING));
        deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
        return deviceSupport;
    } catch (Exception e) {
        // FIXME: localize
        throw new GBException("cannot connect to " + gbDevice, e);
    }
}
Also used : PebbleSupport(nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Example 4 with GBException

use of nodomain.freeyourgadget.gadgetbridge.GBException in project Gadgetbridge by Freeyourgadget.

the class HPlusHandlerThread method processDaySummary.

/**
     * Process a day summary message
     * This message includes aggregates regarding an entire day
     *
     * @param data the message from the device
     * @return boolean indicating success or fail
     */
public boolean processDaySummary(byte[] data) {
    HPlusDataRecordDaySummary record;
    try {
        record = new HPlusDataRecordDaySummary(data);
    } catch (IllegalArgumentException e) {
        LOG.debug((e.getMessage()));
        return false;
    }
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
        HPlusHealthActivitySample sample = createSample(dbHandler, record.timestamp);
        sample.setRawKind(record.type);
        sample.setSteps(record.steps);
        sample.setDistance(record.distance);
        sample.setCalories(record.calories);
        sample.setDistance(record.distance);
        //TODO: Find an alternative approach for Day Summary Heart Rate
        sample.setHeartRate((record.maxHeartRate - record.minHeartRate) / 2);
        sample.setRawHPlusHealthData(record.getRawData());
        sample.setProvider(provider);
        provider.addGBActivitySample(sample);
    } catch (GBException ex) {
        LOG.debug((ex.getMessage()));
    } catch (Exception ex) {
        LOG.debug(ex.getMessage());
    }
    mGetDaySummaryTime = GregorianCalendar.getInstance();
    mGetDaySummaryTime.add(Calendar.SECOND, DAY_SUMMARY_SYNC_PERIOD);
    return true;
}
Also used : HPlusHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) HPlusHealthSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Example 5 with GBException

use of nodomain.freeyourgadget.gadgetbridge.GBException in project Gadgetbridge by Freeyourgadget.

the class DeviceHelper method removeBond.

/**
     * Attempts to removing the bonding with the given device. Returns true
     * if bonding was supposedly successful and false if anything went wrong
     * @param device
     * @return
     */
public boolean removeBond(GBDevice device) throws GBException {
    BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
    if (defaultAdapter != null) {
        BluetoothDevice remoteDevice = defaultAdapter.getRemoteDevice(device.getAddress());
        if (remoteDevice != null) {
            try {
                Method method = BluetoothDevice.class.getMethod("removeBond", (Class[]) null);
                Object result = method.invoke(remoteDevice, (Object[]) null);
                return Boolean.TRUE.equals(result);
            } catch (Exception e) {
                throw new GBException("Error removing bond to device: " + device, e);
            }
        }
    }
    return false;
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) Method(java.lang.reflect.Method) BluetoothAdapter(android.bluetooth.BluetoothAdapter) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Aggregations

GBException (nodomain.freeyourgadget.gadgetbridge.GBException)7 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)4 HPlusHealthSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider)3 HPlusHealthActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)3 BluetoothDevice (android.bluetooth.BluetoothDevice)2 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 Intent (android.content.Intent)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)1 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)1 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1 PebbleSupport (nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport)1