riku
2021-09-24 4f1b7973c53b45f57e451191bfd5a3d2136a004c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
        }
    }
}