Start with some stuff
commit
af5786b04b
|
@ -0,0 +1,4 @@
|
|||
status.toml
|
||||
pkg/
|
||||
src/github.com/
|
||||
statusscreen
|
|
@ -0,0 +1,11 @@
|
|||
package statusscreen
|
||||
|
||||
// Config is a struct with my config
|
||||
type Config struct {
|
||||
SL struct {
|
||||
APIKey string
|
||||
RefreshDelay int
|
||||
SiteID int
|
||||
APIURL string
|
||||
}
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
package statusscreen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SL is my SL struct
|
||||
type SL struct {
|
||||
Config *Config
|
||||
}
|
||||
|
||||
// SLResponse is a struct to get the interesting data from API Responses
|
||||
type SLResponse struct {
|
||||
StatusCode int
|
||||
ExecutionTime int
|
||||
|
||||
ResponseData struct {
|
||||
LatestUpdate string
|
||||
Metros []struct {
|
||||
LineNumber string
|
||||
Destination string
|
||||
TimeTabledDateTime string
|
||||
ExpectedDateTime string
|
||||
TransportMode string
|
||||
}
|
||||
StopPointDeviations []struct {
|
||||
Deviation struct {
|
||||
Text string
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetOutput returns a rendered result of this module
|
||||
func (sl *SL) GetOutput() string {
|
||||
var b bytes.Buffer
|
||||
|
||||
// Prepare table
|
||||
table := tablewriter.NewWriter(&b)
|
||||
table.SetHeader([]string{
|
||||
"Line",
|
||||
"Destination",
|
||||
"DisplayTime",
|
||||
"Timetable",
|
||||
"Expcted",
|
||||
})
|
||||
|
||||
sldata := sl.getTimeTable()
|
||||
|
||||
for _, v := range sldata.ResponseData.Metros {
|
||||
table.Append([]string{
|
||||
v.LineNumber,
|
||||
v.Destination,
|
||||
sl.formatDisplayTime(v.ExpectedDateTime),
|
||||
v.TimeTabledDateTime[11:], // Remove YYYY-MM-DDT from the beginning of the string
|
||||
v.ExpectedDateTime[11:], // Remove YYYY-MM-DDT from the beginning of the string
|
||||
})
|
||||
}
|
||||
|
||||
table.Render()
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (sl *SL) getTimeTable() SLResponse {
|
||||
// Construct URL with APIKey and Site
|
||||
url := fmt.Sprintf(sl.Config.SL.APIURL, sl.Config.SL.APIKey, sl.Config.SL.SiteID)
|
||||
|
||||
// Set up http client to talk to SL
|
||||
client := http.Client{
|
||||
Timeout: time.Second * 5,
|
||||
}
|
||||
|
||||
// Do request
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Read response
|
||||
body, readErr := ioutil.ReadAll(resp.Body)
|
||||
if readErr != nil {
|
||||
log.Fatal(readErr)
|
||||
}
|
||||
|
||||
// Parse response from SL
|
||||
parsedResponse := SLResponse{}
|
||||
parseErr := json.Unmarshal(body, &parsedResponse)
|
||||
if parseErr != nil {
|
||||
log.Fatal(parseErr)
|
||||
}
|
||||
|
||||
return parsedResponse
|
||||
}
|
||||
|
||||
func (sl *SL) formatDisplayTime(expected string) string {
|
||||
myTimeZone, _ := time.Now().Zone()
|
||||
|
||||
then, err := time.Parse("2006-01-02T15:04:05 MST", expected+" "+myTimeZone)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
timeLeft := time.Since(then).Seconds() * -1
|
||||
|
||||
if timeLeft <= 60 && timeLeft > 0 {
|
||||
return fmt.Sprintf("%d sec", int(timeLeft))
|
||||
}
|
||||
|
||||
if timeLeft <= 3600 && timeLeft > 0 {
|
||||
return fmt.Sprintf("%d min", int(timeLeft)/60)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%02d:%02d:%02d", then.Hour(), then.Minute(), then.Second())
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/BurntSushi/toml"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"statusscreen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config := getConfig("status.toml")
|
||||
|
||||
sl := statusscreen.SL{&config}
|
||||
|
||||
fmt.Println(sl.GetOutput())
|
||||
}
|
||||
|
||||
func getConfig(configFile string) statusscreen.Config {
|
||||
var config statusscreen.Config
|
||||
// Read the configfile
|
||||
|
||||
file, err := ioutil.ReadFile(configFile)
|
||||
if err != nil {
|
||||
fmt.Printf("File error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Parse config
|
||||
if _, err := toml.Decode(string(file), &config); err != nil {
|
||||
fmt.Printf("Config error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
Loading…
Reference in New Issue