feat: add sensor component
This commit is contained in:
parent
a1c5b08ae4
commit
16180d221e
78
pkg/component/pressure_sensor.go
Normal file
78
pkg/component/pressure_sensor.go
Normal file
@ -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
|
||||||
|
}
|
42
pkg/component/sensor.go
Normal file
42
pkg/component/sensor.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user