Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ package com.github.umercodez.sensorspot.data.gpsdataprovider
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.json.JSONObject

private val jsonConf = Json {
encodeDefaults = true
}



@Serializable
data class GpsData(
val type: String = "android.gps",
Expand All @@ -38,7 +41,19 @@ data class GpsData(
val time: Long,
val bearing: Float
){
fun toJson() = jsonConf.encodeToString(this)
fun toJson(includeType: Boolean = true) :String {
val json = mapOf(
"type" to if (includeType) type else null,
"latitude" to latitude,
"longitude" to longitude,
"altitude" to altitude,
"accuracy" to accuracy,
"speed" to speed,
"time" to time,
"bearing" to bearing
).filterValues { it != null }
return JSONObject(json).toString()
}
}

interface GpsDataProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
* Copyright (C) 2025 Umer Farooq (umerfarooq2383@gmail.com)
*
* SensorSpot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SensorSpot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
*
*/
/*
* This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
* Copyright (C) 2025 Umer Farooq (umerfarooq2383@gmail.com)
*
* SensorSpot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SensorSpot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
*
*/
package com.github.umercodez.sensorspot.data.repositories.settings


Expand All @@ -29,6 +29,7 @@ data class Settings(
val brokerPort: Int,
val qos: Int,
val topic: String,
val dedicatedTopics: Boolean,
val connectionTimeoutSecs: Int,
val useCredentials: Boolean,
val userName: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
* Copyright (C) 2025 Umer Farooq (umerfarooq2383@gmail.com)
*
* SensorSpot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SensorSpot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
*
*/
/*
* This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
* Copyright (C) 2025 Umer Farooq (umerfarooq2383@gmail.com)
*
* SensorSpot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SensorSpot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
*
*/
package com.github.umercodez.sensorspot.data.repositories.settings

import android.content.Context
Expand Down Expand Up @@ -53,6 +53,7 @@ class SettingsRepositoryImp(
val USE_CREDENTIALS = booleanPreferencesKey("use_credentials")
val QOS = intPreferencesKey("qos")
val TOPIC = stringPreferencesKey("topic")
val DEDICATED_TOPICS = booleanPreferencesKey("dedicated_topics")
val CONNECTION_TIMEOUT_SECS = intPreferencesKey("connection_timeout_secs")
val SENSOR_SAMPLING_RATE = intPreferencesKey("sensor_sampling_rate")
}
Expand All @@ -69,6 +70,7 @@ class SettingsRepositoryImp(
useCredentials = pref[Key.USE_CREDENTIALS] ?: MqttConfigDefaults.USE_CREDENTIALS,
qos = pref[Key.QOS] ?: MqttConfigDefaults.QOS,
topic = pref[Key.TOPIC] ?: MqttConfigDefaults.TOPIC,
dedicatedTopics = pref[Key.DEDICATED_TOPICS] ?: MqttConfigDefaults.DEDICATED_TOPICS,
connectionTimeoutSecs = pref[Key.CONNECTION_TIMEOUT_SECS]
?: MqttConfigDefaults.CONNECTION_TIMEOUT_SECS,
sensorSamplingRate = pref[Key.SENSOR_SAMPLING_RATE]
Expand All @@ -94,6 +96,7 @@ class SettingsRepositoryImp(
pref[Key.USE_CREDENTIALS] = settings.useCredentials
pref[Key.QOS] = settings.qos
pref[Key.TOPIC] = settings.topic
pref[Key.DEDICATED_TOPICS] = settings.dedicatedTopics
pref[Key.CONNECTION_TIMEOUT_SECS] = settings.connectionTimeoutSecs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
package com.github.umercodez.sensorspot.data.sensoreventprovider

import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.json.JSONObject


@Serializable
data class SensorEvent(
val type: String,
val values: List<Float>,
val timestamp: Long
){
fun toJson() = Json.encodeToString(this)
fun toJson(includeType: Boolean = true): String {
val json = mapOf(
"type" to if (includeType) type else null,
"values" to values,
"timestamp" to timestamp
).filterValues { it != null }
return JSONObject(json).toString()
}
}
interface SensorEventProvider {
val events: Flow<SensorEvent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object MqttConfigDefaults{
const val BROKER_PORT = 1883
const val QOS = 0
const val TOPIC = "android/sensor"
const val DEDICATED_TOPICS = false
const val CONNECTION_TIMEOUT_SECS = 5
const val USE_CREDENTIALS = false
const val USER_NAME = ""
Expand All @@ -40,6 +41,7 @@ data class MqttConfig(
val brokerPort: Int = MqttConfigDefaults.BROKER_PORT,
val qos: Int = MqttConfigDefaults.QOS,
val topic: String = MqttConfigDefaults.TOPIC,
val dedicatedTopics: Boolean = MqttConfigDefaults.DEDICATED_TOPICS,
val connectionTimeoutSecs: Int = MqttConfigDefaults.CONNECTION_TIMEOUT_SECS,
val useCredentials: Boolean = MqttConfigDefaults.USE_CREDENTIALS,
val userName: String = MqttConfigDefaults.USER_NAME,
Expand All @@ -54,6 +56,7 @@ data class MqttConfig(
brokerPort = settings.brokerPort,
qos = settings.qos,
topic = settings.topic,
dedicatedTopics = settings.dedicatedTopics,
connectionTimeoutSecs = settings.connectionTimeoutSecs,
useCredentials = settings.useCredentials,
userName = settings.userName,
Expand Down
Loading