Skip to content

Commit 253d89a

Browse files
authored
Merge pull request #2 from olegbilovus/feat-mqtt_dedicated_topics
feat: add dedicated topics support for MQTT configuration
2 parents 43f5ec3 + 9a0800d commit 253d89a

File tree

14 files changed

+753
-671
lines changed

14 files changed

+753
-671
lines changed

data/src/main/java/com/github/umercodez/sensorspot/data/gpsdataprovider/GpsDataProvider.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ package com.github.umercodez.sensorspot.data.gpsdataprovider
2222
import kotlinx.coroutines.flow.SharedFlow
2323
import kotlinx.serialization.Serializable
2424
import kotlinx.serialization.json.Json
25+
import org.json.JSONObject
2526

2627
private val jsonConf = Json {
2728
encodeDefaults = true
2829
}
2930

31+
32+
3033
@Serializable
3134
data class GpsData(
3235
val type: String = "android.gps",
@@ -38,7 +41,19 @@ data class GpsData(
3841
val time: Long,
3942
val bearing: Float
4043
){
41-
fun toJson() = jsonConf.encodeToString(this)
44+
fun toJson(includeType: Boolean = true) :String {
45+
val json = mapOf(
46+
"type" to if (includeType) type else null,
47+
"latitude" to latitude,
48+
"longitude" to longitude,
49+
"altitude" to altitude,
50+
"accuracy" to accuracy,
51+
"speed" to speed,
52+
"time" to time,
53+
"bearing" to bearing
54+
).filterValues { it != null }
55+
return JSONObject(json).toString()
56+
}
4257
}
4358

4459
interface GpsDataProvider {

data/src/main/java/com/github/umercodez/sensorspot/data/repositories/settings/Settings.kt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
/*
2-
* This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
3-
* Copyright (C) 2025 Umer Farooq (umerfarooq2383@gmail.com)
4-
*
5-
* SensorSpot is free software: you can redistribute it and/or modify
6-
* it under the terms of the GNU General Public License as published by
7-
* the Free Software Foundation, either version 3 of the License, or
8-
* (at your option) any later version.
9-
*
10-
* SensorSpot is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU General Public License
16-
* along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
17-
*
18-
*/
1+
/*
2+
* This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
3+
* Copyright (C) 2025 Umer Farooq (umerfarooq2383@gmail.com)
4+
*
5+
* SensorSpot is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* SensorSpot is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
*/
1919
package com.github.umercodez.sensorspot.data.repositories.settings
2020

2121

@@ -29,6 +29,7 @@ data class Settings(
2929
val brokerPort: Int,
3030
val qos: Int,
3131
val topic: String,
32+
val dedicatedTopics: Boolean,
3233
val connectionTimeoutSecs: Int,
3334
val useCredentials: Boolean,
3435
val userName: String,

data/src/main/java/com/github/umercodez/sensorspot/data/repositories/settings/SettingsRepositoryImp.kt

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
/*
2-
* This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
3-
* Copyright (C) 2025 Umer Farooq (umerfarooq2383@gmail.com)
4-
*
5-
* SensorSpot is free software: you can redistribute it and/or modify
6-
* it under the terms of the GNU General Public License as published by
7-
* the Free Software Foundation, either version 3 of the License, or
8-
* (at your option) any later version.
9-
*
10-
* SensorSpot is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU General Public License
16-
* along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
17-
*
18-
*/
1+
/*
2+
* This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
3+
* Copyright (C) 2025 Umer Farooq (umerfarooq2383@gmail.com)
4+
*
5+
* SensorSpot is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* SensorSpot is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
*/
1919
package com.github.umercodez.sensorspot.data.repositories.settings
2020

2121
import android.content.Context
@@ -53,6 +53,7 @@ class SettingsRepositoryImp(
5353
val USE_CREDENTIALS = booleanPreferencesKey("use_credentials")
5454
val QOS = intPreferencesKey("qos")
5555
val TOPIC = stringPreferencesKey("topic")
56+
val DEDICATED_TOPICS = booleanPreferencesKey("dedicated_topics")
5657
val CONNECTION_TIMEOUT_SECS = intPreferencesKey("connection_timeout_secs")
5758
val SENSOR_SAMPLING_RATE = intPreferencesKey("sensor_sampling_rate")
5859
}
@@ -69,6 +70,7 @@ class SettingsRepositoryImp(
6970
useCredentials = pref[Key.USE_CREDENTIALS] ?: MqttConfigDefaults.USE_CREDENTIALS,
7071
qos = pref[Key.QOS] ?: MqttConfigDefaults.QOS,
7172
topic = pref[Key.TOPIC] ?: MqttConfigDefaults.TOPIC,
73+
dedicatedTopics = pref[Key.DEDICATED_TOPICS] ?: MqttConfigDefaults.DEDICATED_TOPICS,
7274
connectionTimeoutSecs = pref[Key.CONNECTION_TIMEOUT_SECS]
7375
?: MqttConfigDefaults.CONNECTION_TIMEOUT_SECS,
7476
sensorSamplingRate = pref[Key.SENSOR_SAMPLING_RATE]
@@ -94,6 +96,7 @@ class SettingsRepositoryImp(
9496
pref[Key.USE_CREDENTIALS] = settings.useCredentials
9597
pref[Key.QOS] = settings.qos
9698
pref[Key.TOPIC] = settings.topic
99+
pref[Key.DEDICATED_TOPICS] = settings.dedicatedTopics
97100
pref[Key.CONNECTION_TIMEOUT_SECS] = settings.connectionTimeoutSecs
98101
}
99102

data/src/main/java/com/github/umercodez/sensorspot/data/sensoreventprovider/SensorEventProvider.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919
package com.github.umercodez.sensorspot.data.sensoreventprovider
2020

2121
import kotlinx.coroutines.flow.Flow
22-
import kotlinx.serialization.Serializable
23-
import kotlinx.serialization.json.Json
22+
import org.json.JSONObject
23+
2424

25-
@Serializable
2625
data class SensorEvent(
2726
val type: String,
2827
val values: List<Float>,
2928
val timestamp: Long
3029
){
31-
fun toJson() = Json.encodeToString(this)
30+
fun toJson(includeType: Boolean = true): String {
31+
val json = mapOf(
32+
"type" to if (includeType) type else null,
33+
"values" to values,
34+
"timestamp" to timestamp
35+
).filterValues { it != null }
36+
return JSONObject(json).toString()
37+
}
3238
}
3339
interface SensorEventProvider {
3440
val events: Flow<SensorEvent>

data/src/main/java/com/github/umercodez/sensorspot/data/sensorpublisher/MqttConfig.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ object MqttConfigDefaults{
2626
const val BROKER_PORT = 1883
2727
const val QOS = 0
2828
const val TOPIC = "android/sensor"
29+
const val DEDICATED_TOPICS = false
2930
const val CONNECTION_TIMEOUT_SECS = 5
3031
const val USE_CREDENTIALS = false
3132
const val USER_NAME = ""
@@ -40,6 +41,7 @@ data class MqttConfig(
4041
val brokerPort: Int = MqttConfigDefaults.BROKER_PORT,
4142
val qos: Int = MqttConfigDefaults.QOS,
4243
val topic: String = MqttConfigDefaults.TOPIC,
44+
val dedicatedTopics: Boolean = MqttConfigDefaults.DEDICATED_TOPICS,
4345
val connectionTimeoutSecs: Int = MqttConfigDefaults.CONNECTION_TIMEOUT_SECS,
4446
val useCredentials: Boolean = MqttConfigDefaults.USE_CREDENTIALS,
4547
val userName: String = MqttConfigDefaults.USER_NAME,
@@ -54,6 +56,7 @@ data class MqttConfig(
5456
brokerPort = settings.brokerPort,
5557
qos = settings.qos,
5658
topic = settings.topic,
59+
dedicatedTopics = settings.dedicatedTopics,
5760
connectionTimeoutSecs = settings.connectionTimeoutSecs,
5861
useCredentials = settings.useCredentials,
5962
userName = settings.userName,

0 commit comments

Comments
 (0)