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 --- a/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt +++ b/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt @@ -17,6 +17,8 @@ import java.lang.ref.WeakReference import java.util.* import java.util.concurrent.TimeUnit +import java.util.concurrent.locks.ReentrantLock +import kotlin.concurrent.withLock typealias PositionsMap = HashMap > @@ -31,6 +33,7 @@ private val respondersMap = HashMap>>() private val currentPositions = PositionsMap() + private val positionsLock = ReentrantLock() private lateinit var lifecycle: LifecycleOwner //TODO: remove class reference to context (always require context in all methods) @@ -177,9 +180,7 @@ } Log.d(DEBUG_TAG, "Removed: $removed, respondersMap: $respondersMap") } - fun getPositions(): PositionsMap{ - return currentPositions - } + /** * Cancel the notification @@ -201,7 +202,9 @@ else { val resp = wrD.get()!! resp.onStatusUpdate(LivePositionsServiceStatus.OK) - resp.onUpdateReceived(currentPositions) + positionsLock.withLock { + resp.onUpdateReceived(currentPositions) + } //sent = true count++ } @@ -316,12 +319,14 @@ ) //add update - var valid = false - if(!currentPositions.contains(lineId)) - currentPositions[lineId] = HashMap() - currentPositions[lineId]?.let{ - it[vehicleId] = posUpdate - valid = true + //var valid = false + positionsLock.withLock { + 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") @@ -369,11 +374,13 @@ fun clearOldPositions(timeMins: Int){ val currentTimeStamp = makeUnixTimestamp() var c = 0 - for((k, manyp) in currentPositions.entries){ - for ((t, p) in manyp.entries){ - if (currentTimeStamp - p.timestamp > timeMins*60){ - manyp.remove(t) - c+=1 + positionsLock.withLock{ + for ((k, manyp) in currentPositions.entries) { + for ((t, p) in manyp.entries) { + if (currentTimeStamp - p.timestamp > timeMins * 60) { + manyp.remove(t) + c += 1 + } } } } diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt @@ -842,18 +842,18 @@ val symbolsToUpdate = ArrayList() for (upsWithTrp in incomingData.values){ - val pos = upsWithTrp.first - val vehID = pos.vehicle - var animate = false + val newPos = upsWithTrp.first + val vehID = newPos.vehicle + //var animate = false if (vehsOld.contains(vehID)){ //update position only if the starting or the stopping position of the animation are in the view val oldPos = positionsByVehDict[vehID] var avoidShowingUpdateBecauseIsImpossible = false oldPos?.let{ - if(oldPos.routeID!=pos.routeID) { - val dist = LatLng(it.latitude, it.longitude).distanceTo(LatLng(pos.latitude, pos.longitude)) - val speed = dist*3.6 / (pos.timestamp - it.timestamp) //this should be in km/h - Log.w(DEBUG_TAG, "Vehicle $vehID changed route from ${oldPos.routeID} to ${pos.routeID}, distance: $dist, speed: $speed") + if(oldPos.routeID!=newPos.routeID) { + val dist = LatLng(it.latitude, it.longitude).distanceTo(LatLng(newPos.latitude, newPos.longitude)) + val speed = dist*3.6 / (newPos.timestamp - it.timestamp) //this should be in km/h + Log.w(DEBUG_TAG, "Vehicle $vehID changed route from ${oldPos.routeID} to ${newPos.routeID}, distance: $dist, speed: $speed") if (speed > 120 || speed < 0){ avoidShowingUpdateBecauseIsImpossible = true } @@ -865,17 +865,23 @@ continue } - val samePosition = oldPos?.let { (oldPos.latitude==pos.latitude)&&(oldPos.longitude == pos.longitude) }?:false + val samePosition = oldPos?.let { (oldPos.latitude==newPos.latitude)&&(oldPos.longitude == newPos.longitude) }?:false + if(!samePosition) { val isPositionInBounds = isInsideVisibleRegion( - pos.latitude, pos.longitude, false + newPos.latitude, newPos.longitude, false ) || (oldPos?.let { isInsideVisibleRegion(it.latitude,it.longitude, false) } ?: false) + if ((newPos.bearing==null && oldPos?.bearing!=null)){ + //copy old bearing + newPos.bearing = oldPos.bearing + } if (isPositionInBounds) { //animate = true //this moves both the icon and the label - moveVehicleToNewPosition(pos) + moveVehicleToNewPosition(newPos) } else { - positionsByVehDict[vehID] = pos + + positionsByVehDict[vehID] = newPos /*busLabelSymbolsByVeh[vehID]?.let { it.latLng = LatLng(pos.latitude, pos.longitude) symbolsToUpdate.add(it) @@ -885,13 +891,13 @@ } } } - else if(pos.latitude>0 && pos.longitude>0) { + else if(newPos.latitude>0 && newPos.longitude>0) { //we should not have to check for this // update it simply - positionsByVehDict[vehID] = pos + positionsByVehDict[vehID] = newPos //createLabelForVehicle(pos) }else{ - Log.w(DEBUG_TAG, "Update ignored for veh $vehID on line ${pos.routeID}, lat: ${pos.latitude}, lon ${pos.longitude}") + Log.w(DEBUG_TAG, "Update ignored for veh $vehID on line ${newPos.routeID}, lat: ${newPos.latitude}, lon ${newPos.longitude}") } } @@ -907,7 +913,7 @@ //removeVehicleLabel(vehID) } } - //update UI + //finally, update UI updatePositionsIcons() }