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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.flightfeather.uav.repository.impl
 
import com.flightfeather.uav.common.utils.GsonUtils
import com.flightfeather.uav.domain.MyMapper
import com.flightfeather.uav.domain.entity.*
import com.flightfeather.uav.domain.mapper.RealTimeDataGridMapper
import com.flightfeather.uav.domain.mapper.RealTimeDataMapper
import com.flightfeather.uav.domain.mapper.RealTimeDataUavMapper
import com.flightfeather.uav.domain.mapper.RealTimeDataVehicleMapper
import com.flightfeather.uav.lightshare.bean.DataVo
import com.flightfeather.uav.repository.AirDataRepository
import com.flightfeather.uav.socket.bean.AirData
import com.flightfeather.uav.socket.bean.AirDataPackage
import com.flightfeather.uav.socket.eunm.FactorType
import com.flightfeather.uav.socket.eunm.UWDeviceType
import com.google.gson.Gson
import org.springframework.stereotype.Repository
import java.text.SimpleDateFormat
 
/**
 * @author riku
 * Date: 2020/6/11
 */
@Repository
class AirDataRepositoryImpl(
    private val realTimeDataMapper: RealTimeDataMapper,
    private val realTimeDataVehicleMapper: RealTimeDataVehicleMapper,
    private val realTimeDataUavMapper: RealTimeDataUavMapper,
    private val realTimeDataGridMapper: RealTimeDataGridMapper
): AirDataRepository {
 
    override fun saveAirData(dataPackage: AirDataPackage): Int {
        val data = RealTimeData().apply {
            deviceCode = dataPackage.deviceCode
            latitude
            longitude
            altitude
            height
            factors = Gson().toJson(dataPackage.dataUnit)
            dataTime = dataPackage.dataTime
        }
        dataPackage.dataUnit.forEach {
            if (it is AirData) {
                when (it.factorId?.toInt()) {
                    FactorType.LAT.value -> {
                        data.latitude = it.factorData?.toBigDecimal()
                    }
                    FactorType.LNG.value -> {
                        data.longitude = it.factorData?.toBigDecimal()
                    }
                    FactorType.TIME.value -> {
                        it.statusList?.takeIf {l-> l.isNotEmpty() }?.get(0)?.let {d ->
                            data.dataTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d)
                        }
                    }
                }
            }
        }
        realTimeDataMapper.insert(data)
 
        return 0
    }
 
    override fun savePrepData(dataPackage: AirDataPackage): Int {
        var mapper: MyMapper<out BaseRealTimeData?>? = null
        when (UWDeviceType.getType(dataPackage.deviceCode)) {
            UWDeviceType.VEHICLE -> {
                mapper = realTimeDataVehicleMapper
                RealTimeDataVehicle()
            }
            UWDeviceType.UAV -> {
                mapper = realTimeDataUavMapper
                RealTimeDataUav()
            }
            UWDeviceType.GRID -> {
                mapper = realTimeDataGridMapper
                RealTimeDataGrid()
            }
            else -> null
        }?.run {
            deviceCode = dataPackage.deviceCode
            dataPackage.dataUnit.forEach {
                if (it is AirData) {
                    when (it.factorId?.toInt()) {
                        FactorType.NO2.value -> no2 = it.factorData?.toFloat()
                        FactorType.CO.value -> co = it.factorData?.toFloat()
                        FactorType.H2S.value -> h2s = it.factorData?.toFloat()
                        FactorType.SO2.value -> so2 = it.factorData?.toFloat()
                        FactorType.O3.value -> o3 = it.factorData?.toFloat()
                        FactorType.PM25.value -> pm25 = it.factorData?.toFloat()
                        FactorType.PM10.value -> pm10 = it.factorData?.toFloat()
                        FactorType.TEMPERATURE.value -> temperature = it.factorData?.toFloat()
                        FactorType.HUMIDITY.value -> humidity = it.factorData?.toFloat()
                        FactorType.VOC.value -> voc = it.factorData?.toFloat()
                        FactorType.NOI.value -> noi = it.factorData?.toFloat()
                        FactorType.VELOCITY.value -> velocity = it.factorData?.toFloat()
                        FactorType.WIND_SPEED.value -> windSpeed = it.factorData?.toFloat()
                        FactorType.WIND_DIRECTION.value -> windDirection = it.factorData?.toFloat()
                        FactorType.HEIGHT.value -> height = it.factorData?.toFloat()
 
                        FactorType.LAT.value -> latitude = it.factorData?.toBigDecimal()
                        FactorType.LNG.value -> longitude = it.factorData?.toBigDecimal()
                        FactorType.TIME.value -> it.statusList?.takeIf { l -> l.isNotEmpty() }?.get(0)?.let { d ->
                            dataTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d)
                        }
                    }
                }
            }
 
//            mapper?.insert(this)
            return 1
        }
        return 0
    }
 
    override fun savePrepData(dataList: List<RealTimeData>): Int {
        var count = 0
        dataList.forEach {vo ->
            when (UWDeviceType.getType(vo.deviceCode)) {
                UWDeviceType.VEHICLE -> {
                    val d = RealTimeDataVehicle()
                    dataTransform(vo, d)
                    realTimeDataVehicleMapper.insert(d)
                    count++
                }
                UWDeviceType.UAV -> {
                    val d = RealTimeDataUav()
                    dataTransform(vo, d)
                    realTimeDataUavMapper.insert(d)
                    count++
                }
                UWDeviceType.GRID -> {
                    val d = RealTimeDataGrid()
                    dataTransform(vo, d)
                    realTimeDataGridMapper.insert(d)
                    count++
                }
            }
        }
        return count
    }
 
    private fun dataTransform(vo: RealTimeData, bean: BaseRealTimeData) {
        bean.apply {
            deviceCode = vo.deviceCode
            latitude = vo.latitude
            longitude = vo.longitude
            dataTime = vo.dataTime
            createTime = vo.createTime
            GsonUtils.parserJsonToArrayBeans(vo.factors, AirData::class.java).forEach {
                when (it.factorId?.toInt()) {
                    FactorType.NO2.value -> no2 = it.factorData?.toFloat()
                    FactorType.CO.value -> co = it.factorData?.toFloat()
                    FactorType.H2S.value -> h2s = it.factorData?.toFloat()
                    FactorType.SO2.value -> so2 = it.factorData?.toFloat()
                    FactorType.O3.value -> o3 = it.factorData?.toFloat()
                    FactorType.PM25.value -> pm25 = it.factorData?.toFloat()
                    FactorType.PM10.value -> pm10 = it.factorData?.toFloat()
                    FactorType.TEMPERATURE.value -> temperature = it.factorData?.toFloat()
                    FactorType.HUMIDITY.value -> humidity = it.factorData?.toFloat()
                    FactorType.VOC.value -> voc = it.factorData?.toFloat()
                    FactorType.NOI.value -> noi = it.factorData?.toFloat()
                    FactorType.VELOCITY.value -> velocity = it.factorData?.toFloat()
                    FactorType.WIND_SPEED.value -> windSpeed = it.factorData?.toFloat()
                    FactorType.WIND_DIRECTION.value -> windDirection = it.factorData?.toFloat()
                    FactorType.HEIGHT.value -> height = it.factorData?.toFloat()
                }
            }
 
        }
    }
}