2019-06-13 19:25:32 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2019-06-15 11:56:41 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// 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
|
2019-06-15 11:02:52 +00:00
|
|
|
func (c *Configuration) GetHumiditySensors(option Option) []sensor.HumiditySensor {
|
2019-06-15 11:56:41 +00:00
|
|
|
humiditySensors := c.getHumiditySensors()
|
2019-06-15 11:02:52 +00:00
|
|
|
|
|
|
|
switch option {
|
|
|
|
case ENABLED:
|
|
|
|
for i, humiditySensor := range humiditySensors {
|
2019-06-15 11:56:41 +00:00
|
|
|
if !humiditySensor.SensorEnabled {
|
2019-06-15 11:02:52 +00:00
|
|
|
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 11:56:41 +00:00
|
|
|
return c.convertHumiditySensors(humiditySensors)
|
2019-06-15 11:02:52 +00:00
|
|
|
case DISABLED:
|
|
|
|
for i, humiditySensor := range humiditySensors {
|
2019-06-15 11:56:41 +00:00
|
|
|
if humiditySensor.SensorEnabled {
|
2019-06-15 11:02:52 +00:00
|
|
|
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 11:56:41 +00:00
|
|
|
return c.convertHumiditySensors(humiditySensors)
|
2019-06-15 11:02:52 +00:00
|
|
|
default:
|
2019-06-15 11:56:41 +00:00
|
|
|
return c.convertHumiditySensors(humiditySensors)
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
// 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 {
|
2019-06-15 11:56:41 +00:00
|
|
|
for _, s := range c.getHumiditySensors() {
|
2019-06-15 11:02:52 +00:00
|
|
|
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
|
2019-06-15 09:49:46 +00:00
|
|
|
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.TemperatureSensor {
|
2019-06-15 11:56:41 +00:00
|
|
|
temperatureSensors := c.getTemperatureSensors()
|
2019-06-15 09:49:46 +00:00
|
|
|
|
|
|
|
switch option {
|
|
|
|
case ENABLED:
|
|
|
|
for i, temperatureSensor := range temperatureSensors {
|
2019-06-15 11:56:41 +00:00
|
|
|
if !temperatureSensor.SensorEnabled {
|
2019-06-15 09:49:46 +00:00
|
|
|
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 11:56:41 +00:00
|
|
|
return c.convertTemperatureSensors(temperatureSensors)
|
2019-06-15 09:49:46 +00:00
|
|
|
case DISABLED:
|
|
|
|
for i, temperatureSensor := range temperatureSensors {
|
2019-06-15 11:56:41 +00:00
|
|
|
if temperatureSensor.SensorEnabled {
|
2019-06-15 09:49:46 +00:00
|
|
|
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 11:56:41 +00:00
|
|
|
return c.convertTemperatureSensors(temperatureSensors)
|
2019-06-15 09:49:46 +00:00
|
|
|
default:
|
2019-06-15 11:56:41 +00:00
|
|
|
return c.convertTemperatureSensors(temperatureSensors)
|
2019-06-15 09:49:46 +00:00
|
|
|
}
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
// 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 {
|
2019-06-15 11:56:41 +00:00
|
|
|
for _, s := range c.getTemperatureSensors() {
|
2019-06-15 11:02:52 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
func (c *Configuration) convertHumiditySensors(sensors []*types.Sensor) []sensor.HumiditySensor {
|
2019-06-13 19:25:32 +00:00
|
|
|
humiditySensors := make([]sensor.HumiditySensor, 0)
|
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
for _, s := range sensors {
|
2019-06-13 19:25:32 +00:00
|
|
|
switch s.SensorModel {
|
|
|
|
case types.DHT11:
|
|
|
|
humiditySensors = append(humiditySensors, &sensor.DHT11{
|
|
|
|
Sensor: s,
|
|
|
|
})
|
|
|
|
case types.DHT22:
|
|
|
|
humiditySensors = append(humiditySensors, &sensor.DHT22{
|
|
|
|
Sensor: s,
|
|
|
|
})
|
2019-06-15 11:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return humiditySensors
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) convertTemperatureSensors(sensors []*types.Sensor) []sensor.TemperatureSensor {
|
|
|
|
temperatureSensors := make([]sensor.TemperatureSensor, 0)
|
2019-06-15 11:56:41 +00:00
|
|
|
|
2019-06-15 11:02:52 +00:00
|
|
|
for _, s := range sensors {
|
|
|
|
switch s.SensorModel {
|
|
|
|
case types.DHT11:
|
|
|
|
temperatureSensors = append(temperatureSensors, &sensor.DHT11{
|
|
|
|
Sensor: s,
|
|
|
|
})
|
|
|
|
case types.DHT22:
|
2019-06-13 19:25:32 +00:00
|
|
|
temperatureSensors = append(temperatureSensors, &sensor.DHT22{
|
|
|
|
Sensor: s,
|
|
|
|
})
|
|
|
|
case types.DS18B20:
|
|
|
|
temperatureSensors = append(temperatureSensors, &sensor.DS18B20{
|
|
|
|
Sensor: s,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 11:02:52 +00:00
|
|
|
return temperatureSensors
|
2019-06-13 19:25:32 +00:00
|
|
|
}
|
2019-06-15 11:56:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|