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()) }
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) } }
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) }
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) }