Skip to content

Instantly share code, notes, and snippets.

View BK1031's full-sized avatar

Bharat Kathi BK1031

View GitHub Profile
Computer Information:
Manufacturer: Apple
Model: MacBookPro11,4
Form Factor: Laptop
No Touch Input Detected
Processor Information:
CPU Vendor: GenuineIntel
CPU Brand: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
CPU Family: 0x6
@BK1031
BK1031 / main.go
Last active July 9, 2024 22:24
racecar_analytics mqtt connection
package main
import (
"fmt"
"log"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
@BK1031
BK1031 / main.go
Created July 9, 2024 21:06
racecar_analytics mqtt subscription functions
func SubscribeECU() {
Client.Subscribe("ingest/ecu", 0, func(client mqtt.Client, msg mqtt.Message) {
fmt.Println("[MQ] Received ecu message")
})
}
func SubscribeBattery() {
Client.Subscribe("ingest/battery", 0, func(client mqtt.Client, msg mqtt.Message) {
fmt.Println("[MQ] Received battery message")
})
@BK1031
BK1031 / main.go
Created July 9, 2024 21:09
racecar_analytics mqtt subscription
func main() {
ConnectMQTT()
SubscribeECU()
SubscribeBattery()
for {}
}
@BK1031
BK1031 / model.go
Last active July 9, 2024 22:20
racecar_analytics structs
package main
import (
"time"
)
type ECU struct {
ID int `json:"id"`
MotorRPM int `json:"motor_rpm"`
Speed int `json:"speed"`
@BK1031
BK1031 / model.go
Last active July 9, 2024 21:53
racecar_analytics struct from bytes helper functions
func ECUFromBytes(data []byte) ECU {
var ecu ECU
// MotorRPM is bytes 0-1
ecu.MotorRPM = int(binary.BigEndian.Uint16(data[0:2]))
// Speed is byte 2
ecu.Speed = int(data[2])
// Throttle is bytes 3-4
ecu.Throttle = int(binary.BigEndian.Uint16(data[3:5]))
// BrakePressure is bytes 5-6
ecu.BrakePressure = int(binary.BigEndian.Uint16(data[5:7]))
@BK1031
BK1031 / main.go
Last active July 11, 2024 11:22
racecar_analytics subscribe mqtt handlers with byte decoding
func SubscribeECU() {
Client.Subscribe("ingest/ecu", 0, func(client mqtt.Client, msg mqtt.Message) {
ecu := ECUFromBytes(msg.Payload()) // add this
fmt.Printf("[MQ] Received ecu message: %v\n", ecu)
})
}
func SubscribeBattery() {
Client.Subscribe("ingest/battery", 0, func(client mqtt.Client, msg mqtt.Message) {
battery := BatteryFromBytes(msg.Payload()) // add this
@BK1031
BK1031 / db.go
Last active July 10, 2024 00:00
racecar_analytics db connection function
package main
import (
"fmt"
"log"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
@BK1031
BK1031 / main.go
Last active July 11, 2024 11:24
racecar_analytics db connection
func main() {
ConnectMQTT()
SubscribeECU()
SubscribeBattery()
ConnectDB() // add this
for {
}
}
@BK1031
BK1031 / model.go
Last active July 11, 2024 11:25
racecar_analytics update structs for db mapping
type ECU struct {
ID int `json:"id" gorm:"primaryKey"` // add this
MotorRPM int `json:"motor_rpm"`
Speed int `json:"speed"`
Throttle int `json:"throttle"`
BrakePressure int `json:"brake_pressure"`
CreatedAt time.Time `json:"created_at" gorm:"precision:6"` // add this
}
type Battery struct {