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
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
package main | |
import ( | |
"net/http" | |
"time" | |
"github.com/gin-gonic/gin" | |
) | |
var Port = "9000" |
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() | |
StartServer() // 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 StartServer() { | |
r := gin.Default() | |
r.GET("/ecu", GetAllECUs) | |
r.GET("/battery", GetAllBatteries) | |
r.GET("/ecu/averages", GetECUAverages) // add this | |
r.GET("/battery/averages", GetBatteryAverages) // add this | |
r.Run(":" + Port) | |
} | |
// other functions here |
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
var ecuCallbacks []func(ecu ECU) | |
var batteryCallbacks []func(battery Battery) | |
func RegisterECUCallback(callback func(ecu ECU)) { | |
ecuCallbacks = append(ecuCallbacks, callback) | |
} | |
func RegisterBatteryCallback(callback func(battery Battery)) { | |
batteryCallbacks = append(batteryCallbacks, callback) | |
} |
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 | |
} | |
PushECU(ecu) // add this | |
return ecu, nil | |
} | |
func CreateBattery(battery Battery) (Battery, error) { |
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 StartServer() { | |
r := gin.Default() | |
r.GET("/ecu", GetAllECUs) | |
r.GET("/battery", GetAllBatteries) | |
r.GET("/ecu/averages", GetECUAverages) | |
r.GET("/battery/averages", GetBatteryAverages) | |
r.GET("/ecu/stream", StreamECUs) // add this | |
r.GET("/battery/stream", StreamBatteries) // add this | |
r.Run(":" + Port) | |
} |
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
import React from "react"; | |
import useWebSocket, { ReadyState } from "react-use-websocket"; | |
import axios from "axios"; | |
function App() { | |
const [ecuSocketUrl] = React.useState("ws://localhost:9000/ecu/stream"); | |
const { lastMessage: lastEcuMessage, readyState } = useWebSocket(ecuSocketUrl); | |
const [ecuData, setEcuData] = React.useState([{}]); | |
React.useEffect(() => { |
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
import { | |
LineChart, | |
Line, | |
XAxis, | |
YAxis, | |
CartesianGrid, | |
Tooltip, | |
ResponsiveContainer, | |
} from "recharts"; |