[M] Move readMiDatabase into MiFitnessModels
This commit is contained in:
@@ -4,8 +4,13 @@ package org.hydev.wearsync.mi
|
||||
|
||||
import com.influxdb.annotations.Column
|
||||
import com.influxdb.annotations.Measurement
|
||||
import com.topjohnwu.superuser.Shell
|
||||
import org.hydev.wearsync.GSON
|
||||
import org.hydev.wearsync.reflectToString
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import java.sql.Connection
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
|
||||
@@ -54,3 +59,50 @@ data class SleepState(
|
||||
@Column
|
||||
var state: Int,
|
||||
)
|
||||
|
||||
fun readMiDatabase(): Pair<List<SleepDaytime>, List<SleepState>>
|
||||
{
|
||||
val PATH = "/data/data/com.mi.health/databases/1804898679/cn/fitness_data"
|
||||
Database.connect("jdbc:sqlite:${PATH}", "org.sqlite.JDBC")
|
||||
TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
|
||||
|
||||
val days = transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
|
||||
return@transaction SleepSegment.selectAll().mapNotNull {
|
||||
val json = it[SleepSegment.value]
|
||||
when (it[SleepSegment.key])
|
||||
{
|
||||
"watch_night_sleep" -> GSON.fromJson(json, SleepNight::class.java)
|
||||
"watch_daytime_sleep" -> GSON.fromJson(json, SleepDaytime::class.java)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println(days.filterIsInstance<SleepNight>().take(45)
|
||||
.map { it.bedtime.hours }.map { if (it < 12) it + 24 else it }.average() - 24)
|
||||
|
||||
val rawStates = days.flatMap { it.items }.sortedBy { it.start_time }
|
||||
|
||||
// Awake: 5
|
||||
rawStates.filter { it.state == 5 }.forEach { it.state = 0 }
|
||||
|
||||
// Use state 0 to fill between the start-end gaps
|
||||
var states = mutableListOf<SleepState>()
|
||||
rawStates.forEachIndexed { i, si ->
|
||||
states += si
|
||||
if (i != 0)
|
||||
{
|
||||
val last = rawStates[i - 1]
|
||||
if (last.end_time < si.start_time) states +=
|
||||
SleepState(start_time = last.end_time, end_time = si.start_time, 0)
|
||||
}
|
||||
}
|
||||
states += SleepState(start_time = rawStates.last().end_time, end_time = Instant.now(), 0)
|
||||
|
||||
// Remove duplicates
|
||||
states = states.filterIndexed { i, si -> i == 0 || si.state != states[i - 1].state }.toMutableList()
|
||||
|
||||
return days to states.toList()
|
||||
}
|
||||
@@ -3,19 +3,7 @@ package org.hydev.wearsync
|
||||
import com.influxdb.client.InfluxDBClientFactory
|
||||
import com.influxdb.client.InfluxDBClientOptions
|
||||
import com.influxdb.client.kotlin.InfluxDBClientKotlinFactory
|
||||
import org.hydev.wearsync.mi.SleepDaytime
|
||||
import org.hydev.wearsync.mi.SleepNight
|
||||
import org.hydev.wearsync.mi.SleepSegment
|
||||
import org.hydev.wearsync.mi.SleepState
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.StdOutSqlLogger
|
||||
import org.jetbrains.exposed.sql.addLogger
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import java.sql.Connection
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import org.hydev.wearsync.mi.readMiDatabase
|
||||
|
||||
|
||||
suspend fun main(args: Array<String>)
|
||||
@@ -24,51 +12,13 @@ suspend fun main(args: Array<String>)
|
||||
|
||||
val opts = InfluxDBClientOptions.builder()
|
||||
.url("https://influx.hydev.org")
|
||||
.authenticateToken("meow".toCharArray())
|
||||
.authenticateToken("TASCtBVdyrXiATQQrdsqF7r6HFOZThijJeA22v1Zu3MUVsTAd4MTGsu8Sh0Nutf5u2KaQ4ut7CP8zsJ--p6Phg==".toCharArray())
|
||||
.bucket("test").org("hydev").build()
|
||||
|
||||
val influx = InfluxDBClientKotlinFactory.create(opts)
|
||||
val influxJava = InfluxDBClientFactory.create(opts)
|
||||
|
||||
val gs = makeGson()
|
||||
val PATH = "/ws/Android/WearSyncData/com.mi.health/databases/1804898679/cn/fitness_data"
|
||||
Database.connect("jdbc:sqlite:${PATH}", "org.sqlite.JDBC")
|
||||
TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE
|
||||
|
||||
val days = transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
|
||||
return@transaction SleepSegment.selectAll().mapNotNull {
|
||||
val json = it[SleepSegment.value]
|
||||
when (it[SleepSegment.key])
|
||||
{
|
||||
"watch_night_sleep" -> gs.fromJson(json, SleepNight::class.java)
|
||||
"watch_daytime_sleep" -> gs.fromJson(json, SleepDaytime::class.java)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val rawStates = days.flatMap { it.items }.sortedBy { it.start_time }
|
||||
|
||||
// Awake: 5
|
||||
rawStates.filter { it.state == 5 }.forEach { it.state = 0 }
|
||||
|
||||
// Use state 0 to fill between the start-end gaps
|
||||
var states = mutableListOf<SleepState>()
|
||||
rawStates.forEachIndexed { i, si ->
|
||||
states += si
|
||||
if (i != 0)
|
||||
{
|
||||
val last = rawStates[i - 1]
|
||||
if (last.end_time < si.start_time) states +=
|
||||
SleepState(start_time = last.end_time, end_time = si.start_time, 0)
|
||||
}
|
||||
}
|
||||
states += SleepState(start_time = rawStates.last().end_time, end_time = Instant.now(), 0)
|
||||
|
||||
// Remove duplicates
|
||||
states = states.filterIndexed { i, si -> i == 0 || si.state != states[i - 1].state }.toMutableList()
|
||||
val (days, states) = readMiDatabase()
|
||||
|
||||
println(days.joinToString("\n"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user