jiazx0107@163.com
2023-12-14 a69402c8b67d8ce4b698d0c394d15ff43b5d99d0
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
package com.fzzy.protocol.fzzy.cmd;
 
 
import com.fzzy.api.data.GatewayDeviceProtocol;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.protocol.fzzy.data.SendMessage;
import com.fzzy.protocol.fzzy.server.FzzyServerEngine;
import com.ld.io.api.InvokeResult;
import com.ld.io.api.IoSession;
import com.ld.io.api.IoSessionQuery;
import lombok.extern.slf4j.Slf4j;
 
import java.util.List;
 
@Slf4j
public class BaseRemoteImpl {
 
    /**
     * 发送命令
     *
     * @param ip
     * @param port
     * @param msg
     * @return
     */
    public InvokeResult send(String ip, int port, byte[] msg) {
        IoSessionQuery sessionFactory = FzzyServerEngine.ioServer.getSessionQuery();
        List<IoSession> sessions = sessionFactory.getAllSession();
        IoSession session = null;
        for (IoSession ioSession : sessions) {
            if (ContextUtil.getServerKey(ip, port).equals(
                    ioSession.getBusinessKey())) {
                session = ioSession;
                break;
            }
        }
        if (null == session) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        return session.invoke(msg);
    }
 
    public InvokeResult send(SendMessage message) {
       // log.debug("平台发送报文---->>控制柜--{}", message.toString());
        IoSessionQuery sessionFactory = FzzyServerEngine.ioServer.getSessionQuery();
        List<IoSession> sessions = sessionFactory.getAllSession();
        IoSession session = null;
        for (IoSession ioSession : sessions) {
            if (ContextUtil.getServerKey(message.getIp(), message.getPort()).equals(
                    ioSession.getBusinessKey())) {
                session = ioSession;
                break;
            }
        }
        if (null == session) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        return session.invoke(message.getByteMsg());
    }
 
}