Differential D161 Diff 503 app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt
Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt
| Show All 14 Lines | BusTO - ViewModel components | ||||
| You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
| */ | */ | ||||
| package it.reyboz.bustorino.viewmodels | package it.reyboz.bustorino.viewmodels | ||||
| import android.app.Application | import android.app.Application | ||||
| import android.util.Log | import android.util.Log | ||||
| import androidx.lifecycle.* | import androidx.lifecycle.* | ||||
| import androidx.work.WorkInfo | |||||
| import androidx.work.WorkManager | |||||
| import com.android.volley.DefaultRetryPolicy | import com.android.volley.DefaultRetryPolicy | ||||
| import com.android.volley.Response | import com.android.volley.Response | ||||
| import it.reyboz.bustorino.backend.NetworkVolleyManager | import it.reyboz.bustorino.backend.NetworkVolleyManager | ||||
| import it.reyboz.bustorino.backend.gtfs.GtfsRtPositionsRequest | import it.reyboz.bustorino.backend.gtfs.GtfsRtPositionsRequest | ||||
| import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate | import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate | ||||
| import it.reyboz.bustorino.backend.mato.MQTTMatoClient | import it.reyboz.bustorino.backend.mato.MQTTMatoClient | ||||
| import it.reyboz.bustorino.data.GtfsRepository | import it.reyboz.bustorino.data.GtfsRepository | ||||
| import it.reyboz.bustorino.data.MatoPatternsDownloadWorker | import it.reyboz.bustorino.data.MatoPatternsDownloadWorker | ||||
| import it.reyboz.bustorino.data.MatoTripsDownloadWorker | |||||
| import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops | import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops | ||||
| import kotlinx.coroutines.delay | import kotlinx.coroutines.delay | ||||
| import kotlinx.coroutines.launch | import kotlinx.coroutines.launch | ||||
| import java.util.* | |||||
| import kotlin.collections.ArrayList | |||||
| import kotlin.collections.HashMap | |||||
| import kotlin.collections.HashSet | |||||
| class LivePositionsViewModel(application: Application): AndroidViewModel(application) { | class LivePositionsViewModel(application: Application): AndroidViewModel(application) { | ||||
| private val gtfsRepo = GtfsRepository(application) | private val gtfsRepo = GtfsRepository(application) | ||||
| //private val updates = UpdatesMap() | //private val updates = UpdatesMap() | ||||
| private val updatesLiveData = MutableLiveData<ArrayList<LivePositionUpdate>>() | private val updatesLiveData = MutableLiveData<ArrayList<LivePositionUpdate>>() | ||||
| private val netVolleyManager = NetworkVolleyManager.getInstance(application) | private val netVolleyManager = NetworkVolleyManager.getInstance(application) | ||||
| private var mqttClient = MQTTMatoClient() | private var mqttClient = MQTTMatoClient() | ||||
| private var lineListening = "" | private var lineListening = "" | ||||
| private var lastTimeReceived: Long = 0 | private var lastTimeReceived: Long = 0 | ||||
| private val gtfsRtRequestRunning = MutableLiveData<Boolean>(false) | private val gtfsRtRequestRunning = MutableLiveData<Boolean>(false) | ||||
| private val lastFailedTripsRequest = HashMap<String, Date>() | |||||
| private val workManager = WorkManager.getInstance(application) | |||||
| private var lastRequestedDownloadTrips = MutableLiveData<List<String>>() | |||||
| var isLastWorkResultGood = workManager | |||||
| .getWorkInfosForUniqueWorkLiveData(MatoTripsDownloadWorker.TAG_TRIPS).map { it -> | |||||
| if (it.isEmpty()) return@map false | |||||
| var res = true | |||||
| if(it[0].state == WorkInfo.State.FAILED){ | |||||
| val currDate = Date() | |||||
| res = false | |||||
| lastRequestedDownloadTrips.value?.let { trips-> | |||||
| for(tr in trips){ | |||||
| lastFailedTripsRequest[tr] = currDate | |||||
| } | |||||
| } | |||||
| } | |||||
| return@map res | |||||
| } | |||||
| /** | /** | ||||
| * Responder to the MQTT Client | * Responder to the MQTT Client | ||||
| */ | */ | ||||
| private val matoPositionListener = MQTTMatoClient.Companion.MQTTMatoListener{ | private val matoPositionListener = MQTTMatoClient.Companion.MQTTMatoListener{ | ||||
| val mupds = ArrayList<LivePositionUpdate>() | val mupds = ArrayList<LivePositionUpdate>() | ||||
| if(lineListening==MQTTMatoClient.LINES_ALL){ | if(lineListening==MQTTMatoClient.LINES_ALL){ | ||||
| for(sdic in it.values){ | for(sdic in it.values){ | ||||
| ▲ Show 20 Lines • Show All 153 Lines • ▼ Show 20 Lines | class LivePositionsViewModel(application: Application): AndroidViewModel(application) { | ||||
| } | } | ||||
| override fun onCleared() { | override fun onCleared() { | ||||
| //stop the MQTT Service | //stop the MQTT Service | ||||
| Log.d(DEBUG_TI, "Clearing the live positions view model, stopping the mqttClient") | Log.d(DEBUG_TI, "Clearing the live positions view model, stopping the mqttClient") | ||||
| mqttClient.disconnect() | mqttClient.disconnect() | ||||
| super.onCleared() | super.onCleared() | ||||
| } | } | ||||
| //Request trips download | |||||
| fun downloadTripsFromMato(trips: List<String>): Boolean{ | |||||
| if(trips.isEmpty()) | |||||
| return false | |||||
| var shouldContinue = false | |||||
| val currentDateTime = Date().time | |||||
| for (tr in trips){ | |||||
| if (!lastFailedTripsRequest.containsKey(tr)){ | |||||
| shouldContinue = true | |||||
| break | |||||
| } else{ | |||||
| //Log.i(DEBUG_TI, "Last time the trip has failed is ${lastFailedTripsRequest[tr]}") | |||||
| if ((lastFailedTripsRequest[tr]!!.time - currentDateTime) > MAX_TIME_RETRY){ | |||||
| shouldContinue =true | |||||
| break | |||||
| } | |||||
| } | |||||
| } | |||||
| if (shouldContinue) { | |||||
| //if one trip | |||||
| val workRequ =MatoTripsDownloadWorker.requestMatoTripsDownload(trips, getApplication(), "BusTO-MatoTripsDown") | |||||
| workRequ?.let { req -> | |||||
| Log.d(DEBUG_TI, "Enqueueing new work, saving work info") | |||||
| lastRequestedDownloadTrips.postValue(trips) | |||||
| //isLastWorkResultGood = | |||||
| } | |||||
| } else{ | |||||
| Log.w(DEBUG_TI, "Requested to fetch data for ${trips.size} trips but they all have failed before in the last $MAX_MINUTES_RETRY mins") | |||||
| } | |||||
| return shouldContinue | |||||
| } | |||||
| companion object{ | companion object{ | ||||
| private const val DEBUG_TI = "BusTO-LivePosViewModel" | private const val DEBUG_TI = "BusTO-LivePosViewModel" | ||||
| private const val MAX_MINUTES_RETRY = 3 | |||||
| private const val MAX_TIME_RETRY = MAX_MINUTES_RETRY*60*1000 //3 minutes (in milliseconds) | |||||
| } | } | ||||
| } | } | ||||
| No newline at end of file | No newline at end of file | ||||
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register