77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package business
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type SensorType int
|
|
|
|
const (
|
|
SensorTypeTemperatureHumidity SensorType = 1 // 温湿度传感器
|
|
SensorTypeWaterLeak SensorType = 2 // 水浸传感器
|
|
SensorTypeSmoke SensorType = 3 // 烟雾传感器
|
|
)
|
|
|
|
// ModbusConfig Modbus通信配置
|
|
type ModbusConfig struct {
|
|
Port string
|
|
BaudRate int
|
|
DataBits int
|
|
StopBits int
|
|
Parity string
|
|
Timeout time.Duration
|
|
RetryCount int
|
|
RetryDelay time.Duration
|
|
SensorInterval time.Duration
|
|
BusResetDelay time.Duration
|
|
}
|
|
type SensorRegisterConfig struct {
|
|
RegisterAddress uint16
|
|
RegisterCount uint16
|
|
}
|
|
|
|
// GetSensorTypeDescription 获取传感器类型描述
|
|
func GetSensorTypeDescription(sensorType SensorType) string {
|
|
switch sensorType {
|
|
case SensorTypeTemperatureHumidity:
|
|
return "TemperatureHumidityTransducer(1)"
|
|
case SensorTypeWaterLeak:
|
|
return "WaterLeakTransducer(2)"
|
|
case SensorTypeSmoke:
|
|
return "SmokeTransducer(3)"
|
|
default:
|
|
return "UnknownTransducer"
|
|
}
|
|
}
|
|
|
|
// DefaultModbusConfig 获取默认Modbus配置
|
|
func DefaultModbusConfig() *ModbusConfig {
|
|
return &ModbusConfig{
|
|
Port: "/dev/ttyS5",
|
|
BaudRate: 9600,
|
|
DataBits: 8,
|
|
StopBits: 1,
|
|
Parity: "N",
|
|
Timeout: 3 * time.Second, // 缩短超时时间
|
|
RetryCount: 2, // 减少重试次数
|
|
RetryDelay: 100 * time.Millisecond,
|
|
SensorInterval: 200 * time.Millisecond, // 传感器间读取间隔
|
|
BusResetDelay: 50 * time.Millisecond, // 总线稳定时间
|
|
}
|
|
}
|
|
|
|
// GetRegisterConfig 根据传感器类型获取寄存器配置
|
|
func GetRegisterConfig(sensorType SensorType) (*SensorRegisterConfig, error) {
|
|
switch sensorType {
|
|
case SensorTypeTemperatureHumidity:
|
|
return &SensorRegisterConfig{RegisterAddress: 0x00, RegisterCount: 2}, nil
|
|
case SensorTypeWaterLeak:
|
|
return &SensorRegisterConfig{RegisterAddress: 0x00, RegisterCount: 1}, nil
|
|
case SensorTypeSmoke:
|
|
return &SensorRegisterConfig{RegisterAddress: 0x03, RegisterCount: 1}, nil
|
|
default:
|
|
return nil, fmt.Errorf("Unsupported sensor types")
|
|
}
|
|
}
|