You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package handlerfactory
  2. import (
  3. "git.aiterp.net/lucifer/new-server/models"
  4. "gopkg.in/yaml.v2"
  5. "io"
  6. "io/ioutil"
  7. "strings"
  8. )
  9. type FactoryConfig struct {
  10. Mode FCMOde `yaml:"mode"`
  11. Target string `yaml:"target"`
  12. SceneNames []string `yaml:"scenes"`
  13. TriggerDeviceID string `yaml:"trigger_device_id"`
  14. After []After `yaml:"after"`
  15. From string `yaml:"from"`
  16. To string `yaml:"to"`
  17. }
  18. type FCMOde string
  19. var (
  20. SceneDimmer FCMOde = "SceneDimmer"
  21. MotionSensor FCMOde = "MotionSensor"
  22. )
  23. type After struct {
  24. Minutes int `yaml:"minutes"`
  25. NewState models.NewDeviceState `yaml:"new_state"`
  26. }
  27. func ParseConfigs(reader io.Reader) ([]FactoryConfig, error) {
  28. readAll, err := ioutil.ReadAll(reader)
  29. if err != nil {
  30. return nil, err
  31. }
  32. splits := strings.Split(string(readAll), "\n---\n")
  33. configs := make([]FactoryConfig, 0, 8)
  34. for _, split := range splits {
  35. var factoryConfig FactoryConfig
  36. err := yaml.NewDecoder(strings.NewReader(split)).Decode(&factoryConfig)
  37. if err != nil {
  38. return nil, err
  39. }
  40. configs = append(configs, factoryConfig)
  41. }
  42. return configs, nil
  43. }
  44. func (f *FactoryConfig) FromTime() models.TimeOfDay {
  45. tod, _ := models.ParseTimeOfDay(f.From)
  46. return tod
  47. }
  48. func (f *FactoryConfig) ToTime() models.TimeOfDay {
  49. tod, _ := models.ParseTimeOfDay(f.To)
  50. return tod
  51. }