mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
Mijia LYWSD: Handle and persist live and historic data samples
This commit is contained in:
@@ -54,7 +54,7 @@ public class GBDaoGenerator {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
final Schema schema = new Schema(93, MAIN_PACKAGE + ".entities");
|
final Schema schema = new Schema(94, MAIN_PACKAGE + ".entities");
|
||||||
|
|
||||||
Entity userAttributes = addUserAttributes(schema);
|
Entity userAttributes = addUserAttributes(schema);
|
||||||
Entity user = addUserInfo(schema, userAttributes);
|
Entity user = addUserInfo(schema, userAttributes);
|
||||||
@@ -141,6 +141,8 @@ public class GBDaoGenerator {
|
|||||||
addWena3Vo2Sample(schema, user, device);
|
addWena3Vo2Sample(schema, user, device);
|
||||||
addWena3StressSample(schema, user, device);
|
addWena3StressSample(schema, user, device);
|
||||||
addFemometerVinca2TemperatureSample(schema, user, device);
|
addFemometerVinca2TemperatureSample(schema, user, device);
|
||||||
|
addMijiaLywsdRealtimeSample(schema, user, device);
|
||||||
|
addMijiaLywsdHistoricSample(schema, user, device);
|
||||||
addMiScaleWeightSample(schema, user, device);
|
addMiScaleWeightSample(schema, user, device);
|
||||||
addColmiActivitySample(schema, user, device);
|
addColmiActivitySample(schema, user, device);
|
||||||
addColmiHeartRateSample(schema, user, device);
|
addColmiHeartRateSample(schema, user, device);
|
||||||
@@ -1590,6 +1592,24 @@ public class GBDaoGenerator {
|
|||||||
return sample;
|
return sample;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Entity addMijiaLywsdRealtimeSample(Schema schema, Entity user, Entity device) {
|
||||||
|
Entity sample = addEntity(schema, "MijiaLywsdRealtimeSample");
|
||||||
|
addCommonTimeSampleProperties("AbstractTemperatureSample", sample, user, device);
|
||||||
|
addTemperatureProperties(sample);
|
||||||
|
sample.addIntProperty("humidity").notNull();
|
||||||
|
return sample;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Entity addMijiaLywsdHistoricSample(Schema schema, Entity user, Entity device) {
|
||||||
|
Entity sample = addEntity(schema, "MijiaLywsdHistoricSample");
|
||||||
|
addCommonTimeSampleProperties("AbstractTimeSample", sample, user, device);
|
||||||
|
sample.addFloatProperty("minTemperature").notNull();
|
||||||
|
sample.addFloatProperty("maxTemperature").notNull();
|
||||||
|
sample.addIntProperty("minHumidity").notNull();
|
||||||
|
sample.addIntProperty("maxHumidity").notNull();
|
||||||
|
return sample;
|
||||||
|
}
|
||||||
|
|
||||||
private static Entity addMiScaleWeightSample(Schema schema, Entity user, Entity device) {
|
private static Entity addMiScaleWeightSample(Schema schema, Entity user, Entity device) {
|
||||||
Entity sample = addEntity(schema, "MiScaleWeightSample");
|
Entity sample = addEntity(schema, "MiScaleWeightSample");
|
||||||
addCommonTimeSampleProperties("AbstractWeightSample", sample, user, device);
|
addCommonTimeSampleProperties("AbstractWeightSample", sample, user, device);
|
||||||
|
|||||||
@@ -954,6 +954,12 @@ public class DebugActivity extends AbstractGBActivity {
|
|||||||
|
|
||||||
private void testNewFunctionality() {
|
private void testNewFunctionality() {
|
||||||
GBApplication.deviceService().onTestNewFunction();
|
GBApplication.deviceService().onTestNewFunction();
|
||||||
|
|
||||||
|
//try (DBHandler db = GBApplication.acquireDB()) {
|
||||||
|
// db.getDatabase().execSQL("DROP TABLE IF EXISTS TABLE_NAME_TO_DROP_HERE");
|
||||||
|
//} catch (final Exception e) {
|
||||||
|
// GB.log("Error accessing database", GB.ERROR, e);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void shareLog() {
|
private void shareLog() {
|
||||||
|
|||||||
+42
@@ -25,9 +25,11 @@ import nodomain.freeyourgadget.gadgetbridge.R;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsCustomizer;
|
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsCustomizer;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator;
|
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
|
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.model.TemperatureSample;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport;
|
import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.mijia_lywsd.MijiaLywsdSupport;
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.mijia_lywsd.MijiaLywsdSupport;
|
||||||
|
|
||||||
@@ -76,5 +78,45 @@ public abstract class AbstractMijiaLywsdCoordinator extends AbstractBLEDeviceCoo
|
|||||||
// nothing to delete, yet
|
// nothing to delete, yet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TimeSampleProvider<? extends TemperatureSample> getTemperatureSampleProvider(final GBDevice device, final DaoSession session) {
|
||||||
|
return new MijiaLywsdRealtimeSampleProvider(device, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsTemperatureMeasurement() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsContinuousTemperature() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsActivityTracking() {
|
||||||
|
return false; // FIXME: Enable this once temperature fetching is enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsSleepMeasurement() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsStepCounter() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsSpeedzones() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsActivityTabs() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract boolean supportsSetTime();
|
public abstract boolean supportsSetTime();
|
||||||
}
|
}
|
||||||
|
|||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
/* Copyright (C) 2025 José Rebelo
|
||||||
|
|
||||||
|
This file is part of Gadgetbridge.
|
||||||
|
|
||||||
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as published
|
||||||
|
by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Gadgetbridge is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
package nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import de.greenrobot.dao.AbstractDao;
|
||||||
|
import de.greenrobot.dao.Property;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractTimeSampleProvider;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.MijiaLywsdHistoricSample;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.MijiaLywsdHistoricSampleDao;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
|
|
||||||
|
public class MijiaLywsdHistoricSampleProvider extends AbstractTimeSampleProvider<MijiaLywsdHistoricSample> {
|
||||||
|
public MijiaLywsdHistoricSampleProvider(final GBDevice device, final DaoSession session) {
|
||||||
|
super(device, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public AbstractDao<MijiaLywsdHistoricSample, ?> getSampleDao() {
|
||||||
|
return getSession().getMijiaLywsdHistoricSampleDao();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected Property getTimestampSampleProperty() {
|
||||||
|
return MijiaLywsdHistoricSampleDao.Properties.Timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected Property getDeviceIdentifierSampleProperty() {
|
||||||
|
return MijiaLywsdHistoricSampleDao.Properties.DeviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MijiaLywsdHistoricSample createSample() {
|
||||||
|
return new MijiaLywsdHistoricSample();
|
||||||
|
}
|
||||||
|
}
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
/* Copyright (C) 2025 José Rebelo
|
||||||
|
|
||||||
|
This file is part of Gadgetbridge.
|
||||||
|
|
||||||
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as published
|
||||||
|
by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Gadgetbridge is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
package nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import de.greenrobot.dao.AbstractDao;
|
||||||
|
import de.greenrobot.dao.Property;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractTimeSampleProvider;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.MijiaLywsdRealtimeSample;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.MijiaLywsdRealtimeSampleDao;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
|
|
||||||
|
public class MijiaLywsdRealtimeSampleProvider extends AbstractTimeSampleProvider<MijiaLywsdRealtimeSample> {
|
||||||
|
public MijiaLywsdRealtimeSampleProvider(final GBDevice device, final DaoSession session) {
|
||||||
|
super(device, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public AbstractDao<MijiaLywsdRealtimeSample, ?> getSampleDao() {
|
||||||
|
return getSession().getMijiaLywsdRealtimeSampleDao();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected Property getTimestampSampleProperty() {
|
||||||
|
return MijiaLywsdRealtimeSampleDao.Properties.Timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected Property getDeviceIdentifierSampleProperty() {
|
||||||
|
return MijiaLywsdRealtimeSampleDao.Properties.DeviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MijiaLywsdRealtimeSample createSample() {
|
||||||
|
return new MijiaLywsdRealtimeSample();
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -199,6 +199,14 @@ public class BleNamesResolver {
|
|||||||
//mCharacteristics.put("0000fff2-0000-1000-8000-00805f9b34fb", "(Propr: Nothing CMF Command Write");
|
//mCharacteristics.put("0000fff2-0000-1000-8000-00805f9b34fb", "(Propr: Nothing CMF Command Write");
|
||||||
//mCharacteristics.put("02f00000-0000-0000-0000-00000000ffe1", "(Propr: Nothing CMF Data Write");
|
//mCharacteristics.put("02f00000-0000-0000-0000-00000000ffe1", "(Propr: Nothing CMF Data Write");
|
||||||
//mCharacteristics.put("02f00000-0000-0000-0000-00000000ffe2", "(Propr: Nothing CMF Data Read");
|
//mCharacteristics.put("02f00000-0000-0000-0000-00000000ffe2", "(Propr: Nothing CMF Data Read");
|
||||||
|
//mCharacteristics.put("00010203-0405-0607-0809-0a0b0c0d2b12", "(Propr: Telink OTA Write)");
|
||||||
|
//mCharacteristics.put("ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6", "(Propr: Lywsd TIME)");
|
||||||
|
//mCharacteristics.put("ebe0ccc4-7a0a-4b0c-8a1a-6ff2997da3a6", "(Propr: Lywsd BATTERY)");
|
||||||
|
//mCharacteristics.put("ebe0ccbe-7a0a-4b0c-8a1a-6ff2997da3a6", "(Propr: Lywsd TEMPERATURE_UNIT)");
|
||||||
|
//mCharacteristics.put("ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6", "(Propr: Lywsd CONN_INTERVAL)");
|
||||||
|
//mCharacteristics.put("ebe0ccbc-7a0a-4b0c-8a1a-6ff2997da3a6", "(Propr: Lywsd HISTORY)");
|
||||||
|
//mCharacteristics.put("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6", "(Propr: Lywsd LIVE_DATA)");
|
||||||
|
//mCharacteristics.put("ebe0ccba-7a0a-4b0c-8a1a-6ff2997da3a6", "(Propr: Lywsd HISTORY_LAST_ID)");
|
||||||
|
|
||||||
mValueFormats.put(Integer.valueOf(52), "32bit float");
|
mValueFormats.put(Integer.valueOf(52), "32bit float");
|
||||||
mValueFormats.put(Integer.valueOf(50), "16bit float");
|
mValueFormats.put(Integer.valueOf(50), "16bit float");
|
||||||
|
|||||||
+209
-16
@@ -33,12 +33,22 @@ import java.util.SimpleTimeZone;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd.AbstractMijiaLywsdCoordinator;
|
import nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd.AbstractMijiaLywsdCoordinator;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd.MijiaLywsdHistoricSampleProvider;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.devices.mijia_lywsd.MijiaLywsdRealtimeSampleProvider;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.MijiaLywsdHistoricSample;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.MijiaLywsdRealtimeSample;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
|
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService;
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
|
||||||
@@ -50,23 +60,25 @@ import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.Dev
|
|||||||
import static nodomain.freeyourgadget.gadgetbridge.util.GB.hexdump;
|
import static nodomain.freeyourgadget.gadgetbridge.util.GB.hexdump;
|
||||||
|
|
||||||
public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
|
public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(MijiaLywsdSupport.class);
|
private static final Logger LOG = LoggerFactory.getLogger(MijiaLywsdSupport.class);
|
||||||
|
|
||||||
|
private static final UUID UUID_BASE_SERVICE = UUID.fromString("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
private static final UUID UUID_TIME = UUID.fromString("ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6");
|
private static final UUID UUID_TIME = UUID.fromString("ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
private static final UUID UUID_BATTERY = UUID.fromString("ebe0ccc4-7a0a-4b0c-8a1a-6ff2997da3a6");
|
private static final UUID UUID_BATTERY = UUID.fromString("ebe0ccc4-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
private static final UUID UUID_SCALE = UUID.fromString("ebe0ccbe-7a0a-4b0c-8a1a-6ff2997da3a6");
|
private static final UUID UUID_SCALE = UUID.fromString("ebe0ccbe-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
|
private static final UUID UUID_HISTORY = UUID.fromString("ebe0ccbc-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
|
private static final UUID UUID_LIVE_DATA = UUID.fromString("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
|
private static final UUID UUID_HISTORY_LAST_ID = UUID.fromString("ebe0ccba-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
private static final UUID UUID_COMFORT_LEVEL = UUID.fromString("ebe0ccd7-7a0a-4b0c-8a1a-6ff2997da3a6");
|
private static final UUID UUID_COMFORT_LEVEL = UUID.fromString("ebe0ccd7-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
private static final UUID UUID_CONN_INTERVAL = UUID.fromString("ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6");
|
private static final UUID UUID_CONN_INTERVAL = UUID.fromString("ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6");
|
||||||
|
|
||||||
private final DeviceInfoProfile<MijiaLywsdSupport> deviceInfoProfile;
|
private final DeviceInfoProfile<MijiaLywsdSupport> deviceInfoProfile;
|
||||||
private final GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo();
|
private final GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo();
|
||||||
private final GBDeviceEventBatteryInfo batteryCmd = new GBDeviceEventBatteryInfo();
|
private final GBDeviceEventBatteryInfo batteryCmd = new GBDeviceEventBatteryInfo();
|
||||||
private final IntentListener mListener = new IntentListener() {
|
private final IntentListener mListener = intent -> {
|
||||||
@Override
|
String s = intent.getAction();
|
||||||
public void notify(Intent intent) {
|
if (Objects.equals(s, DeviceInfoProfile.ACTION_DEVICE_INFO)) {
|
||||||
String s = intent.getAction();
|
handleDeviceInfo(Objects.requireNonNull(intent.getParcelableExtra(DeviceInfoProfile.EXTRA_DEVICE_INFO)));
|
||||||
if (Objects.equals(s, DeviceInfoProfile.ACTION_DEVICE_INFO)) {
|
|
||||||
handleDeviceInfo((nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfo) intent.getParcelableExtra(DeviceInfoProfile.EXTRA_DEVICE_INFO));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -74,12 +86,14 @@ public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
|
|||||||
public static final int COMFORT_LEVEL_LENGTH_LYWSD03 = 6;
|
public static final int COMFORT_LEVEL_LENGTH_LYWSD03 = 6;
|
||||||
public static final int COMFORT_LEVEL_LENGTH_XMWSDJ04 = 8;
|
public static final int COMFORT_LEVEL_LENGTH_XMWSDJ04 = 8;
|
||||||
|
|
||||||
|
private int startupTime = 0;
|
||||||
|
|
||||||
public MijiaLywsdSupport() {
|
public MijiaLywsdSupport() {
|
||||||
super(LOG);
|
super(LOG);
|
||||||
addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS);
|
addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS);
|
||||||
addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE);
|
addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE);
|
||||||
addSupportedService(GattService.UUID_SERVICE_DEVICE_INFORMATION);
|
addSupportedService(GattService.UUID_SERVICE_DEVICE_INFORMATION);
|
||||||
addSupportedService(UUID.fromString("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6"));
|
addSupportedService(MijiaLywsdSupport.UUID_BASE_SERVICE);
|
||||||
deviceInfoProfile = new DeviceInfoProfile<>(this);
|
deviceInfoProfile = new DeviceInfoProfile<>(this);
|
||||||
deviceInfoProfile.addListener(mListener);
|
deviceInfoProfile.addListener(mListener);
|
||||||
addSupportedProfile(deviceInfoProfile);
|
addSupportedProfile(deviceInfoProfile);
|
||||||
@@ -93,8 +107,15 @@ public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
|
|||||||
final boolean supportsSetTime = getCoordinator().supportsSetTime();
|
final boolean supportsSetTime = getCoordinator().supportsSetTime();
|
||||||
if (supportsSetTime && GBApplication.getPrefs().getBoolean("datetime_synconconnect", true)) {
|
if (supportsSetTime && GBApplication.getPrefs().getBoolean("datetime_synconconnect", true)) {
|
||||||
setTime(builder);
|
setTime(builder);
|
||||||
|
} else {
|
||||||
|
getTime(builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: We can't enable this without properly handling the historic data id and live data, otherwise
|
||||||
|
// it will cause battery drain on both the phone and device
|
||||||
|
//builder.notify(getCharacteristic(MijiaLywsdSupport.UUID_HISTORY), true);
|
||||||
|
//builder.notify(getCharacteristic(MijiaLywsdSupport.UUID_LIVE_DATA), true);
|
||||||
|
|
||||||
getBatteryInfo(builder);
|
getBatteryInfo(builder);
|
||||||
getComfortLevel(builder);
|
getComfortLevel(builder);
|
||||||
setConnectionInterval(builder);
|
setConnectionInterval(builder);
|
||||||
@@ -134,6 +155,11 @@ public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
|
|||||||
builder.read(comfortCharacteristc);
|
builder.read(comfortCharacteristc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getTime(TransactionBuilder builder) {
|
||||||
|
BluetoothGattCharacteristic timeCharacteristic = getCharacteristic(MijiaLywsdSupport.UUID_TIME);
|
||||||
|
builder.read(timeCharacteristic);
|
||||||
|
}
|
||||||
|
|
||||||
private void setTemperatureScale(TransactionBuilder builder, SharedPreferences prefs) {
|
private void setTemperatureScale(TransactionBuilder builder, SharedPreferences prefs) {
|
||||||
String scale = prefs.getString(PREF_TEMPERATURE_SCALE_CF, "");
|
String scale = prefs.getString(PREF_TEMPERATURE_SCALE_CF, "");
|
||||||
BluetoothGattCharacteristic scaleCharacteristc = getCharacteristic(MijiaLywsdSupport.UUID_SCALE);
|
BluetoothGattCharacteristic scaleCharacteristc = getCharacteristic(MijiaLywsdSupport.UUID_SCALE);
|
||||||
@@ -177,11 +203,147 @@ public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleBatteryInfo(byte[] value, int status) {
|
private void handleBatteryInfo(byte[] value, int status) {
|
||||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
if (status != BluetoothGatt.GATT_SUCCESS) {
|
||||||
batteryCmd.level = ((short) value[0]);
|
LOG.warn("Unsuccessful response for handleBatteryInfo: {}", status);
|
||||||
batteryCmd.state = (batteryCmd.level > 20) ? BatteryState.BATTERY_NORMAL : BatteryState.BATTERY_LOW;
|
return;
|
||||||
handleGBDeviceEvent(batteryCmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
batteryCmd.level = ((short) value[0]);
|
||||||
|
batteryCmd.state = (batteryCmd.level > 20) ? BatteryState.BATTERY_NORMAL : BatteryState.BATTERY_LOW;
|
||||||
|
handleGBDeviceEvent(batteryCmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleHistory(final byte[] value, int status) {
|
||||||
|
if (status != BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
LOG.warn("Unsuccessful response for handleHistory: {}", status);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.length != 14) {
|
||||||
|
LOG.warn("Unexpected history length {}", value.length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ByteBuffer buf = ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
|
||||||
|
final int id = buf.getInt();
|
||||||
|
final int uptimeOffset = buf.getInt();
|
||||||
|
final int maxTemperature = buf.getShort();
|
||||||
|
final int maxHumidity = buf.get() & 0xff;
|
||||||
|
final int minTemperature = buf.getShort();
|
||||||
|
final int minHumidity = buf.get() & 0xff;
|
||||||
|
|
||||||
|
// Devices that do not support setting the time report the live data as an offset from the uptime
|
||||||
|
// other devices report the correct timestamp.
|
||||||
|
final int ts = (!getCoordinator().supportsSetTime() ? startupTime : 0) + uptimeOffset;
|
||||||
|
|
||||||
|
LOG.info(
|
||||||
|
"Got history: id={}, uptimeOffset={}, ts={}, minTemperature={}, maxTemperature={}, minHumidity={}, maxHumidity={}",
|
||||||
|
id,
|
||||||
|
uptimeOffset,
|
||||||
|
ts,
|
||||||
|
minTemperature / 10.0f,
|
||||||
|
maxTemperature / 10.0f,
|
||||||
|
minHumidity,
|
||||||
|
maxHumidity
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!getCoordinator().supportsSetTime() && startupTime <= 0) {
|
||||||
|
LOG.warn("Startup time is unknown - ignoring sample");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (DBHandler handler = GBApplication.acquireDB()) {
|
||||||
|
final DaoSession session = handler.getDaoSession();
|
||||||
|
final GBDevice gbDevice = getDevice();
|
||||||
|
final Device device = DBHelper.getDevice(gbDevice, session);
|
||||||
|
final User user = DBHelper.getUser(session);
|
||||||
|
|
||||||
|
final MijiaLywsdHistoricSampleProvider sampleProvider = new MijiaLywsdHistoricSampleProvider(gbDevice, session);
|
||||||
|
|
||||||
|
final MijiaLywsdHistoricSample sample = sampleProvider.createSample();
|
||||||
|
sample.setTimestamp(ts * 1000L);
|
||||||
|
sample.setMinTemperature(minTemperature / 10.0f);
|
||||||
|
sample.setMaxTemperature(maxTemperature / 10.0f);
|
||||||
|
sample.setMinHumidity(minHumidity);
|
||||||
|
sample.setMaxHumidity(maxHumidity);
|
||||||
|
sample.setDevice(device);
|
||||||
|
sample.setUser(user);
|
||||||
|
|
||||||
|
sampleProvider.addSample(sample);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
GB.toast(getContext(), "Error saving historic sample", Toast.LENGTH_LONG, GB.ERROR);
|
||||||
|
LOG.error("Error saving historic samples", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleLiveData(final byte[] value, int status) {
|
||||||
|
if (status != BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
LOG.warn("Unsuccessful response for handleLiveData: {}", status);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.length != 5) {
|
||||||
|
LOG.warn("Unexpected live data length {}", value.length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ByteBuffer buf = ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
|
||||||
|
final int temperature = buf.getShort();
|
||||||
|
final int humidity = buf.get() & 0xff;
|
||||||
|
final int voltage = buf.getShort();
|
||||||
|
|
||||||
|
LOG.info(
|
||||||
|
"Got mijia live data: temperature={}, humidity={}, voltage={}",
|
||||||
|
temperature / 100f,
|
||||||
|
humidity,
|
||||||
|
voltage / 1000f
|
||||||
|
);
|
||||||
|
|
||||||
|
try (DBHandler handler = GBApplication.acquireDB()) {
|
||||||
|
final DaoSession session = handler.getDaoSession();
|
||||||
|
final GBDevice gbDevice = getDevice();
|
||||||
|
final Device device = DBHelper.getDevice(gbDevice, session);
|
||||||
|
final User user = DBHelper.getUser(session);
|
||||||
|
|
||||||
|
final MijiaLywsdRealtimeSampleProvider sampleProvider = new MijiaLywsdRealtimeSampleProvider(gbDevice, session);
|
||||||
|
|
||||||
|
final MijiaLywsdRealtimeSample sample = sampleProvider.createSample();
|
||||||
|
sample.setTimestamp(System.currentTimeMillis());
|
||||||
|
sample.setTemperature(temperature / 100.0f);
|
||||||
|
sample.setHumidity(humidity);
|
||||||
|
sample.setDevice(device);
|
||||||
|
sample.setUser(user);
|
||||||
|
|
||||||
|
sampleProvider.addSample(sample);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
GB.toast(getContext(), "Error saving live sample", Toast.LENGTH_LONG, GB.ERROR);
|
||||||
|
LOG.error("Error saving live samples", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning: this voltage value is not reliable, so I am not sure
|
||||||
|
// it's even worth using
|
||||||
|
batteryCmd.voltage = voltage / 1000f;
|
||||||
|
handleGBDeviceEvent(batteryCmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleTime(final byte[] value, int status) {
|
||||||
|
if (status != BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
LOG.warn("Unsuccessful response for handleTime: {}", status);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.length != 4) {
|
||||||
|
LOG.warn("Unexpected time length {}", value.length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int uptime = BLETypeConversions.toUint32(value);
|
||||||
|
|
||||||
|
startupTime = (int) ((System.currentTimeMillis() / 1000L) - uptime);
|
||||||
|
|
||||||
|
LOG.info("Got mijia time={}, startupTime={}", uptime, startupTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleComfortLevel(byte[] value, int status) {
|
private void handleComfortLevel(byte[] value, int status) {
|
||||||
@@ -264,8 +426,24 @@ public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
UUID characteristicUUID = characteristic.getUuid();
|
final UUID characteristicUUID = characteristic.getUuid();
|
||||||
LOG.info("Unhandled characteristic changed: " + characteristicUUID);
|
|
||||||
|
if (MijiaLywsdSupport.UUID_HISTORY.equals(characteristicUUID)) {
|
||||||
|
handleHistory(characteristic.getValue(), BluetoothGatt.GATT_SUCCESS);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MijiaLywsdSupport.UUID_LIVE_DATA.equals(characteristicUUID)) {
|
||||||
|
handleLiveData(characteristic.getValue(), BluetoothGatt.GATT_SUCCESS);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MijiaLywsdSupport.UUID_TIME.equals(characteristicUUID)) {
|
||||||
|
handleTime(characteristic.getValue(), BluetoothGatt.GATT_SUCCESS);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG.warn("Unhandled characteristic changed: {}", characteristicUUID);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,7 +465,22 @@ public class MijiaLywsdSupport extends AbstractBTLEDeviceSupport {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.info("Unhandled characteristic read: " + characteristicUUID);
|
if (MijiaLywsdSupport.UUID_HISTORY.equals(characteristicUUID)) {
|
||||||
|
handleHistory(characteristic.getValue(), status);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MijiaLywsdSupport.UUID_LIVE_DATA.equals(characteristicUUID)) {
|
||||||
|
handleLiveData(characteristic.getValue(), status);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MijiaLywsdSupport.UUID_TIME.equals(characteristicUUID)) {
|
||||||
|
handleTime(characteristic.getValue(), status);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG.warn("Unhandled characteristic read: {}", characteristicUUID);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user