jiazx0107@163.com
2024-01-04 50da1f165cc96d49d72694606920b77c74e1747c
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
package com.fzzy.gateway.service;
 
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiCommonDevice;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.service.repository.GatewayDeviceRep;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Slf4j
@Component
public class GatewayDeviceService {
 
    @Resource
    private GatewayDeviceRep gatewayDeviceRep;
 
    /**
     * gatewayDeviceService#listAll
     *
     * @return
     */
    @DataProvider
    public List<GatewayDevice> listAll() {
        Sort sort = new Sort(Sort.Direction.ASC, "deviceId");
 
 
        List<GatewayDevice> list = gatewayDeviceRep.findAll(sort);
 
        if (null == list || list.isEmpty()) return list;
 
        List<GatewayDevice> result = new ArrayList<>();
 
        ApiCommonDevice commonDevice;
        for (GatewayDevice device : list) {
            commonDevice = Constant.getCommonDeviceCache(device.getDeviceSn());
            if (null != commonDevice) {
                device.setIp(commonDevice.getIp());
                device.setPort(commonDevice.getPort());
                device.setStatus(commonDevice.getStatus());
                device.setOnlineTime(commonDevice.getOnlineTime());
            }
            result.add(device);
        }
        return result;
    }
 
    /**
     * gatewayDeviceService#updateSave
     *
     * @param data
     */
    @DataResolver
    public void updateSave(GatewayDevice data) {
        GatewayDevice data2 = new GatewayDevice();
        BeanUtils.copyProperties(data, data2);
 
 
        if (null == data2.getStatus()) {
            data.setStatus(Constant.YN_Y);
            if (GatewayDeviceType.TYPE_07.equals(data2.getType())) {
                data.setStatus(Constant.YN_N);
            }
        }
 
        if (null == data2.getDeviceSn()) {
            if (null != data2.getIp()) {
                data.setDeviceSn(data2.getIp());
            } else {
                data.setDeviceSn(data2.getDeviceId());
            }
        }
 
        if (null == data2.getId()) {
            data2.setId(ContextUtil.getUUID());
            gatewayDeviceRep.save(data2);
        } else {
            gatewayDeviceRep.save(data2);
        }
        flushCache();
    }
 
    public void updateData(GatewayDevice device) {
        device.setStatus(Constant.YN_Y);
 
        gatewayDeviceRep.save(device);
 
        flushCacheOne(device);
    }
 
 
    /**
     * gatewayDeviceService#delData
     *
     * @param data
     */
    @Expose
    public String delData(GatewayDevice data) {
        GatewayDevice data2 = new GatewayDevice();
        BeanUtils.copyProperties(data, data2);
        gatewayDeviceRep.delete(data2);
 
        GatewayUtils.removeCache(data2);
 
        flushCache();
        return null;
    }
 
    public void flushCache() {
        List<GatewayDevice> list = listAll();
        if (null == list || list.isEmpty()) return;
        for (GatewayDevice device : list) {
            GatewayUtils.add2Cache(device);
        }
    }
 
    private void flushCacheOne(GatewayDevice device) {
        GatewayUtils.add2Cache(device);
    }
 
 
    @Expose
    public String test() {
        log.info("-----------test-------------------");
        return "SUCCESS";
    }
 
    /**
     * 根据实际通讯分机设置,当前分机在线
     *
     * @param commonDevice 实际通讯设备
     */
    public void onlineByCommonDevice(ApiCommonDevice commonDevice) {
        List<GatewayDevice> list = GatewayUtils.getCacheByDeviceSn2(commonDevice.getSn());
        if (null == list || list.isEmpty()) return;
 
        for (GatewayDevice device : list) {
            device.setIp(commonDevice.getIp());
            device.setPort(commonDevice.getPort());
            device.setOnlineTime(new Date());
            device.setStatus(Constant.YN_Y);
 
            GatewayUtils.add2Cache(device);
        }
    }
 
    public void OfflineByCommonDevice(ApiCommonDevice commonDevice) {
        List<GatewayDevice> list = GatewayUtils.getCacheByDeviceSn2(commonDevice.getSn());
        if (null == list || list.isEmpty()) return;
 
        for (GatewayDevice device : list) {
            device.setIp(commonDevice.getIp());
            device.setPort(commonDevice.getPort());
            device.setStatus(Constant.YN_N);
            GatewayUtils.add2Cache(device);
        }
    }
 
 
}