From 2bb3a9641975018b4227f7a02012b57db5a6facc Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Tue, 24 Jan 2023 22:15:02 -0500 Subject: [PATCH] [+] Create sql test to read db and upload data --- .../test/java/org/hydev/wearsync/SqlTest.kt | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 app/src/test/java/org/hydev/wearsync/SqlTest.kt diff --git a/app/src/test/java/org/hydev/wearsync/SqlTest.kt b/app/src/test/java/org/hydev/wearsync/SqlTest.kt new file mode 100644 index 0000000..8fd0a13 --- /dev/null +++ b/app/src/test/java/org/hydev/wearsync/SqlTest.kt @@ -0,0 +1,95 @@ +package org.hydev.wearsync + +import com.google.gson.* +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.* + +fun Long.ensureMs() = if (this < 99999999999) this * 1000 else this +fun makeGson(): Gson { + val sd = JsonSerializer { src, _, _ -> if (src == null) null else JsonPrimitive(src.time / 1000) } + val dd = JsonDeserializer { json, _, _ -> if (json == null) null else Date(json.asLong.ensureMs()) } + val si = JsonSerializer { src, _, _ -> if (src == null) null else JsonPrimitive(src.epochSecond) } + val di = JsonDeserializer { json, _, _ -> if (json == null) null else Date(json.asLong.ensureMs()).toInstant() } + + return GsonBuilder() + .registerTypeAdapter(Date::class.java, sd) + .registerTypeAdapter(Date::class.java, dd) + .registerTypeAdapter(Instant::class.java, si) + .registerTypeAdapter(Instant::class.java, di) + .create() +} + + +suspend fun main(args: Array) +{ + println("Hi") + + val opts = InfluxDBClientOptions.builder() + .url("https://influx.hydev.org") + .authenticateToken("meow".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() + 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() + + println(days.joinToString("\n")) + + influxJava.dropAll("test", "hydev") + + influx add days + influx add states +} \ No newline at end of file