riku
2019-10-30 57b3b0851b2144073522a43640c2acc9452e1719
src/main/kotlin/com/flightfeather/obd/lightshare/service/impl/ObdDataServiceImpl.kt
@@ -1,9 +1,14 @@
package com.flightfeather.obd.lightshare.service.impl
import com.flightfeather.obd.lightshare.bean.*
import com.flightfeather.obd.lightshare.eunm.CarStatus
import com.flightfeather.obd.lightshare.service.ObdDataService
import com.flightfeather.obd.repository.*
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.*
/**
 * @author riku
@@ -15,7 +20,8 @@
        val carLoginRepository: CarLoginRepository,
        val carLogoutRepository: CarLogoutRepository,
        val obdInfoRepository: ObdInfoRepository,
        val dataStreamRepository: DataStreamRepository
        val dataStreamRepository: DataStreamRepository,
        val vehicleRepository: VehicleRepository
) : ObdDataService {
    override fun getDataByVinCode(vinCode: String, pageNum: Int?, pageSize: Int?): MutableList<ObdDataVo>
@@ -27,12 +33,83 @@
    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>
            = obdInfoRepository.getObdInfo(deviceCode, pageNum, pageSize)
    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 getCoordinate(deviceCode: String): LatLngVo
            = dataStreamRepository.getCoordinate(deviceCode)
    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).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
    }
}