1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-10-28 22:27:38 +01:00

Do not re-import old values from MiScale

This commit is contained in:
oliexdev
2025-09-11 18:20:24 +02:00
parent 9cb4b22e4c
commit 1b7f4eb567
2 changed files with 344 additions and 1 deletions

View File

@@ -0,0 +1,325 @@
{
"formatVersion": 1,
"database": {
"version": 9,
"identityHash": "c57120b01500758a03c6c58fbadc199b",
"entities": [
{
"tableName": "User",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `icon` TEXT NOT NULL, `birthDate` INTEGER NOT NULL, `gender` TEXT NOT NULL, `heightCm` REAL NOT NULL, `activityLevel` TEXT NOT NULL, `useAssistedWeighing` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "icon",
"columnName": "icon",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "birthDate",
"columnName": "birthDate",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "gender",
"columnName": "gender",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "heightCm",
"columnName": "heightCm",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "activityLevel",
"columnName": "activityLevel",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "useAssistedWeighing",
"columnName": "useAssistedWeighing",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
},
{
"tableName": "Measurement",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `userId` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, FOREIGN KEY(`userId`) REFERENCES `User`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "userId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "timestamp",
"columnName": "timestamp",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_Measurement_userId",
"unique": false,
"columnNames": [
"userId"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_Measurement_userId` ON `${TABLE_NAME}` (`userId`)"
},
{
"name": "index_Measurement_userId_timestamp",
"unique": true,
"columnNames": [
"userId",
"timestamp"
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_Measurement_userId_timestamp` ON `${TABLE_NAME}` (`userId`, `timestamp`)"
}
],
"foreignKeys": [
{
"table": "User",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"userId"
],
"referencedColumns": [
"id"
]
}
]
},
{
"tableName": "MeasurementValue",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `measurementId` INTEGER NOT NULL, `typeId` INTEGER NOT NULL, `floatValue` REAL, `intValue` INTEGER, `textValue` TEXT, `dateValue` INTEGER, FOREIGN KEY(`measurementId`) REFERENCES `Measurement`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`typeId`) REFERENCES `MeasurementType`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "measurementId",
"columnName": "measurementId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "typeId",
"columnName": "typeId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "floatValue",
"columnName": "floatValue",
"affinity": "REAL"
},
{
"fieldPath": "intValue",
"columnName": "intValue",
"affinity": "INTEGER"
},
{
"fieldPath": "textValue",
"columnName": "textValue",
"affinity": "TEXT"
},
{
"fieldPath": "dateValue",
"columnName": "dateValue",
"affinity": "INTEGER"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_MeasurementValue_measurementId",
"unique": false,
"columnNames": [
"measurementId"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_MeasurementValue_measurementId` ON `${TABLE_NAME}` (`measurementId`)"
},
{
"name": "index_MeasurementValue_typeId",
"unique": false,
"columnNames": [
"typeId"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_MeasurementValue_typeId` ON `${TABLE_NAME}` (`typeId`)"
}
],
"foreignKeys": [
{
"table": "Measurement",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"measurementId"
],
"referencedColumns": [
"id"
]
},
{
"table": "MeasurementType",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"typeId"
],
"referencedColumns": [
"id"
]
}
]
},
{
"tableName": "MeasurementType",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `key` TEXT NOT NULL, `name` TEXT, `color` INTEGER NOT NULL, `icon` TEXT NOT NULL, `unit` TEXT NOT NULL, `inputType` TEXT NOT NULL, `displayOrder` INTEGER NOT NULL, `isDerived` INTEGER NOT NULL, `isEnabled` INTEGER NOT NULL, `isPinned` INTEGER NOT NULL, `isOnRightYAxis` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "key",
"columnName": "key",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT"
},
{
"fieldPath": "color",
"columnName": "color",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "icon",
"columnName": "icon",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "unit",
"columnName": "unit",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "inputType",
"columnName": "inputType",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "displayOrder",
"columnName": "displayOrder",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isDerived",
"columnName": "isDerived",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isEnabled",
"columnName": "isEnabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isPinned",
"columnName": "isPinned",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isOnRightYAxis",
"columnName": "isOnRightYAxis",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_MeasurementType_key",
"unique": true,
"columnNames": [
"key"
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_MeasurementType_key` ON `${TABLE_NAME}` (`key`)"
}
]
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'c57120b01500758a03c6c58fbadc199b')"
]
}
}

View File

@@ -319,7 +319,9 @@ class MiScaleHandler : ScaleDeviceHandler() {
val native = if (isLbs || isCatty) weightRaw / 100.0f else weightRaw / 200.0f val native = if (isLbs || isCatty) weightRaw / 100.0f else weightRaw / 200.0f
val dt = parseMinuteDate(year, month, day, hour, minute) ?: return false val dt = parseMinuteDate(year, month, day, hour, minute) ?: return false
if (!plausible(dt)) return false val ts = (dt.time / 1000).toInt()
val lastTs = getLastImportedTimestamp(user.id)
if (ts <= lastTs) return false
val m = ScaleMeasurement().apply { val m = ScaleMeasurement().apply {
dateTime = dt dateTime = dt
@@ -342,6 +344,8 @@ class MiScaleHandler : ScaleDeviceHandler() {
} }
publish(m) publish(m)
updateLastImportedTimestamp(user.id, ts)
return true return true
} }
@@ -373,12 +377,17 @@ class MiScaleHandler : ScaleDeviceHandler() {
val dt = parseMinuteDate(year, month, day, hour, minute) ?: return false val dt = parseMinuteDate(year, month, day, hour, minute) ?: return false
if (!plausible(dt)) return false if (!plausible(dt)) return false
val ts = (dt.time / 1000).toInt()
val lastTs = getLastImportedTimestamp(user.id)
if (ts <= lastTs) return false
val m = ScaleMeasurement().apply { val m = ScaleMeasurement().apply {
dateTime = dt dateTime = dt
weight = ConverterUtils.toKilogram(native, user.scaleUnit) weight = ConverterUtils.toKilogram(native, user.scaleUnit)
userId = user.id userId = user.id
} }
publish(m) publish(m)
updateLastImportedTimestamp(user.id, ts)
return true return true
} }
@@ -475,4 +484,13 @@ class MiScaleHandler : ScaleDeviceHandler() {
}.onFailure { logI("Alternate fallback write failed: ${it.message}") } }.onFailure { logI("Alternate fallback write failed: ${it.message}") }
} }
} }
private fun getLastImportedTimestamp(userId: Int): Int {
return settingsGetInt("last_imported_ts_$userId", 0)
}
private fun updateLastImportedTimestamp(userId: Int, timestamp: Int) {
settingsPutInt("last_imported_ts_$userId", timestamp)
}
} }