Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
tunnel => conn
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Jul 14, 2024
1 parent 99a6058 commit e9d612a
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 37 deletions.
25 changes: 13 additions & 12 deletions connect/messager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

type Messenger struct {
Tunnel Tunnel
Conn

Timeout time.Duration

mu sync.Mutex
Expand All @@ -17,64 +18,64 @@ 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) {
m.mu.Lock()
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) {
m.mu.Lock()
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) {
m.mu.Lock()
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)
}
4 changes: 0 additions & 4 deletions connect/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@ type Tunnel interface {

Open() error

Close() error

Available() bool

//Online() bool
}
6 changes: 0 additions & 6 deletions modbus/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions modbus/modbus-rtu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down
4 changes: 2 additions & 2 deletions modbus/modbus-tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down
6 changes: 2 additions & 4 deletions modbus/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions protocol/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions protocol/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package protocol
2 changes: 1 addition & 1 deletion protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 0 additions & 4 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e9d612a

Please sign in to comment.