Skip to content

Commit

Permalink
Pass car by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
tma authored May 13, 2022
1 parent 2f1d751 commit 4b71e38
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func main() {
os.Getenv("ABRP_API_KEY"),
)

app.MessagesSubscribe(mqttAddress, car)
app.WebStart(port, car)
app.MessagesSubscribe(mqttAddress, &car)
app.WebStart(port, &car)
}
8 changes: 4 additions & 4 deletions internal/app/abrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var abrpSendQuit = make(chan bool)

func abrpSendActivate(car Car, endTime time.Time) {
func abrpSendActivate(car *Car, endTime time.Time) {
car.abrpSendActive = true
car.abrpUpdatesEndTime = endTime

Expand All @@ -23,7 +23,7 @@ func abrpSendDeactivate() {

const abrpSendInterval = 1 * time.Second

func abrpSendLoop(car Car, endTime time.Time) {
func abrpSendLoop(car *Car, endTime time.Time) {
log.Println("Start sending to ABRP...")

timer := -1
Expand Down Expand Up @@ -78,14 +78,14 @@ func abrpSendLoop(car Car, endTime time.Time) {
}
}

func abrpSendLoopStop(car Car) {
func abrpSendLoopStop(car *Car) {
car.abrpSendActive = false
car.abrpUpdatesEndTime = time.Time{}
}

const abrpUrl = "https://api.iternio.com/1/tlm/send"

func abrpSend(car Car) {
func abrpSend(car *Car) {
log.Println("Sending to ABRP...")

data, error := json.Marshal(map[string]interface{}{"tlm": car.abrpData})
Expand Down
4 changes: 2 additions & 2 deletions internal/app/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func NewCar(number string, carModel string, abrpToken string, abrpApiKey string)
}
}

func updateCarTmData(car Car, topic string, payload string) {
func updateCarTmData(car *Car, topic string, payload string) {
car.tmData[topic] = payload
}

func updateCarAbrpData(car Car, topic string, payload string) {
func updateCarAbrpData(car *Car, topic string, payload string) {
switch topic {
case "latitude":
car.abrpData["lat"] = payload
Expand Down
8 changes: 4 additions & 4 deletions internal/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var indexTemplate = template.Must(template.ParseFiles("web/templates/index.html"))

func indexHandler(car Car) http.HandlerFunc {
func indexHandler(car *Car) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
carTmData, _ := json.MarshalIndent(car.tmData, "", " ")
carAbrpData, _ := json.MarshalIndent(car.abrpData, "", " ")
Expand Down Expand Up @@ -44,7 +44,7 @@ func indexHandler(car Car) http.HandlerFunc {
}
}

func abrpSendContinuousHandler(car Car) http.HandlerFunc {
func abrpSendContinuousHandler(car *Car) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if car.abrpSendActive {
abrpSendDeactivate()
Expand All @@ -64,7 +64,7 @@ func abrpSendContinuousHandler(car Car) http.HandlerFunc {
}
}

func abrpSendNowHandler(car Car) http.HandlerFunc {
func abrpSendNowHandler(car *Car) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
abrpSend(car)

Expand All @@ -74,7 +74,7 @@ func abrpSendNowHandler(car Car) http.HandlerFunc {

var rootPath string

func WebStart(port string, car Car) {
func WebStart(port string, car *Car) {
log.Println("Starting HTTP server on port " + port)

router := mux.NewRouter()
Expand Down
2 changes: 1 addition & 1 deletion internal/app/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
mqtt "github.com/eclipse/paho.mqtt.golang"
)

func MessagesSubscribe(mqttAddress string, car Car) {
func MessagesSubscribe(mqttAddress string, car *Car) {
log.Println("Connecting to MQTT server on " + mqttAddress)

opts := mqtt.NewClientOptions().AddBroker(mqttAddress).SetClientID("tm-to-abrp-car" + car.number)
Expand Down

0 comments on commit 4b71e38

Please sign in to comment.