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);
}
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations