Upstream sync
This commit is contained in:
@@ -23,98 +23,96 @@ func SaveDeviceInfo(c *gin.Context) {
|
||||
err := json.Unmarshal(rawData, &deviceInfo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
|
||||
log.Printf("Invalid json pyload : %v",err)
|
||||
log.Printf("Invalid json pyload : %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
deviceInfo.RawJSON = string(rawData)
|
||||
deviceInfo.X = deviceInfo.AccInfo.X
|
||||
deviceInfo.Y = deviceInfo.AccInfo.Y
|
||||
deviceInfo.Z = deviceInfo.AccInfo.Z
|
||||
|
||||
// Attempt to find the device by IMEI; if not found, create a new device
|
||||
var device models.Device
|
||||
if err := shared.GetDb().Unscoped().Where("device_id = ?", deviceInfo.ExternalDeviceID).First(&device).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// Create new device
|
||||
newDevice := models.Device{
|
||||
IMEI: deviceInfo.IMEI,
|
||||
IMSI: deviceInfo.IMSI,
|
||||
DeviceID: deviceInfo.ExternalDeviceID,
|
||||
DeviceConfiguration: string(rawData),
|
||||
}
|
||||
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
|
||||
log.Printf("CREATE -Device DB Error: %v",err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create new device"})
|
||||
return
|
||||
}
|
||||
deviceInfo.DeviceID = newDevice.ID
|
||||
} else {
|
||||
log.Printf("CREATE -Device DB Error: %v",err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
// Attempt to find the device by IMEI; if not found, create a new device
|
||||
var device models.Device
|
||||
if err := shared.GetDb().Unscoped().Where("device_id = ?", deviceInfo.ExternalDeviceID).First(&device).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// Create new device
|
||||
newDevice := models.Device{
|
||||
IMEI: deviceInfo.IMEI,
|
||||
IMSI: deviceInfo.IMSI,
|
||||
DeviceID: deviceInfo.ExternalDeviceID,
|
||||
DeviceConfiguration: string(rawData),
|
||||
}
|
||||
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
|
||||
log.Printf("CREATE -Device DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create new device"})
|
||||
return
|
||||
}
|
||||
deviceInfo.DeviceID = newDevice.ID
|
||||
} else {
|
||||
log.Printf("Current device deleted at: %v",device.DeletedAt)
|
||||
|
||||
if device.DeletedAt != nil {
|
||||
// Use raw SQL to update the record
|
||||
if err := shared.GetDb().Exec("UPDATE devices SET deleted_at = NULL, company_id = NULL WHERE id = ?", device.ID).Error; err != nil {
|
||||
log.Printf("UNDELETE -Device DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not revive deleted device"})
|
||||
return
|
||||
} else {
|
||||
log.Printf("Device undeleted successfuly : %v",device.DeletedAt)
|
||||
}
|
||||
}
|
||||
deviceInfo.DeviceID = device.ID
|
||||
log.Printf("CREATE -Device DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Printf("Current device deleted at: %v", device.DeletedAt)
|
||||
|
||||
if device.DeletedAt != nil {
|
||||
// Use raw SQL to update the record
|
||||
if err := shared.GetDb().Exec("UPDATE devices SET deleted_at = NULL, company_id = NULL WHERE id = ?", device.ID).Error; err != nil {
|
||||
log.Printf("UNDELETE -Device DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not revive deleted device"})
|
||||
return
|
||||
} else {
|
||||
log.Printf("Device undeleted successfuly : %v", device.DeletedAt)
|
||||
}
|
||||
}
|
||||
deviceInfo.DeviceID = device.ID
|
||||
}
|
||||
|
||||
// Save deviceInfo to your database
|
||||
if err := shared.GetDb().Create(&deviceInfo).Error; err != nil {
|
||||
log.Printf("SaveDeviceInfo CREATE -DeviceInfo DB Error: %v",err)
|
||||
log.Printf("SaveDeviceInfo CREATE -DeviceInfo DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info"})
|
||||
return
|
||||
}
|
||||
log.Printf("Successfully received and saved device info: %v",deviceInfo)
|
||||
log.Printf("Successfully received and saved device info: %v", deviceInfo)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved device info", "data": deviceInfo})
|
||||
}
|
||||
|
||||
func GetDeviceData(c *gin.Context) {
|
||||
// Get the device ID and contract ID from query parameters
|
||||
deviceIDStr := c.DefaultQuery("device_id", "")
|
||||
contractIDStr := c.DefaultQuery("contract_id", "")
|
||||
// Get the device ID and contract ID from query parameters
|
||||
deviceIDStr := c.DefaultQuery("device_id", "")
|
||||
contractIDStr := c.DefaultQuery("contract_id", "")
|
||||
|
||||
if deviceIDStr == "" {
|
||||
log.Printf("GetDeviceData Error: Device ID is required")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Device ID is required"})
|
||||
return
|
||||
}
|
||||
if deviceIDStr == "" {
|
||||
log.Printf("GetDeviceData Error: Device ID is required")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Device ID is required"})
|
||||
return
|
||||
}
|
||||
|
||||
deviceID, err := strconv.ParseUint(deviceIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetDeviceData Error: Invalid Device ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Device ID"})
|
||||
return
|
||||
}
|
||||
deviceID, err := strconv.ParseUint(deviceIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetDeviceData Error: Invalid Device ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Device ID"})
|
||||
return
|
||||
}
|
||||
|
||||
contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetDeviceData Error: Invalid Contract ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Contract ID"})
|
||||
return
|
||||
}
|
||||
contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetDeviceData Error: Invalid Contract ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Contract ID"})
|
||||
return
|
||||
}
|
||||
|
||||
contract , st, err := contract.GetContractByID(contractID)
|
||||
contract, st, err := contract.GetContractByID(contractID)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
deviceConnectedToContract := false
|
||||
deviceConnectedToContract := false
|
||||
for _, contractDeviceID := range contract.DeviceIDs {
|
||||
if deviceID == uint64(contractDeviceID) {
|
||||
deviceConnectedToContract = true
|
||||
@@ -122,24 +120,22 @@ func GetDeviceData(c *gin.Context) {
|
||||
}
|
||||
|
||||
if !deviceConnectedToContract {
|
||||
log.Printf("Device %v is not connected to contract %v", deviceID, contractID )
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Device is not present int his contract"})
|
||||
return
|
||||
}
|
||||
log.Printf("Device %v is not connected to contract %v", deviceID, contractID)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Device is not present int his contract"})
|
||||
return
|
||||
}
|
||||
|
||||
featureCollection, st, err := device.GetDeviceInfoForContract(deviceID, contract)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the GeoJSON feature collection
|
||||
c.JSON(http.StatusOK, featureCollection)
|
||||
// Respond with the GeoJSON feature collection
|
||||
c.JSON(http.StatusOK, featureCollection)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func GetDevicesByContract(c *gin.Context) {
|
||||
// Get the contract ID from query parameter
|
||||
contractIDStr := c.DefaultQuery("contract_id", "")
|
||||
@@ -167,5 +163,3 @@ func GetDevicesByContract(c *gin.Context) {
|
||||
// Respond with the devices
|
||||
c.JSON(http.StatusOK, devices)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user