66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package types
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// Sensor represents a sensor with all his settings. The struct does not
|
|
// contains any read method.
|
|
type Sensor struct {
|
|
ID string `json:"id" xml:"id"`
|
|
Name string `json:"name" xml:"name"`
|
|
Location string `json:"location" xml:"location"`
|
|
WireID *string `json:"wire_id" xml:"wire_id"`
|
|
I2CBus *int `json:"i2c_bus" xml:"i2c_bus"`
|
|
I2CAddress *uint8 `json:"i2c_address" xml:"i2c_address"`
|
|
GPIONumber string `json:"gpio_number" xml:"gpio_number"`
|
|
Model string `json:"model" xml:"model"`
|
|
Enabled bool `json:"enabled" xml:"enabled"`
|
|
TickDuration string `json:"tick_duration" xml:"tick_duration"`
|
|
DeviceID string `json:"device_id" xml:"device_id"`
|
|
CreationDate time.Time `json:"creation_date" xml:"creation_date"`
|
|
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
|
}
|
|
|
|
// GetTicker returns a new ticker, which tick every when the sensor should be
|
|
// read
|
|
func (s *Sensor) GetTicker() *time.Ticker {
|
|
duration, err := time.ParseDuration(s.TickDuration)
|
|
if err != nil {
|
|
duration = time.Minute
|
|
}
|
|
return time.NewTicker(duration)
|
|
}
|
|
|
|
// FilterSensorByMeasuredValueTypes filters sensors by the measured values types
|
|
// which they measure
|
|
func FilterSensorByMeasuredValueTypes(sensors []*Sensor, measuredValueTypes ...MeasuredValueType) ([]*Sensor, error) {
|
|
cachedSensors := make([]*Sensor, 0)
|
|
|
|
mapping := map[MeasuredValueType][]string{
|
|
Humidity: {"BME280", "DHT11", "DHT22"},
|
|
Pressure: {"BME280"},
|
|
Temperature: {"BME280", "DHT11", "DHT22", "DS18B20"},
|
|
}
|
|
|
|
for i := range measuredValueTypes {
|
|
mappedSensors, ok := mapping[measuredValueTypes[i]]
|
|
if !ok {
|
|
return nil, fmt.Errorf("No mapping for measured values type %v available", measuredValueTypes[i])
|
|
}
|
|
|
|
LOOP:
|
|
for j := range sensors {
|
|
for k := range mappedSensors {
|
|
if sensors[j].Model == mappedSensors[k] {
|
|
cachedSensors = append(cachedSensors, sensors[j])
|
|
continue LOOP
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return cachedSensors, nil
|
|
}
|