vince
2024-04-26 b1c572949997a5d82d9b609163ff280a1c49627d
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
package com.fzzy.gateway.controller;
 
import com.fzzy.async.fzzy40.entity.Fz40Grain;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceReportService;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.BaseResp;
import com.fzzy.gateway.entity.GatewayDevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
 
 
/**
 * 与网关相关的接口数据对接,由第三方对接
 */
@Slf4j
@Controller
@RequestMapping("/gateway/api/v1")
public class GatewayDataApi {
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
 
 
    /**
     * 粮情数据推送,第三方软件平台推送粮情数据到网关
     *
     * @param grainData
     * @return
     * @throws Exception
     */
    @PostMapping("/push-grain")
    public @ResponseBody
    BaseResp pushGrain(@RequestBody Fz40Grain grainData) {
 
        BaseResp resp = new BaseResp();
        try {
 
            log.info("----------主动推送粮情接口执行----{}",grainData);
 
            //根据系统仓库编码获取配置设备信息
            GatewayDevice device = GatewayUtils.getCacheByDepotSysId(grainData.getDepotId());
 
            if (null == device) {
                resp.setMsg("网关中没有获取到配置的终端设备信息,无法执行。仓库编码=" + grainData.getDepotId());
                resp.setCode(BaseResp.CODE_500);
                return resp;
            }
 
            //数据封装转换
            GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(device.getPushProtocol());
            if (null == reportService) {
                log.error("------------粮情推送失败,系统不存在当前协议执行类----{}", device.getDeviceName());
                resp.setMsg("粮情推送失败,系统不存在当前协议执行类,网关设备=" + device.getDeviceName());
                resp.setCode(BaseResp.CODE_500);
                return resp;
            }
 
            //数据封装
            resp = reportService.grainData2GatewayApiInfo(grainData, device);
 
            BaseReqData reqData = new BaseReqData();
            reqData.setProductId(device.getProductId());
            reqData.setDeviceName(device.getDeviceName());
            reqData.setDeviceId(device.getDeviceId());
 
            reqData.setDevice(device);
            reqData.setData(resp.getData());
 
            //手动推送到平台
            resp = reportService.reportGrainDataByHand(reqData);
 
        } catch (Exception e) {
            log.error("-----------手动推送粮情数据执行异常---{}", e);
            resp.setCode(BaseResp.CODE_500);
            resp.setMsg("执行异常:" + e.getMessage());
        }
        return resp;
    }
}