Added route for saving device info
This commit is contained in:
31
controllers/DevicesController.go
Normal file
31
controllers/DevicesController.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gitlab.com/pactual1/backend/models"
|
||||||
|
"gitlab.com/pactual1/backend/shared"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SaveDeviceInfofunc(c *gin.Context) {
|
||||||
|
var deviceInfo models.DeviceInfo
|
||||||
|
rawData, _ := c.GetRawData()
|
||||||
|
|
||||||
|
// Unmarshal to the important info structure
|
||||||
|
err := json.Unmarshal(rawData, &deviceInfo)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert raw JSON bytes to string
|
||||||
|
deviceInfo.RawJSON = string(rawData)
|
||||||
|
|
||||||
|
// Save deviceInfo to your database here
|
||||||
|
db :=shared.GetDb()
|
||||||
|
db.Create(&deviceInfo)
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully received device info", "data": deviceInfo})
|
||||||
|
}
|
||||||
5
main.go
5
main.go
@@ -11,7 +11,6 @@ import (
|
|||||||
"gitlab.com/pactual1/backend/services/contact"
|
"gitlab.com/pactual1/backend/services/contact"
|
||||||
"gitlab.com/pactual1/backend/services/erp"
|
"gitlab.com/pactual1/backend/services/erp"
|
||||||
"gitlab.com/pactual1/backend/services/messaging"
|
"gitlab.com/pactual1/backend/services/messaging"
|
||||||
"gitlab.com/pactual1/backend/services/sensor"
|
|
||||||
"gitlab.com/pactual1/backend/shared"
|
"gitlab.com/pactual1/backend/shared"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -44,6 +43,7 @@ func main() {
|
|||||||
company := Admin.AddResource(&models.Company{})
|
company := Admin.AddResource(&models.Company{})
|
||||||
company.Meta(&admin.Meta{Name: "Users", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
company.Meta(&admin.Meta{Name: "Users", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
||||||
company.Meta(&admin.Meta{Name: "Devices", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
company.Meta(&admin.Meta{Name: "Devices", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
||||||
|
// company.Meta(&admin.Meta{Name: "DeviceInfos", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
||||||
|
|
||||||
// Add User and Device resources
|
// Add User and Device resources
|
||||||
Admin.AddResource(&models.User{})
|
Admin.AddResource(&models.User{})
|
||||||
@@ -63,19 +63,16 @@ func main() {
|
|||||||
// Initialize channels
|
// Initialize channels
|
||||||
messagingChannel := make(chan string)
|
messagingChannel := make(chan string)
|
||||||
erpChannel := make(chan string)
|
erpChannel := make(chan string)
|
||||||
sensorChannel := make(chan string)
|
|
||||||
contactChannel := make(chan string)
|
contactChannel := make(chan string)
|
||||||
|
|
||||||
// Start services and pass the respective channels
|
// Start services and pass the respective channels
|
||||||
go messaging.MessagingService(messagingChannel)
|
go messaging.MessagingService(messagingChannel)
|
||||||
go erp.ERPService(erpChannel)
|
go erp.ERPService(erpChannel)
|
||||||
go sensor.SensorService(sensorChannel)
|
|
||||||
go contact.ContactService(contactChannel)
|
go contact.ContactService(contactChannel)
|
||||||
|
|
||||||
// Sending messages via channels
|
// Sending messages via channels
|
||||||
messagingChannel <- "Send an email"
|
messagingChannel <- "Send an email"
|
||||||
erpChannel <- "Update ERP record"
|
erpChannel <- "Update ERP record"
|
||||||
sensorChannel <- "Trigger sensor"
|
|
||||||
contactChannel <- "Update contact info"
|
contactChannel <- "Update contact info"
|
||||||
|
|
||||||
// Initialize Gin
|
// Initialize Gin
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ type Device struct {
|
|||||||
DeviceName string
|
DeviceName string
|
||||||
DeviceConfiguration string `gorm:"type:json"`
|
DeviceConfiguration string `gorm:"type:json"`
|
||||||
CompanyID uint
|
CompanyID uint
|
||||||
|
DeviceInfos []DeviceInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Device)Update() (bool, error) {
|
func (Device)Update() (bool, error) {
|
||||||
|
|||||||
39
models/device_info.go
Normal file
39
models/device_info.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
// Location holds latitude and longitude.
|
||||||
|
type Location struct {
|
||||||
|
Lat float64 `json:"lat"`
|
||||||
|
Lon float64 `json:"lon"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImportantInfo holds fields that are important for quick access.
|
||||||
|
type SensorData struct {
|
||||||
|
IMEI string `json:"imei"`
|
||||||
|
IMSI string `json:"imsi"`
|
||||||
|
Timestamp int64 `json:"timestamp"`
|
||||||
|
Lat float64 `json:"lat"`
|
||||||
|
Lon float64 `json:"lon"`
|
||||||
|
WifiLoc Location `json:"wifi_location"`
|
||||||
|
CellLoc Location `json:"cell_location"`
|
||||||
|
Temperature float64 `json:"temperature"`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type DeviceInfo struct {
|
||||||
|
gorm.Model
|
||||||
|
RawJSON string `json:"raw_json" gorm:"type:json"`
|
||||||
|
SensorData
|
||||||
|
DeviceID uint
|
||||||
|
}
|
||||||
|
|
||||||
|
func (DeviceInfo)Update() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
func (DeviceInfo)Create() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
func (DeviceInfo)Delete() (bool, error) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
@@ -9,6 +9,6 @@ import (
|
|||||||
func RegisterPublicRoutes(r *gin.Engine){
|
func RegisterPublicRoutes(r *gin.Engine){
|
||||||
|
|
||||||
r.GET("/publicmessage", controllers.GetPublicText)
|
r.GET("/publicmessage", controllers.GetPublicText)
|
||||||
// r.GET("/companies", controllers.Ge)
|
r.POST("/device_info", controllers.SaveDeviceInfofunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
package sensor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func SensorService(ch chan string) {
|
|
||||||
for msg := range ch {
|
|
||||||
fmt.Println("Sensor Service: ", msg)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -34,7 +34,7 @@ func Init() error{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
//TODO AUTOMIGRATE models once we have them
|
//TODO AUTOMIGRATE models once we have them
|
||||||
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{})
|
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user