package config import ( "fmt" "time" "github.com/go-flucky/flucky/pkg/sensor" "github.com/go-flucky/flucky/pkg/types" uuid "github.com/satori/go.uuid" ) 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"` LEDs []*types.LED `json:"leds"` RGBLEDs []*types.RGBLED `json:"rgb_leds"` Sensors []*types.Sensor `json:"sensors"` } // AddLED add a new LED func (c *Configuration) AddLED(led *types.LED) error { // check if LEDID is a valid UUID string if !validUUID.MatchString(led.LEDID) { led.LEDID = uuid.NewV4().String() } // check if sensor name and sensor uuid already exists for _, l := range c.LEDs { if l.LEDName == led.LEDName { return fmt.Errorf("LED %v already exists", led.LEDName) } if l.LEDID == led.LEDID { return fmt.Errorf("LED %v with UUID %v already exists", led.LEDName, led.LEDID) } } // check if sensor has a valid device id if led.DeviceID != c.Device.DeviceID { led.DeviceID = c.Device.DeviceID } // overwrite creation date led.CreationDate = time.Now() // check c.LEDs = append(c.LEDs, led) return nil } // AddRGBLED add a new RGBLED func (c *Configuration) AddRGBLED(rgbLED *types.RGBLED) error { // check if RGBLEDID is a valid UUID string if !validUUID.MatchString(rgbLED.RGBLEDID) { rgbLED.RGBLEDID = uuid.NewV4().String() } // check if sensor name and sensor uuid already exists for _, l := range c.RGBLEDs { if l.RGBLEDName == rgbLED.RGBLEDName { return fmt.Errorf("RGBLED %v already exists", rgbLED.RGBLEDName) } if l.RGBLEDID == rgbLED.RGBLEDID { return fmt.Errorf("RGBLED %v with UUID %v already exists", rgbLED.RGBLEDName, rgbLED.RGBLEDID) } } // check if sensor has a valid device id if rgbLED.DeviceID != c.Device.DeviceID { rgbLED.DeviceID = c.Device.DeviceID } // overwrite creation date rgbLED.CreationDate = time.Now() // check c.RGBLEDs = append(c.RGBLEDs, rgbLED) return nil } // 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 } // DisableRGBLED enables a rgb led by its name or its unique UUID func (c *Configuration) DisableRGBLED(name string) error { found := false for _, rgbled := range c.RGBLEDs { // disable sensor matched after name if !validUUID.MatchString(name) && rgbled.RGBLEDName == name { rgbled.RGBLEDEnabled = false found = true break } // disable sensor matched by uuid if validUUID.MatchString(name) && rgbled.RGBLEDID == name { rgbled.RGBLEDEnabled = false found = true break } } if !found { return fmt.Errorf("Can not found RGB-LED %v", name) } 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 } // EnableRGBLED enables a rgb led by its name or its unique UUID func (c *Configuration) EnableRGBLED(name string) error { found := false for _, rgbled := range c.RGBLEDs { // disable sensor matched after name if !validUUID.MatchString(name) && rgbled.RGBLEDName == name { rgbled.RGBLEDEnabled = true found = true break } // disable sensor matched by uuid if validUUID.MatchString(name) && rgbled.RGBLEDID == name { rgbled.RGBLEDEnabled = true found = true break } } if !found { return fmt.Errorf("Can not found RGB-LED %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) } // RemoveLED deletes a LED by its name or its unique UUID func (c *Configuration) RemoveLED(name string) error { for i, led := range c.LEDs { // remove machted name if !validUUID.MatchString(name) && led.LEDName == name { c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...) return nil } // remove machted uuid if validUUID.MatchString(name) && led.LEDID == name { c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...) return nil } } return fmt.Errorf("Can not find LED %v", name) } // RemoveRGBLED deletes a LED by its name or its unique UUID func (c *Configuration) RemoveRGBLED(name string) error { for i, rgbLED := range c.RGBLEDs { // remove machted name if !validUUID.MatchString(name) && rgbLED.RGBLEDName == name { c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...) return nil } // remove machted uuid if validUUID.MatchString(name) && rgbLED.RGBLEDID == name { c.LEDs = append(c.LEDs[:i], c.LEDs[i+1:]...) return nil } } return fmt.Errorf("Can not find RGBLED %v", name) } // 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) } // RenameRGBLED renames a sensor identified by the name or the UUID func (c *Configuration) RenameRGBLED(oldName, newName string) error { for _, rgbled := range c.RGBLEDs { if rgbled.RGBLEDName == oldName || rgbled.RGBLEDID == oldName { rgbled.RGBLEDName = newName return nil } } return fmt.Errorf("Could not find rgb-led %v to replace into with %v", oldName, newName) } // 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 }