Skip to content

Commit

Permalink
改差不多了
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Jul 14, 2024
1 parent 44c7057 commit 99a6058
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 43 deletions.
12 changes: 1 addition & 11 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/god-jason/bucket/log"
"github.com/zgwit/iot-gateway/connect"
"github.com/zgwit/iot-gateway/db"
"github.com/zgwit/iot-gateway/protocol"
"github.com/zgwit/iot-gateway/tunnel"
"net"
)
Expand Down Expand Up @@ -48,14 +47,5 @@ func (c *Client) Open() error {

c.Conn = &connect.NetConn{Conn: conn}

//加载协议
c.Adapter, err = protocol.Create(c, c.ProtocolName, c.ProtocolOptions)
if err != nil {
return err
}

//启动轮询
go c.Poll()

return nil
return c.Start(c)
}
28 changes: 22 additions & 6 deletions modbus/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ func (adapter *Adapter) Get(id, name string) (any, error) {
station := adapter.stations[id]
slave := station.Int("slave", 1)

//todo error
mapper, code, address := adapter.mappers[product_id].Lookup(name)
mapper := adapter.mappers[product_id]
if mapper == nil {
return nil, errors.New("没有地址映射")
}
point, code, address := mapper.Lookup(name)
if point == nil {
return nil, errors.New("找不到数据点")
}

Expand All @@ -66,20 +69,24 @@ func (adapter *Adapter) Get(id, name string) (any, error) {
return nil, err
}

return mapper.Parse(address, data)
return point.Parse(address, data)
}

func (adapter *Adapter) Set(id, name string, value any) error {
product_id := adapter.devices[id]
station := adapter.stations[id]
slave := station.Int("slave", 1)

mapper, code, address := adapter.mappers[product_id].Lookup(name)
mapper := adapter.mappers[product_id]
if mapper == nil {
return errors.New("没有地址映射")
}
point, code, address := mapper.Lookup(name)
if point == nil {
return errors.New("地址找不到")
}

data, err := mapper.Encode(value)
data, err := point.Encode(value)
if err != nil {
return err
}
Expand All @@ -95,19 +102,28 @@ func (adapter *Adapter) Sync(id string) (map[string]any, error) {
//if d.pollers == nil || d.mappers == nil {
// return nil, nil
//}
mapper := adapter.mappers[product_id]
if mapper == nil {
return nil, errors.New("没有地址映射")
}

values := make(map[string]any)
for _, poller := range *adapter.pollers[product_id] {
if poller == nil {
continue
}
data, err := adapter.modbus.Read(uint8(slave), poller.Code, poller.Address, poller.Length)
if err != nil {
return nil, err
}
err = poller.Parse(adapter.mappers[product_id], data, values)
err = poller.Parse(mapper, data, values)
if err != nil {
return nil, err
}
}

//TODO 过滤器

//TODO 计算器

return values, nil
Expand Down
12 changes: 1 addition & 11 deletions serial/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"github.com/god-jason/bucket/log"
"github.com/zgwit/iot-gateway/db"
"github.com/zgwit/iot-gateway/protocol"
"github.com/zgwit/iot-gateway/tunnel"
"go.bug.st/serial"
"time"
Expand Down Expand Up @@ -58,14 +57,5 @@ func (s *Serial) Open() error {

s.Conn = port

//启动轮询
s.Adapter, err = protocol.Create(s, s.ProtocolName, s.ProtocolOptions)
if err != nil {
return err
}

//启动轮询
go s.Poll()

return nil
return s.Start(s)
}
17 changes: 3 additions & 14 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,13 @@ func (s *Server) handleSingle(c *net.TCPConn) (err error) {
}
l.Running = true
l.Status = "正常"
l.Conn = &connect.NetConn{c}
l.Conn = &connect.NetConn{Conn: c}

s.children[k] = l
//以ServerID保存
links.Store(s.Id, l)

//启动轮询
l.Adapter, err = protocol.Create(l, l.ProtocolName, l.ProtocolOptions)
if err != nil {
return err
}

//启动轮询
go l.Poll()

return nil
return l.Start(l)
}

func (s *Server) handleIncoming(c *net.TCPConn) error {
Expand Down Expand Up @@ -134,9 +125,7 @@ func (s *Server) handleRegister(c *net.TCPConn) error {
s.children[sn] = &l
links.Store(l.Id, &l)

//启动轮询
l.Adapter, err = protocol.Create(&l, l.ProtocolName, l.ProtocolOptions)
return err
return l.Start(&l)
}

// Open 打开
Expand Down
28 changes: 27 additions & 1 deletion tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Tunnel struct {
Adapter protocol.Adapter `json:"-" xorm:"-"`

//设备
devices []*device.Device
devices []*device.Device `json:"-" xorm:"-"`

//透传
pipe io.ReadWriteCloser
Expand Down Expand Up @@ -213,6 +213,32 @@ func (l *Tunnel) Pipe(pipe io.ReadWriteCloser) {
//go io.Copy(l.conn, pipe)
}

func (l *Tunnel) Start(conn connect.Tunnel) (err error) {
//加载协议
l.Adapter, err = protocol.Create(conn, l.ProtocolName, l.ProtocolOptions)
if err != nil {
return err
}

l.devices, err = device.LoadByTunnel(l.Id)
if err != nil {
return err
}

//加载设备
for _, d := range l.devices {
err = l.Adapter.Mount(d.Id, d.ProductId, d.Station)
if err != nil {
log.Error(err)
//return err
}
}

go l.Poll()

return nil
}

func (l *Tunnel) Poll() {

//设备上线
Expand Down

0 comments on commit 99a6058

Please sign in to comment.