diff --git a/gr24/controller/bcm_controller.go b/gr24/controller/bcm_controller.go deleted file mode 100644 index e8a228e..0000000 --- a/gr24/controller/bcm_controller.go +++ /dev/null @@ -1,34 +0,0 @@ -package controller - -import ( - "gr24/model" - "gr24/service" - "gr24/utils" - "net/http" - "strconv" - - "github.com/gin-gonic/gin" -) - -func ConnectBCMSocket(c *gin.Context) { - conn, err := SocketUpgrader.Upgrade(c.Writer, c.Request, nil) - if err != nil { - utils.SugarLogger.Errorln("[WS - gr24/bcm] error while Upgrading websocket connection\n", err.Error()) - c.AbortWithError(http.StatusInternalServerError, err) - return - } - defer conn.Close() - service.SubscribeBCM(func(bcm model.BCM) { - _ = conn.WriteJSON(bcm) - }) - - for { - messageType, p, err := conn.ReadMessage() - if err != nil { - utils.SugarLogger.Errorln("[WS - gr24/bcm] error while reading message\n", err.Error()) - c.AbortWithError(http.StatusInternalServerError, err) - break - } - utils.SugarLogger.Infoln("[WS - gr24/bcm] Received message ("+strconv.Itoa(messageType)+"): ", string(p)) - } -} diff --git a/gr24/controller/vdm_controller.go b/gr24/controller/vdm_controller.go deleted file mode 100644 index d405323..0000000 --- a/gr24/controller/vdm_controller.go +++ /dev/null @@ -1,34 +0,0 @@ -package controller - -import ( - "gr24/model" - "gr24/service" - "gr24/utils" - "net/http" - "strconv" - - "github.com/gin-gonic/gin" -) - -func ConnectVDMSocket(c *gin.Context) { - conn, err := SocketUpgrader.Upgrade(c.Writer, c.Request, nil) - if err != nil { - utils.SugarLogger.Errorln("[WS - gr24/vdm] error while Upgrading websocket connection\n", err.Error()) - c.AbortWithError(http.StatusInternalServerError, err) - return - } - defer conn.Close() - service.SubscribeVDM(func(vdm model.VDM) { - _ = conn.WriteJSON(vdm) - }) - - for { - messageType, p, err := conn.ReadMessage() - if err != nil { - utils.SugarLogger.Errorln("[WS - gr24/vdm] error while reading message\n", err.Error()) - c.AbortWithError(http.StatusInternalServerError, err) - break - } - utils.SugarLogger.Infoln("[WS - gr24/vdm] Received message ("+strconv.Itoa(messageType)+"): ", string(p)) - } -} diff --git a/gr24/controller/wheel_controller.go b/gr24/controller/wheel_controller.go deleted file mode 100644 index 1e298f4..0000000 --- a/gr24/controller/wheel_controller.go +++ /dev/null @@ -1,34 +0,0 @@ -package controller - -import ( - "gr24/model" - "gr24/service" - "gr24/utils" - "net/http" - "strconv" - - "github.com/gin-gonic/gin" -) - -func ConnectWheelSocket(c *gin.Context) { - conn, err := SocketUpgrader.Upgrade(c.Writer, c.Request, nil) - if err != nil { - utils.SugarLogger.Errorln("[WS - gr24/wheel] error while Upgrading websocket connection\n", err.Error()) - c.AbortWithError(http.StatusInternalServerError, err) - return - } - defer conn.Close() - service.SubscribeWheel(func(wheel model.Wheel) { - _ = conn.WriteJSON(wheel) - }) - - for { - messageType, p, err := conn.ReadMessage() - if err != nil { - utils.SugarLogger.Errorln("[WS - gr24/wheel] error while reading message\n", err.Error()) - c.AbortWithError(http.StatusInternalServerError, err) - break - } - utils.SugarLogger.Infoln("[WS - gr24/wheel] Received message ("+strconv.Itoa(messageType)+"): ", string(p)) - } -} diff --git a/gr24/model/bcm.go b/gr24/model/bcm.go deleted file mode 100644 index 5434817..0000000 --- a/gr24/model/bcm.go +++ /dev/null @@ -1,68 +0,0 @@ -package model - -import ( - "time" - - "github.com/gaucho-racing/mapache-go" -) - -type BCM struct { - ID string `json:"id" gorm:"primaryKey"` - VehicleID string `json:"vehicle_id"` - CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;precision:6"` - AccelX float64 `json:"accel_x"` - AccelY float64 `json:"accel_y"` - AccelZ float64 `json:"accel_z"` - GyroX float64 `json:"gyro_x"` - GyroY float64 `json:"gyro_y"` - GyroZ float64 `json:"gyro_z"` - Millis int `json:"millis"` -} - -func (BCM) TableName() string { - return "gr24_bcm" -} - -func NewBCMNode() mapache.Node { - return []mapache.Field{ - { - Name: "AccelX", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - { - Name: "AccelY", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - { - Name: "AccelZ", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - mapache.NewField("blank", 2, mapache.Unsigned, mapache.BigEndian), - { - Name: "GyroX", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - { - Name: "GyroY", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - { - Name: "GyroZ", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - mapache.NewField("blank", 2, mapache.Unsigned, mapache.BigEndian), - mapache.NewField("Millis", 4, mapache.Unsigned, mapache.BigEndian), - } -} diff --git a/gr24/model/wheel.go b/gr24/model/wheel.go deleted file mode 100644 index 8edd6a9..0000000 --- a/gr24/model/wheel.go +++ /dev/null @@ -1,114 +0,0 @@ -package model - -import ( - "time" - - "github.com/gaucho-racing/mapache-go" -) - -type Wheel struct { - ID string `json:"id" gorm:"primaryKey"` - VehicleID string `json:"vehicle_id"` - CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;precision:6"` - Location string `json:"wheel_location"` - Suspension int `json:"suspension"` - WheelSpeed float64 `json:"wheel_speed"` - AccelX float64 `json:"imu_accel_x"` - AccelY float64 `json:"imu_accel_y"` - AccelZ float64 `json:"imu_accel_z"` - GyroX float64 `json:"imu_gyro_x"` - GyroY float64 `json:"imu_gyro_y"` - GyroZ float64 `json:"imu_gyro_z"` - BrakeTempOne int `json:"brake_temp_one"` - BrakeTempTwo int `json:"brake_temp_two"` - TireTempOne int `json:"tire_temp_one"` - TireTempTwo int `json:"tire_temp_two"` - Millis int `json:"millis"` -} - -func (Wheel) TableName() string { - return "gr24_wheel" -} - -func NewWheelNode() mapache.Node { - return []mapache.Field{ - { - Name: "Suspension", - Size: 1, - Sign: mapache.Unsigned, - Endian: mapache.BigEndian, - }, - { - Name: "WheelSpeed", - Size: 2, - Sign: mapache.Unsigned, - Endian: mapache.BigEndian, - }, - mapache.NewField("blank", 5, mapache.Unsigned, mapache.BigEndian), - { - Name: "AccelX", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - { - Name: "AccelY", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - { - Name: "AccelZ", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - mapache.NewField("blank", 2, mapache.Unsigned, mapache.BigEndian), - { - Name: "GyroX", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - { - Name: "GyroY", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - { - Name: "GyroZ", - Size: 2, - Sign: mapache.Signed, - Endian: mapache.BigEndian, - }, - mapache.NewField("blank", 2, mapache.Unsigned, mapache.BigEndian), - { - Name: "BrakeTempOne", - Size: 1, - Sign: mapache.Unsigned, - Endian: mapache.BigEndian, - }, - { - Name: "BrakeTempTwo", - Size: 1, - Sign: mapache.Unsigned, - Endian: mapache.BigEndian, - }, - mapache.NewField("blank", 6, mapache.Unsigned, mapache.BigEndian), - { - Name: "TireTempOne", - Size: 1, - Sign: mapache.Unsigned, - Endian: mapache.BigEndian, - }, - { - Name: "TireTempTwo", - Size: 1, - Sign: mapache.Unsigned, - Endian: mapache.BigEndian, - }, - mapache.NewField("blank", 6, mapache.Unsigned, mapache.BigEndian), - mapache.NewField("Millis", 4, mapache.Unsigned, mapache.BigEndian), - } -} diff --git a/gr24/service/bcm_service.go b/gr24/service/bcm_service.go deleted file mode 100644 index 39d19aa..0000000 --- a/gr24/service/bcm_service.go +++ /dev/null @@ -1,78 +0,0 @@ -package service - -import ( - "gr24/database" - "gr24/model" - "gr24/utils" - "strings" - "time" - - mqtt "github.com/eclipse/paho.mqtt.golang" - "github.com/google/uuid" -) - -var bcmCallbacks []func(bcm model.BCM) - -// bcmNotify calls all the functions registered to bcmCallbacks -func bcmNotify(bcm model.BCM) { - for _, callback := range bcmCallbacks { - callback(bcm) - } -} - -// SubscribeBCM registers a function to be called when a new bcm is received -func SubscribeBCM(callback func(bcm model.BCM)) { - bcmCallbacks = append(bcmCallbacks, callback) -} - -// BCMIngestCallback is the callback function for handling incoming mqtt bcm frames -var BCMIngestCallback = func(client mqtt.Client, msg mqtt.Message) { - utils.SugarLogger.Infof("[MQ-%s] Received bcm frame", msg.Topic()) - bcm := BCMFromBytes(msg.Payload()) - if bcm.ID != "" { - bcm.VehicleID = strings.Split(msg.Topic(), "/")[1] - bcm = scaleBCM(bcm) - utils.SugarLogger.Infoln(bcm) - bcmNotify(bcm) - go func() { - err := CreateBCM(bcm) - if err != nil { - utils.SugarLogger.Errorln(err) - } - }() - } -} - -// BCMFromBytes converts a byte array to a bcm struct -// If the conversion fails, an empty bcm struct is returned -func BCMFromBytes(data []byte) model.BCM { - var bcm model.BCM - bcmFields := model.NewBCMNode() - err := bcmFields.FillFromBytes(data) - if err != nil { - utils.SugarLogger.Errorln("Failed to parse bcm:", err) - return bcm - } - bcm.ID = uuid.New().String() - bcm.CreatedAt = time.Now() - bcm.AccelX = float64(bcmFields[0].Value) - bcm.AccelY = float64(bcmFields[1].Value) - bcm.AccelZ = float64(bcmFields[2].Value) - bcm.GyroX = float64(bcmFields[4].Value) - bcm.GyroY = float64(bcmFields[5].Value) - bcm.GyroZ = float64(bcmFields[6].Value) - bcm.Millis = bcmFields[8].Value - return bcm -} - -// scaleBCM does not scale the bcm values to be between 0 and 100 -func scaleBCM(bcm model.BCM) model.BCM { - return bcm -} - -func CreateBCM(bcm model.BCM) error { - if result := database.DB.Create(&bcm); result.Error != nil { - return result.Error - } - return nil -} diff --git a/gr24/service/pedal_service.go b/gr24/service/pedal_service.go index fb5f7eb..1105bcc 100644 --- a/gr24/service/pedal_service.go +++ b/gr24/service/pedal_service.go @@ -6,10 +6,8 @@ import ( "gr24/utils" "math" "strings" - "time" mqtt "github.com/eclipse/paho.mqtt.golang" - "github.com/google/uuid" ) var pedalCallbacks []func(pedal model.Pedal) @@ -54,11 +52,18 @@ func PedalFromBytes(data []byte) model.Pedal { utils.SugarLogger.Errorln("Failed to parse pedal:", err) return pedal } - pedal.ID = uuid.New().String() - pedal.CreatedAt = time.Now() - pedal.AppsOneRaw = pedalFields[0].Value - pedal.AppsTwoRaw = pedalFields[1].Value - pedal.Millis = pedalFields[3].Value + // pedal.ID = uuid.New().String() + // pedal.CreatedAt = time.Now() + // pedal.AppsOneRaw = pedalFields[0].Value + // pedal.AppsTwoRaw = pedalFields[1].Value + // pedal.Millis = pedalFields[3].Value + // for i, p := range pedalFields { + // signal := mapache.Signal{ + // ID: uuid.New(), + // SignalID: pedalFields[i].Name, + // Value: + // } + // } return pedal } diff --git a/gr24/service/wheel_service.go b/gr24/service/wheel_service.go deleted file mode 100644 index 7a8f24b..0000000 --- a/gr24/service/wheel_service.go +++ /dev/null @@ -1,85 +0,0 @@ -package service - -import ( - "gr24/database" - "gr24/model" - "gr24/utils" - "strings" - "time" - - mqtt "github.com/eclipse/paho.mqtt.golang" - "github.com/google/uuid" -) - -var wheelCallbacks []func(wheel model.Wheel) - -// wheelNotify calls all the functions registered to wheelCallbacks -func wheelNotify(wheel model.Wheel) { - for _, callback := range wheelCallbacks { - callback(wheel) - } -} - -// SubscribeWheel registers a function to be called when a new wheel is received -func SubscribeWheel(callback func(wheel model.Wheel)) { - wheelCallbacks = append(wheelCallbacks, callback) -} - -// WheelIngestCallback is the callback function for handling incoming mqtt wheel frames -var WheelIngestCallback = func(client mqtt.Client, msg mqtt.Message) { - utils.SugarLogger.Infof("[MQ-%s] Received wheel frame", msg.Topic()) - wheel := WheelFromBytes(msg.Payload()) - if wheel.ID != "" { - wheel.VehicleID = strings.Split(msg.Topic(), "/")[1] - wheel.Location = strings.Split(msg.Topic(), "/")[3] - wheel = scaleWheel(wheel) - utils.SugarLogger.Infoln(wheel) - wheelNotify(wheel) - go func() { - err := CreateWheel(wheel) - if err != nil { - utils.SugarLogger.Errorln(err) - } - }() - } -} - -// WheelFromBytes converts a byte array to a wheel struct -// If the conversion fails, an empty wheel struct is returned -func WheelFromBytes(data []byte) model.Wheel { - var wheel model.Wheel - wheelFields := model.NewWheelNode() - err := wheelFields.FillFromBytes(data) - if err != nil { - utils.SugarLogger.Errorln("Failed to parse wheel:", err) - return wheel - } - wheel.ID = uuid.New().String() - wheel.CreatedAt = time.Now() - wheel.Suspension = wheelFields[0].Value - wheel.WheelSpeed = float64(wheelFields[1].Value) - wheel.AccelX = float64(wheelFields[3].Value) - wheel.AccelY = float64(wheelFields[4].Value) - wheel.AccelZ = float64(wheelFields[5].Value) - wheel.GyroX = float64(wheelFields[7].Value) - wheel.GyroY = float64(wheelFields[8].Value) - wheel.GyroZ = float64(wheelFields[9].Value) - wheel.BrakeTempOne = wheelFields[11].Value - wheel.BrakeTempTwo = wheelFields[12].Value - wheel.TireTempOne = wheelFields[14].Value - wheel.TireTempTwo = wheelFields[15].Value - wheel.Millis = wheelFields[17].Value - return wheel -} - -// scaleWheel does not scale the wheel values to be between 0 and 100 -func scaleWheel(wheel model.Wheel) model.Wheel { - return wheel -} - -func CreateWheel(wheel model.Wheel) error { - if result := database.DB.Create(&wheel); result.Error != nil { - return result.Error - } - return nil -}