package cn.flightfeather.supervision.common.net
|
|
import com.google.gson.Gson
|
import com.google.gson.JsonObject
|
import com.google.gson.JsonParser
|
|
/**
|
* @author riku
|
* Date: 2020/10/15
|
* 数据抓取服务接口
|
*/
|
object VOCHttpService {
|
|
private var token = ""
|
private val httpMethod = HttpMethod("139.9.45.230", 8080)
|
|
data class DeviceInfo(
|
val deviceCode: List<String>,
|
val startTime: String,
|
val endTime: String
|
)
|
|
/**
|
* 获取令牌
|
*/
|
fun getToken(): Boolean {
|
val response = httpMethod.post("/hwt/shanghai/token")
|
if (response.success) {
|
val json = JsonParser.parseString(response.m.responseBodyAsString)
|
if (json.isJsonObject && json.asJsonObject.has("token")) {
|
token = json.asJsonObject["token"].asString
|
return true
|
}
|
return false
|
} else {
|
println("============================汽修监测数据接口token获取失败")
|
return false
|
}
|
}
|
|
/**
|
* 获取监测数据
|
*/
|
fun getVOCData(deviceInfo: DeviceInfo):JsonObject? {
|
val data = Gson().toJson(deviceInfo)
|
val response = httpMethod.post("/hwt/shanghai/data/voc", data, listOf(Pair("Authorization", token)))
|
if (response.success) {
|
val json = JsonParser.parseString(response.m.responseBodyAsString)
|
return if (json.isJsonObject && json.asJsonObject["code"].asInt == 401) {
|
if (getToken()) {
|
getVOCData(deviceInfo)
|
} else {
|
null
|
}
|
} else {
|
return if (json.isJsonObject) {
|
json.asJsonObject
|
} else {
|
null
|
}
|
}
|
} else {
|
return null
|
}
|
}
|
}
|