riku
2020-06-02 9a5f9bfc4f4b153dd0175c63f563d8047e1e2515
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
package com.flightfeather.uav.lightshare.service.impl
 
import com.flightfeather.uav.lightshare.bean.*
import com.flightfeather.uav.lightshare.eunm.CarStatus
import com.flightfeather.uav.lightshare.service.ObdDataService
import com.flightfeather.uav.repository.*
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import java.time.ZoneId
 
/**
 * @author riku
 * Date: 2019/8/27
 */
@Service
class ObdDataServiceImpl(
        val obdDataRepository: ObdDataRepository,
        val carLoginRepository: CarLoginRepository,
        val carLogoutRepository: CarLogoutRepository,
        val obdInfoRepository: ObdInfoRepository,
        val dataStreamRepository: DataStreamRepository,
        val vehicleRepository: VehicleRepository
) : ObdDataService {
 
    override fun getDataByVinCode(vinCode: String?, pageNum: Int?, pageSize: Int?): MutableList<ObdDataVo>{
        val resultList = mutableListOf<ObdDataVo>()
 
        vehicleRepository.getVehicleInfo(pageNum, pageSize).forEach { vehicleInfo ->
            val dataStream = dataStreamRepository.getLatestDataStream(vehicleInfo.obdDeviceCode)
            val obdInfo = obdInfoRepository.getObdInfo(vehicleInfo.obdDeviceCode, 1, 1).takeIf { it.isNotEmpty() }?.get(0)
 
            resultList.add(ObdDataVo().apply {
                obdVin = obdInfo?.obdDeviceCode
                obdTime = dataStream?.obdDataTime
                obdLng = dataStream?.obdLong
                obdLat = dataStream?.obdLat
                obdProtocol = obdInfo?.obdProtocol
                obdMil = obdInfo?.obdMil
//                obdIdCode
//                obdVerificationCode
                obdFaultCodeNum = obdInfo?.obdFaultCodeNum
                obdFaultCode = obdInfo?.obdFaultCode
                obdSpeed = dataStream?.obdSpeed?.toInt()
                obdAirPressure = dataStream?.obdAirPressure
                obdEngineTorque = dataStream?.obdEngineTorque
                obdFrictionTorque = dataStream?.obdFrictionTorque
                obdEngineRpm = dataStream?.obdEngineRpm?.toInt()
                obdEngineFuelFlow = dataStream?.obdEngineFuelFlow
                obdScrUpstreamNo = dataStream?.obdScrUpstreamNox
                obdScrDownstreamNo = dataStream?.obdScrDownstreamNox
                obdRemainReactant = dataStream?.obdRemainReactant
                obdAirInput = dataStream?.obdAirInput
                obdScrInputTemp = dataStream?.obdScrInputTemp
                obdScrOutputTemp = dataStream?.obdScrOutputTemp
                obdDpf = dataStream?.obdDpf
                obdEngineCoolantTemp = dataStream?.obdEngineCoolantTemp
                obdFuelLevel = dataStream?.obdFuelLevel
//                obdLocationStatus = dataStream?.obdLocationStatus
                obdTotalMileage = dataStream?.obdTotalMileage
                obdEngineTorqueMode = dataStream?.obdEngineTorqueMode?.toString()
                obdAcceleratorPedal = dataStream?.obdAcceleratorPedal
                obdTotalOilConsumption = dataStream?.obdTotalOilConsumption
                obdUreaBoxTemp = dataStream?.obdUreaBoxTemp
                obdUreaVolume = dataStream?.obdUreaVolume?.toInt()
                obdTotalUreaConsume = dataStream?.obdTotalUreaConsume
                obdDpfTemp = dataStream?.obdDpfTemp
//                obdFirmwareVersion = dataStream?.
            })
        }
 
        return resultList
    }
 
    override fun getLoginData(deviceCode: String, pageNum: Int?, pageSize: Int?, startTime: String?, endTime: String?): List<CarLoginVo>
            = carLoginRepository.getLoginData(deviceCode, pageNum, pageSize, startTime, endTime)
 
    override fun getLogoutData(deviceCode: String, pageNum: Int?, pageSize: Int?, startTime: String?, endTime: String?): List<CarLogoutVo>
            = carLogoutRepository.getLogoutData(deviceCode, pageNum, pageSize, startTime, endTime)
 
    override fun getObdInfo(deviceCode: String, pageNum: Int?, pageSize: Int?): List<ObdInfoVo>{
        val resultList = mutableListOf<ObdInfoVo>()
        obdInfoRepository.getObdInfo(deviceCode, pageNum, pageSize).forEach {
            val vo = ObdInfoVo()
            BeanUtils.copyProperties(it, vo)
            resultList.add(vo)
        }
        return resultList
    }
 
    override fun getDataStream(deviceCode: String, pageNum: Int?, pageSize: Int?, startTime: String?, endTime: String?): List<DataStreamVo>
            = dataStreamRepository.getDataStream(deviceCode, pageNum, pageSize, startTime, endTime)
 
    override fun getDataStreamCount(deviceCode: String, startTime: String?, endTime: String?): Int
            = dataStreamRepository.getDataStreamCount(deviceCode, startTime, endTime)
 
    override fun getCoordinate(deviceCode: String): LatLngVo{
        val latLngVo = LatLngVo()
        dataStreamRepository.getLatestDataStream(deviceCode)?.let {
            latLngVo.apply {
                this.deviceCode = it.obdDeviceCode
                obdDataTime = it.obdDataTime
                lat = it.obdLat
                lng = it.obdLong
            }
        }
 
        return latLngVo
    }
 
    override fun getTrack(deviceCode: String, startTime: String, endTime: String): List<LatLngVo> {
        val resultList = mutableListOf<LatLngVo>()
        //2019.10.30 目前前端安卓设定最长获取的时长为6小时前至现在的数据,按照obd 每10秒中产生一条数据,最多4 * 360 = 1440 条
        dataStreamRepository.getDataStream(deviceCode, 1, 1500, startTime, endTime).run {
            sortedBy {
                it.obdDataTime
            }
        }.forEach {
            val latLngVo = LatLngVo().apply {
                this.deviceCode = it.obdDeviceCode
                obdDataTime = it.obdDataTime
                lat = it.obdLat
                lng = it.obdLong
            }
            resultList.add(latLngVo)
        }
        return resultList
    }
 
    override fun getLatestCoordinate(pageNum: Int?, pageSize: Int?): List<LatLngVo> {
        val resultList = mutableListOf<LatLngVo>()
 
        val now = LocalDateTime.now()
 
        vehicleRepository.getVehicleInfo(pageNum, pageSize).forEach { vehicleInfo ->
            val dataStream = dataStreamRepository.getLatestDataStream(vehicleInfo.obdDeviceCode)
            val obdInfo = obdInfoRepository.getObdInfo(vehicleInfo.obdDeviceCode, 1, 1).takeIf { it.isNotEmpty() }?.get(0)
 
            //获取数据采样时间,如果为空,则取当前时间的前24小时
            val dataTime = dataStream?.obdDataTime?.toInstant()?.atZone(ZoneId.systemDefault())?.toLocalDateTime()
                    ?: LocalDateTime.now().minusDays(1)
 
            resultList.add(LatLngVo().apply {
                deviceCode = vehicleInfo.obdDeviceCode
                vin = obdInfo?.obdVin
                license = vehicleInfo.obdLicencePlate
                obdDataTime = dataStream?.obdDataTime
                carType = vehicleInfo.obdVehicleType
                //采样时间和当前时间相差超过10分钟认为设备处于离线状态
                //todo 2019.10.25 其余两种车辆状态,后续需处理
                status = if (now.minusMinutes(10).isAfter(dataTime)) {
                    CarStatus.OffLine.value
                } else {
                    CarStatus.OnLine.value
                }
                lat = dataStream?.obdLat
                lng = dataStream?.obdLong
            })
        }
 
        return resultList
 
    }
}