diff --git a/app/build.gradle b/app/build.gradle index 604c35e..18a2e27 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,184 +1,188 @@ plugins { id 'com.google.protobuf' id 'kotlin-kapt' id 'kotlin-android' id 'com.android.application' } android { compileSdk 35 namespace "it.reyboz.bustorino" defaultConfig { applicationId "it.reyboz.bustorino" - minSdkVersion 21 + minSdkVersion 24 targetSdkVersion 35 buildToolsVersion = '35.0.1' versionCode 65 versionName "2.4.2" vectorDrawables.useSupportLibrary = true multiDexEnabled true javaCompileOptions { annotationProcessorOptions { arguments = ["room.schemaLocation": "$projectDir/assets/schemas/".toString()] } } testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } testOptions { unitTests.returnDefaultValues = true } sourceSets { androidTest.assets.srcDirs += files("$projectDir/assets/schemas/".toString()) } buildTypes { debug { applicationIdSuffix ".debug" versionNameSuffix "-dev" } gitpull{ applicationIdSuffix ".gitdev" versionNameSuffix "-gitdev" } } repositories { mavenCentral() mavenLocal() } dependencies { //new libraries } compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlin { jvmToolchain 17 } lint { abortOnError false } androidResources { generateLocaleConfig true } buildFeatures{ buildConfig = true } splits{ abi{ enable true reset() // Specifies a list of ABIs for Gradle to create APKs for. include "x86", "x86_64","armeabi-v7a", "arm64-v8a" universalApk true } } + packagingOptions { resources.excludes.add("META-INF/*") } + } protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.22.3' } generateProtoTasks { all().each { task -> task.builtins { java { option 'lite' } } } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" implementation 'androidx.legacy:legacy-support-v4:1.0.0' // Guava implementation for DBUpdateWorker implementation 'com.google.guava:guava:32.0.1-android' implementation "androidx.fragment:fragment-ktx:$fragment_version" implementation "androidx.activity:activity:$activity_version" implementation "androidx.annotation:annotation:1.9.1" implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" implementation "androidx.appcompat:appcompat:$appcompat_version" implementation "androidx.appcompat:appcompat-resources:$appcompat_version" implementation "androidx.preference:preference:$preference_version" implementation "androidx.work:work-runtime:$work_version" implementation "androidx.work:work-runtime-ktx:$work_version" implementation "com.google.android.material:material:1.12.0" implementation 'androidx.constraintlayout:constraintlayout:2.2.1' implementation "androidx.coordinatorlayout:coordinatorlayout:1.3.0" implementation 'org.jsoup:jsoup:1.15.3' implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1' implementation 'com.android.volley:volley:1.2.1' //maplibre implementation 'org.maplibre.gl:android-sdk:11.8.6' implementation 'org.maplibre.gl:android-sdk-turf:6.0.1' implementation 'org.maplibre.gl:android-plugin-annotation-v9:3.0.2' // remember to enable maven repo jitpack.io when wanting to use osmbonuspack //implementation 'com.github.MKergall:osmbonuspack:6.9.0' // ACRA implementation "ch.acra:acra-mail:$acra_version" implementation "ch.acra:acra-dialog:$acra_version" // google transit realtime implementation 'com.google.protobuf:protoc:3.22.3' implementation 'com.google.protobuf:protobuf-javalite:3.22.3' // mqtt library - implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5' - implementation 'com.github.hannesa2:paho.mqtt.android:4.4' + //implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5' + //implementation 'com.github.hannesa2:paho.mqtt.android:4.4' + implementation("com.hivemq:hivemq-mqtt-client:1.3.10") + implementation(platform("com.hivemq:hivemq-mqtt-client-websocket:1.3.10")) // ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" // LiveData implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" // Lifecycles only (without ViewModel or LiveData) implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" // Legacy implementation 'androidx.legacy:legacy-support-v4:1.0.0' // Room components implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-ktx:$room_version" kapt "androidx.room:room-compiler:$room_version" //multidex - we need this to build the app implementation "androidx.multidex:multidex:$multidex_version" implementation 'de.siegmar:fastcsv:2.2.2' testImplementation 'junit:junit:4.12' implementation 'junit:junit:4.12' implementation "androidx.test.ext:junit:1.1.5" implementation "androidx.test:core:$androidXTestVersion" implementation "androidx.test:runner:$androidXTestVersion" implementation "androidx.room:room-testing:$room_version" androidTestImplementation "androidx.test.ext:junit:1.1.5" androidTestImplementation "androidx.test:core:$androidXTestVersion" androidTestImplementation "androidx.test:runner:$androidXTestVersion" androidTestImplementation "androidx.test:rules:$androidXTestVersion" androidTestImplementation "androidx.room:room-testing:$room_version" } diff --git a/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt b/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt index 2b00d9d..647b5c3 100644 --- a/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt +++ b/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt @@ -1,411 +1,426 @@ package it.reyboz.bustorino.backend.mato -import android.app.Notification import android.app.NotificationManager import android.content.Context -import android.os.Build import android.util.Log import androidx.lifecycle.LifecycleOwner -import info.mqtt.android.service.Ack -import info.mqtt.android.service.MqttAndroidClient -import info.mqtt.android.service.QoS -import it.reyboz.bustorino.backend.Notifications +import com.hivemq.client.mqtt.MqttClient +import com.hivemq.client.mqtt.lifecycle.MqttClientAutoReconnect +import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient +import com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder +import com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate -import org.eclipse.paho.client.mqttv3.* import org.json.JSONArray import org.json.JSONException import java.lang.ref.WeakReference import java.util.* +import java.util.concurrent.TimeUnit typealias PositionsMap = HashMap > -class MQTTMatoClient(): MqttCallbackExtended{ +class MQTTMatoClient(){ private var isStarted = false - private var subscribedToAll = false + //private var subscribedToAll = false - private var client: MqttAndroidClient? = null + private var client: Mqtt3AsyncClient? = null //private var clientID = "" private val respondersMap = HashMap>>() private val currentPositions = PositionsMap() private lateinit var lifecycle: LifecycleOwner //TODO: remove class reference to context (always require context in all methods) private var context: Context?= null - private var connectionTrials = 0 - private var notification: Notification? = null + //private var connectionTrials = 0 + //private var notification: Notification? = null - //private lateinit var notification: Notification - private fun connect(context: Context, iMqttActionListener: IMqttActionListener?){ - - val clientID = "mqtt-explorer-${getRandomString(8)}"//"mqttjs_${getRandomString(8)}" + private fun connect(context: Context, onConnect: OnConnect){ + //val clientID = "mqtt-explorer-${getRandomString(8)}"//"mqttjs_${getRandomString(8)}" + if (this.context ==null) + this.context = context.applicationContext //notification = Notifications.makeMQTTServiceNotification(context) - client = MqttAndroidClient(context,SERVER_ADDR,clientID,Ack.AUTO_ACK) - // WE DO NOT WANT A FOREGROUND SERVICE -> it's only more mayhem - // (and the positions need to be downloaded only when the app is shown) - // update, 2024-04: Google Play doesn't understand our needs, so we put back the notification - // and add a video of it working as Google wants - /*if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ - //we need a notification - Notifications.createLivePositionsChannel(context) - val notific = Notifications.makeMQTTServiceNotification(context) - client!!.setForegroundService(notific) - notification=notific - }*/ - - - val options = MqttConnectOptions() - //options.sslProperties = - options.isCleanSession = true - val headersPars = Properties() - headersPars.setProperty("Origin","https://mato.muoversiatorino.it") - headersPars.setProperty("Host","mapi.5t.torino.it") - options.customWebSocketHeaders = headersPars - - Log.d(DEBUG_TAG,"client name: $clientID") + val mclient = makeClientBuilder().buildAsync() + Log.d(DEBUG_TAG, "Connecting to MQTT server") //actually connect - isStarted = true - client!!.connect(options,null, iMqttActionListener) - client!!.setCallback(this) + val mess = mclient.connect().whenComplete { subAck, throwable -> + if(throwable!=null){ + Log.w(DEBUG_TAG,"Failed to connect to MQTT server:") + Log.w(DEBUG_TAG,throwable.toString()) + } + else{ + isStarted = true + Log.d(DEBUG_TAG, "Connected to MQTT server") + onConnect.onConnectSuccess() + } + } + client = mclient + /* if (mess.returnCode != Mqtt3ConnAckReturnCode.SUCCESS){ + Log.w(DEBUG_TAG,"Failed to connect to MQTT client") + return false + } else{ + client = blockingClient.toAsync() + isStarted = true + return true + } - if (this.context ==null) - this.context = context.applicationContext + */ } - override fun connectComplete(reconnect: Boolean, serverURI: String?) { + /*override fun connectComplete(reconnect: Boolean, serverURI: String?) { Log.d(DEBUG_TAG, "Connected to server, reconnect: $reconnect") Log.d(DEBUG_TAG, "Have listeners: $respondersMap") } - private fun connectTopic(topic: String){ + + */ + private fun subscribeTopic(topic: String){ if(context==null){ Log.e(DEBUG_TAG, "Trying to connect but context is null") return } - connectionTrials += 1 - connect(context!!, object : IMqttActionListener{ - override fun onSuccess(asyncActionToken: IMqttToken?) { - val disconnectedBufferOptions = DisconnectedBufferOptions() - disconnectedBufferOptions.isBufferEnabled = true - disconnectedBufferOptions.bufferSize = 100 - disconnectedBufferOptions.isPersistBuffer = false - disconnectedBufferOptions.isDeleteOldestMessages = false - client!!.setBufferOpts(disconnectedBufferOptions) - client!!.subscribe(topic, QoS.AtMostOnce.value) - isStarted = true - } - override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) { - Log.e(DEBUG_TAG, "FAILED To connect to the server",exception) - if (connectionTrials < 10) { - Log.d(DEBUG_TAG, "Reconnecting") - connectTopic(topic) + client!!.subscribeWith() + .topicFilter(topic).callback {publish -> + messageArrived(publish.topic.toString(), publish) + }.send().whenComplete { subAck, throwable-> + if(throwable != null){ + Log.e(DEBUG_TAG, "Error while subscribing to topic $topic", throwable) } - else { - //reset connection trials - connectionTrials = 0 + else{ + //add the responder to the list + Log.d(DEBUG_TAG, "Subscribed to topic $topic, responders ready: ${respondersMap[topic]}") } } - }) } fun startAndSubscribe(lineId: String, responder: MQTTMatoListener, context: Context): Boolean{ //start the client, and then subscribe to the topic val topic = mapTopic(lineId) this.context = context.applicationContext + synchronized(this) { if(!isStarted){ - connectTopic(topic) + connect(context.applicationContext) { + //when connection is done, run this + if (!respondersMap.contains(lineId)){ + respondersMap[lineId] = ArrayList() + } + respondersMap[lineId]!!.add(WeakReference(responder)) + subscribeTopic(topic) + + } //wait for connection - } else { - client!!.subscribe(topic, QoS.AtMostOnce.value) } + //recheck if it is started } - - synchronized(this){ - if (!respondersMap.contains(lineId)) - respondersMap[lineId] = ArrayList() - respondersMap[lineId]!!.add(WeakReference(responder)) - Log.d(DEBUG_TAG, "Add MQTT Listener for line $lineId, topic $topic") - } - return true } fun stopMatoRequests(responder: MQTTMatoListener){ var removed = false - for ((linekey,responderList)in respondersMap.entries){ + for ((lineTopic,responderList)in respondersMap.entries){ var done = false for (el in responderList){ if (el.get()==null){ responderList.remove(el) } else if(el.get() == responder){ responderList.remove(el) done = true } if (done) break } - if(done) Log.d(DEBUG_TAG, "Removed one listener for line $linekey, listeners: $responderList") + if(done) Log.d(DEBUG_TAG, "Removed one listener for topic $lineTopic, listeners: $responderList") //if (done) break if (responderList.isEmpty()){ //actually unsubscribe try { - client?.unsubscribe(mapTopic(linekey)) + val topic = mapTopic(lineTopic) + client?.run{ + unsubscribeWith().addTopicFilter(topic).send().whenComplete { subAck, throwable -> + if (throwable!=null){ + //error occurred + Log.e(DEBUG_TAG, "Error while unsubscribing to topic $topic",throwable) + } + } + } } catch (e: Exception){ Log.e(DEBUG_TAG, "Tried unsubscribing but there was an error in the client library:\n$e") } } removed = done || removed } // check responders map, remove lines that have no responders for(line in respondersMap.keys){ if(respondersMap[line]?.isEmpty() == true){ respondersMap.remove(line) } } Log.d(DEBUG_TAG, "Removed: $removed, respondersMap: $respondersMap") } fun getPositions(): PositionsMap{ return currentPositions } /** * Cancel the notification */ fun removeNotification(context: Context){ val notifManager = context.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notifManager.cancel(MQTT_NOTIFICATION_ID) } private fun sendUpdateToResponders(responders: ArrayList>): Int{ //var sent = false var count = 0 for (wrD in responders) { if (wrD.get() == null) { Log.d(DEBUG_TAG, "Removing weak reference") responders.remove(wrD) } else { wrD.get()!!.onUpdateReceived(currentPositions) //sent = true count++ } } return count } - override fun connectionLost(cause: Throwable?) { + /*override fun connectionLost(cause: Throwable?) { var doReconnect = false for ((line,elms) in respondersMap.entries){ if(!elms.isEmpty()){ doReconnect = true break } } if (!doReconnect){ Log.d(DEBUG_TAG, "Disconnected, but no responders to give the positions, avoid reconnecting") //finish here return } Log.w(DEBUG_TAG, "Lost connection in MQTT Mato Client") synchronized(this){ // isStarted = false //var i = 0 // while(i < 20 && !isStarted) { connect(context!!, object: IMqttActionListener{ override fun onSuccess(asyncActionToken: IMqttToken?) { //relisten to messages for ((line,elms) in respondersMap.entries){ val topic = mapTopic(line) if(elms.isEmpty()) respondersMap.remove(line) else { client!!.subscribe(topic, QoS.AtMostOnce.value, null, null) Log.d(DEBUG_TAG, "Resubscribed with topic $topic") } } Log.d(DEBUG_TAG, "Reconnected to MQTT Mato Client") } override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) { Log.w(DEBUG_TAG, "Failed to reconnect to MQTT server") } }) } } - override fun messageArrived(topic: String?, message: MqttMessage?) { - if (topic==null || message==null) return + */ + + fun messageArrived(topic: String?, message: Mqtt3Publish) { + if (topic==null) return //Log.d(DEBUG_TAG,"Arrived message on topic $topic, ${String(message.payload)}") parseMessageAndAddToList(topic, message) //GlobalScope.launch { } } - private fun parseMessageAndAddToList(topic: String, message: MqttMessage){ + private fun parseMessageAndAddToList(topic: String, message: Mqtt3Publish){ + //val mqttTopic = message.topic + //Log.d(DEBUG_TAG, "Topic of message received: $mqttTopic") val vals = topic.split("/") val lineId = vals[1] val vehicleId = vals[2] val timestamp = (System.currentTimeMillis() / 1000 ) as Long - val messString = String(message.payload) - + val messString = String(message.payloadAsBytes) + Log.d(DEBUG_TAG, "Received message on topic: $topic") try { val jsonList = JSONArray(messString) - /*val posUpdate = MQTTPositionUpdate(lineId+"U", vehicleId, - jsonList.getDouble(0), - jsonList.getDouble(1), - if(jsonList.get(2).equals(null)) null else jsonList.getInt(2), - if(jsonList.get(3).equals(null)) null else jsonList.getInt(3), - if(jsonList.get(4).equals(null)) null else jsonList.getString(4)+"U", - if(jsonList.get(5).equals(null)) null else jsonList.getInt(5), - if(jsonList.get(6).equals(null)) null else jsonList.getInt(6), - //full - ) - */ if(jsonList.get(4)==null){ Log.d(DEBUG_TAG, "We have null tripId: line $lineId veh $vehicleId: $jsonList") return } val posUpdate = LivePositionUpdate( jsonList.getString(4)+"U", null, null, lineId+"U", vehicleId, jsonList.getDouble(0), //latitude jsonList.getDouble(1), //longitude if(jsonList.get(2).equals(null)) null else jsonList.getInt(2).toFloat(), //"heading" (same as bearing?) timestamp, if(jsonList.get(6).equals(null)) null else jsonList.getInt(6).toString() //nextStop ) //add update var valid = false if(!currentPositions.contains(lineId)) currentPositions[lineId] = HashMap() currentPositions[lineId]?.let{ it[vehicleId] = posUpdate valid = true } //sending //Log.d(DEBUG_TAG, "Parsed update on topic $topic, line $lineId, responders $respondersMap") var cc = 0 if (LINES_ALL in respondersMap.keys) { val count = sendUpdateToResponders(respondersMap[LINES_ALL]!!) cc +=count } if(lineId in respondersMap.keys){ cc += sendUpdateToResponders(respondersMap[lineId]!!) } //Log.d(DEBUG_TAG, "Sent to $cc responders, have $respondersMap") if(cc==0){ Log.w(DEBUG_TAG, "We have received an update but apparently there is no one to send it") var emptyResp = true for(en in respondersMap.values){ if(!en.isEmpty()){ emptyResp=false break } } //try unsubscribing to all if(emptyResp) { Log.d(DEBUG_TAG, "Unsubscribe all") - client!!.unsubscribe(LINES_ALL) + //client!!.unsubscribe(LINES_ALL) } } //Log.d(DEBUG_TAG, "We have update on line $lineId, vehicle $vehicleId") } catch (e: JSONException){ Log.w(DEBUG_TAG,"Cannot decipher message on topic $topic, line $lineId, veh $vehicleId (bad JSON)") } catch (e: Exception){ Log.e(DEBUG_TAG, "Exception occurred", e) } } - override fun deliveryComplete(token: IMqttDeliveryToken?) { - //NOT USED (we're not sending any messages) - } - /*/** * Stop the service forever. Client has not to be used again!! */ fun closeClientForever(){ client.disconnect() client.close() }*/ fun disconnect(){ client?.disconnect() } companion object{ - const val SERVER_ADDR="wss://mapi.5t.torino.it:443/scre" + const val SERVER_ADDR="mapi.5t.torino.it" + const val SERVER_PATH="/scre" const val LINES_ALL="ALL" private const val DEBUG_TAG="BusTO-MatoMQTT" //this has to match the value in MQTT library (MQTTAndroidClient) const val MQTT_NOTIFICATION_ID: Int = 77 + @JvmStatic fun mapTopic(lineId: String): String{ return if(lineId== LINES_ALL || lineId == "#") "#" else{ "/${lineId}/#" } } fun getRandomString(length: Int) : String { val allowedChars = ('a'..'f') + ('0'..'9') return (1..length) .map { allowedChars.random() } .joinToString("") } fun interface MQTTMatoListener{ //positionsMap is a dict with line -> vehicle -> Update fun onUpdateReceived(posUpdates: PositionsMap) } + fun getHTTPHeaders(): HashMap { + val headers = HashMap() + headers["Origin"] = "https://mato.muoversiatorino.it" + headers["Host"] = "mapi.5t.torino.it" + + return headers + } + private fun makeClientBuilder() : Mqtt3ClientBuilder{ + val clientID = "mqtt-explorer-${getRandomString(8)}" + + val r = MqttClient.builder() + .useMqttVersion3() + .identifier(clientID) + .serverHost(SERVER_ADDR) + .serverPort(443) + .sslWithDefaultConfig() + .automaticReconnect(MqttClientAutoReconnect.builder() + .initialDelay(500, TimeUnit.MILLISECONDS) + .maxDelay(60, TimeUnit.SECONDS).build()) + .webSocketConfig() + .httpHeaders(getHTTPHeaders()) + .serverPath("scre") + .applyWebSocketConfig() + //.webSocketWithDefaultConfig() + return r + } + + + } + + private fun interface OnConnect{ + fun onConnectSuccess() } } -data class MQTTPositionUpdate( +/*data class MQTTPositionUpdate( val lineId: String, val vehicleId: String, val latitude: Double, val longitude: Double, val heading: Int?, val speed: Int?, val tripId: String?, val direct: Int?, val nextStop: Int?, //val full: Int? -) \ No newline at end of file +)*/ \ No newline at end of file diff --git a/build.gradle b/build.gradle index afea5f8..4170bc6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,50 +1,50 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { mavenCentral() maven { url 'https://maven.google.com' } google() maven { url 'https://jitpack.io' } } //TODO: Migrate tfrom kapt to KSP //kotlin ext.kotlin_version = '2.1.10' ext.coroutines_version = "1.10.2" dependencies { classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.4' // or latest - classpath 'com.android.tools.build:gradle:8.6.0' + classpath 'com.android.tools.build:gradle:8.6.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } ext { androidXTestVersion = "1.5.0" //multidex multidex_version = "2.0.1" //libraries versions fragment_version = "1.6.1" activity_version = "1.7.2" appcompat_version = "1.7.0" preference_version = "1.2.1" work_version = "2.9.0" acra_version = "5.11.3" lifecycle_version = "2.7.0" arch_version = "2.1.0" room_version = "2.5.2" } allprojects { repositories { maven { url 'https://maven.google.com' } google() mavenCentral() maven { url "https://jitpack.io" } } }