jiazx0107@163.com
2024-01-05 7fe5e6110fb2516a81c2258e8d29fc19ee781d76
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
package com.fzzy.gateway;
 
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiCommonDevice;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.gateway.entity.GatewayDevice;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
/**
 * 常量
 */
@Component
public class GatewayUtils {
 
    /**
     * 设备缓存
     */
    public static Map<String, GatewayDevice> cacheMapDeviceId = new HashMap<>();
 
    /**
     * 设备缓存-只针对
     */
    public static Map<String, GatewayDevice> cacheMapDeviceWeight = new HashMap<>();
 
 
    public static void add2Cache(GatewayDevice device) {
        cacheMapDeviceId.put(device.getDeviceId(), device);
        //保存地磅
        if (GatewayDeviceType.TYPE_01.getCode().equals(device.getType())) {
            cacheMapDeviceWeight.put(device.getDeviceId(), device);
        }
    }
 
    public static Collection<GatewayDevice> allCacheDevice() {
        return cacheMapDeviceId.values();
    }
 
    public static GatewayDevice getCacheByDeviceId(String deviceId) {
        return cacheMapDeviceId.get(deviceId);
    }
 
 
    /**
     * 针对一个分机对廒间的情况
     *
     * @param deviceSn
     * @return
     */
    public static GatewayDevice getCacheByDeviceSn(String deviceSn) {
        Collection<GatewayDevice> list = allCacheDevice();
        if (null == list || list.isEmpty()) return null;
 
        for (GatewayDevice device : list) {
            if (deviceSn.equals(device.getDeviceSn())) return device;
        }
 
        return null;
    }
    public static GatewayDevice getCacheByDeviceSIp(String ip) {
        Collection<GatewayDevice> list = allCacheDevice();
        if (null == list || list.isEmpty()) return null;
 
        for (GatewayDevice device : list) {
            if (ip.equals(device.getIp())) return device;
        }
        return null;
    }
 
    /**
     * 针对一个通讯分机对多个仓情况
     *
     * @param deviceSn
     * @return
     */
    public static List<GatewayDevice> getCacheByDeviceSn2(String deviceSn) {
        Collection<GatewayDevice> list = allCacheDevice();
        if (null == list || list.isEmpty()) return null;
 
        List<GatewayDevice> result = new ArrayList<>();
        for (GatewayDevice device : list) {
            if (deviceSn.equals(device.getDeviceSn())) result.add(device);
        }
        return result;
    }
 
 
    public static List<GatewayDevice> getCacheByDeviceType(String deviceType) {
        List<GatewayDevice> result = new ArrayList<>();
        for (GatewayDevice device : cacheMapDeviceId.values()) {
            if (deviceType.equals(device.getType())) result.add(device);
        }
        return result;
    }
 
    public static GatewayDevice getCacheByDeviceTypeOne(String deviceType) {
        for (GatewayDevice device : cacheMapDeviceId.values()) {
            if (deviceType.equals(device.getType())) return device;
        }
        return null;
    }
 
 
    public static void removeCache(GatewayDevice data) {
        cacheMapDeviceId.remove(data.getDeviceId());
        //cacheMapDeviceSn.remove(data.getDeviceSn());
    }
 
    public static String getStatus(String sn) {
        ApiCommonDevice device = Constant.getCommonDeviceCache(sn);
        if (null == device) return Constant.YN_N;
        return device.getStatus();
    }
 
 
    public static boolean isOnline(String deviceId) {
        GatewayDevice device = getCacheByDeviceId(deviceId);
        if (null == device.getStatus() || Constant.YN_N.equals(device.getStatus())) return false;
        return true;
    }
 
 
    public static Collection<GatewayDevice> listDeviceWeight() {
        return cacheMapDeviceWeight.values();
    }
}