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

Commit 99a6058

Browse files
committed
改差不多了
1 parent 44c7057 commit 99a6058

File tree

5 files changed

+54
-43
lines changed

5 files changed

+54
-43
lines changed

client/client.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/god-jason/bucket/log"
77
"github.com/zgwit/iot-gateway/connect"
88
"github.com/zgwit/iot-gateway/db"
9-
"github.com/zgwit/iot-gateway/protocol"
109
"github.com/zgwit/iot-gateway/tunnel"
1110
"net"
1211
)
@@ -48,14 +47,5 @@ func (c *Client) Open() error {
4847

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

51-
//加载协议
52-
c.Adapter, err = protocol.Create(c, c.ProtocolName, c.ProtocolOptions)
53-
if err != nil {
54-
return err
55-
}
56-
57-
//启动轮询
58-
go c.Poll()
59-
60-
return nil
50+
return c.Start(c)
6151
}

modbus/adapter.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ func (adapter *Adapter) Get(id, name string) (any, error) {
5454
station := adapter.stations[id]
5555
slave := station.Int("slave", 1)
5656

57-
//todo error
58-
mapper, code, address := adapter.mappers[product_id].Lookup(name)
57+
mapper := adapter.mappers[product_id]
5958
if mapper == nil {
59+
return nil, errors.New("没有地址映射")
60+
}
61+
point, code, address := mapper.Lookup(name)
62+
if point == nil {
6063
return nil, errors.New("找不到数据点")
6164
}
6265

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

69-
return mapper.Parse(address, data)
72+
return point.Parse(address, data)
7073
}
7174

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

77-
mapper, code, address := adapter.mappers[product_id].Lookup(name)
80+
mapper := adapter.mappers[product_id]
7881
if mapper == nil {
82+
return errors.New("没有地址映射")
83+
}
84+
point, code, address := mapper.Lookup(name)
85+
if point == nil {
7986
return errors.New("地址找不到")
8087
}
8188

82-
data, err := mapper.Encode(value)
89+
data, err := point.Encode(value)
8390
if err != nil {
8491
return err
8592
}
@@ -95,19 +102,28 @@ func (adapter *Adapter) Sync(id string) (map[string]any, error) {
95102
//if d.pollers == nil || d.mappers == nil {
96103
// return nil, nil
97104
//}
105+
mapper := adapter.mappers[product_id]
106+
if mapper == nil {
107+
return nil, errors.New("没有地址映射")
108+
}
98109

99110
values := make(map[string]any)
100111
for _, poller := range *adapter.pollers[product_id] {
112+
if poller == nil {
113+
continue
114+
}
101115
data, err := adapter.modbus.Read(uint8(slave), poller.Code, poller.Address, poller.Length)
102116
if err != nil {
103117
return nil, err
104118
}
105-
err = poller.Parse(adapter.mappers[product_id], data, values)
119+
err = poller.Parse(mapper, data, values)
106120
if err != nil {
107121
return nil, err
108122
}
109123
}
110124

125+
//TODO 过滤器
126+
111127
//TODO 计算器
112128

113129
return values, nil

serial/serial.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"github.com/god-jason/bucket/log"
66
"github.com/zgwit/iot-gateway/db"
7-
"github.com/zgwit/iot-gateway/protocol"
87
"github.com/zgwit/iot-gateway/tunnel"
98
"go.bug.st/serial"
109
"time"
@@ -58,14 +57,5 @@ func (s *Serial) Open() error {
5857

5958
s.Conn = port
6059

61-
//启动轮询
62-
s.Adapter, err = protocol.Create(s, s.ProtocolName, s.ProtocolOptions)
63-
if err != nil {
64-
return err
65-
}
66-
67-
//启动轮询
68-
go s.Poll()
69-
70-
return nil
60+
return s.Start(s)
7161
}

server/server.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,13 @@ func (s *Server) handleSingle(c *net.TCPConn) (err error) {
4848
}
4949
l.Running = true
5050
l.Status = "正常"
51-
l.Conn = &connect.NetConn{c}
51+
l.Conn = &connect.NetConn{Conn: c}
5252

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

57-
//启动轮询
58-
l.Adapter, err = protocol.Create(l, l.ProtocolName, l.ProtocolOptions)
59-
if err != nil {
60-
return err
61-
}
62-
63-
//启动轮询
64-
go l.Poll()
65-
66-
return nil
57+
return l.Start(l)
6758
}
6859

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

137-
//启动轮询
138-
l.Adapter, err = protocol.Create(&l, l.ProtocolName, l.ProtocolOptions)
139-
return err
128+
return l.Start(&l)
140129
}
141130

142131
// Open 打开

tunnel/tunnel.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Tunnel struct {
5252
Adapter protocol.Adapter `json:"-" xorm:"-"`
5353

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

5757
//透传
5858
pipe io.ReadWriteCloser
@@ -213,6 +213,32 @@ func (l *Tunnel) Pipe(pipe io.ReadWriteCloser) {
213213
//go io.Copy(l.conn, pipe)
214214
}
215215

216+
func (l *Tunnel) Start(conn connect.Tunnel) (err error) {
217+
//加载协议
218+
l.Adapter, err = protocol.Create(conn, l.ProtocolName, l.ProtocolOptions)
219+
if err != nil {
220+
return err
221+
}
222+
223+
l.devices, err = device.LoadByTunnel(l.Id)
224+
if err != nil {
225+
return err
226+
}
227+
228+
//加载设备
229+
for _, d := range l.devices {
230+
err = l.Adapter.Mount(d.Id, d.ProductId, d.Station)
231+
if err != nil {
232+
log.Error(err)
233+
//return err
234+
}
235+
}
236+
237+
go l.Poll()
238+
239+
return nil
240+
}
241+
216242
func (l *Tunnel) Poll() {
217243

218244
//设备上线

0 commit comments

Comments
 (0)