Setnewstates as method, sort, check and set to skip noop

master v0.0.6
Bel LaPointe 2022-01-11 17:04:16 -05:00
parent b4007a8eb5
commit ac9ccf633a
1 changed files with 23 additions and 20 deletions

35
main.go
View File

@ -11,6 +11,7 @@ import (
"local/truckstop/message" "local/truckstop/message"
"log" "log"
"regexp" "regexp"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -66,17 +67,28 @@ func matrixrecv() error {
states[state] = struct{}{} states[state] = struct{}{}
} }
} }
setNewStates(states)
return nil
}
func setNewStates(states map[config.State]struct{}) {
if len(states) == 0 { if len(states) == 0 {
return nil return
} }
conf := *config.Get() newstates := []config.State{}
conf.States = []config.State{}
for k := range states { for k := range states {
conf.States = append(conf.States, k) newstates = append(newstates, k)
} }
log.Printf("%+v, %+v", states, conf) sort.Slice(newstates, func(i, j int) bool {
return newstates[i] < newstates[j]
})
conf := *config.Get()
if fmt.Sprint(newstates) == fmt.Sprint(conf.States) {
return
}
conf.States = newstates
log.Printf("updating config new states: %+v", conf)
config.Set(conf) config.Set(conf)
return nil
} }
func email() error { func email() error {
@ -101,16 +113,7 @@ func email() error {
states[state] = struct{}{} states[state] = struct{}{}
} }
} }
if len(states) == 0 { setNewStates(states)
return nil
}
conf := *config.Get()
conf.States = []config.State{}
for k := range states {
conf.States = append(conf.States, k)
}
log.Printf("%+v, %+v", states, conf)
config.Set(conf)
return nil return nil
} }