Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34a5a3dfc8 | ||
|
|
92beedc955 | ||
|
|
cfd1a177dd | ||
|
|
db1838b3ab | ||
|
|
ec030676ea | ||
|
|
3b4dd1ba70 | ||
|
|
003ffb1231 | ||
|
|
cdabce7a56 | ||
|
|
0ab534624a | ||
|
|
b3ce49788b | ||
|
|
bbe839bb88 | ||
|
|
60f19968e3 |
@@ -87,7 +87,7 @@ func (fe FastExact) _login(username, password string, db storage.DB) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b, _ := ioutil.ReadAll(resp.Body)
|
b, _ := ioutil.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("bad status logging into fast exact: %d: %s", resp.StatusCode, b)
|
return fmt.Errorf("bad status logging into fast exact: %d: %s", resp.StatusCode, b)
|
||||||
@@ -152,6 +152,7 @@ func (fe FastExact) searchOneZip(zip string) ([]Job, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
return fe.parse(resp)
|
return fe.parse(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +166,7 @@ func (fe FastExact) searchOneState(state config.State) ([]Job, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
return fe.parse(resp)
|
return fe.parse(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -224,14 +224,26 @@ func (ntg NTGVision) SearchZips(zips []string) ([]Job, error) {
|
|||||||
func (ntg NTGVision) workingHours(now time.Time) bool {
|
func (ntg NTGVision) workingHours(now time.Time) bool {
|
||||||
// TODO assert M-F 9-4 EST
|
// TODO assert M-F 9-4 EST
|
||||||
now = now.In(time.FixedZone("EST", -5*60*60))
|
now = now.In(time.FixedZone("EST", -5*60*60))
|
||||||
logtr.Debugf("ntg.workingHours: %s: weekday=%v (sun=%v, sat=%v), hour=%v (ok=9..16)", now.String(), now.Weekday(), time.Sunday, time.Saturday, now.Hour())
|
working := config.Get().Brokers.NTG.Working
|
||||||
switch now.Weekday() {
|
logtr.Debugf("ntg.workingHours: now=%s, weekday=%v, hour=%v (ok=%+v)", now.String(), now.Weekday(), now.Hour(), working)
|
||||||
case time.Sunday, time.Saturday:
|
if ok := func() bool {
|
||||||
|
for _, hr := range working.Hours {
|
||||||
|
if now.Hour() == hr {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}(); !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
switch now.Hour() {
|
if ok := func() bool {
|
||||||
case 9, 10, 11, 12, 13, 14, 15, 16:
|
for _, weekday := range working.Weekdays {
|
||||||
default:
|
if now.Weekday() == time.Weekday(weekday) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}(); !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@@ -362,6 +374,9 @@ func (ntg NTGVision) _searchStates(states []config.State) (io.ReadCloser, error)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
b, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
resp.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
b, _ := ioutil.ReadAll(resp.Body)
|
b, _ := ioutil.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
package broker
|
package broker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"local/truckstop/config"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWorkingHoursNTG(t *testing.T) {
|
func TestWorkingHoursNTG(t *testing.T) {
|
||||||
|
os.Setenv("CONFIG", "../config.json")
|
||||||
|
if err := config.Refresh(nil); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
ntg := NTGVision{}
|
ntg := NTGVision{}
|
||||||
if !ntg.workingHours(time.Date(2022, 1, 27, 12, 0, 0, 0, time.FixedZone("MST", -7*60*60))) {
|
if !ntg.workingHours(time.Date(2022, 1, 27, 12, 0, 0, 0, time.FixedZone("MST", -7*60*60))) {
|
||||||
t.Fatal("noon MST not ok")
|
t.Fatal("noon MST not ok")
|
||||||
|
|||||||
@@ -79,6 +79,10 @@
|
|||||||
"UseZips": true,
|
"UseZips": true,
|
||||||
"RadiusMiles": 200,
|
"RadiusMiles": 200,
|
||||||
"NTG": {
|
"NTG": {
|
||||||
|
"Working": {
|
||||||
|
"Hours": [6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
|
||||||
|
"Weekdays": [1, 2, 3, 4, 5]
|
||||||
|
},
|
||||||
"Enabled": false,
|
"Enabled": false,
|
||||||
"JobInfo": true,
|
"JobInfo": true,
|
||||||
"Mock": true,
|
"Mock": true,
|
||||||
|
|||||||
@@ -83,6 +83,10 @@
|
|||||||
"Password": "p"
|
"Password": "p"
|
||||||
},
|
},
|
||||||
"NTG": {
|
"NTG": {
|
||||||
|
"Working": {
|
||||||
|
"Hours": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
|
||||||
|
"Weekdays": [0, 1, 2, 3, 4, 5, 6]
|
||||||
|
},
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"JobInfo": true,
|
"JobInfo": true,
|
||||||
"Mock": true,
|
"Mock": true,
|
||||||
|
|||||||
@@ -67,6 +67,10 @@ type Config struct {
|
|||||||
UseZips bool
|
UseZips bool
|
||||||
RadiusMiles int
|
RadiusMiles int
|
||||||
NTG struct {
|
NTG struct {
|
||||||
|
Working struct {
|
||||||
|
Hours []int
|
||||||
|
Weekdays []int
|
||||||
|
}
|
||||||
Enabled bool
|
Enabled bool
|
||||||
JobInfo bool
|
JobInfo bool
|
||||||
Mock bool
|
Mock bool
|
||||||
|
|||||||
17
main.go
17
main.go
@@ -126,7 +126,9 @@ func matrixrecv() error {
|
|||||||
logtr.Errorf("failed to mark state gathered @%s: %v", key, err)
|
logtr.Errorf("failed to mark state gathered @%s: %v", key, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setNewZips(zips)
|
if config.Get().Brokers.UseZips {
|
||||||
|
setNewZips(zips)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
func() {
|
func() {
|
||||||
logtr.Verbosef("looking for states")
|
logtr.Verbosef("looking for states")
|
||||||
@@ -150,7 +152,9 @@ func matrixrecv() error {
|
|||||||
logtr.Errorf("failed to mark state gathered @%s: %v", key, err)
|
logtr.Errorf("failed to mark state gathered @%s: %v", key, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setNewStates(states)
|
if !config.Get().Brokers.UseZips {
|
||||||
|
setNewStates(states)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
func() {
|
func() {
|
||||||
logtr.Verbosef("looking for radius")
|
logtr.Verbosef("looking for radius")
|
||||||
@@ -371,7 +375,7 @@ func __main() error {
|
|||||||
logtr.Errorf("failed _main: %v", err)
|
logtr.Errorf("failed _main: %v", err)
|
||||||
}
|
}
|
||||||
if config.Get().Once {
|
if config.Get().Once {
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -522,15 +526,12 @@ func updateDeadJobs(jobs []broker.Job) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
if err := message.NewMatrix().Remove(recorded.MatrixID); err != nil {
|
|
||||||
logtr.Debugf("failed to remove matrix: %v: %v", recorded.MatrixID, err)
|
|
||||||
}
|
|
||||||
if err := db.Set(listEntry, nil); err != nil {
|
if err := db.Set(listEntry, nil); err != nil {
|
||||||
logtr.Debugf("failed to remove db: %v: %v", listEntry, err)
|
logtr.Debugf("failed to remove db: %v: %v", listEntry, err)
|
||||||
}
|
}
|
||||||
for _, imageid := range recorded.MatrixImageIDs {
|
for _, imageid := range recorded.MatrixImageIDs {
|
||||||
if err := message.NewMatrix().Remove(imageid); err != nil {
|
if err := message.NewMatrix().Update(imageid, "<job no longer available>"); err != nil {
|
||||||
logtr.Debugf("failed to remove matrix image: %v: %v", imageid, err)
|
logtr.Debugf("failed to update matrix image: %v: %v", imageid, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func uploadImage(b []byte) (string, error) {
|
|||||||
request.Header.Set("Authorization", "Bearer "+images.AccessToken)
|
request.Header.Set("Authorization", "Bearer "+images.AccessToken)
|
||||||
request.Header.Set("Content-Type", writer.FormDataContentType())
|
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
|
|
||||||
c := &http.Client{Timeout: time.Minute}
|
c := &http.Client{Timeout: time.Minute * 5}
|
||||||
response, err := c.Do(request)
|
response, err := c.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package message
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"local/truckstop/config"
|
"local/truckstop/config"
|
||||||
@@ -44,8 +45,27 @@ func newMatrix(conf config.Matrix, cont string) Matrix {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Matrix) closeclient(client *gomatrix.Client) {
|
||||||
|
go func() {
|
||||||
|
client.Client.CloseIdleConnections()
|
||||||
|
client.StopSync()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func (m Matrix) getclient() (*gomatrix.Client, error) {
|
func (m Matrix) getclient() (*gomatrix.Client, error) {
|
||||||
return gomatrix.NewClient(m.homeserver, m.username, m.token)
|
client, err := gomatrix.NewClient(m.homeserver, m.username, m.token)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
client.Client = &http.Client{
|
||||||
|
Timeout: time.Minute * 5,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Matrix) Continuation() string {
|
func (m Matrix) Continuation() string {
|
||||||
@@ -79,6 +99,7 @@ func (m *Matrix) Receive() ([]Message, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer m.closeclient(c)
|
||||||
messages := make([]Message, 0)
|
messages := make([]Message, 0)
|
||||||
result, err := c.Messages(m.room, "999999999999999999", m.Continuation(), 'b', 50)
|
result, err := c.Messages(m.room, "999999999999999999", m.Continuation(), 'b', 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -134,6 +155,7 @@ func (m Matrix) Remove(id string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer m.closeclient(c)
|
||||||
_, err = c.RedactEvent(m.room, id, &gomatrix.ReqRedact{Reason: "stale"})
|
_, err = c.RedactEvent(m.room, id, &gomatrix.ReqRedact{Reason: "stale"})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -147,6 +169,7 @@ func (m Matrix) Update(id, text string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer m.closeclient(c)
|
||||||
type MRelatesTo struct {
|
type MRelatesTo struct {
|
||||||
EventID string `json:"event_id"`
|
EventID string `json:"event_id"`
|
||||||
RelType string `json:"rel_type"`
|
RelType string `json:"rel_type"`
|
||||||
@@ -190,6 +213,7 @@ func (m Matrix) SendTracked(text string) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
defer m.closeclient(c)
|
||||||
resp, err := c.SendText(m.room, text)
|
resp, err := c.SendText(m.room, text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -225,6 +249,7 @@ func (m Matrix) SendImageTracked(uri string) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
defer m.closeclient(c)
|
||||||
mediaUpload, err := c.UploadToContentRepo(bytes.NewReader(b), "image/jpeg", int64(len(b)))
|
mediaUpload, err := c.UploadToContentRepo(bytes.NewReader(b), "image/jpeg", int64(len(b)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
Reference in New Issue
Block a user