Skip to content

Commit

Permalink
Return senseBox, sensor and sensor history in one call. CodeforKarlsr…
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaelz committed Jan 2, 2018
1 parent e783c49 commit 14a4892
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package de.codefor.karlsruhe.opensense.data
import android.util.Log
import de.codefor.karlsruhe.opensense.data.boxes.BoxesApi
import de.codefor.karlsruhe.opensense.data.boxes.model.SenseBox
import de.codefor.karlsruhe.opensense.data.boxes.model.Sensor
import de.codefor.karlsruhe.opensense.data.boxes.model.SensorHistory
import io.reactivex.Single
import io.reactivex.functions.BiFunction
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.moshi.MoshiConverterFactory

typealias SensorData = Pair<Sensor, List<SensorHistory>>

object OpenSenseMapService {
private val boxesApi: BoxesApi
Expand All @@ -31,8 +34,13 @@ object OpenSenseMapService {
return boxesApi.getAllBoxes()
}

fun getSensorHistory(boxId: String, sensorId: String): Single<List<SensorHistory>> {
Log.i("OpenSenseMapService", "getSensorHistory() boxId: $boxId, sensorId: $sensorId")
return boxesApi.getSensorHistory(boxId, sensorId)
fun getSenseBoxAndSensorData(boxId: String, sensorId: String): Single<Pair<SenseBox, SensorData>> {
Log.i("OpenSenseMapService", "getSenseBoxAndSensorData() boxId: $boxId, sensorId: $sensorId")
return boxesApi.getBox(boxId).zipWith(boxesApi.getSensorHistory(boxId, sensorId),
BiFunction<SenseBox, List<SensorHistory>, Pair<SenseBox, SensorData>> { senseBox, sensorHistory ->
val sensor = senseBox.sensors?.first { it.id == sensorId }
?: throw IllegalStateException("The box $boxId doesn't contain the sensor id $sensorId")
Pair(senseBox, SensorData(sensor, sensorHistory))
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import de.codefor.karlsruhe.opensense.data.OpenSenseMapService
import de.codefor.karlsruhe.opensense.data.SensorData
import de.codefor.karlsruhe.opensense.data.boxes.model.SenseBox
import de.codefor.karlsruhe.opensense.data.boxes.model.Sensor
import de.codefor.karlsruhe.opensense.data.boxes.model.SensorHistory
import de.codefor.karlsruhe.opensense.widget.base.BaseWidget
import de.codefor.karlsruhe.opensense.widget.base.BaseWidgetConfigurationActivity
import io.reactivex.Single
Expand Down Expand Up @@ -69,17 +69,13 @@ object WidgetHelper {
.observeOn(AndroidSchedulers.mainThread())
}

internal fun getSensorHistory(context: Context, appWidgetId: Int): Single<List<SensorHistory>> {
internal fun getSenseBoxAndSensorData(context: Context, appWidgetId: Int): Single<Pair<SenseBox, SensorData>> {
val boxId = loadBoxId(context, appWidgetId)
// We keep it really simple here and just use the first selected sensor of this widget
val sensorId = loadSensorIds(context, appWidgetId).firstOrNull() ?: return Single.just(emptyList())
val sensorId = loadSensorIds(context, appWidgetId).firstOrNull() ?: return Single.error(IllegalStateException("No sensor id stored"))

Log.i("WidgetHelper", "getSensorHistory() boxId: $boxId, sensorId: $sensorId")
return getSensorHistory(boxId, sensorId)
}

private fun getSensorHistory(boxId: String, sensorId: String): Single<List<SensorHistory>> {
return OpenSenseMapService.getSensorHistory(boxId, sensorId)
Log.i("WidgetHelper", "getSenseBoxAndSensorData() boxId: $boxId, sensorId: $sensorId")
return OpenSenseMapService.getSenseBoxAndSensorData(boxId, sensorId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.androidplot.xy.SimpleXYSeries
import com.androidplot.xy.XYGraphWidget
import com.androidplot.xy.XYPlot
import de.codefor.karlsruhe.opensense.R
import de.codefor.karlsruhe.opensense.data.boxes.model.SenseBox
import de.codefor.karlsruhe.opensense.data.boxes.model.Sensor
import de.codefor.karlsruhe.opensense.data.boxes.model.SensorHistory
import de.codefor.karlsruhe.opensense.widget.WidgetHelper
import de.codefor.karlsruhe.opensense.widget.base.BaseWidget
Expand Down Expand Up @@ -44,34 +46,37 @@ class PlotWidget : BaseWidget() {
}
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views)

WidgetHelper.getSensorHistory(context, appWidgetId).subscribe(
WidgetHelper.getSenseBoxAndSensorData(context, appWidgetId).subscribe(
// onSuccess
{ sensorHist ->
{ (senseBox, sensorData) ->
// Kotlin doesn't support nested destructuring, so we do it here
val (sensor, sensorHist) = sensorData
views.apply {
//Show refresh button, hide progress bar
setViewVisibility(R.id.plot_widget_refresh_button, View.VISIBLE)
setViewVisibility(R.id.plot_widget_progress_bar, View.GONE)
// TODO: Return box in above call and set box name and sensor name
setTextViewText(R.id.plot_widget_box_name, "sensebox name")
setTextViewText(R.id.plot_widget_sensor_title, "sensor")
setTextViewText(R.id.plot_widget_box_name, senseBox.name)
setTextViewText(R.id.plot_widget_sensor_title, sensor.title)
}
drawPlot(context, appWidgetId, appWidgetManager, sensorHist)
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views)
drawPlot(context, appWidgetId, appWidgetManager, senseBox, sensor, sensorHist)
}, {
views.apply {
// Show refresh button, hide progress bar
setViewVisibility(R.id.plot_widget_refresh_button, View.VISIBLE)
setViewVisibility(R.id.plot_widget_progress_bar, View.GONE)
// Remove values
setViewVisibility(R.id.plot_widget_error_text, View.VISIBLE)
setViewVisibility(R.id.plot_widget_img, View.GONE)
//setViewVisibility(R.id.plot_widget_img, View.GONE)
}
setOnClickPendingIntents(context, appWidgetId, views)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
)
}

private fun drawPlot(context: Context, appWidgetId: Int, appWidgetManager: AppWidgetManager, sensorHist: List<SensorHistory>) {
private fun drawPlot(context: Context, appWidgetId: Int, appWidgetManager: AppWidgetManager,
senseBox: SenseBox, sensor: Sensor, sensorHist: List<SensorHistory>) {
val views = RemoteViews(context.packageName, R.layout.plot_widget)

val plot = XYPlot(context, context.getString(R.string.plot_history_title))
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/plot_widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
android:id="@+id/plot_widget_error_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/plot_widget_configuration_button"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center"
Expand Down

0 comments on commit 14a4892

Please sign in to comment.