-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
188 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ go.work | |
tmp.go | ||
|
||
config.yaml | ||
|
||
scc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"scc/config" | ||
"scc/screen" | ||
"time" | ||
) | ||
|
||
type tapReponse struct { | ||
Orders []screen.TapOrder `json:"orders"` | ||
} | ||
|
||
var ( | ||
tapURL = config.GetConfig().Tap.URL | ||
timestampLayout = config.GetConfig().Tap.TimestampLayout | ||
lastOrderTimestamp = time.Time{} | ||
) | ||
|
||
func runTapRequests(app *screen.ScreenApp) { | ||
for true { | ||
recentOrders, err := tapGetRecentOrders() | ||
if err != nil { | ||
log.Printf("Error: Unable to get recent order: %s\n", err) | ||
} | ||
for _, order := range recentOrders.Orders { | ||
timestamp, err := time.Parse(timestampLayout, order.OrderCreatedAt) | ||
if err != nil { | ||
log.Printf("Error: Unable to parse timestamp: %s\n", err) | ||
} | ||
|
||
if order.ProductCategory == "beverages" && timestamp.After(lastOrderTimestamp) { | ||
app.Tap.Update(&order) | ||
lastOrderTimestamp = timestamp | ||
} | ||
} | ||
|
||
time.Sleep(1 * time.Minute) | ||
} | ||
} | ||
|
||
func tapGetRecentOrders() (*tapReponse, error) { | ||
req, err := http.NewRequest("GET", tapURL, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Accept", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
response := &tapReponse{} | ||
if err := json.NewDecoder(resp.Body).Decode(response); err != nil { | ||
return nil, err | ||
} | ||
|
||
return response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.