Skip to content

Commit

Permalink
yaml config file and additional headers
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcichra committed Oct 28, 2023
1 parent 2676da6 commit de63485
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
9 changes: 9 additions & 0 deletions gps_collector/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
gpsd:
url: localhost
port: 2947
tpv_interval: 10
ingestd:
url: https://ingest.jrcichra.dev
database: public
table: gps
additional_headers: []
2 changes: 2 additions & 0 deletions gps_collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ require (
golang.org/x/sys v0.13.0 // indirect
)

require gopkg.in/yaml.v2 v2.4.0

require github.com/pkg/errors v0.9.1 // indirect
3 changes: 3 additions & 0 deletions gps_collector/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ github.com/stratoberry/go-gpsd v1.3.0 h1:JxJOEC4SgD0QY65AE7B1CtJtweP73nqJghZeLNU
github.com/stratoberry/go-gpsd v1.3.0/go.mod h1:nVf/vTgfYxOMxiQdy9BtJjojbFRtG8H3wNula++VgkU=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
68 changes: 44 additions & 24 deletions gps_collector/gps_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@ package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"flag"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/joncrlsn/dque"
"github.com/stratoberry/go-gpsd"
"gopkg.in/yaml.v2"
)

const url = "https://ingest.jrcichra.dev"
const database = "public"
const table = "gps"
const TPVInterval = 10
type GPSDConfig struct {
URL string `json:"url"`
Port int `json:"port"`
TPVInterval int `json:"tpv_interval"`
}

type IngestdConfig struct {
URL string `json:"url"`
Database string `json:"database"`
Table string `json:"table"`
AdditionalHeaders []string `json:"additional_headers"`
}

type Config struct {
GPSDConfig `json:"gpsd"`
IngestdConfig `json:"ingestd"`
}

// gps record with hostname metadata
// jonathandbriggs: Added cputemp scraping for raspi.
Expand All @@ -28,11 +43,6 @@ type dbRecord struct {
Cputemp float64 `json:"cputemp"`
}

// gpsRecord - a basic GPS datapoint
type gpsRecord struct {
Value gpsd.TPVReport
}

// dbRecordBuilder - abstracts out a dbRecord for dque to work
func dbRecordBuilder() interface{} {
return &dbRecord{}
Expand Down Expand Up @@ -61,14 +71,6 @@ func makeGPS(hostname string, port int) *gpsd.Session {
return gps
}

func dbrToBytes(dbr *dbRecord) []byte {
b, err := json.Marshal(dbr)
if err != nil {
panic(err)
}
return b
}

func dbrToMap(dbr *dbRecord) map[string]interface{} {
m := make(map[string]interface{})
b, err := json.Marshal(dbr)
Expand All @@ -83,7 +85,7 @@ func dbrToMap(dbr *dbRecord) map[string]interface{} {
return m
}

func queueToPost(q *dque.DQue, h *http.Client) {
func queueToPost(q *dque.DQue, h *http.Client, cfg Config) {
for {
// Only dequeue if we could successfully POST
var t interface{}
Expand Down Expand Up @@ -130,13 +132,18 @@ func queueToPost(q *dque.DQue, h *http.Client) {

log.Println("POSTING:", string(b))
// post
req, err := http.NewRequest("POST", url+"/"+database+"/"+table, bytes.NewBuffer(b))

req, err := http.NewRequest("POST", cfg.IngestdConfig.URL+"/"+cfg.IngestdConfig.Database+"/"+cfg.IngestdConfig.Table, bytes.NewBuffer(b))
if err != nil {
log.Println(err)
continue
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "gps-collector")
for _, header := range cfg.IngestdConfig.AdditionalHeaders {
kv := strings.SplitN(header, "=", 2)
req.Header.Set(kv[0], kv[1])
}

resp, err := h.Do(req)
if err != nil {
Expand All @@ -159,8 +166,21 @@ func queueToPost(q *dque.DQue, h *http.Client) {
}

func main() {
// parse the config file
configPath := flag.String("config", "config.yaml", "path to config file")
flag.Parse()

configFile, err := os.ReadFile(*configPath)
if err != nil {
panic(err)
}
var cfg Config
if err := yaml.Unmarshal(configFile, &cfg); err != nil {
panic(err)
}

//connect to gps
gps := makeGPS("localhost", 2947)
gps := makeGPS(cfg.GPSDConfig.URL, cfg.GPSDConfig.Port)
q := makeQueue()
h := &http.Client{}
h.Timeout = time.Second * 20
Expand All @@ -170,9 +190,9 @@ func main() {
panic(err)
}
// Handle sending off HTTP posts
go queueToPost(q, h)
go queueToPost(q, h, cfg)
// Ticker for only one TPV per interval
ticker := time.NewTicker(TPVInterval * time.Second)
ticker := time.NewTicker(time.Duration(cfg.GPSDConfig.TPVInterval) * time.Second)
// GPS loop
gps.AddFilter("TPV", func(r interface{}) {
// This anon function is called every time a new TPV value comes in, scoped this way so we can use q easily
Expand All @@ -181,7 +201,7 @@ func main() {
// Only enqueue if the ticker went off
tpv := r.(*gpsd.TPVReport)
// Include the CPU temp.
tmp, _ := ioutil.ReadFile(`/sys/class/thermal/thermal_zone0/temp`)
tmp, _ := os.ReadFile(`/sys/class/thermal/thermal_zone0/temp`)
// Trim the newline off the end of the CPU Temp.
if len(tmp) > 0 {
tmp = tmp[:len(tmp)-1]
Expand Down

0 comments on commit de63485

Please sign in to comment.