riku
2019-09-15 4d44ed185203088052b10a8d1e3526fcbbc88331
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
package com.flightfeather.obd.socket
 
import com.flightfeather.obd.common.utils.FileUtil
import com.flightfeather.obd.lightshare.bean.BaseJson
import com.flightfeather.obd.lightshare.bean.ObdDataVo
import com.flightfeather.obd.repository.ObdDataRepository
import com.flightfeather.obd.socket.decoder.VehicleDataDecoder
import com.google.gson.Gson
import io.netty.channel.ChannelHandlerContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.util.FileSystemUtils
import javax.annotation.PostConstruct
import javax.annotation.Resource
 
/**
 * 处理socket接收的消息
 * Date: 2019.8.27
 * @author riku
 */
 
@Component
class MessageManager{
 
    companion object{
        private lateinit var instance: MessageManager
    }
 
    @Autowired
    lateinit var obdDataRepository: ObdDataRepository
 
    val vehicleDataDecoder = VehicleDataDecoder()
 
    @PostConstruct
    fun init() {
        instance = this
        instance.obdDataRepository = this.obdDataRepository
    }
 
    fun dealStringMsg(msg: String, ctx: ChannelHandlerContext?) {
//        try {
//            val baseJson = Gson().fromJson<BaseJson>(msg, BaseJson::class.java)
//            when (baseJson.cmdCode) {
//                2001 -> {
//                    val data = Gson().fromJson(msg, ObdDataVo::class.java)
//                    DeviceSession.saveDevice(data.obdVin, ctx)
//                    instance.obdDataRepository.saveObdData(data)
//                }
//            }
//        } catch (e: Throwable) {
//        }
 
        saveToTxt(msg)
        val packageData = vehicleDataDecoder.decode(msg)
    }
 
    fun dealByteArrayMsg(msg: ByteArray, ctx: ChannelHandlerContext?) {
        val b = ByteArray(20) {19}
        println(b)
    }
 
    fun saveToTxt(msg: String) {
        val data = "data=> $msg"
        FileUtil.instance?.saveObdData(data)
    }
}