修改modbus驱采仿真更新逻辑使用注册驱采处理任务方式实现

This commit is contained in:
walker 2023-12-18 17:59:56 +08:00
parent d527e528fd
commit ee4c04cf3d
1 changed files with 4 additions and 65 deletions

View File

@ -1,7 +1,6 @@
package cidcmodbus
import (
"context"
"fmt"
"log/slog"
"time"
@ -54,19 +53,6 @@ type cidcModbusService struct {
modbusConfig *config.CidcModbusConfig // modbus驱采配置
ecsUid string // 联锁集中站uid
qcms service.IotQcMappingService // modbus驱采映射服务
cancel context.CancelFunc
}
func (s *cidcModbusService) run(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
}
s.update()
time.Sleep(500 * time.Millisecond)
}
}
func (s *cidcModbusService) update() {
@ -79,17 +65,18 @@ func (s *cidcModbusService) update() {
if qcs == nil {
panic(fmt.Sprintf("联锁驱采Modbus服务查询状态失败,集中站联锁驱采状态不存在: %s", s.ecsUid))
}
// 写采集数据
err := s.qcms.WriteCjBytes(qcs.Cbs)
if err != nil {
slog.Error("联锁驱采Modbus服务写采集数据失败", err)
}
// 读驱动数据
qs := s.qcms.GetQdBytes()
qcs.Qbs = qs
}
// Stop implements CidcModbusService.
func (s *cidcModbusService) Stop() {
s.cancel()
s.qcms.Stop()
}
@ -106,10 +93,6 @@ func newCidcModbusService(vs *memory.VerifySimulation, modbusConfig *config.Cidc
return nil, fmt.Errorf("联锁驱采Modbus服务创建失败,仿真不存在集中站联锁: %s", ecsId)
}
qcs := component.CiQcStateType.Get(qce)
err := checkConfigMapping(qcs, modbusConfig)
if err != nil {
return nil, fmt.Errorf("联锁驱采Modbus服务创建失败,Modbus地址映射配置错误: %s", err)
}
qcms, err := service.NewModbusQcService(converToModbusDcConfig(modbusConfig), make([]byte, len(qcs.Qbs)), make([]byte, len(qcs.Cbs)))
if err != nil {
return nil, fmt.Errorf("联锁驱采Modbus服务创建失败: %s", err)
@ -120,55 +103,11 @@ func newCidcModbusService(vs *memory.VerifySimulation, modbusConfig *config.Cidc
ecsUid: ecsId,
qcms: qcms,
}
ctx, cancel := context.WithCancel(context.Background())
go cms.run(ctx)
cms.cancel = cancel
// 注册驱采数据仿真更新
qcms.RegisterQcDataHandleScheduleTask(cms.update, time.Millisecond*time.Duration(modbusConfig.Interval))
return cms, nil
}
func checkConfigMapping(qcs *component.CiQcState, modbusConfig *config.CidcModbusConfig) error {
for _, mdm := range modbusConfig.Mapping {
if mdm.Type == proto.DataType_CollectTable {
switch mdm.Function {
case proto.Modbus_ReadCoil, proto.Modbus_ReadDiscreteInput, proto.Modbus_WriteCoil, proto.Modbus_WriteCoils, proto.Modbus_RWCoils:
end := mdm.Start + mdm.Quantity
if end > uint32(len(qcs.Cbs)*8) {
return fmt.Errorf("Modbus地址映射配置错误,采集表地址超出范围: 起始位地址=%d,位数量=%d,实际位长度=%d", mdm.Start, mdm.Quantity, len(qcs.Cbs)*8)
}
case proto.Modbus_ReadInputRegister, proto.Modbus_ReadHoldingRegister, proto.Modbus_WriteRegister, proto.Modbus_WriteRegisters, proto.Modbus_RWRegisters:
end := mdm.Start + mdm.Quantity*2
if end > uint32(len(qcs.Cbs)) {
return fmt.Errorf("Modbus地址映射配置错误,采集表地址超出范围: 起始字节地址=%d,字数量=%d,实际位长度=%d", mdm.Start, mdm.Quantity, len(qcs.Cbs))
}
}
} else if mdm.Type == proto.DataType_DriveTable {
switch mdm.Function {
case proto.Modbus_ReadCoil, proto.Modbus_ReadDiscreteInput, proto.Modbus_WriteCoil, proto.Modbus_WriteCoils, proto.Modbus_RWCoils:
end := mdm.Start + mdm.Quantity
if end > uint32(len(qcs.Qbs)*8) {
return fmt.Errorf("Modbus地址映射配置错误,驱动表地址超出范围: 起始位地址=%d,位数量=%d,实际位长度=%d", mdm.Start, mdm.Quantity, len(qcs.Qbs)*8)
}
case proto.Modbus_ReadInputRegister, proto.Modbus_ReadHoldingRegister, proto.Modbus_WriteRegister, proto.Modbus_WriteRegisters, proto.Modbus_RWRegisters:
end := mdm.Start + mdm.Quantity*2
if end > uint32(len(qcs.Qbs)) {
return fmt.Errorf("Modbus地址映射配置错误,驱动表地址超出范围: 起始字节地址=%d,字数量=%d,实际位长度=%d", mdm.Start, mdm.Quantity, len(qcs.Qbs))
}
}
}
}
return nil
}
func getEcsIdByName(vs *memory.VerifySimulation, ecsName string) string {
ecsId := ""
for _, s := range vs.Repo.StationList() {
if ecsName == s.GetCode() {
ecsId = s.Id()
}
}
return ecsId
}
func converToModbusDcConfig(config *config.CidcModbusConfig) *proto.ModbusConfig {
return &proto.ModbusConfig{
Url: config.Url,