feiyu02
2025-03-27 bde043c8fd1a076f44c402dd56c62d401afbfb16
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package com.flightfeather.uav.common.utils
 
import com.alibaba.fastjson.JSONObject
import com.flightfeather.uav.common.exception.BizException
import com.flightfeather.uav.domain.entity.GridAodDetail
import com.flightfeather.uav.domain.entity.GridDataDetail
import com.flightfeather.uav.domain.entity.RealTimeData
import com.flightfeather.uav.domain.entity.RealTimeDataVehicle
import com.flightfeather.uav.socket.bean.AirData
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.CellType
import org.apache.poi.ss.util.CellAddress
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 {
        private const val TMP = "65536"
        private const val SPCOND = "65548"
        private const val TUR = "65702"
        private const val DO = "65542"
        private const val PH = "65558"
 
        private val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    }
 
    /**
     * 转换静安区车载走航数据
     */
    fun exchangeJinanData(deviceCode: String, file: InputStream): List<RealTimeDataVehicle> {
        val headers = listOf(
            "PostTime",
            "Lon",
            "Lat",
            "PM25",
            "PM10",
            "Temperature",
            "Humidity",
            "Speed",
        )
        val result = mutableListOf<RealTimeDataVehicle>()
        try {
            ExcelUtil.readXLXS(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 BizException("文件格式错误, 表头[${cellText}]应该为[${head}]")
                        }
                    } else {
                        throw BizException("文件格式错误, 表头[${head}]缺失")
                    }
                }
                true
            }, onRow = {
                val data = RealTimeDataVehicle().apply {
                    this.deviceCode = deviceCode
                    dataTime = DateUtil.instance.StringToDate(
                        it.getCell(0)?.stringCellValue?.trim()?.split(".")?.get(0)
                    )
                    longitude = it.getCell(1)?.stringCellValue?.trim()?.toBigDecimal()
                    latitude = it.getCell(2)?.stringCellValue?.trim()?.toBigDecimal()
                    pm25 = it.getCell(3)?.stringCellValue?.trim()?.toFloat()?: 0F
                    pm10 = it.getCell(4)?.stringCellValue?.trim()?.toFloat()?: 0F
                    temperature = it.getCell(5)?.stringCellValue?.trim()?.toFloat()?: 0F
                    humidity = it.getCell(6)?.stringCellValue?.trim()?.toFloat()?: 0F
                    velocity = it.getCell(7)?.stringCellValue?.trim()?.toFloat()?: 0F
                    no2 = 0F
                    co = 0F
                    h2s = 0F
                    so2 = 0F
                    o3 = 0F
                    voc = 0F
                    noi = 0F
                    windSpeed = 0F
                    windDirection = 0F
                }
                result.add(data)
            })
        } catch (e: Exception) {
            e.printStackTrace()
            throw BizException("excel文件内容错误,数据转换失败!", e)
        }
        return result
    }
 
    /**
     * 转换PM2.5表格数据
     */
    fun exchangeGridData(file: InputStream, correctRangeGridCells: Set<Int>): List<GridDataDetail> {
        // 模仿已有代码判断表头正确性 并在错误时抛出异常
        val headers = listOf(
            "pointid",
            "PM2.5"
        )
        val result = mutableListOf<GridDataDetail>()
        try {
            ExcelUtil.readXLXS(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 BizException("文件格式错误, 表头[${cellText}]应该为[${head}]")
                        }
                    } else {
                        throw BizException("文件格式错误, 表头[${head}]缺失")
                    }
                }
                true
            },
                // 判断单元格数据正确性,并在报错后抛出异常
                onRow = {
                    val data = GridDataDetail().apply {
                        // 转换网格组单元格id
                        // 得到当前单元格的定位信息 (eg: B3,BA3...)
                        val cellIdCellPosStr = CellAddress(it.rowNum, 0).formatAsString()
                        // 得到当前单元格数据类型
                        val cellIdCellType = it.getCell(0).cellType
                        when (cellIdCellType) {
                            CellType.NUMERIC -> {
                                // 当值为数字类型时转化为网格单元格id
                                this.cellId = it.getCell(0)?.numericCellValue?.toInt()
                            }
                            else -> {
                                throw BizException("单元格[${cellIdCellPosStr}]不是数字类型")
                            }
                        }
 
                        // 转换PM2.5
                        // 得到当前单元格的定位信息 (eg: B3,BA3...)
                        val pm25CellPosStr = CellAddress(it.rowNum, 1).formatAsString()
                        // 得到当前单元格数据类型
                        val pm25CellType = it.getCell(1).cellType
                        when (pm25CellType) {
                            CellType.STRING -> {
                                // 当值为字符串时有两种情况
                                // 空白或者是‘/’字符 转化为空值
                                // 否则报错
                                val stringValue = it.getCell(1)?.stringCellValue?.trim()
                                if (stringValue != null) {
                                    if (stringValue == "/" || stringValue.isBlank()) {
                                        this.pm25 = null
                                    }
                                }else {
                                    throw BizException("单元格[${pm25CellPosStr}]不是数字类型或者文本类型")
                                }
                            }
                            CellType.NUMERIC -> {
                                // 当值为数字类型时转化为pm2.5数据
                                this.pm25 = it.getCell(1)?.numericCellValue?.toFloat()
                            }
                            else -> {
                                throw BizException("单元格[${pm25CellPosStr}]不是数字类型")
                            }
                        }
                    }
                    result.add(data)
                })
        } catch (e: BizException) {
            throw e
        }
        // 判断网格单元格编号是否在正确的范围内
        val resultCellIdSet = result.asSequence().map { it.cellId ?: -1 }.toSet()
        // 用户导入中缺少的的单元格
        val shortCells = correctRangeGridCells - resultCellIdSet
        // excel 中多出的的单元格
        val outCells = resultCellIdSet - correctRangeGridCells
        if (shortCells.isNotEmpty()) {
            throw BizException("导入数据中缺少以下网格单元格:${shortCells.joinToString(",")}")
        }
        if (outCells.isNotEmpty()) {
            throw BizException("导入数据中有多余网格单元格:${outCells.joinToString(",")}")
        }
        return result
    }
 
    /**
     * 转换AOD表格数据
     */
    fun exchangeGridAod(file: InputStream, correctRangeGridCells: Set<Int>): List<GridAodDetail> {
        // 模仿已有代码判断表头正确性 并在错误时抛出异常
        val headers = listOf(
            "pointid",
            "AOD"
        )
        val result = mutableListOf<GridAodDetail>()
        try {
            ExcelUtil.readXLXS(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 BizException("文件格式错误, 表头[${cellText}]应该为[${head}]")
                        }
                    } else {
                        throw BizException("文件格式错误, 表头[${head}]缺失")
                    }
                }
                true
            },
                // 判断单元格数据正确性,并在报错后抛出异常
                onRow = {
                    val data = GridAodDetail().apply {
                        // 转换网格组单元格id
                        // 得到当前单元格的定位信息 (eg: B3,BA3...)
                        val cellIdCellPosStr = CellAddress(it.rowNum, 0).formatAsString()
                        // 得到当前单元格数据类型
                        val cellIdCellType = it.getCell(0).cellType
                        when (cellIdCellType) {
                            CellType.NUMERIC -> {
                                // 当值为数字类型时转化为网格单元格id
                                this.cellId = it.getCell(0)?.numericCellValue?.toInt()
                            }
                            else -> {
                                throw BizException("单元格[${cellIdCellPosStr}]不是数字类型")
                            }
                        }
 
                        // 转换AOD
                        // 得到当前单元格的定位信息 (eg: B3,BA3...)
                        val aodCellPosStr = CellAddress(it.rowNum, 1).formatAsString()
                        // 得到当前单元格数据类型
                        val aodCellType = it.getCell(1).cellType
                        when (aodCellType) {
                            CellType.STRING -> {
                                // 当值为字符串时有两种情况
                                // 空白或者是‘/’字符 转化为空值
                                // 否则报错
                                val stringValue = it.getCell(1)?.stringCellValue?.trim()
                                if (stringValue != null) {
                                    if (stringValue == "/" || stringValue.isBlank()) {
                                        this.aod = null
                                    }
                                }else {
                                    throw BizException("单元格[${aodCellPosStr}]不是数字类型或者文本类型")
                                }
                            }
                            CellType.NUMERIC -> {
                                // 当值为数字类型时转化为pm2.5数据
                                this.aod = it.getCell(1)?.numericCellValue?.toFloat()
                            }
                            else -> {
                                throw BizException("单元格[${aodCellPosStr}]不是数字类型")
                            }
                        }
                    }
                    result.add(data)
                })
        } catch (e: BizException) {
            throw e
        }
        // 判断网格单元格编号是否在正确的范围内
        val resultCellIdSet = result.asSequence().map { it.cellId ?: -1 }.toSet()
        // 用户导入中缺少的的单元格
        val shortCells = correctRangeGridCells - resultCellIdSet
        // excel 中多出的的单元格
        val outCells = resultCellIdSet - correctRangeGridCells
        if (shortCells.isNotEmpty()) {
            throw BizException("导入数据中缺少以下网格单元格:${shortCells.joinToString(",")}")
        }
        if (outCells.isNotEmpty()) {
            throw BizException("导入数据中有多余网格单元格:${outCells.joinToString(",")}")
        }
        return result
    }
 
    /**
     * 转换车载走航数据
     */
    fun exchangeVehicleData(deviceCode: String, file: InputStream): List<RealTimeDataVehicle> {
        val headers = listOf(
            "data_time",
            "longitude",
            "latitude",
            "NO2",
            "CO",
            "H2S",
            "SO2",
            "O3",
            "PM25",
            "PM10",
            "temperature",
            "humidity",
            "VOC",
            "NOI",
            "velocity",
            "wind_speed",
            "wind_direction"
        )
        val result = mutableListOf<RealTimeDataVehicle>()
        try {
            ExcelUtil.readXLXS(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 BizException("文件格式错误, 表头[${head}]应该为[${cellText}]")
                        }
                    } else {
                        throw BizException("文件格式错误, 表头[${head}]缺失")
                    }
                }
                true
            }, onRow = {
                val data = RealTimeDataVehicle().apply {
                    this.deviceCode = deviceCode
                    dataTime = it.getCell(0).dateCellValue
                    longitude = it.getCell(1).numericCellValue.toBigDecimal()
                    latitude = it.getCell(2).numericCellValue.toBigDecimal()
                    no2 = it.getCell(3).numericCellValue.toFloat()
                    co = it.getCell(4).numericCellValue.toFloat()
                    h2s = it.getCell(5).numericCellValue.toFloat()
                    so2 = it.getCell(6).numericCellValue.toFloat()
                    o3 = it.getCell(7).numericCellValue.toFloat()
                    pm25 = it.getCell(8).numericCellValue.toFloat()
                    pm10 = it.getCell(9).numericCellValue.toFloat()
                    temperature = it.getCell(10).numericCellValue.toFloat()
                    humidity = it.getCell(11).numericCellValue.toFloat()
                    voc = it.getCell(12).numericCellValue.toFloat()
                    noi = it.getCell(13).numericCellValue.toFloat()
                    velocity = it.getCell(14).numericCellValue.toFloat()
                    windSpeed = it.getCell(15).numericCellValue.toFloat()
                    windDirection = it.getCell(16).numericCellValue.toFloat()
                }
                result.add(data)
            })
        } catch (e: Exception) {
            e.printStackTrace()
            throw BizException("excel文件内容错误,数据转换失败!", e)
        }
        return result
    }
 
    /**
     * 转换无人船的水质监测数据
     */
    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() {
        //source
        val path = "E:\\工作\\金山走航\\MissionData.xls"
        val workbook = HSSFWorkbook(FileInputStream(path))
        val sheet = workbook.getSheetAt(0)
 
        //target
        val fileName = "无人船数据.xls"
        val filePath = "e:/$fileName"
        val out = FileOutputStream(File(filePath))
 
        val heads = listOf("时间", "经度", "纬度", "Temperature(°C)", "spCond(uS/cm)", "Turbidity(NTU)", "DO(mg/L)", "PH")
        val contents = mutableListOf<Array<Any>>()
 
        val lastRow = mutableListOf<Any>()
        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 cList = mutableListOf<Any>()
            //时间
            val date = Date(time)
            val d = format.format(date)
            cList.add(d)
            //经度
            cList.add(lng)
            cList.add(lat)
            //
            val jO = JSONObject.parseObject(value)
            cList.add((jO[TMP] ?: "0").toString())
            cList.add((jO[SPCOND] ?: "0").toString())
            cList.add((jO[TUR] ?: "0").toString())
            cList.add((jO[DO] ?: "0").toString())
            cList.add((jO[PH] ?: "0").toString())
 
//            if (lastRow.isEmpty()) {
//                lastRow.addAll(cList)
//            } else {
//                for (i in cList.indices) {
//                    if (cList[i] == "") {
//                        cList[i] = lastRow[i]
//                    }
//                }
//                lastRow.clear()
//                lastRow.addAll(cList)
//            }
 
            contents.add(cList.toTypedArray())
        }
 
 
        val newWorkBook = SXSSFWorkbook(10000)
        ExcelUtil.write2(heads, contents, newWorkBook, "data")
 
        newWorkBook.write(out)
        newWorkBook.close()
        workbook.close()
        out.flush()
        out.close()
    }
 
    fun doTask2() {
        //source
        val path = "E:\\工作\\金山走航\\无人船数据-补充空白值.xls"
        val workbook = HSSFWorkbook(FileInputStream(path))
        val sheet = workbook.getSheetAt(0)
 
        //target
        val fileName = "无人船走航数据.xls"
        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 contents = mutableListOf<Array<Any>>()
 
        for (i in 1..sheet.lastRowNum) {
            val row = sheet.getRow(i)
            val deviceCode = "0c"
            val latitude = row.getCell(2).numericCellValue
            val longitude = row.getCell(1).numericCellValue
            val altitude = ""
            val height = ""
            val dataTime = row.getCell(0).stringCellValue
            val createTime = dataTime
 
            val factorsList = mutableListOf<AirData>()
            val tmp = row.getCell(3).numericCellValue
            val spC = row.getCell(4).numericCellValue
            val tur = row.getCell(5).numericCellValue
            val dO = row.getCell(6).numericCellValue
            val ph = row.getCell(7).numericCellValue
 
            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
                })
            }
 
            val factors = JSONObject.toJSON(factorsList).toString()
            val cList = mutableListOf<Any>()
            cList.add("")
            cList.add(deviceCode)
            cList.add(latitude)
            cList.add(longitude)
            cList.add(altitude)
            cList.add(height)
            cList.add(factors)
            cList.add(dataTime)
            cList.add(createTime)
 
            contents.add(cList.toTypedArray())
        }
 
        val newWorkBook = SXSSFWorkbook(10000)
        ExcelUtil.write2(heads, contents, newWorkBook, "data")
 
        newWorkBook.write(out)
        newWorkBook.close()
        workbook.close()
        out.flush()
        out.close()
    }
}