示例#1
0
文件: main.go 项目: kienhung/gohome
func (self *Service) PersistFile(automata *gofsm.Automata) {
	w, err := os.Create(config.ConfigPath("automata.state"))
	if err != nil {
		log.Fatalln("Persisting automata state failed:", err)
	}
	defer w.Close()
	enc := json.NewEncoder(w)
	enc.Encode(automata.Persist())
}
示例#2
0
文件: main.go 项目: kienhung/gohome
func (self *Service) PersistStore(automata *gofsm.Automata, automaton string) {
	state := automata.Persist()
	v := state[automaton]
	key := "gohome/state/automata/" + automaton
	value, _ := json.Marshal(v)
	err := services.Stor.Set(key, string(value))
	if err != nil {
		log.Println("Persisting automata state to store failed:", err)
	}
}
示例#3
0
文件: main.go 项目: kienhung/gohome
func (self *Service) RestoreFile(automata *gofsm.Automata) {
	r, err := os.Open(config.ConfigPath("automata.state"))
	if err != nil {
		log.Println("Restoring automata state failed:", err)
		return
	}
	dec := json.NewDecoder(r)
	var p gofsm.AutomataState
	err = dec.Decode(&p)
	if err != nil {
		log.Println("Restoring automata state failed:", err)
		return
	}
	automata.Restore(p)
}
示例#4
0
文件: main.go 项目: kienhung/gohome
func (self *Service) RestoreStore(automata *gofsm.Automata) {
	p := gofsm.AutomataState{}
	for name := range automata.Automaton {
		key := "gohome/state/automata/" + name
		value, err := services.Stor.Get(key)
		var ps gofsm.AutomatonState
		if err == nil {
			err = json.Unmarshal([]byte(value), &ps)
		}
		if err != nil {
			log.Println("Restoring automata state from store failed:", err)
			continue
		}
		p[name] = ps
	}
	automata.Restore(p)
}