upstream sync
This commit is contained in:
@@ -1,17 +1,22 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jinzhu/gorm"
|
||||
"gitlab.com/pactual1/backend/config"
|
||||
"gitlab.com/pactual1/backend/database/contract"
|
||||
"gitlab.com/pactual1/backend/database/device"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/services/blockchain"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
@@ -37,18 +42,18 @@ func SaveDeviceInfo(c *gin.Context) {
|
||||
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{
|
||||
device = models.Device{
|
||||
IMEI: deviceInfo.IMEI,
|
||||
IMSI: deviceInfo.IMSI,
|
||||
DeviceID: deviceInfo.ExternalDeviceID,
|
||||
DeviceConfiguration: string(rawData),
|
||||
}
|
||||
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
|
||||
if err := shared.GetDb().Create(&device).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
|
||||
deviceInfo.DeviceID = device.ID
|
||||
} else {
|
||||
log.Printf("CREATE -Device DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
@@ -76,6 +81,33 @@ func SaveDeviceInfo(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info"})
|
||||
return
|
||||
}
|
||||
|
||||
if device.CurrentContractID != nil {
|
||||
deviceContract, _, err := contract.GetContractByID(*device.CurrentContractID)
|
||||
if err != nil {
|
||||
log.Printf("SaveDeviceInfo - GetContractByID error : %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch device contract"})
|
||||
return
|
||||
}
|
||||
|
||||
if deviceContract.Status == models.ContractStatusActive {
|
||||
deviceInfoBytes, _ := json.Marshal(deviceInfo)
|
||||
deviceInfoEncryptedStr, err := shared.NewEncryptionClient(config.AppConfig.Service.BlockchainSecret).Encrypt(string(deviceInfoBytes))
|
||||
if err != nil {
|
||||
log.Printf("SaveDeviceInfo - Enrypt error : %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt device info"})
|
||||
return
|
||||
}
|
||||
|
||||
err = blockchain.NewService(config.AppConfig.Blockchain).AddIOTData(context.Background(), shared.CovertUintToByte32(deviceContract.ID), shared.CovertUintToByte32(device.ID), big.NewInt(time.Now().Unix()), []byte(deviceInfoEncryptedStr))
|
||||
if err != nil {
|
||||
log.Printf("SaveDeviceInfo CREATE -DeviceInfo Blockchain Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info in blockchain"})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
@@ -105,8 +137,7 @@ func GetDeviceData(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
contract, st, err := contract.GetContractByID(contractID)
|
||||
|
||||
contract, st, err := contract.GetContractByID(uint(contractID))
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user