Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Infrared signal selection #1

Merged
merged 36 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c63d277
v2 prepare
makeevrserg Jul 16, 2024
f9a7b76
v2 prepare
makeevrserg Jul 16, 2024
54ee0ae
v2 prepare
makeevrserg Jul 17, 2024
a445da9
v2 prepare
makeevrserg Jul 17, 2024
88e3986
v2 prepare
makeevrserg Jul 17, 2024
5a24875
v2 prepare
makeevrserg Jul 17, 2024
8623965
v2 prepare
makeevrserg Jul 17, 2024
526163a
v2 prepare
makeevrserg Jul 17, 2024
b6fcdd6
v2 prepare
makeevrserg Jul 17, 2024
ee03642
v2 prepare
makeevrserg Jul 17, 2024
d18e93c
v2 prepare
makeevrserg Jul 17, 2024
d06cb68
v2 prepare
makeevrserg Jul 17, 2024
c520cfc
v2 prepare
makeevrserg Jul 17, 2024
65c83e2
v2 prepare
makeevrserg Jul 17, 2024
fee0a74
update irdb
makeevrserg Jul 17, 2024
46807cf
Fix v1 issues
makeevrserg Jul 23, 2024
c8e4641
V2 prepare
makeevrserg Jul 23, 2024
e05a932
V2 prepare
makeevrserg Jul 23, 2024
680725f
V2 prepare
makeevrserg Jul 23, 2024
889a659
V2 prepare
makeevrserg Jul 23, 2024
9ad9d6a
V2 prepare
makeevrserg Jul 23, 2024
bff5092
V2 prepare
makeevrserg Jul 23, 2024
90a94c9
V2 prepare
makeevrserg Jul 23, 2024
0807d88
V2 prepare
makeevrserg Jul 23, 2024
1a0c3a9
V2 prepare
makeevrserg Jul 24, 2024
b41e4a7
V2 prepare
makeevrserg Jul 24, 2024
848338b
V2 prepare
makeevrserg Jul 24, 2024
dcd7b86
V2 prepare
makeevrserg Jul 24, 2024
dcfb83f
V2 prepare
makeevrserg Jul 24, 2024
548f7d7
V2 prepare
makeevrserg Jul 24, 2024
a4cbd32
V2 prepare
makeevrserg Jul 25, 2024
21f0974
V2 prepare
makeevrserg Jul 25, 2024
933cd60
V2 prepare
makeevrserg Jul 25, 2024
ac1d76e
V2 prepare
makeevrserg Jul 25, 2024
2088de6
V2 prepare
makeevrserg Jul 25, 2024
b6b156f
V2 prepare
makeevrserg Jul 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ -18,6 +18,15 @@ interface DatabaseFactory {
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
Loading