This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func SubscribeECU() { | |
Client.Subscribe("ingest/ecu", 0, func(client mqtt.Client, msg mqtt.Message) { | |
ecu := ECUFromBytes(msg.Payload()) | |
// ------ add this ------ | |
ecu, err := CreateECU(ecu) | |
if err != nil { | |
fmt.Printf("failed to create ecu: %v", err) | |
} | |
// ---------------------- | |
fmt.Printf("[MQ] Received ecu message: %v\n", ecu) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func CreateECU(ecu ECU) (ECU, error) { | |
result := DB.Create(&ecu) | |
if result.Error != nil { | |
return ECU{}, result.Error | |
} | |
return ecu, nil | |
} | |
func CreateBattery(battery Battery) (Battery, error) { | |
result := DB.Create(&battery) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
ConnectMQTT() | |
SubscribeECU() | |
SubscribeBattery() | |
ConnectDB() // add this | |
for { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"gorm.io/driver/mysql" | |
"gorm.io/gorm" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
) | |
type ECU struct { | |
ID int `json:"id"` | |
MotorRPM int `json:"motor_rpm"` | |
Speed int `json:"speed"` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
ConnectMQTT() | |
SubscribeECU() | |
SubscribeBattery() | |
for {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
}) |