diff --git a/connect/messager.go b/connect/messager.go index 7a855e6..3255164 100644 --- a/connect/messager.go +++ b/connect/messager.go @@ -7,7 +7,8 @@ import ( ) type Messenger struct { - Tunnel Tunnel + Conn + Timeout time.Duration mu sync.Mutex @@ -17,21 +18,21 @@ func (m *Messenger) Ask(request []byte, response []byte) (int, error) { m.mu.Lock() defer m.mu.Unlock() - //s := bufio.NewReader(m.Tunnel) + //s := bufio.NewReader(m.Conn) //先写 - _, err := m.Tunnel.Write(request) + _, err := m.Conn.Write(request) if err != nil { return 0, err } //读超时 - err = m.Tunnel.SetReadTimeout(m.Timeout) + err = m.Conn.SetReadTimeout(m.Timeout) if err != nil { return 0, err } - return m.Tunnel.Read(response) + return m.Conn.Read(response) } func (m *Messenger) AskAtLeast(request []byte, response []byte, min int) (int, error) { @@ -39,18 +40,18 @@ func (m *Messenger) AskAtLeast(request []byte, response []byte, min int) (int, e defer m.mu.Unlock() //先写 - _, err := m.Tunnel.Write(request) + _, err := m.Conn.Write(request) if err != nil { return 0, err } //读超时 - err = m.Tunnel.SetReadTimeout(m.Timeout) + err = m.Conn.SetReadTimeout(m.Timeout) if err != nil { return 0, err } - return io.ReadAtLeast(m.Tunnel, response, min) + return io.ReadAtLeast(m.Conn, response, min) } func (m *Messenger) Read(response []byte) (int, error) { @@ -58,12 +59,12 @@ func (m *Messenger) Read(response []byte) (int, error) { defer m.mu.Unlock() //读超时 - err := m.Tunnel.SetReadTimeout(m.Timeout) + err := m.Conn.SetReadTimeout(m.Timeout) if err != nil { return 0, err } //读 - return m.Tunnel.Read(response) + return m.Conn.Read(response) } func (m *Messenger) ReadAtLeast(response []byte, min int) (int, error) { @@ -71,10 +72,10 @@ func (m *Messenger) ReadAtLeast(response []byte, min int) (int, error) { defer m.mu.Unlock() //读超时 - err := m.Tunnel.SetReadTimeout(m.Timeout) + err := m.Conn.SetReadTimeout(m.Timeout) if err != nil { return 0, err } - return io.ReadAtLeast(m.Tunnel, response, min) + return io.ReadAtLeast(m.Conn, response, min) } diff --git a/connect/tunnel.go b/connect/tunnel.go index 6462de5..5c3e96d 100644 --- a/connect/tunnel.go +++ b/connect/tunnel.go @@ -8,9 +8,5 @@ type Tunnel interface { Open() error - Close() error - - Available() bool - //Online() bool } diff --git a/modbus/adapter.go b/modbus/adapter.go index c916cd6..e96143a 100644 --- a/modbus/adapter.go +++ b/modbus/adapter.go @@ -3,12 +3,10 @@ package modbus import ( "errors" "github.com/god-jason/bucket/types" - "github.com/zgwit/iot-gateway/connect" "github.com/zgwit/iot-gateway/product" ) type Adapter struct { - tunnel connect.Tunnel modbus Modbus //device=>product_id @@ -20,10 +18,6 @@ type Adapter struct { pollers map[string]*[]*Poller } -func (adapter *Adapter) Tunnel() connect.Tunnel { - return adapter.tunnel -} - func (adapter *Adapter) Mount(id string, product_id string, station types.Options) (err error) { adapter.devices[id] = product_id adapter.stations[id] = station diff --git a/modbus/modbus-rtu.go b/modbus/modbus-rtu.go index bed9bce..ce50fd8 100644 --- a/modbus/modbus-rtu.go +++ b/modbus/modbus-rtu.go @@ -15,11 +15,11 @@ type RTU struct { buf []byte } -func NewRTU(tunnel connect.Tunnel, opts types.Options) *RTU { +func NewRTU(conn connect.Conn, opts types.Options) *RTU { rtu := &RTU{ messenger: connect.Messenger{ Timeout: time.Millisecond * time.Duration(opts.Int64("timeout", 1000)), - Tunnel: tunnel, + Conn: conn, }, buf: make([]byte, opts.Int("buffer", 256)), } diff --git a/modbus/modbus-tcp.go b/modbus/modbus-tcp.go index 63cfa6a..d964f28 100644 --- a/modbus/modbus-tcp.go +++ b/modbus/modbus-tcp.go @@ -15,11 +15,11 @@ type TCP struct { buf []byte } -func NewTCP(tunnel connect.Tunnel, opts types.Options) *TCP { +func NewTCP(conn connect.Conn, opts types.Options) *TCP { tcp := &TCP{ messenger: connect.Messenger{ Timeout: time.Millisecond * time.Duration(opts.Int64("timeout", 1000)), - Tunnel: tunnel, + Conn: conn, }, buf: make([]byte, opts.Int("buffer", 256)), } diff --git a/modbus/modbus.go b/modbus/modbus.go index b139274..7b9e724 100644 --- a/modbus/modbus.go +++ b/modbus/modbus.go @@ -70,9 +70,8 @@ var stationForm = []types.SmartField{ var modbusRtu = &protocol.Protocol{ Name: "modbus-rtu", Label: "Modbus RTU", - Factory: func(conn connect.Tunnel, opts map[string]any) protocol.Adapter { + Factory: func(conn connect.Conn, opts map[string]any) protocol.Adapter { return &Adapter{ - tunnel: conn, modbus: NewRTU(conn, opts), devices: make(map[string]string), stations: make(map[string]types.Options), @@ -89,9 +88,8 @@ var modbusRtu = &protocol.Protocol{ var modbusTCP = &protocol.Protocol{ Name: "modbus-tcp", Label: "Modbus TCP", - Factory: func(conn connect.Tunnel, opts map[string]any) protocol.Adapter { + Factory: func(conn connect.Conn, opts map[string]any) protocol.Adapter { return &Adapter{ - tunnel: conn, modbus: NewTCP(conn, opts), devices: make(map[string]string), stations: make(map[string]types.Options), diff --git a/protocol/adapter.go b/protocol/adapter.go index 95a6fc2..dd05ae0 100644 --- a/protocol/adapter.go +++ b/protocol/adapter.go @@ -2,11 +2,10 @@ package protocol import ( "github.com/god-jason/bucket/types" - "github.com/zgwit/iot-gateway/connect" ) type Adapter interface { - Tunnel() connect.Tunnel + //Tunnel() connect.Tunnel //设备动态添加 Mount(device string, product string, station types.Options) error diff --git a/protocol/config.go b/protocol/config.go new file mode 100644 index 0000000..2d0eaff --- /dev/null +++ b/protocol/config.go @@ -0,0 +1 @@ +package protocol diff --git a/protocol/protocol.go b/protocol/protocol.go index f69e21a..b6707bd 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -5,7 +5,7 @@ import ( "github.com/zgwit/iot-gateway/connect" ) -type Factory func(conn connect.Tunnel, opts map[string]any) Adapter +type Factory func(conn connect.Conn, opts map[string]any) Adapter type Protocol struct { Name string `json:"name"` diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index a6d61cc..536bf38 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -65,10 +65,6 @@ func (l *Tunnel) ID() string { return l.Id } -func (l *Tunnel) Available() bool { - return l.Running -} - func (l *Tunnel) Keep(open func() error) { if l.keeping { return