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
62 lines
1.4 KiB
package handlerfactory
|
|
|
|
import (
|
|
"git.aiterp.net/lucifer/new-server/models"
|
|
"gopkg.in/yaml.v2"
|
|
"io"
|
|
"io/ioutil"
|
|
"strings"
|
|
)
|
|
|
|
type FactoryConfig struct {
|
|
Mode FCMOde `yaml:"mode"`
|
|
Target string `yaml:"target"`
|
|
SceneNames []string `yaml:"scenes"`
|
|
TriggerDeviceID string `yaml:"trigger_device_id"`
|
|
After []After `yaml:"after"`
|
|
From string `yaml:"from"`
|
|
To string `yaml:"to"`
|
|
}
|
|
|
|
type FCMOde string
|
|
|
|
var (
|
|
SceneDimmer FCMOde = "SceneDimmer"
|
|
MotionSensor FCMOde = "MotionSensor"
|
|
)
|
|
|
|
type After struct {
|
|
Minutes int `yaml:"minutes"`
|
|
NewState models.NewDeviceState `yaml:"new_state"`
|
|
}
|
|
|
|
func ParseConfigs(reader io.Reader) ([]FactoryConfig, error) {
|
|
readAll, err := ioutil.ReadAll(reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
splits := strings.Split(string(readAll), "\n---\n")
|
|
configs := make([]FactoryConfig, 0, 8)
|
|
for _, split := range splits {
|
|
var factoryConfig FactoryConfig
|
|
err := yaml.NewDecoder(strings.NewReader(split)).Decode(&factoryConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
configs = append(configs, factoryConfig)
|
|
}
|
|
|
|
return configs, nil
|
|
}
|
|
|
|
func (f *FactoryConfig) FromTime() models.TimeOfDay {
|
|
tod, _ := models.ParseTimeOfDay(f.From)
|
|
return tod
|
|
}
|
|
|
|
func (f *FactoryConfig) ToTime() models.TimeOfDay {
|
|
tod, _ := models.ParseTimeOfDay(f.To)
|
|
return tod
|
|
}
|