Skip to content

Commit

Permalink
Update Infrared signal selection (#1)
Browse files Browse the repository at this point in the history
## Background:
This PR introduces new version of infrared signal selection with fixed several issues of previous version

## Changes:
- Database normalization fix
- Configuration generator for categories
- Configuration generator for devices
- Fix brands sorting
- Fix exception on non-existing brands
- Fix duplicate signals
- Improve parser speed
- Fix device configuration sha hash generation
  • Loading branch information
makeevrserg authored Jul 25, 2024
1 parent 3878a74 commit 732cfaa
Show file tree
Hide file tree
Showing 138 changed files with 2,163 additions and 1,288 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ build

jars
output
.env
.env
DB_FILE.*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY . .


RUN ./gradlew :web-api:shadowJar
RUN ./gradlew :modules:parser:shadowJar
RUN ./gradlew :modules:kenerator:sql:shadowJar

FROM openjdk:24-slim as parser

Expand Down
2 changes: 1 addition & 1 deletion IRDB
Submodule IRDB updated 5013 files
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```bash
# Run parser and fill DB
./gradlew :modules:parser:run
./gradlew :modules:kenerator:sql:run
# Run server
./gradlew :web-api:run
```
Expand All @@ -15,7 +15,7 @@ Output jars will be located in [generated ./jars folder](./jars)

```bash
# Shadow parser
./gradlew :modules:parser:shadowJar
./gradlew :modules:kenerator:sql:shadowJar
# Shadow server
./gradlew :web-api:shadowJar
```
Expand All @@ -26,7 +26,16 @@ Output jars will be located in [generated ./jars folder](./jars)
# Path to https://github.com/flipperdevices/IRDB/tree/dev/database
IR_FOLDER_PATH="./IRDB/database"
FBACKEND_PORT=8080
# H2 Section
# SQLite Section
# [H2, POSTGRES]
FBACKEND_DB_TYPE="H2"
# Only for H2
DB_FULL_PATH="./folder/DB_FILE"
# SQL Remote section
# Only for POSTGRES
DB_NAME=SOME_NAME
DB_HOST=192.168.0.1
DB_PORT=1234
DB_USER=ROOT
DB_PASSWORD=PASSWORD
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.parallel=true
makeevrserg.project.name=IfrBackend
makeevrserg.project.url=https://github.com/makeevrserg/IfrSample
makeevrserg.project.group=com.flipperdevices.ifrmvp.backend
makeevrserg.project.version.string=0.1.0
makeevrserg.project.version.string=0.2.0
makeevrserg.project.description=Api for IfrSample
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
# Java
Expand Down
8 changes: 8 additions & 0 deletions local.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Thu May 23 11:47:58 MSK 2024
sdk.dir=/Users/romanmakeev/Library/Android/sdk

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.net.InetAddress

object EnvKonfig {
internal enum class DBType {
H2
H2, POSTGRES
}

val IR_DATABASE_PATH: String
Expand All @@ -22,17 +22,44 @@ object EnvKonfig {
?.toIntOrNull()
?: 8080

internal val FBACKEND_DB_TYPE: DBType
get() = KSystem.getenvOrNull("FBACKEND_DB_TYPE")
?.let { type -> DBType.entries.firstOrNull { entry -> entry.name == type } }
?: DBType.H2
private object DbKonfig {
val FBACKEND_DB_TYPE: DBType
get() = KSystem.getenvOrNull("FBACKEND_DB_TYPE")
?.let { type -> DBType.entries.firstOrNull { entry -> entry.name == type } }
?: DBType.H2

internal val FBACKEND_DB_NAME: String
get() = KSystem.getenvOrNull("DB_FULL_PATH")
?: BuildKonfig.FALLBACK_DB_FULL_PATH
val FBACKEND_DB_FULL_PATH: String
get() = KSystem.getenvOrNull("DB_FULL_PATH")
?: BuildKonfig.FALLBACK_DB_FULL_PATH

val DB_NAME: String
get() = KSystem.requireEnv("DB_NAME")

val DB_HOST: String
get() = KSystem.requireEnv("DB_HOST")

val DB_PORT: String
get() = KSystem.requireEnv("DB_PORT")

val DB_USER: String
get() = KSystem.requireEnv("DB_USER")

val DB_PASSWORD: String
get() = KSystem.requireEnv("DB_PASSWORD")
}

val signalDatabaseConnection: DBConnection
get() = when (EnvKonfig.FBACKEND_DB_TYPE) {
EnvKonfig.DBType.H2 -> DBConnection.H2(EnvKonfig.FBACKEND_DB_NAME)
get() = when (DbKonfig.FBACKEND_DB_TYPE) {
DBType.H2 -> DBConnection.H2(
path = DbKonfig.FBACKEND_DB_FULL_PATH
)

DBType.POSTGRES -> DBConnection.Postgres(
host = DbKonfig.DB_HOST,
port = DbKonfig.DB_PORT,
user = DbKonfig.DB_USER,
password = DbKonfig.DB_PASSWORD,
name = DbKonfig.DB_NAME
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ internal object KSystem {
}
return value
}

fun requireEnv(key: String): String {
return getenvOrNull(key) ?: error { "Environment not found: $key" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ package com.flipperdevices.ifrmvp.backend.envkonfig.model

sealed class DBConnection(val driver: String) {
class H2(val path: String) : DBConnection("org.h2.Driver")
class Postgres(
val host: String,
val port: String,
val user: String,
val password: String,
val name: String
) : DBConnection("org.postgresql.Driver")
}
2 changes: 0 additions & 2 deletions modules/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ dependencies {
implementation(libs.exposed.jdbc)
implementation(libs.sql.driver.mysql)
// Services
implementation("com.flipperdevices.ifrmvp.backend:shared-ui-model")
implementation(projects.modules.sharedUiModel)
implementation(projects.modules.buildKonfig)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ interface DatabaseFactory {
return when (dbConnection) {
is DBConnection.H2 -> {
Database.connect(
url = "jdbc:h2:${dbConnection.path};DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false",
url = "jdbc:h2:${dbConnection.path};DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL",
driver = dbConnection.driver
)
}

is DBConnection.Postgres -> {
Database.connect(
url = "jdbc:postgresql://${dbConnection.host}:${dbConnection.port}/${dbConnection.name}",
driver = dbConnection.driver,
user = dbConnection.user,
password = dbConnection.password
)
}
}.also(configure)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ dependencies {
implementation(libs.exposed.core)
implementation(libs.exposed.dao)
implementation("com.h2database:h2:2.2.224")
implementation("org.postgresql:postgresql:42.7.1")
// Local
implementation(projects.modules.buildKonfig)
implementation(projects.modules.sharedBackendModel)
implementation(projects.modules.sharedUiModel)
implementation(projects.modules.model)
implementation(projects.modules.core)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.flipperdevices.ifrmvp.backend.db.signal.dao

import com.flipperdevices.ifrmvp.backend.model.BrandModel
import com.flipperdevices.ifrmvp.backend.model.DeviceCategory
import com.flipperdevices.ifrmvp.backend.model.IfrFileModel

interface TableDao {
suspend fun getCategoryById(categoryId: Long): DeviceCategory
suspend fun getBrandById(brandId: Long): BrandModel
suspend fun ifrFileById(irFileId: Long): IfrFileModel

companion object {
suspend fun TableDao.getCategoryByBrandId(brandId: Long): DeviceCategory {
val brandModel = getBrandById(brandId)
return getCategoryById(brandModel.categoryId)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.flipperdevices.ifrmvp.backend.db.signal.dao

import com.flipperdevices.ifrmvp.backend.db.signal.exception.TableDaoException
import com.flipperdevices.ifrmvp.backend.db.signal.table.BrandTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.CategoryMetaTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.CategoryTable
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileTable
import com.flipperdevices.ifrmvp.backend.model.BrandModel
import com.flipperdevices.ifrmvp.backend.model.CategoryManifest
import com.flipperdevices.ifrmvp.backend.model.CategoryMeta
import com.flipperdevices.ifrmvp.backend.model.DeviceCategory
import com.flipperdevices.ifrmvp.backend.model.IfrFileModel
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction

internal class TableDaoImpl(private val database: Database) : TableDao {
override suspend fun getCategoryById(
categoryId: Long
): DeviceCategory = newSuspendedTransaction(db = database) {
CategoryTable
.selectAll()
.where { CategoryTable.id eq categoryId }
.limit(1)
.map { r ->
DeviceCategory(
id = r[CategoryTable.id].value,
folderName = r[CategoryTable.folderName],
meta = CategoryMetaTable.selectAll()
.where { CategoryMetaTable.categoryId eq categoryId }
.limit(1)
.map {
CategoryMeta(
iconPngBase64 = it[CategoryMetaTable.iconPngBase64],
iconSvgBase64 = it[CategoryMetaTable.iconSvgBase64],
manifest = CategoryManifest(
displayName = it[CategoryMetaTable.displayName],
singularDisplayName = it[CategoryMetaTable.singularDisplayName],
)
)
}.firstOrNull() ?: throw TableDaoException.CategoryMeta(categoryId)
)
}.firstOrNull() ?: throw TableDaoException.CategoryNotFound(categoryId)
}

override suspend fun getBrandById(
brandId: Long
): BrandModel = newSuspendedTransaction(db = database) {
BrandTable
.selectAll()
.where { BrandTable.id eq brandId }
.limit(1)
.map {
BrandModel(
id = it[BrandTable.id].value,
folderName = it[BrandTable.folderName],
categoryId = it[BrandTable.categoryId].value
)
}.firstOrNull() ?: throw TableDaoException.BrandNotFound(brandId)
}

override suspend fun ifrFileById(
irFileId: Long
): IfrFileModel = newSuspendedTransaction(db = database) {
InfraredFileTable
.selectAll()
.where { InfraredFileTable.id eq irFileId }
.limit(1)
.map {
IfrFileModel(
id = it[InfraredFileTable.id].value,
brandId = it[InfraredFileTable.brandId].value,
fileName = it[InfraredFileTable.fileName],
folderName = it[InfraredFileTable.folderName]
)
}.firstOrNull() ?: throw TableDaoException.IrFileNotFound(irFileId)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.flipperdevices.ifrmvp.backend.db.signal.di

import com.flipperdevices.ifrmvp.backend.db.signal.dao.TableDao
import com.flipperdevices.ifrmvp.backend.db.signal.dao.TableDaoImpl
import com.flipperdevices.ifrmvp.backend.db.signal.di.factory.SignalDatabaseFactory
import com.flipperdevices.ifrmvp.backend.envkonfig.model.DBConnection
import org.jetbrains.exposed.sql.Database

interface SignalApiModule {
val database: Database
val tableDao: TableDao

class Default(
signalDbConnection: DBConnection
) : SignalApiModule {
override val database: Database by lazy {
SignalDatabaseFactory(signalDbConnection).create()
}
override val tableDao: TableDao by lazy {
TableDaoImpl(database)
}
}
}
Loading

0 comments on commit 732cfaa

Please sign in to comment.