PKGBUILD/pkg/config/flucky.go

321 lines
8.2 KiB
Go
Raw Normal View History

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"
)
var humiditySensorModels = map[types.SensorModel]types.SensorModel{
types.DHT11: types.DHT11,
types.DHT22: types.DHT22,
}
var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
types.DHT11: types.DHT11,
types.DHT22: types.DHT22,
types.DS18B20: types.DS18B20,
}
// 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(option Option) []sensor.HumiditySensor {
humiditySensors := c.getHumiditySensors()
switch option {
case ENABLED:
for i, humiditySensor := range humiditySensors {
if !humiditySensor.SensorEnabled {
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
}
}
return c.convertHumiditySensors(humiditySensors)
case DISABLED:
for i, humiditySensor := range humiditySensors {
if humiditySensor.SensorEnabled {
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
}
}
return c.convertHumiditySensors(humiditySensors)
default:
return c.convertHumiditySensors(humiditySensors)
}
}
// GetHumiditySensorsByName returns a list of temperature sensors by name,
// uuid or wire-id
func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.HumiditySensor {
configHumiditySensors := make(map[string]*types.Sensor, 0)
for _, name := range names {
for _, s := range c.getHumiditySensors() {
switch name {
case s.SensorID:
configHumiditySensors[s.SensorID] = s
case *s.WireID:
configHumiditySensors[s.SensorID] = s
case s.SensorName:
configHumiditySensors[s.SensorID] = s
}
}
}
humiditySensors := make([]*types.Sensor, 0)
for _, cs := range configHumiditySensors {
humiditySensors = append(humiditySensors, cs)
}
return c.convertHumiditySensors(humiditySensors)
}
// GetTemperatureSensors returns a list of temperature sensors
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.TemperatureSensor {
temperatureSensors := c.getTemperatureSensors()
switch option {
case ENABLED:
for i, temperatureSensor := range temperatureSensors {
if !temperatureSensor.SensorEnabled {
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
}
}
return c.convertTemperatureSensors(temperatureSensors)
case DISABLED:
for i, temperatureSensor := range temperatureSensors {
if temperatureSensor.SensorEnabled {
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
}
}
return c.convertTemperatureSensors(temperatureSensors)
default:
return c.convertTemperatureSensors(temperatureSensors)
}
}
// GetTemperatureSensorsByName returns a list of temperature sensors by name,
// uuid or wire-id
func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.TemperatureSensor {
configTemperatureSensors := make(map[string]*types.Sensor, 0)
for _, name := range names {
for _, s := range c.getTemperatureSensors() {
switch name {
case s.SensorID:
configTemperatureSensors[s.SensorID] = s
case *s.WireID:
configTemperatureSensors[s.SensorID] = s
case s.SensorName:
configTemperatureSensors[s.SensorID] = s
}
}
}
temperatureSensors := make([]*types.Sensor, 0)
for _, cs := range configTemperatureSensors {
temperatureSensors = append(temperatureSensors, cs)
}
return c.convertTemperatureSensors(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) convertHumiditySensors(sensors []*types.Sensor) []sensor.HumiditySensor {
humiditySensors := make([]sensor.HumiditySensor, 0)
for _, s := range sensors {
switch s.SensorModel {
case types.DHT11:
humiditySensors = append(humiditySensors, &sensor.DHT11{
Sensor: s,
})
case types.DHT22:
humiditySensors = append(humiditySensors, &sensor.DHT22{
Sensor: s,
})
}
}
return humiditySensors
}
func (c *Configuration) convertTemperatureSensors(sensors []*types.Sensor) []sensor.TemperatureSensor {
temperatureSensors := make([]sensor.TemperatureSensor, 0)
for _, s := range sensors {
switch s.SensorModel {
case types.DHT11:
temperatureSensors = append(temperatureSensors, &sensor.DHT11{
Sensor: s,
})
case types.DHT22:
temperatureSensors = append(temperatureSensors, &sensor.DHT22{
Sensor: s,
})
case types.DS18B20:
temperatureSensors = append(temperatureSensors, &sensor.DS18B20{
Sensor: s,
})
}
}
return temperatureSensors
}
func (c *Configuration) getHumiditySensors() []*types.Sensor {
humiditySensors := make([]*types.Sensor, 0)
for _, s := range c.Sensors {
if _, ok := humiditySensorModels[s.SensorModel]; ok {
humiditySensors = append(humiditySensors, s)
}
}
return humiditySensors
}
func (c *Configuration) getTemperatureSensors() []*types.Sensor {
temperatureSensors := make([]*types.Sensor, 0)
for _, s := range c.Sensors {
if _, ok := temperatureSensorModels[s.SensorModel]; ok {
temperatureSensors = append(temperatureSensors, s)
}
}
return temperatureSensors
}