2023-07-14 10:16:58 +08:00
|
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-18 10:22:17 +08:00
|
|
|
|
"flag"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/spf13/viper"
|
2023-12-20 14:13:23 +08:00
|
|
|
|
"joylink.club/iot/dto"
|
2024-05-29 17:20:51 +08:00
|
|
|
|
"log/slog"
|
2023-07-14 10:16:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-11-17 17:01:12 +08:00
|
|
|
|
const (
|
|
|
|
|
SystemName = "rtsts"
|
|
|
|
|
)
|
|
|
|
|
|
2023-07-14 10:16:58 +08:00
|
|
|
|
type AppConfig struct {
|
2024-02-04 14:28:04 +08:00
|
|
|
|
// Env string
|
2023-07-14 10:16:58 +08:00
|
|
|
|
Server server
|
|
|
|
|
Datasource datasource
|
|
|
|
|
Logging log
|
2023-07-26 17:51:32 +08:00
|
|
|
|
Messaging messaging
|
2024-02-04 14:28:04 +08:00
|
|
|
|
// Dynamics DynamicsConfig
|
|
|
|
|
// Vobc VobcConfig
|
|
|
|
|
// Interlock InterlockConfig
|
2023-07-14 10:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
type server struct {
|
|
|
|
|
Port int
|
|
|
|
|
}
|
|
|
|
|
type datasource struct {
|
|
|
|
|
Dsn string
|
|
|
|
|
}
|
|
|
|
|
type log struct {
|
|
|
|
|
Level string // 日志打印级别 debug info warn error
|
|
|
|
|
Format string // 输出日志格式 logfmt, json
|
|
|
|
|
Path string // 输出日志文件路径
|
|
|
|
|
FileName string // 输出日志文件名称
|
|
|
|
|
FileMaxSize int // 【日志分割】单个日志文件最多存储量 单位(mb)
|
|
|
|
|
FileMaxBackups int // 【日志分割】日志备份文件最多数量
|
|
|
|
|
MaxAge int // 日志保留时间,单位: 天 (day)
|
|
|
|
|
Compress bool // 是否压缩日志
|
|
|
|
|
Stdout bool // 是否输出到控制台
|
|
|
|
|
}
|
2023-07-26 18:00:00 +08:00
|
|
|
|
|
2023-07-26 17:51:32 +08:00
|
|
|
|
type messaging struct {
|
2023-11-16 16:54:23 +08:00
|
|
|
|
Mqtt mqtt
|
2023-07-26 17:51:32 +08:00
|
|
|
|
}
|
2023-11-16 16:54:23 +08:00
|
|
|
|
|
|
|
|
|
// MQTT客户端配置
|
|
|
|
|
type mqtt struct {
|
|
|
|
|
Address string
|
|
|
|
|
Username string
|
|
|
|
|
Password string
|
2023-07-26 18:00:00 +08:00
|
|
|
|
}
|
2023-10-26 15:06:26 +08:00
|
|
|
|
|
2023-11-16 16:54:23 +08:00
|
|
|
|
// type centrifugo struct {
|
|
|
|
|
// TokenSecret string
|
|
|
|
|
// ApiKey string
|
|
|
|
|
// ApiEndpoint string
|
|
|
|
|
// Address string
|
|
|
|
|
// }
|
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
|
// 第三方配置结构
|
2024-07-04 13:59:39 +08:00
|
|
|
|
type ThirdPartyConfig struct {
|
|
|
|
|
Id int32 `json:"id"`
|
|
|
|
|
Dynamics DynamicsConfig `json:"dynamics" description:"动力学配置"`
|
|
|
|
|
Vobc VobcConfig `json:"vobc" description:"半实物配置"`
|
|
|
|
|
Interlocks []InterlockConfig `json:"interlock" description:"联锁配置"`
|
|
|
|
|
RsspAxleConfig RsspAxleConfig `json:"rsspAxleCfgs" description:"计轴通信配置"`
|
2024-04-28 11:01:12 +08:00
|
|
|
|
//ElectricMachinery ElectricMachineryConfig `json:"electricMachinery" description:"电机配置"`
|
|
|
|
|
ElectricMachinerys []ElectricMachineryConfig `json:"electricMachinerys" description:"电机配置"`
|
|
|
|
|
BtmCanet BtmCanetConfig `json:"btmCanet" description:"BTM关联的网关设备CANET配置"`
|
|
|
|
|
CidcModbus []CidcModbusConfig `json:"cidcModbus" description:"联锁驱采Modbus接口配置"`
|
|
|
|
|
Radar RadarConfig `json:"radar" description:"车载雷达相关配置"`
|
|
|
|
|
Acc AccConfig `json:"acc" description:"车载加速计"`
|
2024-05-24 09:00:43 +08:00
|
|
|
|
//PcSimConfig VehiclePCSimConfig `json:"pcSimConfig" description:"车载pc仿真平台通信"`
|
|
|
|
|
PcSimConfigs []VehiclePCSimConfig `json:"pcSimConfigs" description:"车载pc仿真平台通信"`
|
2024-05-29 13:34:50 +08:00
|
|
|
|
BtmVobc BtmVobcConfig `json:"btmVobc" description:"Btm Vobc 11号线相关配置"`
|
2024-01-24 17:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
type RadarConfig struct {
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
|
|
|
|
RemoteIp string `json:"remoteIp" description:"远端接收列车信息ip"`
|
|
|
|
|
RemotePort uint32 `json:"remotePort" description:"远端接收列车信息端口"`
|
|
|
|
|
//LocalPort uint32 `json:"localPort" description:"本地端口"`
|
2024-01-26 17:57:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AccConfig struct {
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
|
|
|
|
RemoteIp string `json:"remoteIp" description:"远端接收列车信息ip"`
|
|
|
|
|
RemotePort uint32 `json:"remotePort" description:"远端接收列车信息端口"`
|
2023-12-13 11:22:30 +08:00
|
|
|
|
}
|
|
|
|
|
type CidcModbusConfig struct {
|
2023-12-20 14:13:23 +08:00
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
2023-12-21 13:35:04 +08:00
|
|
|
|
Url string `json:"url" description:"接口URL【格式tcp://{ip}:{port}】" default:"tcp://127.0.0.1:502"` // 连接地址
|
2023-12-20 14:13:23 +08:00
|
|
|
|
UnitId uint32 `json:"unitId" description:"从机unitId"` // 从机unitId
|
2023-12-21 13:35:04 +08:00
|
|
|
|
Endianness dto.Modbus_Endianness `json:"endianness" description:"字节序【大端/小端】"` // 16位寄存器字节序
|
2023-12-20 14:13:23 +08:00
|
|
|
|
Interval uint32 `json:"interval" description:"定时请求间隔" default:"1000"` // 循环请求间隔(毫秒),0表示不主动请求,只当有变化时请求
|
|
|
|
|
Timeout uint32 `json:"timeout" description:"请求超时时间" default:"1000"` // 超时时间(毫秒)
|
|
|
|
|
Ecs string `json:"ecs" description:"联锁集中站"` // 所属集中站
|
|
|
|
|
Mapping []ModbusDcMapping `json:"mapping" description:"modbus数据与驱动/采集码表映射配置"`
|
2023-12-13 11:22:30 +08:00
|
|
|
|
}
|
|
|
|
|
type ModbusDcMapping struct {
|
2023-12-21 13:35:04 +08:00
|
|
|
|
Function dto.Modbus_Function `json:"function" description:"Modbus功能"` // 功能
|
|
|
|
|
Addr uint32 `json:"addr" description:"Modbus功能起始地址【位类型的功能为起始位地址,寄存器类型的功能为起始字(2个字节)地址】"` // 起始地址,当功能为位功能时,表示起始位地址,当功能为寄存器功能时,表示起始字(2个字节)地址
|
|
|
|
|
Quantity uint32 `json:"quantity" description:"Modbus读取数量【位类型的为位数,寄存器类型的为字(2个字节)数】"` // 数量,当功能为位功能时,表示位数,当功能为寄存器功能时,表示字(2个字节)数
|
2023-12-20 14:13:23 +08:00
|
|
|
|
// WriteStrategy proto.Modbus_WriteStrategy `json:"writeStrategy" description:"写入策略"` // 当功能为写入类功能时(不包含读写类功能),写策略
|
2023-12-21 13:35:04 +08:00
|
|
|
|
Type dto.DataType `json:"type" description:"映射的数据类型"` // 对应数据类型
|
|
|
|
|
Start uint32 `json:"start" description:"映射数据的起始地址【位类型的为起始位地址,寄存器类型的为起始字节地址】"` // 映射起始地址
|
2023-07-26 17:02:53 +08:00
|
|
|
|
}
|
2023-10-26 15:06:26 +08:00
|
|
|
|
type DynamicsConfig struct {
|
2023-10-31 18:08:17 +08:00
|
|
|
|
Ip string `json:"ip" description:"IP配置"`
|
|
|
|
|
UdpLocalPort int `json:"udpLocalPort" description:"本机监听接收端口"`
|
|
|
|
|
UdpRemotePort int `json:"udpRemotePort" description:"远端接收道岔信息端口"`
|
|
|
|
|
UdpRemoteTrainPort int `json:"udpRemoteTrainPort" description:"远端接收列车信息端口"`
|
|
|
|
|
HttpPort int `json:"httpPort" description:"http服务端口"`
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
2023-08-18 16:20:40 +08:00
|
|
|
|
}
|
2023-10-26 15:06:26 +08:00
|
|
|
|
type VobcConfig struct {
|
2023-10-31 18:08:17 +08:00
|
|
|
|
Ip string `json:"ip" description:"IP配置"`
|
|
|
|
|
LocalPort int `json:"localPort" description:"本机监听接收端口"`
|
|
|
|
|
RemotePort int `json:"remotePort" description:"远端接收列车信息端口"`
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
2023-10-26 15:06:26 +08:00
|
|
|
|
}
|
|
|
|
|
type InterlockConfig struct {
|
2024-07-29 13:53:46 +08:00
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
2023-10-31 18:08:17 +08:00
|
|
|
|
Ip string `json:"ip" description:"IP配置"`
|
|
|
|
|
LocalPort int `json:"localPort" description:"本机监听接收端口"`
|
|
|
|
|
RemotePort int `json:"remotePort" description:"远端接收采集信息端口"`
|
|
|
|
|
Code string `json:"code" description:"所属集中站"`
|
2024-05-29 17:20:51 +08:00
|
|
|
|
Line string `json:"line" description:"联锁通信协议所属线路"`
|
2023-10-24 10:57:04 +08:00
|
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
|
type ElectricMachineryConfig struct {
|
|
|
|
|
Ip string `json:"ip" description:"IP配置"`
|
|
|
|
|
RemotePort int `json:"remotePort" description:"远端接收信息端口"`
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
2024-04-28 11:01:12 +08:00
|
|
|
|
EndPointA bool `json:"endPointA" description:"一号电机"`
|
2023-11-17 16:22:22 +08:00
|
|
|
|
}
|
2023-11-09 13:05:29 +08:00
|
|
|
|
|
2024-05-29 13:34:50 +08:00
|
|
|
|
// BtmVobcConfig 11 号线 BTM vobc 网关设备配置
|
|
|
|
|
type BtmVobcConfig struct {
|
2024-05-29 13:55:21 +08:00
|
|
|
|
LocalUdpIp string `json:"LocalUdpIp" description:"本机监听接收UDP ip 配置"`
|
2024-05-29 13:34:50 +08:00
|
|
|
|
LocalUdpPort int `json:"localUdpPort" description:"本机监听接收UDP端口"`
|
|
|
|
|
RemoteIp string `json:"remoteIp" description:"btm串口设备IP配置"`
|
|
|
|
|
RemoteUdpPort int `json:"remoteUdpPort" description:"btm串口设备UDP端口"`
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-24 15:20:05 +08:00
|
|
|
|
// BtmCanetConfig BTM CANET网关设备配置
|
|
|
|
|
type BtmCanetConfig struct {
|
|
|
|
|
LocalUdpPort int `json:"localUdpPort" description:"本机监听接收UDP端口"`
|
|
|
|
|
RemoteIp string `json:"remoteIp" description:"CANET设备IP配置"`
|
|
|
|
|
RemoteUdpPort int `json:"remoteUdpPort" description:"CANET设备UDP端口"`
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 09:00:43 +08:00
|
|
|
|
type VehiclePCSimConfig2 struct {
|
|
|
|
|
TrainEnds bool `json:"trainEnds" description:"列车端点A"`
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
|
|
|
|
PcSimIp string `json:"pcSimIp" description:"pc仿真平台通信ip"`
|
|
|
|
|
PcSimPort uint32 `json:"pcSimPort" description:"pc仿真平台通信端口"`
|
|
|
|
|
LocalTestingPort uint32 `json:"localTestingPort" description:"本地测试端口"`
|
|
|
|
|
}
|
2023-10-24 10:57:04 +08:00
|
|
|
|
|
2024-03-18 11:39:07 +08:00
|
|
|
|
type VehiclePCSimConfig struct {
|
2024-07-19 15:24:28 +08:00
|
|
|
|
//TrainEnds bool `json:"trainEnds" description:"列车端点A?"`
|
|
|
|
|
ConfigName string `json:"configName" description:"连接名称"`
|
|
|
|
|
Open bool `json:"open" description:"是否开启"`
|
|
|
|
|
PcSimIp string `json:"pcSimIp" description:"pc仿真平台通信ip"`
|
|
|
|
|
PcSimPort uint32 `json:"pcSimPort" description:"pc仿真平台通信端口"`
|
2024-06-13 14:16:52 +08:00
|
|
|
|
//LocalTestingPort uint32 `json:"localTestingPort" description:"本地测试端口"`
|
2024-03-18 11:39:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 13:59:39 +08:00
|
|
|
|
// RsspAxleConfig Rssp计轴通信配置
|
|
|
|
|
type RsspAxleConfig struct {
|
|
|
|
|
Open bool `json:"open" description:"开启"`
|
|
|
|
|
StationCode string `json:"stationCode" description:"集中站编号"`
|
|
|
|
|
NetAConfig RsspNetConfig `json:"netAConfig" description:"A网配置"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RsspNetConfig 计轴通信配置
|
|
|
|
|
type RsspNetConfig struct {
|
2024-07-17 15:44:47 +08:00
|
|
|
|
RemoteIp string `json:"remoteIp" description:"远端IP"`
|
|
|
|
|
RemotePort int `json:"remotePort" description:"远端端口"`
|
|
|
|
|
LocalPort int `json:"localPort" description:"本地端口"`
|
|
|
|
|
|
|
|
|
|
RemoteAddr string `json:"sourceAddr" description:"联锁地址(16进制,2字节)"`
|
|
|
|
|
LocalAddr string `json:"targetAddr" description:"计轴地址(16进制,2字节)"`
|
|
|
|
|
|
|
|
|
|
RemoteSid1 string `json:"remoteSid1" description:"联锁SID_1(16进制,4字节)"`
|
|
|
|
|
RemoteSid2 string `json:"remoteSid2" description:"联锁SID_2(16进制,4字节)"`
|
|
|
|
|
LocalSid1 string `json:"localSid1" description:"计轴SID_1(16进制,4字节)"`
|
|
|
|
|
LocalSid2 string `json:"localSid2" description:"计轴SID_2(16进制,4字节)"`
|
|
|
|
|
|
|
|
|
|
RemoteSinit1 string `json:"remoteSinit1" description:"联锁SINIT_1(16进制,4字节)"`
|
|
|
|
|
RemoteSinit2 string `json:"remoteSinit2" description:"联锁SINIT_2(16进制,4字节)"`
|
|
|
|
|
LocalSinit1 string `json:"localSinit1" description:"计轴SINIT_1(16进制,4字节)"`
|
|
|
|
|
LocalSinit2 string `json:"localSinit2" description:"计轴SINIT_2(16进制,4字节)"`
|
|
|
|
|
|
|
|
|
|
RemoteDataVer1 string `json:"remoteDataVer1" description:"联锁DATAVER_1(16进制,4字节)"`
|
|
|
|
|
RemoteDataVer2 string `json:"remoteDataVer2" description:"联锁DATAVER_2(16进制,4字节)"`
|
|
|
|
|
LocalDataVer1 string `json:"localDataVer1" description:"计轴DATAVER_1(16进制,4字节)"`
|
|
|
|
|
LocalDataVer2 string `json:"localDataVer2" description:"计轴DATAVER_2(16进制,4字节)"`
|
|
|
|
|
|
|
|
|
|
MaxDeviation int `json:"maxDeviation" description:"可容忍的最大时序偏差"`
|
|
|
|
|
WaitSSRTimeout int `json:"waitSSRTimeout" description:"等待SSR回应的定时器超时值(ms)"`
|
|
|
|
|
Period int `json:"period" description:"RSD发送周期(ms)"`
|
2023-11-09 13:05:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2023-07-14 10:16:58 +08:00
|
|
|
|
var Config AppConfig
|
|
|
|
|
|
2023-08-18 10:22:17 +08:00
|
|
|
|
// 获取配置文件名称,从运行flag参数config中获取,若未提供,使用默认'dev'
|
|
|
|
|
func getConfigName() string {
|
|
|
|
|
configName := ""
|
|
|
|
|
flag.StringVar(&configName, "config", "dev", "config name, eg: -config test")
|
|
|
|
|
flag.Parse()
|
|
|
|
|
if configName == "" {
|
|
|
|
|
configName = "dev"
|
|
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
|
slog.Info("读取配置文件", "配置文件名称", configName)
|
2023-08-18 10:22:17 +08:00
|
|
|
|
return configName
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 10:16:58 +08:00
|
|
|
|
// 加载配置
|
|
|
|
|
func LoadConfig() {
|
2023-08-18 10:22:17 +08:00
|
|
|
|
cnf := viper.New()
|
|
|
|
|
cnf.SetConfigName(getConfigName())
|
|
|
|
|
cnf.SetConfigType("yml")
|
|
|
|
|
cnf.AddConfigPath("./config/")
|
|
|
|
|
cnf.AddConfigPath(".")
|
|
|
|
|
err := cnf.ReadInConfig()
|
2023-07-14 10:16:58 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Errorf("读取配置文件错误: %w", err))
|
|
|
|
|
}
|
2023-08-18 10:22:17 +08:00
|
|
|
|
err = cnf.Unmarshal(&Config)
|
2023-07-14 10:16:58 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Errorf("解析配置文件错误: %w", err))
|
|
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
|
slog.Info("成功加载配置", "config", Config)
|
2023-07-14 10:16:58 +08:00
|
|
|
|
}
|