CZT
2023-09-06 71c4fa1e27f75ae4b765c95c67a3069c84dc72ba
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
package com.ld.igds.protocol.zldz.command;
 
import com.ld.igds.io.RemoteGrainService;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.request.BaseRequest;
import com.ld.igds.io.request.CheckGrainRequest;
import com.ld.igds.io.response.BaseResponse;
import com.ld.igds.io.response.GrainResponse;
import com.ld.igds.protocol.zldz.command.builder.CommandBuilder1115;
import com.ld.igds.protocol.zldz.command.builder.CommandBuilder8815;
import com.ld.igds.protocol.zldz.command.builder.CommandBuilder8817;
import com.ld.igds.protocol.zldz.command.message.Message;
import com.ld.igds.protocol.zldz.task.CommandReSendService;
import com.ld.igds.protocol.zldz.task.CommandTask;
import com.ld.igds.protocol.zldz.util.ServerUtils;
import com.ld.io.api.InvokeResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
/**
 * 粮情协议,直接获取粮情,温湿度信息通过心跳定时获取。
 *
 * @author jiazx
 */
@Slf4j
@Component(RemoteGrainServiceImpl.BEAN_ID)
public class RemoteGrainServiceImpl extends BaseRemoteImpl implements
        RemoteGrainService {
 
    public static final String BEAN_ID = "zldz.remoteGrainService";
 
    @Autowired
    private CommandReSendService commandReSendService;
 
    /**
     * 粮情检测,首先需要执行温湿度检测,温湿度检测完成后,再进粮情检测。
     * 温湿度的信息根据分机和温湿度地址存在缓存30分钟,30分钟内不重复采集,系统判断与当前时间差距离当前时间是否超过25分钟
     * ,超过重新获取,没有超过直接采集粮情
     *
     * @param request
     * @return
     */
    @Override
    public GrainResponse checkGrain(CheckGrainRequest request) {
        // Step1 生成粮情信息
        Message message = CommandBuilder1115.getInstance().buildMessage(request);
 
        log.info("平台------>>>分机:" +request.getSerName() + "-粮情采集命令=" + message.toString());
 
        InvokeResult result = send(request.getIp(), request.getPort(), message.getByteMsg());
 
        // 封装返回信息
        if (InvokeResult.SUCCESS == result) {
            CommandTask commandData = new CommandTask();
            commandData.setCompanyId(request.getCompanyId());
            commandData.setIp(request.getIp());
            commandData.setPort(request.getPort());
            commandData.setByteCommand(message.getByteMsg());
            commandData.setCommand(message.getStrMsg());
            commandData.setResultType(ServerUtils.MSG_TYPE_8817);
            commandData.setCableRule(request.getCableRule());
            commandData.setSerName(request.getSerName());
            commandData.setDepotId(request.getDepotId());
            commandReSendService.reSendGrainTask(commandData);
            return new GrainResponse(OrderRespEnum.ORDER_SUCCESS.getCode(),
                    "粮情采集命令发送成功");
        } else {
            log.info("分机" + request.getSerName() + "粮情采集命令发送失败:" + result.getMessage());
            return new GrainResponse(OrderRespEnum.ORDER_ERROR.getCode(), result.getMessage());
        }
    }
 
    /**
     * 服务器回复终端的收到请求
     *
     * @param request
     */
    public BaseResponse reply8815(BaseRequest request) {
        // Step1 生成粮情信息
        Message message = CommandBuilder8815.getInstance().buildMessage(
                request);
 
        log.debug("平台------>>>分机:" +request.getSerName() + "-粮情服务端回复命令=" + message.toString());
 
        // 发送命令
        InvokeResult result = send(request.getIp(), request.getPort(),
                message.getByteMsg());
        // 封装返回信息
        if (InvokeResult.SUCCESS == result) {
            return new BaseResponse(OrderRespEnum.ORDER_SUCCESS.getCode(),
                    "分机" + request.getSerName() + "粮情服务端回复命令发送成功");
        } else {
            log.error("分机" + request.getSerName() + "粮情服务端回复命令发送失败:" + result.getMessage());
            return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "分机" + request.getSerName() + "粮情服务端回复命令发送失败:" + result.getMessage());
        }
    }
 
    /**
     * 回复命令
     *
     * @param request
     * @param hexCurPacket 当前包的hex
     */
    public BaseResponse reply8817(BaseRequest request, String hexCurPacket) {
        // Step1 生成粮情信息,把信息放在request
        request.setBizType(hexCurPacket);
 
        Message message = CommandBuilder8817.getInstance().buildMessage(request);
 
        log.debug("平台------>>>分机:" +request.getSerName() + "-粮情服务端回复收到粮情包命令=" + message.toString());
 
        // 发送命令
        InvokeResult result = send(request.getIp(), request.getPort(),
                message.getByteMsg());
 
        // 封装返回信息
        if (InvokeResult.SUCCESS == result) {
            return new BaseResponse(OrderRespEnum.ORDER_SUCCESS.getCode(),
                    "分机" + request.getSerName() + "粮情服务端回复收到粮情包命令发送成功");
        } else {
            log.info("分机" + request.getSerName() + "粮情服务端回复收到粮情包命令发送失败:" + result.getMessage());
 
            return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "分机" + request.getSerName() + "粮情服务端回复收到粮情包命令发送失败:" + result.getMessage());
        }
    }
}