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"
|
"Error": "6h0m0s..6h0m0s"
|
||||||
},
|
},
|
||||||
"Clients": {
|
"Clients": {
|
||||||
"pa": {
|
|
||||||
"States": [
|
|
||||||
"OH"
|
|
||||||
],
|
|
||||||
"IDs": {
|
|
||||||
"Matrix": "@belandbroc:matrix.org"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"broc": {
|
"broc": {
|
||||||
"States": [
|
"States": [
|
||||||
"OH"
|
"OH"
|
||||||
|
|
@ -20,7 +12,7 @@
|
||||||
"IDs": {
|
"IDs": {
|
||||||
"Matrix": "@belandbroc:matrix.org"
|
"Matrix": "@belandbroc:matrix.org"
|
||||||
},
|
},
|
||||||
"PauseUntil": 5641960674
|
"PauseUntil": 5641848000
|
||||||
},
|
},
|
||||||
"caleb": {
|
"caleb": {
|
||||||
"States": [
|
"States": [
|
||||||
|
|
@ -28,7 +20,17 @@
|
||||||
],
|
],
|
||||||
"IDs": {
|
"IDs": {
|
||||||
"Matrix": "@belandbroc:matrix.org"
|
"Matrix": "@belandbroc:matrix.org"
|
||||||
}
|
},
|
||||||
|
"PauseUntil": -62135596800
|
||||||
|
},
|
||||||
|
"pa": {
|
||||||
|
"States": [
|
||||||
|
"OH"
|
||||||
|
],
|
||||||
|
"IDs": {
|
||||||
|
"Matrix": "@belandbroc:matrix.org"
|
||||||
|
},
|
||||||
|
"PauseUntil": -62135596800
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Storage": [
|
"Storage": [
|
||||||
|
|
|
||||||
63
main.go
63
main.go
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -18,6 +19,12 @@ import (
|
||||||
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if err := config.Refresh(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := matrixrecv(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
lock := &sync.Mutex{}
|
lock := &sync.Mutex{}
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
|
@ -50,6 +57,7 @@ func matrixrecv() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func() {
|
func() {
|
||||||
|
log.Printf("looking for states")
|
||||||
states := map[string]map[config.State]struct{}{}
|
states := map[string]map[config.State]struct{}{}
|
||||||
for _, msg := range messages {
|
for _, msg := range messages {
|
||||||
if _, ok := states[msg.Sender]; ok {
|
if _, ok := states[msg.Sender]; ok {
|
||||||
|
|
@ -62,9 +70,58 @@ func matrixrecv() error {
|
||||||
}
|
}
|
||||||
setNewStates(states)
|
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
|
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{}) {
|
func setNewStates(states map[string]map[config.State]struct{}) {
|
||||||
if len(states) == 0 {
|
if len(states) == 0 {
|
||||||
return
|
return
|
||||||
|
|
@ -124,6 +181,7 @@ func _main() error {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
if config.Get().Once {
|
if config.Get().Once {
|
||||||
|
time.Sleep(time.Second)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -218,3 +276,8 @@ func sendNewStates(client string, states []config.State) error {
|
||||||
sender := message.NewMatrix()
|
sender := message.NewMatrix()
|
||||||
return sender.Send(fmt.Sprintf("%s: now searching for loads from: %+v", client, states))
|
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
|
package message
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"local/truckstop/config"
|
"local/truckstop/config"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
)
|
)
|
||||||
|
|
@ -34,12 +36,15 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||||
if m.mock {
|
if m.mock {
|
||||||
log.Printf("matrix.Receive()")
|
log.Printf("matrix.Receive()")
|
||||||
messages := make([]Message, 0)
|
messages := make([]Message, 0)
|
||||||
for k := range config.Clients() {
|
for k := range config.Get().Clients {
|
||||||
messages = append(messages, Message{Sender: k, Content: "OH"})
|
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
|
return messages, nil
|
||||||
}
|
}
|
||||||
clients := config.Clients()
|
clients := config.Get().Clients
|
||||||
matrixIDs := map[string]struct{}{}
|
matrixIDs := map[string]struct{}{}
|
||||||
for k := range clients {
|
for k := range clients {
|
||||||
matrixIDs[clients[k].IDs.Matrix] = struct{}{}
|
matrixIDs[clients[k].IDs.Matrix] = struct{}{}
|
||||||
|
|
@ -69,7 +74,7 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for i := range messages {
|
for i := range messages {
|
||||||
for k, v := range config.Clients() {
|
for k, v := range config.Get().Clients {
|
||||||
if v.IDs.Matrix == messages[i].Sender {
|
if v.IDs.Matrix == messages[i].Sender {
|
||||||
messages[i].Sender = k
|
messages[i].Sender = k
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue