feiyu02
2024-01-26 01eae19a4870033e879a3faa6749eece92926cab
src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt
@@ -1,9 +1,11 @@
package com.flightfeather.uav.common.utils
import com.alibaba.fastjson.JSONObject
import com.flightfeather.uav.common.exception.ResponseErrorException
import com.flightfeather.uav.domain.entity.RealTimeData
import com.flightfeather.uav.socket.bean.AirData
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.xssf.streaming.SXSSFWorkbook
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
@@ -12,7 +14,7 @@
import java.util.*
/**
 * 无人船采集数据格式转换
 * 采集数据格式转换
 */
class FileExchange {
@@ -26,11 +28,62 @@
        private val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    }
    fun exchangeJinanData(file: File) {
        val headers = listOf(
            "longitude",
            "latitude",
            "data_time",
            "NO2",
            "CO",
            "H2S",
            "SO2",
            "O3",
            "PM25",
            "PM10",
            "temperature",
            "humidity",
            "VOC",
            "NOI",
            "velocity",
            "wind_speed",
            "wind_direction"
        )
        try {
            ExcelUtil.read(file, headerCheck = {
                val cellIterator = it.cellIterator()
                val headIterator = headers.iterator()
                while (headIterator.hasNext()) {
                    val head = headIterator.next()
                    if (cellIterator.hasNext()) {
                        val cellText = cellIterator.next().stringCellValue
                        if (!cellText.equals(head)) {
                            throw ResponseErrorException("文件格式错误, 表头[${head}]应该为[${cellText}]")
                        }
                    } else {
                        throw ResponseErrorException("文件格式错误, 表头[${head}]缺失")
                    }
                }
                true
            }, onRow = {
                it.cellIterator().forEach {
                    it.numericCellValue
                }
            })
        } catch (e: Exception) {
            throw ResponseErrorException("excel文件内容错误,数据转换失败!", e)
        }
    }
    /**
     * 转换无人船的水质监测数据
     */
    fun exchangeBoatData(deviceCode: String, file: InputStream): List<RealTimeData> {
        val workbook = HSSFWorkbook(file)
        val sheet = workbook.getSheetAt(0)
        val dataList = mutableListOf<RealTimeData>()
        val lastData = mutableListOf<Double>()
        for (i in 1 until sheet.lastRowNum) {
            val row = sheet.getRow(i)
@@ -43,11 +96,27 @@
            val datetime = Date(time)
            //监测因子
            val jO = JSONObject.parseObject(value)
            val tmp = jO.getDoubleValue(TMP)
            val spC = jO.getDoubleValue(SPCOND)
            val tur = jO.getDoubleValue(TUR)
            val dO = jO.getDoubleValue(DO)
            val ph = jO.getDoubleValue(PH)
            var tmp = jO.getDoubleValue(TMP)
            var spC = jO.getDoubleValue(SPCOND)
            var tur = jO.getDoubleValue(TUR)
            var dO = jO.getDoubleValue(DO)
            var ph = jO.getDoubleValue(PH)
            if (lastData.isEmpty()) {
                lastData.addAll(listOf(tmp, spC, tur, dO, ph))
            } else {
                if (tmp == .0) tmp = lastData[0]
                if (spC == .0) spC = lastData[1]
                if (tur == .0) tur = lastData[2]
                if (dO == .0) dO = lastData[3]
                if (ph == .0) ph = lastData[4]
            }
            lastData[0] = tmp
            lastData[1] = spC
            lastData[2] = tur
            lastData[3] = dO
            lastData[4] = ph
            val factorsList = mutableListOf<AirData>()
@@ -80,6 +149,18 @@
                    factorId = "5"
                    factorName = "PH"
                    factorData = ph
                    physicalQuantity = 0.0
                })
                add(AirData().apply {
                    factorId = "12"
                    factorName = "LNG"
                    factorData = lng
                    physicalQuantity = 0.0
                })
                add(AirData().apply {
                    factorId = "13"
                    factorName = "LAT"
                    factorData = lat
                    physicalQuantity = 0.0
                })
            }
@@ -153,7 +234,7 @@
        }
        val newWorkBook = HSSFWorkbook()
        val newWorkBook = SXSSFWorkbook(10000)
        ExcelUtil.write2(heads, contents, newWorkBook, "data")
        newWorkBook.write(out)
@@ -174,7 +255,15 @@
        val filePath = "e:/$fileName"
        val out = FileOutputStream(File(filePath))
        val heads = listOf("id", "device_code", "latitude", "longitude", "altitude", "height", "factors", "data_time", "create_time")
        val heads = listOf("id",
            "device_code",
            "latitude",
            "longitude",
            "altitude",
            "height",
            "factors",
            "data_time",
            "create_time")
        val contents = mutableListOf<Array<Any>>()
        for (i in 1..sheet.lastRowNum) {
@@ -242,7 +331,7 @@
            contents.add(cList.toTypedArray())
        }
        val newWorkBook = HSSFWorkbook()
        val newWorkBook = SXSSFWorkbook(10000)
        ExcelUtil.write2(heads, contents, newWorkBook, "data")
        newWorkBook.write(out)