AAPSClient: provide info about charging state

This commit is contained in:
Milos Kozak 2023-02-01 15:18:06 +01:00
parent e2d91f6203
commit ba58fb5ff7
13 changed files with 3731 additions and 8 deletions

View file

@ -48,6 +48,7 @@ interface ProcessedDeviceStatusData {
var clock = 0L
var battery = 0
var isCharging: Boolean? = null
}
val uploaderMap: HashMap<String, Uploader>

View file

@ -22,6 +22,7 @@ data class NSDeviceStatus(
val createdAt: String? = null, // string or string timestamp on previous version of api, in my examples, a lot of treatments don't have date, only created_at, some of them with string others with long...
@SerializedName("date") val date: Long?, // date as milliseconds
@SerializedName("uploaderBattery") val uploaderBattery: Int?,// integer($int64)
@SerializedName("isCharging") val isCharging: Boolean?,
@SerializedName("device") val device: String?, // "openaps://samsung SM-G970F"
@SerializedName("uploader") val uploader: Uploader?,
@ -57,7 +58,7 @@ data class NSDeviceStatus(
)
@Serializable data class Uploader(
@SerializedName("battery") val battery: Int?,
@SerializedName("battery") val battery: Int?
)
@Serializable data class Configuration(

View file

@ -17,6 +17,7 @@ internal fun RemoteDeviceStatus.toNSDeviceStatus(): NSDeviceStatus =
createdAt = createdAt,
date = date,
uploaderBattery = uploaderBattery,
isCharging = isCharging,
device = device,
uploader = NSDeviceStatus.Uploader(uploader?.battery),
pump = pump?.toNSDeviceStatusPump(),
@ -33,6 +34,7 @@ internal fun NSDeviceStatus.toRemoteDeviceStatus(): RemoteDeviceStatus =
createdAt = createdAt,
date = date,
uploaderBattery = uploaderBattery,
isCharging = isCharging,
device = device,
uploader = RemoteDeviceStatus.Uploader(uploader?.battery),
pump = pump?.toRemoteDeviceStatusPump(),

View file

@ -22,6 +22,7 @@ internal data class RemoteDeviceStatus(
val createdAt: String? = null, // string or string timestamp on previous version of api, in my examples, a lot of treatments don't have date, only created_at, some of them with string others with long...
@SerializedName("date") val date: Long?, // date as milliseconds
@SerializedName("uploaderBattery") val uploaderBattery: Int?,// integer($int64)
@SerializedName("isCharging") val isCharging: Boolean?,
@SerializedName("device") val device: String?, // "openaps://samsung SM-G970F"
@SerializedName("uploader") val uploader: Uploader?,
@ -57,7 +58,7 @@ internal data class RemoteDeviceStatus(
)
@Serializable data class Uploader(
@SerializedName("battery") val battery: Int?,
@SerializedName("battery") val battery: Int?
)
@Serializable data class Configuration(

View file

@ -27,7 +27,8 @@ data class DeviceStatus(
var enacted: String? = null,
var suggested: String? = null,
var iob: String? = null,
var uploaderBattery: Int = 0,
var uploaderBattery: Int,
var isCharging: Boolean?,
var configuration: String? = null
) : DBEntryWithTime {

File diff suppressed because it is too large Load diff

View file

@ -44,7 +44,7 @@ import info.nightscout.database.entities.TotalDailyDose
import info.nightscout.database.entities.UserEntry
import info.nightscout.database.entities.VersionChange
const val DATABASE_VERSION = 22
const val DATABASE_VERSION = 23
@Database(version = DATABASE_VERSION,
entities = [APSResult::class, Bolus::class, BolusCalculatorResult::class, Carbs::class,

View file

@ -28,6 +28,7 @@ open class DatabaseModule {
// .addMigrations(migration11to12)
.addMigrations(migration20to21)
.addMigrations(migration21to22)
.addMigrations(migration22to23)
.addCallback(object : Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)
@ -80,4 +81,12 @@ open class DatabaseModule {
}
}
private val migration22to23 = object : Migration(22,23) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE `deviceStatus` ADD COLUMN `isCharging` INTEGER")
// Custom indexes must be dropped on migration to pass room schema checking after upgrade
dropCustomIndexes(database)
}
}
}

View file

@ -780,6 +780,7 @@ class LoopPlugin @Inject constructor(
device = "openaps://" + Build.MANUFACTURER + " " + Build.MODEL,
pump = pump.getJSONStatus(profile, profileName, version).toString(),
uploaderBattery = receiverStatusStore.batteryLevel,
isCharging = receiverStatusStore.isCharging,
configuration = runningConfiguration.configuration().toString()
)
}

View file

@ -155,10 +155,11 @@ class NSDeviceStatusHandler @Inject constructor(
}
}
private fun updateUploaderData(NSDeviceStatus: NSDeviceStatus) {
val clock = NSDeviceStatus.createdAt?.let { dateUtil.fromISODateString(it) } ?: return
val device = NSDeviceStatus.device ?: return
val battery = NSDeviceStatus.uploaderBattery ?: NSDeviceStatus.uploader?.battery ?: return
private fun updateUploaderData(nsDeviceStatus: NSDeviceStatus) {
val clock = nsDeviceStatus.createdAt?.let { dateUtil.fromISODateString(it) } ?: return
val device = nsDeviceStatus.device ?: return
val battery = nsDeviceStatus.uploaderBattery ?: nsDeviceStatus.uploader?.battery ?: return
val isCharging = nsDeviceStatus.isCharging
var uploader = processedDeviceStatusData.uploaderMap[device]
// check if this is new data
@ -166,6 +167,7 @@ class NSDeviceStatusHandler @Inject constructor(
if (uploader == null) uploader = ProcessedDeviceStatusData.Uploader()
uploader.battery = battery
uploader.clock = clock
uploader.isCharging = isCharging
processedDeviceStatusData.uploaderMap[device] = uploader
}
}

View file

@ -133,6 +133,7 @@ class ProcessedDeviceStatusDataImpl @Inject constructor(
override val uploaderStatusSpanned: Spanned
get() {
var isCharging = false
val string = StringBuilder()
string.append("<span style=\"color:${rh.gac(info.nightscout.core.ui.R.attr.nsTitleColor)}\">")
string.append(rh.gs(R.string.uploader_short))
@ -145,10 +146,12 @@ class ProcessedDeviceStatusDataImpl @Inject constructor(
val uploader = pair.value as ProcessedDeviceStatusData.Uploader
if (minBattery >= uploader.battery) {
minBattery = uploader.battery
isCharging = uploader.isCharging ?: false
found = true
}
}
if (found) {
if (isCharging) string.append("")
string.append(minBattery)
string.append("%")
}

View file

@ -16,6 +16,7 @@ fun DeviceStatus.toJson(dateUtil: DateUtil): JSONObject =
iob?.let { iob -> openaps.put("iob", JSONObject(iob)) }
})
if (uploaderBattery != 0) it.put("uploaderBattery", uploaderBattery)
if (isCharging != null) it.put("isCharging", isCharging)
configuration?.let { configuration -> it.put("configuration", JSONObject(configuration)) }
}

View file

@ -27,6 +27,7 @@ fun DeviceStatus.toNSDeviceStatus(): NSDeviceStatus {
pump = pump,
openaps = openAps,
uploaderBattery = if (uploaderBattery != 0) uploaderBattery else null,
isCharging = isCharging,
configuration = gson.fromJson(configuration, NSDeviceStatus.Configuration::class.java),
uploader = null
)