jiazx0107@163.com
2023-11-09 1f5947ccb5b02817dbe5aaae943c0a7fcfc2cd62
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
package com.fzzy.gateway.hx2023.service;
 
import com.alibaba.fastjson2.JSONObject;
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.CloudSendData;
import com.fzzy.gateway.hx2023.data.SyncReqData;
import com.fzzy.mqtt.MqttProviderConfig;
import com.fzzy.mqtt.MqttPublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
 
/**
 * 根据接受到的MQTT信息执行
 */
@Slf4j
@Component
public class OnReceiveMqttService {
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
    @Resource
    private MqttPublishService publishService;
 
 
    /**
     * 当前接收到云端发送信息
     *
     * @param message
     */
    public void onReceiveMessage(String message) {
 
        try {
            CloudSendData cloudSendData = JSONObject.parseObject(message, CloudSendData.class);
 
 
            String functionId = cloudSendData.getFunctionId();
 
            //粮情采集
            if (ScConstant.FUNCTION_getTAndRHInfo.equals(functionId)) {
                getTAndRHInfo(cloudSendData);
            }
 
        } catch (Exception e) {
 
        }
 
    }
 
    private void getTAndRHInfo(CloudSendData cloudSendData) {
 
        String deviceId = cloudSendData.getDeviceId();
 
        GatewayDevice device = GatewayUtils.getCacheByDeviceId(deviceId);
 
        SyncReqData syncReqData = new SyncReqData();
        syncReqData.setDeviceId(deviceId);
        syncReqData.setMessageId(cloudSendData.getMessageId());
        syncReqData.setMessageType(cloudSendData.getMessageType());
        syncReqData.setFunctionId(cloudSendData.getFunctionId());
        syncReqData.setAutoReplay(true);
        syncReqData.setDevice(device);
 
        BaseResp resp = gatewayRemoteManager.getSyncGrainService(device.getSyncProtocol()).syncGrain2(syncReqData);
 
        //自动推送
        if (200 == resp.getCode() && syncReqData.isAutoReplay()) {
            String topic = ScConstant.TOPIC_REPORT;
 
            topic = topic.replace("${productId}", device.getProductId()).replace("${deviceId}", device.getDeviceId());
 
            publishService.publishMsg(topic, resp.getData());
 
            log.info("=======粮情推送==========={}", resp.getData());
        }
    }
}