jl-iot/protocol/modbus/manage.go
walker 0b20dcdf7a modbus驱采映射服务功能调整重构
1,添加驱动采集获取和写入接口
2,创建服务接口调整接收驱采字节数组
mqtt客户端功能代码调整,未完
2023-12-18 15:34:10 +08:00

54 lines
1.0 KiB
Go

package modbus
import "sync"
// modbus客户端管理
type modbusManager struct {
lock sync.Mutex
clientMap map[string]MasterClient
}
var manager = modbusManager{
clientMap: make(map[string]MasterClient),
}
// 获取modbus客户端
func GetClient(url string) (MasterClient, bool) {
manager.lock.Lock()
defer manager.lock.Unlock()
client, ok := manager.clientMap[url]
return client, ok
}
// 新建客户端
func NewClient(conf *ClientConfig) (MasterClient, error) {
manager.lock.Lock()
defer manager.lock.Unlock()
client, err := newClient(conf)
if err != nil {
return nil, err
}
manager.clientMap[conf.Url] = client
return client, err
}
// 获取或新建客户端
func GetOrInitClient(conf *ClientConfig) (MasterClient, error) {
client, ok := GetClient(conf.Url)
if ok {
return client, nil
}
return NewClient(conf)
}
// 删除客户端
func DeleteClient(url string) {
manager.lock.Lock()
defer manager.lock.Unlock()
c := manager.clientMap[url]
if c != nil {
c.Close()
delete(manager.clientMap, url)
}
}