PKGBUILD/pkg/config/flucky.go
Markus Pesch 5220eac16b
fix: breaking changes
changes:
- remove remote operations
- add function to write measured values into a channel
- add get humidity sensors from config
- add get temperature sensors from config
- remove FileLogger
- exclude some functions from pkf into internal
2019-06-13 22:15:48 +02:00

192 lines
4.6 KiB
Go

package config
import (
"fmt"
"time"
"github.com/volker-raschek/flucky/pkg/sensor"
uuid "github.com/satori/go.uuid"
"github.com/volker-raschek/flucky/pkg/types"
)
// Configuration of flucky
type Configuration struct {
Device *types.Device `json:"device"`
Sensors []*types.Sensor `json:"sensors"`
}
// AddSensor add a new sensor
func (c *Configuration) AddSensor(sensor *types.Sensor) error {
// check if sensorID is a valid UUID string
if !validUUID.MatchString(sensor.SensorID) {
sensor.SensorID = uuid.NewV4().String()
}
// check if sensor name and sensor uuid already exists
for _, s := range c.Sensors {
if s.SensorName == sensor.SensorName {
return fmt.Errorf("Sensor %v already exists", s.SensorName)
}
if s.SensorID == sensor.SensorID {
return fmt.Errorf("Sensor %v with UUID %v already exists", s.SensorName, s.SensorID)
}
if *sensor.WireID != "" &&
*s.WireID == *sensor.WireID {
return fmt.Errorf("Sensor with 1wire-id %v already exists as %v", *s.WireID, s.SensorName)
}
}
// check if sensor has a valid device id
if sensor.DeviceID != c.Device.DeviceID {
sensor.DeviceID = c.Device.DeviceID
}
// overwrite creation date
sensor.CreationDate = time.Now()
//TODO: check if wire sensor exists in /dev/bus/w1/devices
// check
c.Sensors = append(c.Sensors, sensor)
return nil
}
// DisableSensor disables a sensor by its name or its unique UUID
func (c *Configuration) DisableSensor(name string) error {
found := false
for _, sensor := range c.Sensors {
// disable sensor matched after name
if !validUUID.MatchString(name) &&
sensor.SensorName == name {
sensor.SensorEnabled = false
found = true
break
}
// remove machted uuid
if validUUID.MatchString(name) &&
sensor.SensorID == name {
sensor.SensorEnabled = false
found = true
break
}
}
if !found {
return fmt.Errorf("Can not found sensor %v", name)
}
return nil
}
// EnableSensor enables a sensor by its name or its unique UUID
func (c *Configuration) EnableSensor(name string) error {
found := false
for _, sensor := range c.Sensors {
// disable sensor matched after name
if !validUUID.MatchString(name) &&
sensor.SensorName == name {
sensor.SensorEnabled = true
found = true
break
}
// remove machted uuid
if validUUID.MatchString(name) &&
sensor.SensorID == name {
sensor.SensorEnabled = true
found = true
break
}
}
if !found {
return fmt.Errorf("Can not found sensor %v", name)
}
return nil
}
// GetHumiditySensors returns a list of humidity sensors
func (c *Configuration) GetHumiditySensors() []sensor.HumiditySensor{
humiditySensors, _ := c.splitSensors()
return humiditySensors
}
// GetTemperatureSensors returns a list of humidity sensors
func (c *Configuration) GetTemperatureSensors() []sensor.TemperatureSensor{
_, temperatureSensors := c.splitSensors()
return temperatureSensors
}
// RemoveSensor deletes a sensor by its name or its unique UUID
func (c *Configuration) RemoveSensor(name string) error {
for i, sensor := range c.Sensors {
// remove machted name
if !validUUID.MatchString(name) &&
sensor.SensorName == name {
c.Sensors = append(c.Sensors[:i], c.Sensors[i+1:]...)
return nil
}
// remove machted uuid
if validUUID.MatchString(name) &&
sensor.SensorID == name {
c.Sensors = append(c.Sensors[:i], c.Sensors[i+1:]...)
return nil
}
}
return fmt.Errorf("Can not find sensor %v", name)
}
// RenameSensor renames a sensor identified by the name or the UUID
func (c *Configuration) RenameSensor(oldName, newName string) error {
for _, sensor := range c.Sensors {
if sensor.SensorName == oldName ||
sensor.SensorID == oldName {
sensor.SensorName = newName
return nil
}
}
return fmt.Errorf("Could not find remote %v to replace into with %v", oldName, newName)
}
func (c *Configuration) splitSensors() ([]sensor.HumiditySensor, []sensor.TemperatureSensor) {
humiditySensors := make([]sensor.HumiditySensor, 0)
temperatureSensors := make([]sensor.TemperatureSensor, 0)
for _, s := range c.Sensors {
switch s.SensorModel {
case types.DHT11:
humiditySensors = append(humiditySensors, &sensor.DHT11{
Sensor: s,
})
temperatureSensors = append(temperatureSensors, &sensor.DHT11{
Sensor: s,
})
case types.DHT22:
humiditySensors = append(humiditySensors, &sensor.DHT22{
Sensor: s,
})
temperatureSensors = append(temperatureSensors, &sensor.DHT22{
Sensor: s,
})
case types.DS18B20:
temperatureSensors = append(temperatureSensors, &sensor.DS18B20{
Sensor: s,
})
}
}
return humiditySensors, temperatureSensors
}