riku
2021-06-21 c2a5872d4ab060e0e19a14be271a4b50d5e6059e
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
package com.flightfeather.uav.common.utils
 
import com.alibaba.fastjson.JSONObject
import com.flightfeather.uav.domain.entity.RealTimeData
import com.flightfeather.uav.socket.bean.AirData
import org.apache.poi.hssf.usermodel.HSSFWorkbook
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 exchangeBoatData(deviceCode: String, file: InputStream): List<RealTimeData> {
        val workbook = HSSFWorkbook(file)
        val sheet = workbook.getSheetAt(0)
 
        val dataList = mutableListOf<RealTimeData>()
 
        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)
            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)
 
            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
                })
            }
 
            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 = HSSFWorkbook()
        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 = HSSFWorkbook()
        ExcelUtil.write2(heads, contents, newWorkBook, "data")
 
        newWorkBook.write(out)
        newWorkBook.close()
        workbook.close()
        out.flush()
        out.close()
    }
}