feiyu02
2024-11-19 752e00503f672ddfe2066afb6c235721a3a912b5
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package cn.flightfeather.supervision.bgtask
 
import cn.flightfeather.supervision.common.net.VOCHttpService
import cn.flightfeather.supervision.domain.entity.DeviceInfo
import cn.flightfeather.supervision.domain.entity.VOCHourValue
import cn.flightfeather.supervision.domain.enumeration.DistrictType
import cn.flightfeather.supervision.domain.enumeration.SceneType
import cn.flightfeather.supervision.domain.mapper.DeviceInfoMapper
import cn.flightfeather.supervision.domain.mapper.VOCHourValueMapper
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import tk.mybatis.mapper.entity.Example
import java.time.LocalDateTime
import java.util.*
import javax.annotation.PostConstruct
 
/**
 * 获取voc监测数据
 */
@Component
class TaskFetchVOC : BaseTimingTask() {
 
    enum class Factor(val value: String, val des: String) {
        Concentration("g29001", "浓度"),
        Humidity("g18101", "湿度"),
        Temperature("g18001", "温度"),
        FanElectricity1("e70101", "风机电流1"),
        FanElectricity2("e70102", "风机电流2"),
        FanElectricity3("e70103", "风机电流3"),
        FanElectricity4("e70104", "风机电流4"),
        FanElectricity5("e70105", "风机电流5"),
        FanElectricity6("e70106", "风机电流6"),
    }
 
    companion object {
        private lateinit var instance: TaskFetchVOC
    }
 
    @Autowired
    lateinit var deviceInfoMapper: DeviceInfoMapper
 
    @Autowired
    lateinit var vocHourValueMapper: VOCHourValueMapper
 
    @PostConstruct
    fun init() {
        instance = this
    }
 
    private val LOGGER: Logger? = LoggerFactory.getLogger(TaskFetchVOC::class.java)
 
    override val period: Long
        get() = 15L
 
    override fun doTask(localtime:LocalDateTime) {
        getVOCData()
    }
 
    private fun getVOCData() {
        val deviceCodeList = mutableListOf<String>()
        deviceInfoMapper.selectByExample(Example(DeviceInfo::class.java).apply {
            createCriteria().andEqualTo("diProvinceCode", "31")
                .andEqualTo("diCityCode", "3100")
                .andEqualTo("diDistrictCode", DistrictType.XuHui.code)
                .andEqualTo("diSceneTypeId", SceneType.VehicleRepair.value)
                .andEqualTo("diDeviceTypeId", 1)
                .andEqualTo("diOnline", true)
        }).forEach {
            it?.let {
                deviceCodeList.add(it.diCode)
            }
        }
 
        val cal = Calendar.getInstance(Locale.CHINA).apply {
            set(Calendar.MINUTE, 0)
            set(Calendar.SECOND, 0)
        }
        var endTime = DateUtil.DateToString(cal.time, "yyyy-MM-dd HH:mm:ss") ?: ""
        cal.add(Calendar.HOUR_OF_DAY, -1)
        var startTime = DateUtil.DateToString(cal.time, "yyyy-MM-dd HH:mm:ss") ?: ""
 
//        startTime = "2023-05-06 13:00:00"
//        endTime = "2023-05-06 15:00:00"
        val deviceInfo = VOCHttpService.DeviceInfo(deviceCodeList, startTime, endTime)
        VOCHttpService.getVOCData(deviceInfo)?.run {
            try {
                if (this["code"].asInt == 200) {
                    val data = this["data"].asJsonObject
                    val unit = mutableMapOf<String, String>()
                    data["rtdMonitorFactorRespVOS"].asJsonArray.forEach { v ->
                        v.asJsonObject.let { o ->
                            if (o["factorCode"].asString.trim() == Factor.Concentration.value) {
                                try {
                                    unit[o["deviceCode"].asString.trim()] = o["factorName"].asString.trim().split("-")[1]
                                } catch (e: IndexOutOfBoundsException) {
                                    LOGGER?.error("获取VOC浓度单位失败", e)
                                }
                            }
                        }
                    }
                    data["rtdMinuteHourDayBeans"].asJsonArray.forEach { e ->
                        e.asJsonObject.let { o ->
                            val hourValueMap = mutableMapOf<String, VOCHourValue>()
 
                            val collectTime = DateUtil.StringToDate(o["collectTime"].asString)
 
                            o["minuteHourDayValueBeans"].asJsonArray.forEach { e1 ->
                                e1.asJsonObject.let { o1 ->
                                    val deviceCode = o1["deviceCode"].asString.toUpperCase()
                                    if (!hourValueMap.containsKey(deviceCode)) {
                                        hourValueMap[deviceCode] = VOCHourValue().apply { vocCreateTime = Date() }
                                    }
                                    val hourValue = hourValueMap[deviceCode]!!
                                    hourValue.vocStatCode = deviceCode
                                    hourValue.vocDataTime = collectTime
                                    hourValue.vocUnit = unit[deviceCode]
                                    val avg = o1["factorAvg"].asString.toDouble()
                                    when (o1["factorCode"].asString) {
                                        Factor.FanElectricity1.value -> hourValue.vocFanElectricity1 = avg
                                        Factor.FanElectricity2.value -> hourValue.vocFanElectricity2 = avg
                                        Factor.FanElectricity3.value -> hourValue.vocFanElectricity3 = avg
                                        Factor.FanElectricity4.value -> hourValue.vocFanElectricity4 = avg
                                        Factor.FanElectricity5.value -> hourValue.vocFanElectricity5 = avg
                                        Factor.FanElectricity6.value -> hourValue.vocFanElectricity6 = avg
                                        Factor.Concentration.value -> hourValue.vocValue = avg
                                        Factor.Temperature.value -> hourValue.vocTemperature = avg
                                        Factor.Humidity.value -> hourValue.vocRh = avg
                                    }
                                }
                            }
                            val r = vocHourValueMapper.selectByExample(Example(VOCHourValue::class.java).apply {
                                createCriteria().andEqualTo("vocDataTime", collectTime)
                            }).map { v ->
                                v?.vocStatCode
                            }
                            hourValueMap.forEach { (t, u) ->
                                if (!r.contains(t)) {
                                    vocHourValueMapper.insertSelective(u)
                                }
                            }
                        }
                    }
                }
            } catch (e: Throwable) {
                e.printStackTrace()
            }
        }
    }
}