From 01eae19a4870033e879a3faa6749eece92926cab Mon Sep 17 00:00:00 2001 From: feiyu02 <risaku@163.com> Date: 星期五, 26 一月 2024 17:10:55 +0800 Subject: [PATCH] 1. 新增多项yml配置文件 2. 新增导入静安监测数据功能模块(未完成) --- src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 170 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt index b282399..efc4c36 100644 --- a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt +++ b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt @@ -1,14 +1,21 @@ 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 +import java.io.InputStream import java.text.SimpleDateFormat import java.util.* +/** + * 閲囬泦鏁版嵁鏍煎紡杞崲 + */ class FileExchange { companion object { @@ -19,6 +26,158 @@ private const val PH = "65558" 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) + val time = row.getCell(2).numericCellValue.toLong() * 1000 + val lat = row.getCell(4).numericCellValue + val lng = row.getCell(5).numericCellValue + val value = row.getCell(6).stringCellValue + + //鏃堕棿 + val datetime = Date(time) + //鐩戞祴鍥犲瓙 + val jO = JSONObject.parseObject(value) + 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>() + + factorsList.apply { + add(AirData().apply { + factorId = "1" + factorName = "TMP" + factorData = tmp + physicalQuantity = 0.0 + }) + add(AirData().apply { + factorId = "2" + factorName = "spC" + factorData = spC + physicalQuantity = 0.0 + }) + add(AirData().apply { + factorId = "3" + factorName = "tur" + factorData = tur + physicalQuantity = 0.0 + }) + add(AirData().apply { + factorId = "4" + factorName = "DO" + factorData = dO + physicalQuantity = 0.0 + }) + add(AirData().apply { + 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 + }) + } + + val factors = JSONObject.toJSON(factorsList).toString() + + dataList.add(RealTimeData().apply { + this.deviceCode = deviceCode + latitude = lat.toBigDecimal() + longitude = lng.toBigDecimal() + dataTime = datetime + createTime = Date() + this.factors = factors + }) + } + + return dataList } fun doTask() { @@ -75,7 +234,7 @@ } - val newWorkBook = HSSFWorkbook() + val newWorkBook = SXSSFWorkbook(10000) ExcelUtil.write2(heads, contents, newWorkBook, "data") newWorkBook.write(out) @@ -96,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) { @@ -164,7 +331,7 @@ contents.add(cList.toTypedArray()) } - val newWorkBook = HSSFWorkbook() + val newWorkBook = SXSSFWorkbook(10000) ExcelUtil.write2(heads, contents, newWorkBook, "data") newWorkBook.write(out) -- Gitblit v1.9.3