mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-01-24 08:37:32 +01:00
Update the device in case it has changed
This commit is contained in:
parent
1a22259b4e
commit
39c7762416
@ -331,25 +331,66 @@ public class DBHelper {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Looks up in the database the Device entity corresponding to the GBDevice. If a device
|
||||||
|
* exists already, it will be updated with the current preferences values. If no device exists
|
||||||
|
* yet, it will be created in the database.
|
||||||
|
*
|
||||||
|
* @param session
|
||||||
|
* @return the device entity corresponding to the given GBDevice
|
||||||
|
*/
|
||||||
public static Device getDevice(GBDevice gbDevice, DaoSession session) {
|
public static Device getDevice(GBDevice gbDevice, DaoSession session) {
|
||||||
Device device = findDevice(gbDevice, session);
|
Device device = findDevice(gbDevice, session);
|
||||||
if (device == null) {
|
if (device == null) {
|
||||||
device = createDevice(session, gbDevice);
|
device = createDevice(gbDevice, session);
|
||||||
|
} else {
|
||||||
|
ensureDeviceUpToDate(device, gbDevice, session);
|
||||||
}
|
}
|
||||||
ensureDeviceAttributes(device, gbDevice, session);
|
ensureDeviceAttributes(device, gbDevice, session);
|
||||||
|
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Device createDevice(DaoSession session, GBDevice gbDevice) {
|
private static void ensureDeviceUpToDate(Device device, GBDevice gbDevice, DaoSession session) {
|
||||||
Device device = new Device();
|
if (!isDeviceUpToDate(device, gbDevice)) {
|
||||||
device.setIdentifier(gbDevice.getAddress());
|
device.setIdentifier(gbDevice.getAddress());
|
||||||
device.setName(gbDevice.getName());
|
device.setName(gbDevice.getName());
|
||||||
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
|
||||||
device.setManufacturer(coordinator.getManufacturer());
|
device.setManufacturer(coordinator.getManufacturer());
|
||||||
device.setType(gbDevice.getType().getKey());
|
device.setType(gbDevice.getType().getKey());
|
||||||
device.setModel(gbDevice.getModel());
|
device.setModel(gbDevice.getModel());
|
||||||
|
|
||||||
|
if (device.getId() == null) {
|
||||||
session.getDeviceDao().insert(device);
|
session.getDeviceDao().insert(device);
|
||||||
|
} else {
|
||||||
|
session.getDeviceDao().update(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isDeviceUpToDate(Device device, GBDevice gbDevice) {
|
||||||
|
if (!Objects.equals(device.getIdentifier(), gbDevice.getAddress())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!Objects.equals(device.getName(), gbDevice.getName())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
|
||||||
|
if (!Objects.equals(device.getManufacturer(), coordinator.getManufacturer())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (device.getType() != gbDevice.getType().getKey()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!Objects.equals(device.getModel(), gbDevice.getModel())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Device createDevice(GBDevice gbDevice, DaoSession session) {
|
||||||
|
Device device = new Device();
|
||||||
|
ensureDeviceUpToDate(device, gbDevice, session);
|
||||||
|
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user