Skip to content

Commit

Permalink
Merge pull request #15 from highmobility/add-eligibility
Browse files Browse the repository at this point in the history
Add eligibility
  • Loading branch information
tonisives authored Mar 10, 2023
2 parents 41e0a21 + 7335b65 commit 8093288
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ext {
coroutinesVersion = '1.6.4'
koinVersion = '3.2.0'
ver = [
"hmkit-fleet" : "0.6.5",
"hmkit-fleet" : "0.7.0",
"hmkit-crypto-telematics": "0.1",
"hmkit-auto-api" : "13.1.1",
]
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=0.6.8
kotlin.code.style=official
version=0.7.0
kotlin.code.style=official
17 changes: 16 additions & 1 deletion hmkit-fleet/src/main/kotlin/HMKitFleet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import java.util.concurrent.CompletableFuture

/**
* HMKitFleet is the access point for the Fleet SDK functionality. It is accessed by
* HMKitFleet.INSTANCE and it's field [configuration] should be set before accessing other functions
* HMKitFleet.INSTANCE. It's field [configuration] should be set before accessing other functions
*/
object HMKitFleet {
init {
Expand All @@ -59,6 +59,21 @@ object HMKitFleet {
*/
lateinit var configuration: ServiceAccountApiConfiguration

/**
* Get the eligibility status for a specific VIN. This can be used to find out if the vehicle has the necessary connectivity to transmit data.
*
* @param vin The vehicle VIN number
* @param brand The vehicle brand
* @return The eligibility status
*/
fun getEligibility(
vin: String,
brand: Brand
): CompletableFuture<Response<EligibilityStatus>> = GlobalScope.future {
logger.debug("HMKitFleet: getEligibility: $vin")
koin.get<UtilityRequests>().getEligibility(vin, brand)
}

/**
* Start the data access clearance process for a vehicle.
*
Expand Down
8 changes: 8 additions & 0 deletions hmkit-fleet/src/main/kotlin/Koin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ internal object Koin {
get()
)
}
single {
UtilityRequests(
get(),
get(),
HMKitFleet.environment.url,
get()
)
}
}

lateinit var koinApplication: KoinApplication
Expand Down
60 changes: 60 additions & 0 deletions hmkit-fleet/src/main/kotlin/model/EligibilityStatus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* The MIT License
*
* Copyright (c) 2014- High-Mobility GmbH (https://high-mobility.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.highmobility.hmkitfleet.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class EligibilityStatus(
val vin: String,
val eligible: Boolean,
@SerialName("data_delivery")
val dataDelivery: List<DataDelivery> = emptyList(),
@SerialName("connectivity_status")
val connectivityStatus: ConnectivityStatus? = null,
@SerialName("primary_user_assigned")
val primaryUserAssigned: Boolean? = null
) {
@Serializable
enum class DataDelivery {
@SerialName("pull")
PULL,

@SerialName("push")
PUSH
}

@Serializable
enum class ConnectivityStatus {
@SerialName("activated")
ACTIVATED,

@SerialName("deactivated")
DEACTIVATED,

@SerialName("unknown")
UNKNOWN
}
}
91 changes: 91 additions & 0 deletions hmkit-fleet/src/main/kotlin/network/UtilityRequests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* The MIT License
*
* Copyright (c) 2014- High-Mobility GmbH (https://high-mobility.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.highmobility.hmkitfleet.network

import kotlinx.serialization.json.*
import com.highmobility.hmkitfleet.model.Brand
import com.highmobility.hmkitfleet.model.ControlMeasure
import com.highmobility.hmkitfleet.model.ClearanceStatus
import com.highmobility.hmkitfleet.model.EligibilityStatus
import com.highmobility.hmkitfleet.model.RequestClearanceResponse
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.Logger
import utils.await
import java.net.HttpURLConnection

internal class UtilityRequests(
client: OkHttpClient,
logger: Logger,
baseUrl: String,
private val authTokenRequests: AuthTokenRequests
) : Requests(
client,
logger, baseUrl
) {
suspend fun getEligibility(
vin: String,
brand: Brand
): Response<EligibilityStatus> {
val body = requestBody(vin, brand)
val authToken = authTokenRequests.getAuthToken()

if (authToken.error != null) return Response(null, authToken.error)

val request = Request.Builder()
.url("${baseUrl}/eligibility")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer ${authToken.response?.authToken}")
.post(body)
.build()

printRequest(request)

val call = client.newCall(request)
val response = call.await()

return tryParseResponse(response, HttpURLConnection.HTTP_OK) { responseBody ->
val eligibilityStatus = Json.decodeFromString<EligibilityStatus>(responseBody)
if (eligibilityStatus.vin != vin) logger.warn("VIN in response does not match VIN in request")
Response(eligibilityStatus, null)
}
}

private fun requestBody(
vin: String,
brand: Brand,
): RequestBody {
val vehicle = buildJsonObject {
put("vin", vin)
put("brand", Json.encodeToJsonElement(brand))
}

val body = Json.encodeToString(vehicle).toRequestBody(mediaType)
return body
}
}
Loading

0 comments on commit 8093288

Please sign in to comment.