diff --git a/broker/ntgvision.go b/broker/ntgvision.go index a7cf391..0da0f9c 100644 --- a/broker/ntgvision.go +++ b/broker/ntgvision.go @@ -16,6 +16,7 @@ import ( type NTGVision struct { searcher interface { search(states []config.State) (io.ReadCloser, error) + searchJob(id int64) (io.ReadCloser, error) } } @@ -43,13 +44,14 @@ type ntgVisionJobInfo struct { Instructions string `json:"instructions"` IsDropTrailer bool `json:"isDropTrailer"` } `json:"stopinfos"` - PayUpTo float32 `json:"payUpTo"` - LoadState string `json:"loadStatus"` + PayUpTo float32 `json:"payUpTo"` + TotalCarrierRate float32 `json:"totalCarrierRate"` + LoadState string `json:"loadStatus"` //CanBidNow bool `json:"canBidNow"` } func (ji ntgVisionJobInfo) IsZero() bool { - return len(ji.StopsInfo) == 0 && ji.PayUpTo == 0 && ji.LoadState == "" + return len(ji.StopsInfo) == 0 && ji.TotalCarrierRate == 0 && ji.PayUpTo == 0 && ji.LoadState == "" } func (ji ntgVisionJobInfo) String() string { @@ -60,6 +62,9 @@ func (ji ntgVisionJobInfo) String() string { if ji.PayUpTo != 0 { out = fmt.Sprintf("\nPAYS:%v\n%s", ji.PayUpTo, out) } + if ji.TotalCarrierRate != 0 { + out = fmt.Sprintf("%s Total_Carrier_Rate:%v", out, ji.TotalCarrierRate) + } if ji.LoadState != "" { out = fmt.Sprintf("%s Auction:%s", out, ji.LoadState) } @@ -90,13 +95,8 @@ func (ntgJob *ntgVisionJob) JobInfo() (ntgVisionJobInfo, error) { } else if err := json.Unmarshal(b, &ntgJob.jobinfo); err == nil { return ntgJob.jobinfo, nil } - ji, err := ntgJob.jobInfo() - if err == ErrNoAuth { - if err := NewNTGVision().refreshAuth(); err != nil { - return ntgVisionJobInfo{}, err - } - ji, err = ntgJob.jobInfo() - } + ntg := NewNTGVision() + ji, err := ntg.SearchJob(ntgJob.ID) if err == nil { ntgJob.jobinfo = ji b, err := json.Marshal(ntgJob.jobinfo) @@ -107,26 +107,24 @@ func (ntgJob *ntgVisionJob) JobInfo() (ntgVisionJobInfo, error) { return ji, err } -func (ntgJob *ntgVisionJob) jobInfo() (ntgVisionJobInfo, error) { +func (ntg NTGVision) searchJob(id int64) (io.ReadCloser, error) { time.Sleep(config.Get().Interval.JobInfo.Get()) - request, err := http.NewRequest(http.MethodGet, fmt.Sprintf(`https://ntgvision.com/api/v1/load/LoadDetails?loadId=%v`, ntgJob.ID), nil) + request, err := http.NewRequest(http.MethodGet, fmt.Sprintf(`https://ntgvision.com/api/v1/load/LoadDetails?loadId=%v`, id), nil) if err != nil { - return ntgVisionJobInfo{}, err + return nil, err } setNTGHeaders(request) resp, err := do(request) if err != nil { - return ntgVisionJobInfo{}, err + return nil, err } defer resp.Body.Close() b, _ := ioutil.ReadAll(resp.Body) log.Printf("fetch ntg job info %+v: %d: %s", request, resp.StatusCode, b) if resp.StatusCode > 400 && resp.StatusCode < 500 && resp.StatusCode != 404 && resp.StatusCode != 410 { - return ntgVisionJobInfo{}, ErrNoAuth + return nil, ErrNoAuth } - var result ntgVisionJobInfo - err = json.Unmarshal(b, &result) - return result, err + return io.NopCloser(bytes.NewReader(b)), nil } func (ntgJob *ntgVisionJob) Job() Job { @@ -162,6 +160,9 @@ func (ntgJob *ntgVisionJob) Job() Job { func NewNTGVision() NTGVision { ntgv := NTGVision{} ntgv.searcher = ntgv + if config.Get().Brokers.NTG.Mock { + ntgv = ntgv.WithMock() + } return ntgv } @@ -170,6 +171,22 @@ func (ntg NTGVision) WithMock() NTGVision { return ntg } +func (ntg NTGVision) SearchJob(id int64) (ntgVisionJobInfo, error) { + rc, err := ntg.searcher.searchJob(id) + if err == ErrNoAuth { + if err := ntg.refreshAuth(); err != nil { + return ntgVisionJobInfo{}, err + } + rc, err = ntg.searcher.searchJob(id) + } + if err != nil { + return ntgVisionJobInfo{}, err + } + var result ntgVisionJobInfo + err = json.NewDecoder(rc).Decode(&result) + return result, err +} + func (ntg NTGVision) Search(states []config.State) ([]Job, error) { rc, err := ntg.searcher.search(states) if err != nil { diff --git a/broker/ntgvision_mock.go b/broker/ntgvision_mock.go index 90cc588..0c75681 100644 --- a/broker/ntgvision_mock.go +++ b/broker/ntgvision_mock.go @@ -20,3 +20,9 @@ func (ntgm NTGVisionMock) search(states []config.State) (io.ReadCloser, error) { b, err := ioutil.ReadFile(path) return io.NopCloser(bytes.NewReader(b)), err } + +func (ntgm NTGVisionMock) searchJob(id int64) (io.ReadCloser, error) { + path := path.Join(os.Getenv("GOPATH"), "src", "local", "truckstop", "broker", "testdata", "ntgvision_jobinfo_response.json") + b, err := ioutil.ReadFile(path) + return io.NopCloser(bytes.NewReader(b)), err +} diff --git a/broker/testdata/ntgvision_jobinfo_response.json b/broker/testdata/ntgvision_jobinfo_response.json new file mode 100644 index 0000000..977cb2d --- /dev/null +++ b/broker/testdata/ntgvision_jobinfo_response.json @@ -0,0 +1,84 @@ +{ + "headerSummary": "A to B", + "loadId": 123, + "miles": 123, + "weight": 123, + "temp": null, + "equipment": "equipment", + "categoryName": "category", + "categoryLabel": "category generic", + "cargoInformation": [ + "carto info" + ], + "loadRequirements": [ + "load requirements" + ], + "stopInfos": [ + { + "loadInfoIds": [ + 123 + ], + "stopDateTime": "0001-01-01T00:00:00", + "isPickup": true, + "location": "A", + "stopDate": "Friday, 01/14/22", + "stopHours": "10:00 - 12:00", + "appointmentTime": "", + "appointmentType": "FCFS", + "instructions": "instruction", + "isDropTrailer": false, + "shipperName": "client", + "processedStopDate": "" + }, + { + "loadInfoIds": [ + 123 + ], + "stopDateTime": "0001-01-01T00:00:00", + "isPickup": false, + "location": "B", + "stopDate": "Monday, 01/17/22", + "stopHours": "09:00 - 10:00", + "appointmentTime": "09:00", + "appointmentType": "APPT", + "instructions": "instructions", + "isDropTrailer": false, + "shipperName": "client", + "processedStopDate": "" + } + ], + "drayStopInfos": null, + "rateInfo": null, + "isDray": false, + "equipmentGroupId": 8, + "totalCarrierRate": 2600, + "payUpTo": 2700, + "firstLoadInfoId": 123, + "loadStatus": "ACTIVE", + "hasBlockingAlert": false, + "customerLoadBlocked": false, + "canSeeRate": false, + "canBookNow": false, + "canBidNow": true, + "buttonData": { + "useBidDialog": false, + "lastBidAmount": null, + "remainingBids": 1, + "carrierPhoneNumber": null, + "carrierPhoneExtension": null + }, + "stopData": [ + { + "addr": "IDX NORTH CAROLINA", + "city": "Washington", + "state": "NC", + "zip": "27889" + }, + { + "addr": "hibbett sports", + "city": "Abilene", + "state": "TX", + "zip": "79606" + } + ] +} diff --git a/broker/testdata/ntgvision_response.json b/broker/testdata/ntgvision_response.json index aabff55..0422e0e 100644 --- a/broker/testdata/ntgvision_response.json +++ b/broker/testdata/ntgvision_response.json @@ -2,12 +2,12 @@ { "id": 4650337, "sDate": "01/12/22", - "sCity": "Columbus", - "sState": "OH", + "sCity": "Advance", + "sState": "NC", "sdh": null, "cDate": "01/13/22", - "cCity": "Jamaica", - "cState": "NY", + "cCity": "Sacramento", + "cState": "CA", "cdh": null, "stopCnt": 2, "miles": 578, @@ -23,8 +23,8 @@ "sState": "NC", "sdh": null, "cDate": "01/13/22", - "cCity": "Atlanta", - "cState": "GA", + "cCity": "Winston-Salem", + "cState": "NC", "cdh": null, "stopCnt": 2, "miles": 378, diff --git a/config.json b/config.json index 3cdbdb9..e6d9ce0 100644 --- a/config.json +++ b/config.json @@ -27,31 +27,12 @@ "Clients": { "bel": { "States": [ - "IL" + "OH" ], "IDs": { "Matrix": "@bel:m.bltrucks.top" }, "Available": 1512328400 - }, - "broc": { - "States": [ - "FL", - "NC" - ], - "IDs": { - "Matrix": "@broc:m.bltrucks.top" - }, - "Available": 5642452800 - }, - "pa": { - "States": [ - "NC" - ], - "IDs": { - "Matrix": "@ron:m.bltrucks.top" - }, - "Available": -62135596800 } }, "Storage": [ @@ -61,7 +42,7 @@ "Message": { "Matrix": { "ReceiveEnabled": true, - "Mock": false, + "Mock": true, "Homeserver": "https://m.bltrucks.top", "Username": "@bot.m.bltrucks.top", "Token": "mvDWB96KXMF8XhOam8EC5XVdQvSEw0CDeClcSWocBcYkwZX3FPNWZ5uOnQk2EmT1cjpzfeuD7gDYPPjOuyZlI3bE9TE35UjNOlZgi0Tugm25s91iVsbIF6kMZsCIhVMSmEf6w3jxX6wQYOWvmDZ4mu6f5c8wr221EMDcOpEzQV09d1zuBSWgKLBgjqAkYHJZ5dTRIWpEDpPgujhOFZa2ld1HiAOxrJKlIrlfDBN0CUsTlGOGplujDAr4VtpFzNRS", diff --git a/main.go b/main.go index c211343..f11f99e 100644 --- a/main.go +++ b/main.go @@ -294,9 +294,6 @@ func once() error { func getJobs() ([]broker.Job, error) { states := config.AllStates() ntg := broker.NewNTGVision() - if config.Get().Brokers.NTG.Mock { - ntg = ntg.WithMock() - } brokers := []broker.Broker{ntg} jobs := []broker.Job{} for _, broker := range brokers {