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
package com.flightfeather.uav.socket
 
import com.flightfeather.uav.socket.bean.AirTypeData
import io.netty.channel.ChannelHandlerContext
import java.util.concurrent.ConcurrentHashMap
 
/**
 * 用于保存连接的设备及对应的session通道以及对应的专属信息
 * Date: 2019.8.27
 * @author riku
 */
class DeviceSession {
 
    companion object{
        private val deviceMap = ConcurrentHashMap<String, ChannelHandlerContext?>()
        private val typeMap = ConcurrentHashMap<String, List<AirTypeData>>()
        private const val DEFAULT_DEVICE = "default_device"
 
        fun saveDevice(deviceCode: String?, channel: ChannelHandlerContext?) {
            deviceCode?.let {
                deviceMap.put(deviceCode, channel)
            }
        }
 
        fun getDevice(deviceCode: String?): ChannelHandlerContext? {
            return if (deviceMap.containsKey(deviceCode))
                deviceMap[deviceCode]
            else
                null
        }
 
        fun saveAirType(deviceCode: String?, types: List<AirTypeData>) {
            if (deviceCode == null) {
                typeMap[DEFAULT_DEVICE] = types
            } else {
                typeMap[deviceCode] = types
            }
        }
 
        fun getAirType(deviceCode: String?): List<AirTypeData>? {
            return when {
                deviceCode == null -> typeMap[DEFAULT_DEVICE]
                typeMap.containsKey(deviceCode) -> typeMap[deviceCode]
                else -> null
            }
        }
    }
}