jiazx0107@163.com
2023-11-09 19860e76e91baf3cfce3c45bfa3ca886788c4ec8
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
package com.fzzy.gateway.service;
 
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.data.BaseResp;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.hx2023.ScConstant;
import com.fzzy.gateway.hx2023.data.SyncReqData;
import com.fzzy.gateway.service.repository.GatewayDeviceRep;
import com.fzzy.mqtt.MqttGatewayService;
import com.fzzy.mqtt.MqttProviderConfig;
import com.fzzy.mqtt.MqttPublishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.List;
 
@Slf4j
@Component
public class GatewayDeviceService2 {
 
    @Resource
    private GatewayDeviceRep gatewayDeviceRep;
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    @Resource
    private MqttGatewayService publishService;
 
    public List<GatewayDevice> listAll() {
        Sort sort = new Sort(Sort.Direction.ASC, "deviceId");
        return gatewayDeviceRep.findAll(sort);
    }
 
 
    /**
     * gatewayDeviceService2#updateSave
     *
     * @param data
     */
    @DataResolver
    public void updateSave(GatewayDevice data) {
        GatewayDevice data2 = new GatewayDevice();
        BeanUtils.copyProperties(data, data2);
 
        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 flushCache() {
        List<GatewayDevice> list = listAll();
        if (null == list || list.isEmpty()) return;
        for (GatewayDevice device : list) {
            GatewayUtils.add2Cache(device);
        }
    }
 
 
    /**
     * 测试MQTT粮情检测
     * gatewayDeviceService#ajaxTestGrain2
     * 粮情推送测试
     *
     * @param data
     * @return
     */
    @Expose
    public String ajaxTestGrain2(GatewayDevice data) {
 
 
        SyncReqData reqData = new SyncReqData();
        reqData.setDevice(data);
        reqData.setAutoReplay(true);
        reqData.setMessageType(ScConstant.MESSAGE_TYPE_INVOKE_FUNCTION);
        reqData.setMessageId(ScConstant.getMessageId());
        reqData.setFunctionId(ScConstant.FUNCTION_getTAndRHInfo);
 
 
        if (!GatewayDeviceType.TYPE_07.getCode().equals(data.getType())) {
            return "ERROR:当前设备非粮情设备不支持当前操作";
        }
 
        if(StringUtils.isEmpty(data.getCableRule())){
            return "ERROR:当前设备没有配置布线规则,无法执行";
        }
 
        BaseResp resp = gatewayRemoteManager.getSyncGrainService(data.getSyncProtocol()).syncGrain2(reqData);
 
        //自动推送
        if (200 == resp.getCode() && reqData.isAutoReplay()) {
            String topic = ScConstant.TOPIC_REPORT;
            topic = topic.replace("${productId}", data.getProductId()).replace("${deviceId}", data.getDeviceId());
 
            publishService.publishMqttWithTopic(resp.getData(),topic);
 
            log.info("----------------------------手动推送MQTT粮情信息---------------------------");
            log.info("-----TOPIC-----{}",topic);
            log.info("-----Message-----{}",resp.getData());
 
        }
 
        return "SUCCESS:执行完成";
    }
 
}