@@ -18,37 +18,40 @@ type Adapter struct {
18
18
pollers map [string ]* []* Poller
19
19
}
20
20
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
24
24
25
25
//加载映射表
26
- adapter .mappers [product_id ], err = product .LoadConfig [Mapper ](product_id , "mapper" )
26
+ adapter .mappers [productId ], err = product .LoadConfig [Mapper ](productId , "mapper" )
27
27
if err != nil {
28
28
return err
29
29
}
30
30
31
31
//加载轮询表
32
- adapter .pollers [product_id ], err = product .LoadConfig [[]* Poller ](product_id , "poller" )
32
+ adapter .pollers [productId ], err = product .LoadConfig [[]* Poller ](productId , "poller" )
33
33
if err != nil {
34
34
return err
35
35
}
36
36
37
37
return nil
38
38
}
39
39
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 )
43
43
return nil
44
44
}
45
45
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 ]
49
52
slave := station .Int ("slave" , 1 )
50
53
51
- mapper := adapter .mappers [product_id ]
54
+ mapper := adapter .mappers [productId ]
52
55
if mapper == nil {
53
56
return nil , errors .New ("没有地址映射" )
54
57
}
@@ -66,12 +69,15 @@ func (adapter *Adapter) Get(id, name string) (any, error) {
66
69
return point .Parse (address , data )
67
70
}
68
71
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 ]
72
78
slave := station .Int ("slave" , 1 )
73
79
74
- mapper := adapter .mappers [product_id ]
80
+ mapper := adapter .mappers [productId ]
75
81
if mapper == nil {
76
82
return errors .New ("没有地址映射" )
77
83
}
@@ -87,22 +93,25 @@ func (adapter *Adapter) Set(id, name string, value any) error {
87
93
return adapter .modbus .Write (uint8 (slave ), code , address , data )
88
94
}
89
95
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 ]
93
102
slave := station .Int ("slave" , 1 )
94
103
95
104
//没有地址表和轮询器,则跳过
96
105
//if d.pollers == nil || d.mappers == nil {
97
106
// return nil, nil
98
107
//}
99
- mapper := adapter .mappers [product_id ]
108
+ mapper := adapter .mappers [productId ]
100
109
if mapper == nil {
101
110
return nil , errors .New ("没有地址映射" )
102
111
}
103
112
104
113
values := make (map [string ]any )
105
- for _ , poller := range * adapter .pollers [product_id ] {
114
+ for _ , poller := range * adapter .pollers [productId ] {
106
115
if poller == nil {
107
116
continue
108
117
}
@@ -116,6 +125,5 @@ func (adapter *Adapter) Sync(id string) (map[string]any, error) {
116
125
}
117
126
}
118
127
119
-
120
128
return values , nil
121
129
}
0 commit comments