This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package <package_name> | |
import android.app.Activity | |
import android.os.Bundle | |
import android.os.Handler | |
import android.util.Log | |
import com.google.android.things.pio.Gpio | |
import com.google.android.things.pio.PeripheralManager | |
import java.io.IOException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Access scopes | |
private val accessScope = listOf("https://www.googleapis.com/auth/cloud-platform.read-only") | |
private var token = "Empty" | |
// Receive Google OAuth 2 token for API access | |
private fun authenticateAndAuthorize() { | |
doAsync { | |
val googleCredential = GoogleCredential | |
.fromStream(ctx.assets.open("service-account.json")) | |
.createScoped(accessScope) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Retrofit interface | |
interface GCPService { | |
@GET("/bigquery/v2/projects/weather-231121/datasets/WeatherDataSet/tables/WeatherDataTable/data") | |
fun getTable(@Header("Authorization") token: String, @Query("startIndex") startIndex: Int): Call<JsonObject> | |
} | |
// Retrofit builder | |
private val retrofitBuilder = Retrofit.Builder() | |
.baseUrl("https://content.googleapis.com") | |
.addConverterFactory(GsonConverterFactory.create()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get table | |
fun pullData() { | |
when (token) { | |
"Empty" -> { | |
authenticateAndAuthorize() | |
} | |
else -> { | |
doAsync { | |
val apiCall = gcpService.getTable("Bearer $token", 15).execute() | |
Log.d("JSON", apiCall.body().toString()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Data classes for JSON deserialization | |
data class JsonTable(@SerializedName("rows") val rows: List<Row>) { | |
data class Row(@SerializedName("f") val f: List<F>) { | |
data class F(@SerializedName("v") val v: String) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Data holders | |
val temperatureData = mutableListOf<Float>() | |
val pressureData = mutableListOf<Float>() | |
val timeData = mutableListOf<Float>() | |
// Convert data from BigQuery JsonTable to data arrays | |
fun processData(tableData: JsonTable) { | |
tableData.rows.forEach { | |
it.f.forEachIndexed { index, element -> | |
when (index) { |
OlderNewer