commit af5786b04bcf2913aa298ea04db72c0e4da35841 Author: Elis Hirwing Date: Sun Oct 15 09:41:36 2017 +0200 Start with some stuff diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58bb2ed --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +status.toml +pkg/ +src/github.com/ +statusscreen diff --git a/src/statusscreen/Config.go b/src/statusscreen/Config.go new file mode 100644 index 0000000..10b921b --- /dev/null +++ b/src/statusscreen/Config.go @@ -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 + } +} diff --git a/src/statusscreen/SL.go b/src/statusscreen/SL.go new file mode 100644 index 0000000..58d8ed0 --- /dev/null +++ b/src/statusscreen/SL.go @@ -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()) +} diff --git a/statusscreen.go b/statusscreen.go new file mode 100644 index 0000000..bf6c483 --- /dev/null +++ b/statusscreen.go @@ -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 +}