get matrix messages for all clients because commands, refresh config and matrix recv on boot, assert broc works for never recv
parent
8f681c7927
commit
f90ec3917b
22
config.json
22
config.json
|
|
@ -5,14 +5,6 @@
|
|||
"Error": "6h0m0s..6h0m0s"
|
||||
},
|
||||
"Clients": {
|
||||
"pa": {
|
||||
"States": [
|
||||
"OH"
|
||||
],
|
||||
"IDs": {
|
||||
"Matrix": "@belandbroc:matrix.org"
|
||||
}
|
||||
},
|
||||
"broc": {
|
||||
"States": [
|
||||
"OH"
|
||||
|
|
@ -20,7 +12,7 @@
|
|||
"IDs": {
|
||||
"Matrix": "@belandbroc:matrix.org"
|
||||
},
|
||||
"PauseUntil": 5641960674
|
||||
"PauseUntil": 5641848000
|
||||
},
|
||||
"caleb": {
|
||||
"States": [
|
||||
|
|
@ -28,7 +20,17 @@
|
|||
],
|
||||
"IDs": {
|
||||
"Matrix": "@belandbroc:matrix.org"
|
||||
}
|
||||
},
|
||||
"PauseUntil": -62135596800
|
||||
},
|
||||
"pa": {
|
||||
"States": [
|
||||
"OH"
|
||||
],
|
||||
"IDs": {
|
||||
"Matrix": "@belandbroc:matrix.org"
|
||||
},
|
||||
"PauseUntil": -62135596800
|
||||
}
|
||||
},
|
||||
"Storage": [
|
||||
|
|
|
|||
63
main.go
63
main.go
|
|
@ -11,6 +11,7 @@ import (
|
|||
"log"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -18,6 +19,12 @@ import (
|
|||
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
||||
|
||||
func main() {
|
||||
if err := config.Refresh(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := matrixrecv(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
lock := &sync.Mutex{}
|
||||
go func() {
|
||||
for {
|
||||
|
|
@ -50,6 +57,7 @@ func matrixrecv() error {
|
|||
return err
|
||||
}
|
||||
func() {
|
||||
log.Printf("looking for states")
|
||||
states := map[string]map[config.State]struct{}{}
|
||||
for _, msg := range messages {
|
||||
if _, ok := states[msg.Sender]; ok {
|
||||
|
|
@ -62,9 +70,58 @@ func matrixrecv() error {
|
|||
}
|
||||
setNewStates(states)
|
||||
}()
|
||||
func() {
|
||||
log.Printf("looking for pauses")
|
||||
pauses := map[string]time.Time{}
|
||||
for _, msg := range messages {
|
||||
if _, ok := pauses[msg.Sender]; ok {
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(msg.Content, "pause until") {
|
||||
continue
|
||||
}
|
||||
t, err := time.ParseInLocation(
|
||||
"2006-01-02",
|
||||
strings.TrimSpace(strings.TrimPrefix(msg.Content, "pause until")),
|
||||
time.Local,
|
||||
)
|
||||
if err == nil {
|
||||
pauses[msg.Sender] = t
|
||||
}
|
||||
}
|
||||
setNewPauses(pauses)
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func setNewPauses(pauses map[string]time.Time) {
|
||||
log.Printf("set new pauses: %+v", pauses)
|
||||
if len(pauses) == 0 {
|
||||
return
|
||||
}
|
||||
conf := *config.Get()
|
||||
changed := map[string]time.Time{}
|
||||
for client, pause := range pauses {
|
||||
clientconf := conf.Clients[client]
|
||||
if clientconf.PauseUntil.Get().Unix() == pause.Unix() {
|
||||
continue
|
||||
}
|
||||
clientconf.PauseUntil = config.Time(pause)
|
||||
conf.Clients[client] = clientconf
|
||||
changed[client] = pause
|
||||
}
|
||||
if len(changed) == 0 {
|
||||
return
|
||||
}
|
||||
log.Printf("updating config new pauses: %+v", conf)
|
||||
config.Set(conf)
|
||||
for client, pause := range changed {
|
||||
if err := sendNewPause(client, pause); err != nil {
|
||||
log.Printf("failed to send new pause %s/%+v: %v", client, pause, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setNewStates(states map[string]map[config.State]struct{}) {
|
||||
if len(states) == 0 {
|
||||
return
|
||||
|
|
@ -124,6 +181,7 @@ func _main() error {
|
|||
log.Println(err)
|
||||
}
|
||||
if config.Get().Once {
|
||||
time.Sleep(time.Second)
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
|
|
@ -218,3 +276,8 @@ func sendNewStates(client string, states []config.State) error {
|
|||
sender := message.NewMatrix()
|
||||
return sender.Send(fmt.Sprintf("%s: now searching for loads from: %+v", client, states))
|
||||
}
|
||||
|
||||
func sendNewPause(client string, pause time.Time) error {
|
||||
sender := message.NewMatrix()
|
||||
return sender.Send(fmt.Sprintf("%s: not searching for loads until %s", client, pause.Format("2006-01-02")))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/truckstop/config"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/gomatrix"
|
||||
)
|
||||
|
|
@ -34,12 +36,15 @@ func (m Matrix) Receive() ([]Message, error) {
|
|||
if m.mock {
|
||||
log.Printf("matrix.Receive()")
|
||||
messages := make([]Message, 0)
|
||||
for k := range config.Clients() {
|
||||
for k := range config.Get().Clients {
|
||||
messages = append(messages, Message{Sender: k, Content: "OH"})
|
||||
if k == "broc" {
|
||||
messages = append(messages, Message{Sender: k, Content: "pause until 2148-10-" + fmt.Sprint(time.Now().Unix()%28)})
|
||||
}
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
clients := config.Clients()
|
||||
clients := config.Get().Clients
|
||||
matrixIDs := map[string]struct{}{}
|
||||
for k := range clients {
|
||||
matrixIDs[clients[k].IDs.Matrix] = struct{}{}
|
||||
|
|
@ -69,7 +74,7 @@ func (m Matrix) Receive() ([]Message, error) {
|
|||
return nil, err
|
||||
}
|
||||
for i := range messages {
|
||||
for k, v := range config.Clients() {
|
||||
for k, v := range config.Get().Clients {
|
||||
if v.IDs.Matrix == messages[i].Sender {
|
||||
messages[i].Sender = k
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue