-
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.
Merge pull request #10 from ZeusWPI/tap
Tap
- Loading branch information
Showing
14 changed files
with
328 additions
and
101 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 |
---|---|---|
|
@@ -20,4 +20,8 @@ | |
# Go workspace file | ||
go.work | ||
|
||
tmp.go | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cammie: | ||
blocked_names: [] | ||
blocked_ips: [] | ||
max_message_length: | ||
spotify: | ||
client_id: | ||
client_secret: |
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,51 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"sync" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type cammieConfig struct { | ||
BlockedNames []string `yaml:"blocked_names"` | ||
BlockedIps []string `yaml:"blocked_ips"` | ||
MaxMessageLength int `yaml:"max_message_length"` | ||
} | ||
|
||
type spotifyConfig struct { | ||
ClientID string `yaml:"client_id"` | ||
ClientSecret string `yaml:"client_secret"` | ||
} | ||
|
||
type tapConfig struct { | ||
URL string `yaml:"url"` | ||
TimestampLayout string `yaml:"timestamp_layout"` | ||
Beers []string `yaml:"beer"` | ||
} | ||
|
||
type Config struct { | ||
Cammie cammieConfig `yaml:"cammie"` | ||
Spotify spotifyConfig `yaml:"spotify"` | ||
Tap tapConfig `yaml:"tap"` | ||
} | ||
|
||
var ( | ||
configInstance *Config | ||
once sync.Once | ||
) | ||
|
||
func GetConfig() *Config { | ||
once.Do(func() { | ||
configInstance = &Config{} | ||
data, err := os.ReadFile("config.yaml") | ||
if err != nil { | ||
log.Fatalf("Failed to read config file: %v", err) | ||
} | ||
if err := yaml.Unmarshal(data, configInstance); err != nil { | ||
log.Fatalf("Failed to unmarshal config: %v", err) | ||
} | ||
}) | ||
return configInstance | ||
} |
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.