From 16180d221ea4909372d5a628d1b766e36c3f6e12 Mon Sep 17 00:00:00 2001 From: Eli Yip Date: Tue, 27 May 2025 15:09:48 +0800 Subject: [PATCH] feat: add sensor component --- pkg/component/pressure_sensor.go | 78 ++++++++++++++++++++++++++++++++ pkg/component/sensor.go | 42 +++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 pkg/component/pressure_sensor.go create mode 100644 pkg/component/sensor.go diff --git a/pkg/component/pressure_sensor.go b/pkg/component/pressure_sensor.go new file mode 100644 index 0000000..1c1d454 --- /dev/null +++ b/pkg/component/pressure_sensor.go @@ -0,0 +1,78 @@ +package component + +import ( + "fmt" + "hands/pkg/device" + "math/rand/v2" + "time" +) + +// PressureSensor 压力传感器实现 +type PressureSensor struct { + id string + config map[string]any + isActive bool + samplingRate int + lastReading time.Time +} + +func NewPressureSensor(id string, config map[string]any) *PressureSensor { + return &PressureSensor{ + id: id, + config: config, + isActive: true, + samplingRate: 100, + lastReading: time.Now(), + } +} + +func (p *PressureSensor) GetID() string { + return p.id +} + +func (p *PressureSensor) GetType() device.ComponentType { + return device.SensorComponent +} + +func (p *PressureSensor) GetConfiguration() map[string]any { + return p.config +} + +func (p *PressureSensor) IsActive() bool { + return p.isActive +} + +func (p *PressureSensor) ReadData() (device.SensorData, error) { + if !p.isActive { + return nil, fmt.Errorf("传感器 %s 未激活", p.id) + } + + // 模拟压力数据读取 + // 在实际实现中,这里应该从 can-bridge 或其他数据源读取真实数据 + pressure := rand.Float64() * 100 // 0-100 的随机压力值 + + values := map[string]any{ + "pressure": pressure, + "unit": "kPa", + "location": p.config["location"], + } + + p.lastReading = time.Now() + return NewSensorData(p.id, values), nil +} + +func (p *PressureSensor) GetDataType() string { + return "pressure" +} + +func (p *PressureSensor) GetSamplingRate() int { + return p.samplingRate +} + +func (p *PressureSensor) SetSamplingRate(rate int) error { + if rate <= 0 || rate > 1000 { + return fmt.Errorf("采样率必须在 1-1000Hz 之间") + } + p.samplingRate = rate + return nil +} diff --git a/pkg/component/sensor.go b/pkg/component/sensor.go new file mode 100644 index 0000000..c684e19 --- /dev/null +++ b/pkg/component/sensor.go @@ -0,0 +1,42 @@ +package component + +import ( + "hands/pkg/device" + "time" +) + +// Sensor 传感器组件接口 +type Sensor interface { + device.Component + ReadData() (device.SensorData, error) + GetDataType() string + GetSamplingRate() int + SetSamplingRate(rate int) error +} + +// SensorDataImpl 传感器数据的具体实现 +type SensorDataImpl struct { + timestamp time.Time + values map[string]any + sensorID string +} + +func NewSensorData(sensorID string, values map[string]any) *SensorDataImpl { + return &SensorDataImpl{ + timestamp: time.Now(), + values: values, + sensorID: sensorID, + } +} + +func (s *SensorDataImpl) Timestamp() time.Time { + return s.timestamp +} + +func (s *SensorDataImpl) Values() map[string]any { + return s.values +} + +func (s *SensorDataImpl) SensorID() string { + return s.sensorID +}