riku
2019-09-05 28b10b7255483c8e2eb5e3e828f8cda658e94c44
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
package com.flightfeather.obd.socket
 
import com.flightfeather.obd.lightshare.bean.BaseJson
import com.flightfeather.obd.lightshare.bean.ObdDataVo
import com.flightfeather.obd.repository.ObdDataRepository
import com.google.gson.Gson
import io.netty.channel.ChannelHandlerContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
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
 
    @PostConstruct
    fun init() {
        instance = this
        instance.obdDataRepository = this.obdDataRepository
    }
 
    fun dealMsg(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) {
//            println("------收到格式错误的数据:$msg")
        }
    }
}