命名调整,文件名调整
This commit is contained in:
parent
374dc77a0e
commit
694ece6350
2
main.go
2
main.go
@ -17,7 +17,7 @@ func main() {
|
|||||||
AddSource: false,
|
AddSource: false,
|
||||||
})))
|
})))
|
||||||
dc := model.NewDC(make([]byte, 2), make([]byte, 2))
|
dc := model.NewDC(make([]byte, 2), make([]byte, 2))
|
||||||
mds, err := service.NewModbusDcService(&proto.ModbusConfig{
|
mds, err := service.NewModbusQcService(&proto.ModbusConfig{
|
||||||
Url: "tcp://127.0.0.1:502",
|
Url: "tcp://127.0.0.1:502",
|
||||||
UnitId: 2,
|
UnitId: 2,
|
||||||
Timeout: 500,
|
Timeout: 500,
|
||||||
|
@ -13,15 +13,15 @@ import (
|
|||||||
sproto "joylink.club/iot/service/proto"
|
sproto "joylink.club/iot/service/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Modbus驱动采集服务
|
// Modbus驱采服务
|
||||||
type modbusDcService struct {
|
type modbusQcService struct {
|
||||||
config *sproto.ModbusConfig
|
config *sproto.ModbusConfig
|
||||||
cli modbus.MasterClient
|
cli modbus.MasterClient
|
||||||
dc model.DC
|
qc model.QC
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewModbusDcService(config *sproto.ModbusConfig, dc model.DC) (IotService, error) {
|
func NewModbusQcService(config *sproto.ModbusConfig, dc model.QC) (IotService, error) {
|
||||||
if err := checkConfig(config); err != nil {
|
if err := checkConfig(config); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -39,10 +39,10 @@ func NewModbusDcService(config *sproto.ModbusConfig, dc model.DC) (IotService, e
|
|||||||
cli.SetUnitId(uint8(config.UnitId))
|
cli.SetUnitId(uint8(config.UnitId))
|
||||||
cli.SetEndianness(convertEndianness(config.Endianness))
|
cli.SetEndianness(convertEndianness(config.Endianness))
|
||||||
cli.Start()
|
cli.Start()
|
||||||
s := &modbusDcService{
|
s := &modbusQcService{
|
||||||
config: config,
|
config: config,
|
||||||
cli: cli,
|
cli: cli,
|
||||||
dc: dc,
|
qc: dc,
|
||||||
}
|
}
|
||||||
s.initOnUpdateTask()
|
s.initOnUpdateTask()
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
@ -51,7 +51,7 @@ func NewModbusDcService(config *sproto.ModbusConfig, dc model.DC) (IotService, e
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) initOnUpdateTask() {
|
func (m *modbusQcService) initOnUpdateTask() {
|
||||||
mapping := m.config.Mapping
|
mapping := m.config.Mapping
|
||||||
for _, mdm := range mapping {
|
for _, mdm := range mapping {
|
||||||
if mdm.WriteStrategy == sproto.Modbus_OnUpdate && isWriteFunction(mdm.Function) {
|
if mdm.WriteStrategy == sproto.Modbus_OnUpdate && isWriteFunction(mdm.Function) {
|
||||||
@ -59,7 +59,7 @@ func (m *modbusDcService) initOnUpdateTask() {
|
|||||||
if mdm.Type == sproto.DataType_CollectTable {
|
if mdm.Type == sproto.DataType_CollectTable {
|
||||||
et = model.DCE_Collect_Update
|
et = model.DCE_Collect_Update
|
||||||
}
|
}
|
||||||
m.dc.On(et, func(d model.DC) {
|
m.qc.On(et, func(d model.QC) {
|
||||||
if !m.cli.IsConnected() {
|
if !m.cli.IsConnected() {
|
||||||
slog.Warn("Modbus驱动采集服务数据更新写入失败,modbus客户端未连接", "url", m.config.Url, "Function", mdm.Function)
|
slog.Warn("Modbus驱动采集服务数据更新写入失败,modbus客户端未连接", "url", m.config.Url, "Function", mdm.Function)
|
||||||
return
|
return
|
||||||
@ -94,7 +94,7 @@ func isWriteFunction(modbus_Function sproto.Modbus_Function) bool {
|
|||||||
modbus_Function == sproto.Modbus_RWRegisters
|
modbus_Function == sproto.Modbus_RWRegisters
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) run(ctx context.Context) {
|
func (m *modbusQcService) run(ctx context.Context) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@ -107,7 +107,7 @@ func (m *modbusDcService) run(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) mappingTaskExecute() {
|
func (m *modbusQcService) mappingTaskExecute() {
|
||||||
if m.cli.IsConnected() {
|
if m.cli.IsConnected() {
|
||||||
for _, mdm := range m.config.Mapping {
|
for _, mdm := range m.config.Mapping {
|
||||||
switch mdm.Function {
|
switch mdm.Function {
|
||||||
@ -198,44 +198,44 @@ func (m *modbusDcService) mappingTaskExecute() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) GetDcBits(mdm *sproto.ModbusDcMapping) []bool {
|
func (m *modbusQcService) GetDcBits(mdm *sproto.ModbusDcMapping) []bool {
|
||||||
switch mdm.Type {
|
switch mdm.Type {
|
||||||
case sproto.DataType_CollectTable: // 采集数据
|
case sproto.DataType_CollectTable: // 采集数据
|
||||||
return m.dc.GetCollectBits(mdm.Start, mdm.Quantity)
|
return m.qc.GetCollectBits(mdm.Start, mdm.Quantity)
|
||||||
case sproto.DataType_DriveTable: // 驱动数据
|
case sproto.DataType_DriveTable: // 驱动数据
|
||||||
return m.dc.GetDriveBits(mdm.Start, mdm.Quantity)
|
return m.qc.GetDriveBits(mdm.Start, mdm.Quantity)
|
||||||
default:
|
default:
|
||||||
panic("未知数据类型")
|
panic("未知数据类型")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) GetDcBytes(mdm *sproto.ModbusDcMapping) []byte {
|
func (m *modbusQcService) GetDcBytes(mdm *sproto.ModbusDcMapping) []byte {
|
||||||
switch mdm.Type {
|
switch mdm.Type {
|
||||||
case sproto.DataType_CollectTable: // 采集数据
|
case sproto.DataType_CollectTable: // 采集数据
|
||||||
return m.dc.GetCollectBytes(mdm.Start, mdm.Quantity*2)
|
return m.qc.GetCollectBytes(mdm.Start, mdm.Quantity*2)
|
||||||
case sproto.DataType_DriveTable: // 驱动数据
|
case sproto.DataType_DriveTable: // 驱动数据
|
||||||
return m.dc.GetDriveBytes(mdm.Start, mdm.Quantity*2)
|
return m.qc.GetDriveBytes(mdm.Start, mdm.Quantity*2)
|
||||||
default:
|
default:
|
||||||
panic("未知数据类型")
|
panic("未知数据类型")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) updateDcByBits(mdm *sproto.ModbusDcMapping, bits []bool) error {
|
func (m *modbusQcService) updateDcByBits(mdm *sproto.ModbusDcMapping, bits []bool) error {
|
||||||
switch mdm.Type {
|
switch mdm.Type {
|
||||||
case sproto.DataType_CollectTable: // 采集数据
|
case sproto.DataType_CollectTable: // 采集数据
|
||||||
return m.dc.UpdateCollectByBits(mdm.Start, bits)
|
return m.qc.UpdateCollectByBits(mdm.Start, bits)
|
||||||
case sproto.DataType_DriveTable: // 驱动数据
|
case sproto.DataType_DriveTable: // 驱动数据
|
||||||
return m.dc.UpdateDriveByBits(mdm.Start, bits)
|
return m.qc.UpdateDriveByBits(mdm.Start, bits)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) updateDcByBytes(mdm *sproto.ModbusDcMapping, bytes []byte) error {
|
func (m *modbusQcService) updateDcByBytes(mdm *sproto.ModbusDcMapping, bytes []byte) error {
|
||||||
switch mdm.Type {
|
switch mdm.Type {
|
||||||
case sproto.DataType_CollectTable: // 采集数据
|
case sproto.DataType_CollectTable: // 采集数据
|
||||||
return m.dc.UpdateCollectByBytes(mdm.Start, bytes)
|
return m.qc.UpdateCollectByBytes(mdm.Start, bytes)
|
||||||
case sproto.DataType_DriveTable: // 驱动数据
|
case sproto.DataType_DriveTable: // 驱动数据
|
||||||
return m.dc.UpdateDriveByBytes(mdm.Start, bytes)
|
return m.qc.UpdateDriveByBytes(mdm.Start, bytes)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -259,12 +259,12 @@ func checkConfig(config *sproto.ModbusConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) Start() error {
|
func (m *modbusQcService) Start() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *modbusDcService) Stop() error {
|
func (m *modbusQcService) Stop() error {
|
||||||
m.cancel()
|
m.cancel()
|
||||||
modbus.DeleteClient(m.config.Url)
|
modbus.DeleteClient(m.config.Url)
|
||||||
return nil
|
return nil
|
@ -15,7 +15,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// 驱动采集数据
|
// 驱动采集数据
|
||||||
type DC interface {
|
type QC interface {
|
||||||
// 更新驱动数据,位数组
|
// 更新驱动数据,位数组
|
||||||
UpdateDriveByBits(start uint32, bits []bool) error
|
UpdateDriveByBits(start uint32, bits []bool) error
|
||||||
// 更新驱动数据,字节数组
|
// 更新驱动数据,字节数组
|
||||||
@ -47,12 +47,12 @@ type DC interface {
|
|||||||
// 发布事件
|
// 发布事件
|
||||||
Emit(event DCEvent)
|
Emit(event DCEvent)
|
||||||
// 订阅事件
|
// 订阅事件
|
||||||
On(event DCEvent, callback func(dc DC))
|
On(event DCEvent, callback func(dc QC))
|
||||||
// 取消订阅
|
// 取消订阅
|
||||||
Off(event DCEvent, callback func(dc DC))
|
Off(event DCEvent, callback func(dc QC))
|
||||||
}
|
}
|
||||||
|
|
||||||
type dc struct {
|
type qc struct {
|
||||||
// 驱动数据
|
// 驱动数据
|
||||||
drive []byte
|
drive []byte
|
||||||
driveBits []bool
|
driveBits []bool
|
||||||
@ -60,36 +60,36 @@ type dc struct {
|
|||||||
collect []byte
|
collect []byte
|
||||||
collectBits []bool
|
collectBits []bool
|
||||||
// 事件
|
// 事件
|
||||||
subscribes map[DCEvent][]func(dc DC)
|
subscribes map[DCEvent][]func(dc QC)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCollect implements DC.
|
// GetCollect implements DC.
|
||||||
func (d *dc) GetCollect() []byte {
|
func (d *qc) GetCollect() []byte {
|
||||||
return d.collect
|
return d.collect
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dc) GetCollectBit(start uint32) bool {
|
func (d *qc) GetCollectBit(start uint32) bool {
|
||||||
if start >= uint32(len(d.collectBits)) {
|
if start >= uint32(len(d.collectBits)) {
|
||||||
panic(fmt.Errorf("GetCollectBit超出范围"))
|
panic(fmt.Errorf("GetCollectBit超出范围"))
|
||||||
}
|
}
|
||||||
return d.collectBits[start]
|
return d.collectBits[start]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dc) GetCollectBits(start uint32, quantity uint32) []bool {
|
func (d *qc) GetCollectBits(start uint32, quantity uint32) []bool {
|
||||||
if start+quantity > uint32(len(d.collectBits)) {
|
if start+quantity > uint32(len(d.collectBits)) {
|
||||||
panic(fmt.Errorf("GetCollectBits超出范围"))
|
panic(fmt.Errorf("GetCollectBits超出范围"))
|
||||||
}
|
}
|
||||||
return d.collectBits[start : start+quantity]
|
return d.collectBits[start : start+quantity]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dc) GetCollectByte(start uint32) byte {
|
func (d *qc) GetCollectByte(start uint32) byte {
|
||||||
if start >= uint32(len(d.collect)) {
|
if start >= uint32(len(d.collect)) {
|
||||||
panic(fmt.Errorf("GetCollectByte超出范围"))
|
panic(fmt.Errorf("GetCollectByte超出范围"))
|
||||||
}
|
}
|
||||||
return d.collect[start]
|
return d.collect[start]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dc) GetCollectBytes(start uint32, quantity uint32) []byte {
|
func (d *qc) GetCollectBytes(start uint32, quantity uint32) []byte {
|
||||||
if start+quantity > uint32(len(d.collect)) {
|
if start+quantity > uint32(len(d.collect)) {
|
||||||
panic(fmt.Errorf("GetCollectBytes超出范围"))
|
panic(fmt.Errorf("GetCollectBytes超出范围"))
|
||||||
}
|
}
|
||||||
@ -97,32 +97,32 @@ func (d *dc) GetCollectBytes(start uint32, quantity uint32) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetDrive implements DC.
|
// GetDrive implements DC.
|
||||||
func (d *dc) GetDrive() []byte {
|
func (d *qc) GetDrive() []byte {
|
||||||
return d.drive
|
return d.drive
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dc) GetDriveBit(start uint32) bool {
|
func (d *qc) GetDriveBit(start uint32) bool {
|
||||||
if start >= uint32(len(d.driveBits)) {
|
if start >= uint32(len(d.driveBits)) {
|
||||||
panic(fmt.Errorf("GetDriveBit超出范围"))
|
panic(fmt.Errorf("GetDriveBit超出范围"))
|
||||||
}
|
}
|
||||||
return d.driveBits[start]
|
return d.driveBits[start]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dc) GetDriveBits(start uint32, quantity uint32) []bool {
|
func (d *qc) GetDriveBits(start uint32, quantity uint32) []bool {
|
||||||
if start+quantity > uint32(len(d.driveBits)) {
|
if start+quantity > uint32(len(d.driveBits)) {
|
||||||
panic(fmt.Errorf("GetDriveBits超出范围"))
|
panic(fmt.Errorf("GetDriveBits超出范围"))
|
||||||
}
|
}
|
||||||
return d.driveBits[start : start+quantity]
|
return d.driveBits[start : start+quantity]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dc) GetDriveByte(start uint32) byte {
|
func (d *qc) GetDriveByte(start uint32) byte {
|
||||||
if start >= uint32(len(d.drive)) {
|
if start >= uint32(len(d.drive)) {
|
||||||
panic(fmt.Errorf("GetDriveByte超出范围"))
|
panic(fmt.Errorf("GetDriveByte超出范围"))
|
||||||
}
|
}
|
||||||
return d.drive[start]
|
return d.drive[start]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dc) GetDriveBytes(start uint32, quantity uint32) []byte {
|
func (d *qc) GetDriveBytes(start uint32, quantity uint32) []byte {
|
||||||
if start+quantity > uint32(len(d.drive)) {
|
if start+quantity > uint32(len(d.drive)) {
|
||||||
panic(fmt.Errorf("GetDriveBytes超出范围"))
|
panic(fmt.Errorf("GetDriveBytes超出范围"))
|
||||||
}
|
}
|
||||||
@ -130,7 +130,7 @@ func (d *dc) GetDriveBytes(start uint32, quantity uint32) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateCollectByBits implements DC.
|
// UpdateCollectByBits implements DC.
|
||||||
func (d *dc) UpdateCollectByBits(start uint32, bits []bool) error {
|
func (d *qc) UpdateCollectByBits(start uint32, bits []bool) error {
|
||||||
total := len(d.collectBits)
|
total := len(d.collectBits)
|
||||||
if start >= uint32(total) {
|
if start >= uint32(total) {
|
||||||
return fmt.Errorf("UpdateCollectByBits参数start超出范围")
|
return fmt.Errorf("UpdateCollectByBits参数start超出范围")
|
||||||
@ -149,7 +149,7 @@ func (d *dc) UpdateCollectByBits(start uint32, bits []bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateCollectByBytes implements DC.
|
// UpdateCollectByBytes implements DC.
|
||||||
func (d *dc) UpdateCollectByBytes(start uint32, values []byte) error {
|
func (d *qc) UpdateCollectByBytes(start uint32, values []byte) error {
|
||||||
total := len(d.collect)
|
total := len(d.collect)
|
||||||
if start >= uint32(total) {
|
if start >= uint32(total) {
|
||||||
return fmt.Errorf("UpdateCollectByBytes参数start超出范围")
|
return fmt.Errorf("UpdateCollectByBytes参数start超出范围")
|
||||||
@ -166,7 +166,7 @@ func (d *dc) UpdateCollectByBytes(start uint32, values []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDriveByBits implements DC.
|
// UpdateDriveByBits implements DC.
|
||||||
func (d *dc) UpdateDriveByBits(start uint32, bits []bool) error {
|
func (d *qc) UpdateDriveByBits(start uint32, bits []bool) error {
|
||||||
total := len(d.driveBits)
|
total := len(d.driveBits)
|
||||||
if start >= uint32(total) {
|
if start >= uint32(total) {
|
||||||
return fmt.Errorf("UpdateDriveByBits参数start超出范围")
|
return fmt.Errorf("UpdateDriveByBits参数start超出范围")
|
||||||
@ -185,7 +185,7 @@ func (d *dc) UpdateDriveByBits(start uint32, bits []bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDriveByBytes implements DC.
|
// UpdateDriveByBytes implements DC.
|
||||||
func (d *dc) UpdateDriveByBytes(start uint32, values []byte) error {
|
func (d *qc) UpdateDriveByBytes(start uint32, values []byte) error {
|
||||||
total := len(d.drive)
|
total := len(d.drive)
|
||||||
if start >= uint32(total) {
|
if start >= uint32(total) {
|
||||||
return fmt.Errorf("UpdateDriveByBytes参数start超出范围")
|
return fmt.Errorf("UpdateDriveByBytes参数start超出范围")
|
||||||
@ -202,7 +202,7 @@ func (d *dc) UpdateDriveByBytes(start uint32, values []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Emit implements DC.
|
// Emit implements DC.
|
||||||
func (d *dc) Emit(event DCEvent) {
|
func (d *qc) Emit(event DCEvent) {
|
||||||
listeners := d.subscribes[event]
|
listeners := d.subscribes[event]
|
||||||
for _, v := range listeners {
|
for _, v := range listeners {
|
||||||
v(d)
|
v(d)
|
||||||
@ -210,22 +210,22 @@ func (d *dc) Emit(event DCEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// On implements DC.
|
// On implements DC.
|
||||||
func (d *dc) On(event DCEvent, callback func(d DC)) {
|
func (d *qc) On(event DCEvent, callback func(d QC)) {
|
||||||
d.subscribes[event] = append(d.subscribes[event], callback)
|
d.subscribes[event] = append(d.subscribes[event], callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Off implements DC.
|
// Off implements DC.
|
||||||
func (d *dc) Off(event DCEvent, callback func(d DC)) {
|
func (d *qc) Off(event DCEvent, callback func(d QC)) {
|
||||||
panic("unimplemented")
|
panic("unimplemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDC(d []byte, c []byte) DC {
|
func NewDC(d []byte, c []byte) QC {
|
||||||
return &dc{
|
return &qc{
|
||||||
drive: d,
|
drive: d,
|
||||||
driveBits: decodeBools(d),
|
driveBits: decodeBools(d),
|
||||||
collect: c,
|
collect: c,
|
||||||
collectBits: decodeBools(c),
|
collectBits: decodeBools(c),
|
||||||
subscribes: make(map[DCEvent][]func(d DC)),
|
subscribes: make(map[DCEvent][]func(d QC)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user