From 694ece63506be679252788f22795ad2587e2f083 Mon Sep 17 00:00:00 2001 From: walker Date: Wed, 13 Dec 2023 11:03:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=BD=E5=90=8D=E8=B0=83=E6=95=B4=EF=BC=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- ...bus_dc_service.go => modbus_dc_mapping.go} | 48 +++++++++--------- service/model/dc.go | 50 +++++++++---------- 3 files changed, 50 insertions(+), 50 deletions(-) rename service/{modbus_dc_service.go => modbus_dc_mapping.go} (87%) diff --git a/main.go b/main.go index c8a82c7..9816576 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ func main() { AddSource: false, }))) 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", UnitId: 2, Timeout: 500, diff --git a/service/modbus_dc_service.go b/service/modbus_dc_mapping.go similarity index 87% rename from service/modbus_dc_service.go rename to service/modbus_dc_mapping.go index fc17f92..d69f3e1 100644 --- a/service/modbus_dc_service.go +++ b/service/modbus_dc_mapping.go @@ -13,15 +13,15 @@ import ( sproto "joylink.club/iot/service/proto" ) -// Modbus驱动采集服务 -type modbusDcService struct { +// Modbus驱采服务 +type modbusQcService struct { config *sproto.ModbusConfig cli modbus.MasterClient - dc model.DC + qc model.QC 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 { return nil, err } @@ -39,10 +39,10 @@ func NewModbusDcService(config *sproto.ModbusConfig, dc model.DC) (IotService, e cli.SetUnitId(uint8(config.UnitId)) cli.SetEndianness(convertEndianness(config.Endianness)) cli.Start() - s := &modbusDcService{ + s := &modbusQcService{ config: config, cli: cli, - dc: dc, + qc: dc, } s.initOnUpdateTask() ctx, cancel := context.WithCancel(context.Background()) @@ -51,7 +51,7 @@ func NewModbusDcService(config *sproto.ModbusConfig, dc model.DC) (IotService, e return s, nil } -func (m *modbusDcService) initOnUpdateTask() { +func (m *modbusQcService) initOnUpdateTask() { mapping := m.config.Mapping for _, mdm := range mapping { if mdm.WriteStrategy == sproto.Modbus_OnUpdate && isWriteFunction(mdm.Function) { @@ -59,7 +59,7 @@ func (m *modbusDcService) initOnUpdateTask() { if mdm.Type == sproto.DataType_CollectTable { 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() { slog.Warn("Modbus驱动采集服务数据更新写入失败,modbus客户端未连接", "url", m.config.Url, "Function", mdm.Function) return @@ -94,7 +94,7 @@ func isWriteFunction(modbus_Function sproto.Modbus_Function) bool { modbus_Function == sproto.Modbus_RWRegisters } -func (m *modbusDcService) run(ctx context.Context) { +func (m *modbusQcService) run(ctx context.Context) { for { select { 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() { for _, mdm := range m.config.Mapping { 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 { case sproto.DataType_CollectTable: // 采集数据 - return m.dc.GetCollectBits(mdm.Start, mdm.Quantity) + return m.qc.GetCollectBits(mdm.Start, mdm.Quantity) case sproto.DataType_DriveTable: // 驱动数据 - return m.dc.GetDriveBits(mdm.Start, mdm.Quantity) + return m.qc.GetDriveBits(mdm.Start, mdm.Quantity) default: panic("未知数据类型") } } -func (m *modbusDcService) GetDcBytes(mdm *sproto.ModbusDcMapping) []byte { +func (m *modbusQcService) GetDcBytes(mdm *sproto.ModbusDcMapping) []byte { switch mdm.Type { 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: // 驱动数据 - return m.dc.GetDriveBytes(mdm.Start, mdm.Quantity*2) + return m.qc.GetDriveBytes(mdm.Start, mdm.Quantity*2) default: panic("未知数据类型") } } -func (m *modbusDcService) updateDcByBits(mdm *sproto.ModbusDcMapping, bits []bool) error { +func (m *modbusQcService) updateDcByBits(mdm *sproto.ModbusDcMapping, bits []bool) error { switch mdm.Type { case sproto.DataType_CollectTable: // 采集数据 - return m.dc.UpdateCollectByBits(mdm.Start, bits) + return m.qc.UpdateCollectByBits(mdm.Start, bits) case sproto.DataType_DriveTable: // 驱动数据 - return m.dc.UpdateDriveByBits(mdm.Start, bits) + return m.qc.UpdateDriveByBits(mdm.Start, bits) } 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 { case sproto.DataType_CollectTable: // 采集数据 - return m.dc.UpdateCollectByBytes(mdm.Start, bytes) + return m.qc.UpdateCollectByBytes(mdm.Start, bytes) case sproto.DataType_DriveTable: // 驱动数据 - return m.dc.UpdateDriveByBytes(mdm.Start, bytes) + return m.qc.UpdateDriveByBytes(mdm.Start, bytes) } return nil } @@ -259,12 +259,12 @@ func checkConfig(config *sproto.ModbusConfig) error { return nil } -func (m *modbusDcService) Start() error { +func (m *modbusQcService) Start() error { return nil } -func (m *modbusDcService) Stop() error { +func (m *modbusQcService) Stop() error { m.cancel() modbus.DeleteClient(m.config.Url) return nil diff --git a/service/model/dc.go b/service/model/dc.go index 783ab53..4e3579e 100644 --- a/service/model/dc.go +++ b/service/model/dc.go @@ -15,7 +15,7 @@ const ( ) // 驱动采集数据 -type DC interface { +type QC interface { // 更新驱动数据,位数组 UpdateDriveByBits(start uint32, bits []bool) error // 更新驱动数据,字节数组 @@ -47,12 +47,12 @@ type DC interface { // 发布事件 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 driveBits []bool @@ -60,36 +60,36 @@ type dc struct { collect []byte collectBits []bool // 事件 - subscribes map[DCEvent][]func(dc DC) + subscribes map[DCEvent][]func(dc QC) } // GetCollect implements DC. -func (d *dc) GetCollect() []byte { +func (d *qc) GetCollect() []byte { return d.collect } -func (d *dc) GetCollectBit(start uint32) bool { +func (d *qc) GetCollectBit(start uint32) bool { if start >= uint32(len(d.collectBits)) { panic(fmt.Errorf("GetCollectBit超出范围")) } 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)) { panic(fmt.Errorf("GetCollectBits超出范围")) } 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)) { panic(fmt.Errorf("GetCollectByte超出范围")) } 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)) { panic(fmt.Errorf("GetCollectBytes超出范围")) } @@ -97,32 +97,32 @@ func (d *dc) GetCollectBytes(start uint32, quantity uint32) []byte { } // GetDrive implements DC. -func (d *dc) GetDrive() []byte { +func (d *qc) GetDrive() []byte { return d.drive } -func (d *dc) GetDriveBit(start uint32) bool { +func (d *qc) GetDriveBit(start uint32) bool { if start >= uint32(len(d.driveBits)) { panic(fmt.Errorf("GetDriveBit超出范围")) } 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)) { panic(fmt.Errorf("GetDriveBits超出范围")) } 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)) { panic(fmt.Errorf("GetDriveByte超出范围")) } 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)) { panic(fmt.Errorf("GetDriveBytes超出范围")) } @@ -130,7 +130,7 @@ func (d *dc) GetDriveBytes(start uint32, quantity uint32) []byte { } // 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) if start >= uint32(total) { return fmt.Errorf("UpdateCollectByBits参数start超出范围") @@ -149,7 +149,7 @@ func (d *dc) UpdateCollectByBits(start uint32, bits []bool) error { } // 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) if start >= uint32(total) { return fmt.Errorf("UpdateCollectByBytes参数start超出范围") @@ -166,7 +166,7 @@ func (d *dc) UpdateCollectByBytes(start uint32, values []byte) error { } // 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) if start >= uint32(total) { return fmt.Errorf("UpdateDriveByBits参数start超出范围") @@ -185,7 +185,7 @@ func (d *dc) UpdateDriveByBits(start uint32, bits []bool) error { } // 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) if start >= uint32(total) { return fmt.Errorf("UpdateDriveByBytes参数start超出范围") @@ -202,7 +202,7 @@ func (d *dc) UpdateDriveByBytes(start uint32, values []byte) error { } // Emit implements DC. -func (d *dc) Emit(event DCEvent) { +func (d *qc) Emit(event DCEvent) { listeners := d.subscribes[event] for _, v := range listeners { v(d) @@ -210,22 +210,22 @@ func (d *dc) Emit(event DCEvent) { } // 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) } // 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") } -func NewDC(d []byte, c []byte) DC { - return &dc{ +func NewDC(d []byte, c []byte) QC { + return &qc{ drive: d, driveBits: decodeBools(d), collect: c, collectBits: decodeBools(c), - subscribes: make(map[DCEvent][]func(d DC)), + subscribes: make(map[DCEvent][]func(d QC)), } }