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

Commit cbc7d61

Browse files
committed
优化
1 parent b2671e7 commit cbc7d61

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

modbus/adapter.go

+30-22
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,40 @@ type Adapter struct {
1818
pollers map[string]*[]*Poller
1919
}
2020

21-
func (adapter *Adapter) Mount(id string, product_id string, station types.Options) (err error) {
22-
adapter.devices[id] = product_id
23-
adapter.stations[id] = station
21+
func (adapter *Adapter) Mount(deviceId string, productId string, station types.Options) (err error) {
22+
adapter.devices[deviceId] = productId
23+
adapter.stations[deviceId] = station
2424

2525
//加载映射表
26-
adapter.mappers[product_id], err = product.LoadConfig[Mapper](product_id, "mapper")
26+
adapter.mappers[productId], err = product.LoadConfig[Mapper](productId, "mapper")
2727
if err != nil {
2828
return err
2929
}
3030

3131
//加载轮询表
32-
adapter.pollers[product_id], err = product.LoadConfig[[]*Poller](product_id, "poller")
32+
adapter.pollers[productId], err = product.LoadConfig[[]*Poller](productId, "poller")
3333
if err != nil {
3434
return err
3535
}
3636

3737
return nil
3838
}
3939

40-
func (adapter *Adapter) Unmount(id string) error {
41-
delete(adapter.devices, id)
42-
delete(adapter.stations, id)
40+
func (adapter *Adapter) Unmount(deviceId string) error {
41+
delete(adapter.devices, deviceId)
42+
delete(adapter.stations, deviceId)
4343
return nil
4444
}
4545

46-
func (adapter *Adapter) Get(id, name string) (any, error) {
47-
product_id := adapter.devices[id]
48-
station := adapter.stations[id]
46+
func (adapter *Adapter) Get(deviceId, name string) (any, error) {
47+
productId, has := adapter.devices[deviceId]
48+
if !has {
49+
return nil, errors.New("设备未注册")
50+
}
51+
station := adapter.stations[deviceId]
4952
slave := station.Int("slave", 1)
5053

51-
mapper := adapter.mappers[product_id]
54+
mapper := adapter.mappers[productId]
5255
if mapper == nil {
5356
return nil, errors.New("没有地址映射")
5457
}
@@ -66,12 +69,15 @@ func (adapter *Adapter) Get(id, name string) (any, error) {
6669
return point.Parse(address, data)
6770
}
6871

69-
func (adapter *Adapter) Set(id, name string, value any) error {
70-
product_id := adapter.devices[id]
71-
station := adapter.stations[id]
72+
func (adapter *Adapter) Set(deviceId, name string, value any) error {
73+
productId, has := adapter.devices[deviceId]
74+
if !has {
75+
return errors.New("设备未注册")
76+
}
77+
station := adapter.stations[deviceId]
7278
slave := station.Int("slave", 1)
7379

74-
mapper := adapter.mappers[product_id]
80+
mapper := adapter.mappers[productId]
7581
if mapper == nil {
7682
return errors.New("没有地址映射")
7783
}
@@ -87,22 +93,25 @@ func (adapter *Adapter) Set(id, name string, value any) error {
8793
return adapter.modbus.Write(uint8(slave), code, address, data)
8894
}
8995

90-
func (adapter *Adapter) Sync(id string) (map[string]any, error) {
91-
product_id := adapter.devices[id]
92-
station := adapter.stations[id]
96+
func (adapter *Adapter) Poll(deviceId string) (map[string]any, error) {
97+
productId, has := adapter.devices[deviceId]
98+
if !has {
99+
return nil, errors.New("设备未注册")
100+
}
101+
station := adapter.stations[deviceId]
93102
slave := station.Int("slave", 1)
94103

95104
//没有地址表和轮询器,则跳过
96105
//if d.pollers == nil || d.mappers == nil {
97106
// return nil, nil
98107
//}
99-
mapper := adapter.mappers[product_id]
108+
mapper := adapter.mappers[productId]
100109
if mapper == nil {
101110
return nil, errors.New("没有地址映射")
102111
}
103112

104113
values := make(map[string]any)
105-
for _, poller := range *adapter.pollers[product_id] {
114+
for _, poller := range *adapter.pollers[productId] {
106115
if poller == nil {
107116
continue
108117
}
@@ -116,6 +125,5 @@ func (adapter *Adapter) Sync(id string) (map[string]any, error) {
116125
}
117126
}
118127

119-
120128
return values, nil
121129
}

protocol/adapter.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ import (
44
"github.com/god-jason/bucket/types"
55
)
66

7+
// Adapter 设备驱动
78
type Adapter interface {
8-
//Tunnel() connect.Tunnel
99

10-
//设备动态添加
11-
Mount(device string, product string, station types.Options) error
12-
Unmount(device string) error
10+
//Mount 挂载设备
11+
Mount(deviceId string, productId string, station types.Options) error
1312

14-
//设备数据操作
15-
Get(device, point string) (any, error)
16-
Set(device, point string, value any) error
17-
Sync(device string) (map[string]any, error)
13+
//Unmount 卸载设备
14+
Unmount(deviceId string) error
15+
16+
//Get 读数据
17+
Get(deviceId, point string) (any, error)
18+
19+
//Set 写数据
20+
Set(deviceId, point string, value any) error
21+
22+
//Poll 读取所有数据
23+
Poll(deviceId string) (map[string]any, error)
1824
}

tunnel/tunnel.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (l *Tunnel) Poll() {
256256
for {
257257
start := time.Now().UnixMilli()
258258
for _, dev := range l.devices {
259-
values, err := l.Adapter.Sync(dev.Id)
259+
values, err := l.Adapter.Poll(dev.Id)
260260
if err != nil {
261261
log.Error(err)
262262
continue

0 commit comments

Comments
 (0)