197 lines
6.1 KiB
Go
197 lines
6.1 KiB
Go
package cidcmodbus
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
|
"joylink.club/iot/service"
|
|
"joylink.club/iot/service/proto"
|
|
"joylink.club/rtsssimulation/component"
|
|
"joylink.club/rtsssimulation/entity"
|
|
)
|
|
|
|
// 联锁驱采Modbus服务
|
|
type CidcModbusService interface {
|
|
Stop()
|
|
}
|
|
|
|
var serviceManage = &cidcModbusServiceManage{}
|
|
|
|
type cidcModbusServiceManage struct {
|
|
services []*cidcModbusService
|
|
}
|
|
|
|
func (m *cidcModbusServiceManage) Stop() {
|
|
for _, s := range m.services {
|
|
s.Stop()
|
|
}
|
|
m.services = nil
|
|
}
|
|
|
|
func Stop() {
|
|
serviceManage.Stop()
|
|
}
|
|
|
|
func Start(vs *memory.VerifySimulation) error {
|
|
for _, cmc := range vs.GetCidcModbusConfig() {
|
|
if cmc.Open {
|
|
cms, err := newCidcModbusService(vs, &cmc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
serviceManage.services = append(serviceManage.services, cms)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type cidcModbusService struct {
|
|
vs *memory.VerifySimulation // 仿真对象
|
|
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() {
|
|
wd := entity.GetWorldData(s.vs.World)
|
|
qce := wd.FindQcEntityByEcsId(s.ecsUid)
|
|
if qce == nil {
|
|
panic(fmt.Sprintf("联锁驱采Modbus服务查询状态失败,仿真不存在集中站联锁: %s", s.ecsUid))
|
|
}
|
|
qcs := component.CiQcStateType.Get(qce)
|
|
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()
|
|
}
|
|
|
|
func newCidcModbusService(vs *memory.VerifySimulation, modbusConfig *config.CidcModbusConfig) (*cidcModbusService, error) {
|
|
station := vs.Repo.FindStationByStationName(modbusConfig.Ecs)
|
|
if station == nil {
|
|
return nil, fmt.Errorf("联锁驱采Modbus服务创建失败,未找到联锁集中站: %s", modbusConfig.Ecs)
|
|
}
|
|
ecsId := station.Id()
|
|
// 获取
|
|
wd := entity.GetWorldData(vs.World)
|
|
qce := wd.FindQcEntityByEcsId(ecsId)
|
|
if qce == nil {
|
|
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)
|
|
}
|
|
cms := &cidcModbusService{
|
|
vs: vs,
|
|
modbusConfig: modbusConfig,
|
|
ecsUid: ecsId,
|
|
qcms: qcms,
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
go cms.run(ctx)
|
|
cms.cancel = cancel
|
|
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,
|
|
UnitId: config.UnitId,
|
|
Endianness: config.Endianness,
|
|
Interval: config.Interval,
|
|
Timeout: config.Timeout,
|
|
Mapping: convertToModbusDcMapping(config.Mapping),
|
|
}
|
|
}
|
|
|
|
func convertToModbusDcMapping(modbusDcMapping []config.ModbusDcMapping) []*proto.ModbusDcMapping {
|
|
res := make([]*proto.ModbusDcMapping, 0)
|
|
for _, mdm := range modbusDcMapping {
|
|
res = append(res, &proto.ModbusDcMapping{
|
|
Function: mdm.Function,
|
|
Addr: mdm.Addr,
|
|
Quantity: mdm.Quantity,
|
|
WriteStrategy: mdm.WriteStrategy,
|
|
Type: mdm.Type,
|
|
Start: mdm.Start,
|
|
})
|
|
}
|
|
return res
|
|
}
|