multi-client but one ntg search, nontrivial config.json change

This commit is contained in:
Bel LaPointe
2022-01-11 22:54:18 -05:00
parent 5a4bcecac7
commit e546034c26
8 changed files with 138 additions and 54 deletions

50
main.go
View File

@@ -49,39 +49,51 @@ func matrixrecv() error {
if err != nil {
return err
}
states := map[config.State]struct{}{}
states := map[string]map[config.State]struct{}{}
for _, msg := range messages {
if len(states) > 0 {
if _, ok := states[msg.Sender]; ok {
continue
}
for _, state := range parseOutStates([]byte(msg)) {
states[state] = struct{}{}
states[msg.Sender] = map[config.State]struct{}{}
for _, state := range parseOutStates([]byte(msg.Content)) {
states[msg.Sender][state] = struct{}{}
}
}
setNewStates(states)
return nil
}
func setNewStates(states map[config.State]struct{}) {
func setNewStates(states map[string]map[config.State]struct{}) {
if len(states) == 0 {
return
}
newstates := []config.State{}
for k := range states {
newstates = append(newstates, k)
}
sort.Slice(newstates, func(i, j int) bool {
return newstates[i] < newstates[j]
})
conf := *config.Get()
if fmt.Sprint(newstates) == fmt.Sprint(conf.States) {
changed := map[string][]config.State{}
for client, clientStates := range states {
newstates := []config.State{}
for k := range clientStates {
newstates = append(newstates, k)
}
sort.Slice(newstates, func(i, j int) bool {
return newstates[i] < newstates[j]
})
clientconf := conf.Clients[client]
if fmt.Sprint(newstates) == fmt.Sprint(clientconf.States) {
continue
}
clientconf.States = newstates
conf.Clients[client] = clientconf
changed[client] = newstates
}
if len(changed) == 0 {
return
}
conf.States = newstates
log.Printf("updating config new states: %+v", conf)
config.Set(conf)
if err := sendNewStates(conf.States); err != nil {
log.Printf("failed to send new states %+v: %v", conf.States, err)
for client, states := range changed {
if err := sendNewStates(client, states); err != nil {
log.Printf("failed to send new states %s/%+v: %v", client, states, err)
}
}
}
@@ -160,7 +172,7 @@ func once() error {
}
func getJobs() ([]broker.Job, error) {
states := config.Get().States
states := config.AllStates()
ntg := broker.NewNTGVision()
if config.Get().Brokers.NTG.Mock {
ntg = ntg.WithMock()
@@ -200,7 +212,7 @@ func sendJob(job broker.Job) error {
return sender.Send(job.FormatMultilineText())
}
func sendNewStates(states []config.State) error {
func sendNewStates(client string, states []config.State) error {
sender := message.NewMatrix()
return sender.Send(fmt.Sprintf("now searching for loads from: %+v", states))
return sender.Send(fmt.Sprintf("%s: now searching for loads from: %+v", client, states))
}