Skip to content

Commit

Permalink
Merge pull request #509 from ruuvi/version/1.5.45
Browse files Browse the repository at this point in the history
Version/1.5.45
  • Loading branch information
andreevdenis committed Sep 23, 2021
2 parents 48aa0b7 + ed76fa5 commit 9d09718
Show file tree
Hide file tree
Showing 54 changed files with 780 additions and 554 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Expand Up @@ -15,8 +15,8 @@ android {
applicationId "com.ruuvi.station"
minSdkVersion 21
targetSdkVersion 30
versionCode 15033
versionName "1.5.33"
versionCode 15045
versionName "1.5.45"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down Expand Up @@ -79,7 +79,7 @@ dependencies {
implementation 'com.opencsv:opencsv:3.10'

implementation 'com.github.ruuvi:com.ruuvi.bluetooth:1.2.1'
implementation 'com.github.ruuvi:com.ruuvi.bluetooth.default:1.2.5'
implementation 'com.github.ruuvi:com.ruuvi.bluetooth.default:1.2.6'
//implementation project(':bluetooth_library')
//implementation project(':default_bluetooth_library')

Expand Down
Expand Up @@ -38,7 +38,7 @@ class AboutActivity : AppCompatActivity(), KodeinAware {

supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title = null
supportActionBar?.setIcon(R.drawable.logo_white)
supportActionBar?.setIcon(R.drawable.logo_2021)

infoText.movementMethod = LinkMovementMethod.getInstance()
operationsText.movementMethod = LinkMovementMethod.getInstance()
Expand Down
Expand Up @@ -52,13 +52,14 @@ class RuuviScannerApplication : Application(), KodeinAware {
override fun onBecameForeground() {
isInForeground = true
defaultOnTagFoundListener.isForeground = true
updateNetwork()
networkDataSyncInteractor.startAutoRefresh()
runtimeBehavior.refreshFeatureFlags()
}

override fun onBecameBackground() {
isInForeground = false
defaultOnTagFoundListener.isForeground = false
networkDataSyncInteractor.stopAutoRefresh()
}
}

Expand Down Expand Up @@ -89,8 +90,6 @@ class RuuviScannerApplication : Application(), KodeinAware {
foreground.addListener(listener)

setupExperimentalFeatures()

updateNetwork()
}

private fun setupDependencyInjection() {
Expand All @@ -111,10 +110,4 @@ class RuuviScannerApplication : Application(), KodeinAware {
runtimeFeatureFlagProvider.setFeatureEnabled(FeatureFlag.RUUVI_NETWORK, true)
}
}

private fun updateNetwork() {
if (networkInteractor.signedIn && Date(preferencesRepository.getLastSyncDate()).diffGreaterThan(60*1000)) {
networkDataSyncInteractor.syncNetworkData()
}
}
}
Expand Up @@ -150,4 +150,6 @@ class PreferencesRepository(
fun setLocale(locale: String) {
preferences.locale = locale
}

fun getUserEmail() = preferences.networkEmail
}
Expand Up @@ -31,7 +31,7 @@ object BluetoothScannerInjectionModule {
BluetoothLibrary.getBluetoothInteractor(instance(), instance(), instance())
}

bind<BluetoothGattInteractor>() with singleton { BluetoothGattInteractor(instance(), instance(), instance(), instance()) }
bind<BluetoothGattInteractor>() with singleton { BluetoothGattInteractor(instance(), instance(), instance(), instance(), instance()) }

bind<BluetoothStateReceiver>() with singleton { BluetoothStateReceiver(instance(), instance()) }

Expand Down
Expand Up @@ -9,6 +9,7 @@ import com.ruuvi.station.database.domain.SensorHistoryRepository
import com.ruuvi.station.database.domain.SensorSettingsRepository
import com.ruuvi.station.database.domain.TagRepository
import com.ruuvi.station.database.tables.TagSensorReading
import com.ruuvi.station.firebase.domain.FirebaseInteractor
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import timber.log.Timber
Expand All @@ -18,7 +19,8 @@ class BluetoothGattInteractor (
private val interactor: BluetoothInteractor,
private val tagRepository: TagRepository,
private val sensorSettingsRepository: SensorSettingsRepository,
private val sensorHistoryRepository: SensorHistoryRepository
private val sensorHistoryRepository: SensorHistoryRepository,
private val firebaseInteractor: FirebaseInteractor
) {
private val syncStatus = MutableStateFlow<GattSyncStatus?> (null)
val syncStatusFlow: StateFlow<GattSyncStatus?> = syncStatus
Expand Down Expand Up @@ -81,6 +83,8 @@ class BluetoothGattInteractor (
}

fun saveGattReadings(sensorId: String, data: List<LogReading>) {
Timber.d("saveGattReadings")
firebaseInteractor.logGattSync(data.size)
val tagReadingList = mutableListOf<TagSensorReading>()
val sensorSettings = sensorSettingsRepository.getSensorSettings(sensorId)
data.forEach { logReading ->
Expand Down
Expand Up @@ -11,7 +11,7 @@ import org.kodein.di.generic.*
object CalibrationInjectionModule {
val module = Kodein.Module(CalibrationInjectionModule.javaClass.name) {
bind<CalibrationInteractor>() with singleton {
CalibrationInteractor(instance(), instance(), instance(), instance())
CalibrationInteractor(instance(), instance(), instance(), instance(), instance())
}

bind<CalibrateTemperatureViewModel>() with factory { args: CalibrationViewModelArgs ->
Expand Down
@@ -1,6 +1,7 @@
package com.ruuvi.station.calibration.domain

import com.ruuvi.station.calibration.model.CalibrationInfo
import com.ruuvi.station.database.domain.SensorHistoryRepository
import com.ruuvi.station.database.domain.SensorSettingsRepository
import com.ruuvi.station.database.domain.TagRepository
import com.ruuvi.station.database.tables.RuuviTagEntity
Expand All @@ -13,6 +14,7 @@ class CalibrationInteractor (
val tagRepository: TagRepository,
val sensorSettingsRepository: SensorSettingsRepository,
val unitsConverter: UnitsConverter,
val sensorHistoryRepository: SensorHistoryRepository,
private val networkInteractor: RuuviNetworkInteractor
) {
fun getSensorData(sensorId: String): RuuviTagEntity? = tagRepository.getTagById(sensorId)
Expand All @@ -29,6 +31,7 @@ class CalibrationInteractor (
val offset = targetCelsius - fromTemperature
sensorSettingsRepository.setSensorTemperatureCalibrationOffset(sensorId, offset)
saveCalibrationToNetwork(sensorId)
recalibrateHistory(sensorId)
}
}

Expand All @@ -37,6 +40,7 @@ class CalibrationInteractor (
fun clearTemperatureCalibration(sensorId: String) {
sensorSettingsRepository.clearTemperatureCalibration(sensorId)
saveCalibrationToNetwork(sensorId)
recalibrateHistory(sensorId)
}

private fun isTemperatureCalibrated(settings: SensorSettings?): Boolean = settings?.temperatureOffset ?: 0.0 != 0.0
Expand Down Expand Up @@ -115,6 +119,7 @@ class CalibrationInteractor (
fun clearPressureCalibration(sensorId: String) {
sensorSettingsRepository.clearPressureCalibration(sensorId)
saveCalibrationToNetwork(sensorId)
recalibrateHistory(sensorId)
}

fun calibratePressure(sensorId: String, targetValue: Double) {
Expand All @@ -125,6 +130,7 @@ class CalibrationInteractor (
val offset = targetPascal - fromPressure
sensorSettingsRepository.setSensorPressureCalibrationOffset(sensorId, offset)
saveCalibrationToNetwork(sensorId)
recalibrateHistory(sensorId)
}
}

Expand All @@ -135,6 +141,7 @@ class CalibrationInteractor (
fun clearHumidityCalibration(sensorId: String) {
sensorSettingsRepository.clearHumidityCalibration(sensorId)
saveCalibrationToNetwork(sensorId)
recalibrateHistory(sensorId)
}

fun calibrateHumidity(sensorId: String, targetValue: Double) {
Expand All @@ -144,10 +151,22 @@ class CalibrationInteractor (
val offset = targetValue - fromHumidity
sensorSettingsRepository.setSensorHumidityOffset(sensorId, offset)
saveCalibrationToNetwork(sensorId)
recalibrateHistory(sensorId)
}
}

fun saveCalibrationToNetwork(sensorId: String) {
networkInteractor.updateSensorCalibration(sensorId)
}

fun recalibrateHistory(sensorId: String) {
val sensorSettings = sensorSettingsRepository.getSensorSettings(sensorId)
sensorSettings?.let {
recalibrateHistory(it)
}
}

fun recalibrateHistory(sensorSettings: SensorSettings) {
sensorHistoryRepository.recalibrate(sensorSettings)
}
}
Expand Up @@ -67,7 +67,7 @@ class DashboardActivity : AppCompatActivity(), KodeinAware {
permissionsInteractor = PermissionsInteractor(this)

supportActionBar?.title = null
supportActionBar?.setIcon(R.drawable.logo)
supportActionBar?.setIcon(R.drawable.logo_2021)

setupViewModel()
setupDrawer()
Expand Down Expand Up @@ -153,33 +153,6 @@ class DashboardActivity : AppCompatActivity(), KodeinAware {
return@setNavigationItemSelectedListener true
}

syncLayout.setOnClickListener {
viewModel.networkDataSync()
}

viewModel.syncResultObserve.observe(this) {syncResult ->
val message = when (syncResult.type) {
NetworkSyncResultType.NONE -> ""
NetworkSyncResultType.SUCCESS -> getString(R.string.network_sync_result_success)
NetworkSyncResultType.EXCEPTION -> getString(R.string.network_sync_result_exception, syncResult.errorMessage)
NetworkSyncResultType.NOT_LOGGED -> getString(R.string.network_sync_result_not_logged)
}
if (message.isNotEmpty()) {
Snackbar.make(mainDrawerLayout, message, Snackbar.LENGTH_SHORT).show()
viewModel.syncResultShowed()
}
}

viewModel.syncInProgressObserve.observe(this) {
if (it) {
Timber.d("Sync in progress")
syncNetworkButton.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_indefinitely))
} else {
Timber.d("Sync not in progress")
syncNetworkButton.clearAnimation()
}
}

viewModel.userEmail.observe(this) {
var user = it
if (user.isNullOrEmpty()) {
Expand All @@ -189,28 +162,12 @@ class DashboardActivity : AppCompatActivity(), KodeinAware {
signedIn = true
}
updateMenu(signedIn)
loggedUserTextView.text = getString(R.string.network_user, user)
}

viewModel.syncStatus.observe(this) {syncStatus->
if (syncStatus.syncInProgress) {
syncStatusTextView.text = getString(R.string.connected_reading_info)
} else {
val lastSyncString =
if (syncStatus.lastSync == Long.MIN_VALUE) {
getString(R.string.never)
} else {
Date(syncStatus.lastSync).describingTimeSince(this)
}
syncStatusTextView.text = getString(R.string.network_synced, lastSyncString)
}
loggedUserTextView.text = user
}
}

private fun login(signedIn: Boolean) {
if (signedIn == false) {
SignInActivity.start(this)
} else {
if (signedIn) {
val builder = AlertDialog.Builder(this)
with(builder)
{
Expand All @@ -223,6 +180,9 @@ class DashboardActivity : AppCompatActivity(), KodeinAware {
}
show()
}
} else {
SignInActivity.start(this)

}
}

Expand All @@ -239,6 +199,7 @@ class DashboardActivity : AppCompatActivity(), KodeinAware {
}

private fun updateMenu(signed: Boolean) {
networkLayout.isVisible = viewModel.userEmail.value?.isNotEmpty() == true
val loginMenuItem = navigationView.menu.findItem(R.id.loginMenuItem)
loginMenuItem?.let {
it.title = if (signed) {
Expand Down
Expand Up @@ -74,10 +74,6 @@ class DashboardActivityViewModel(
}
}

fun networkDataSync() {
networkDataSyncInteractor.syncNetworkData()
}

fun syncResultShowed() {
networkDataSyncInteractor.syncStatusShowed()
}
Expand All @@ -89,6 +85,8 @@ class DashboardActivityViewModel(
}

fun signOut() {
tokenRepository.signOut()
tokenRepository.signOut {
updateTags()
}
}
}
Expand Up @@ -13,7 +13,15 @@ import com.ruuvi.station.database.tables.*
class LocalDatabase {
companion object {
const val NAME = "LocalDatabase"
const val VERSION = 23
const val VERSION = 24
}

@Migration(version = 24, database = LocalDatabase::class)
class Migration24(table: Class<SensorSettings?>?) : AlterTableMigration<SensorSettings?>(table) {
override fun onPreMigrate() {
super.onPreMigrate()
addColumn(SQLiteType.INTEGER, "networkSensor")
}
}

@Migration(version = 23, database = LocalDatabase::class)
Expand Down
Expand Up @@ -5,6 +5,7 @@ import com.raizlabs.android.dbflow.kotlinextensions.from
import com.raizlabs.android.dbflow.sql.language.Method
import com.raizlabs.android.dbflow.sql.language.SQLite
import com.raizlabs.android.dbflow.sql.queriable.StringQuery
import com.ruuvi.station.database.tables.SensorSettings
import com.ruuvi.station.database.tables.TagSensorReading
import com.ruuvi.station.database.tables.TagSensorReading_Table
import timber.log.Timber
Expand Down Expand Up @@ -143,6 +144,25 @@ class SensorHistoryRepository {
}
}

fun recalibrate(sensorSettings: SensorSettings) {
fun executeSQL(sql: String) {
Timber.d("executeSQL $sql")
FlowManager.getWritableDatabase(LocalDatabase.NAME).execSQL(sql)
}
val updateQuery = """
update TagSensorReading
set
temperature = temperature - ifnull(temperatureOffset, 0) + ${sensorSettings.temperatureOffset ?: 0.0},
humidity = humidity - ifnull(humidityOffset, 0) + ${sensorSettings.humidityOffset ?: 0.0},
pressure = pressure - ifnull(pressureOffset, 0) + ${sensorSettings.pressureOffset ?: 0.0},
temperatureOffset = ${sensorSettings.temperatureOffset ?: 0.0},
humidityOffset = ${sensorSettings.humidityOffset ?: 0.0},
pressureOffset = ${sensorSettings.pressureOffset ?: 0.0}
where ruuviTagId = "${sensorSettings.id}"
"""
executeSQL(updateQuery)
}

companion object {
const val POINT_THRESHOLD = 1000
const val HIGH_DENSITY_INTERVAL_MINUTES = 15
Expand Down
Expand Up @@ -104,9 +104,10 @@ class SensorSettingsRepository {
.execute()
}

fun setSensorOwner(sensorId: String, owner: String) {
fun setSensorOwner(sensorId: String, owner: String, isNetworkSensor: Boolean) {
var settings = getSensorSettingsOrCreate(sensorId)
settings.owner = owner
settings.networkSensor = isNetworkSensor
settings.update()
}

Expand Down

0 comments on commit 9d09718

Please sign in to comment.