Added temperature exceded event

This commit is contained in:
Nedim
2023-10-16 19:43:35 +02:00
parent 44efa194fc
commit 2e60460131
7 changed files with 109 additions and 70 deletions

View File

@@ -3,7 +3,6 @@ package controllers
import (
"context"
"encoding/json"
"errors"
"log"
"math/big"
"net/http"
@@ -11,7 +10,6 @@ import (
"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"
@@ -28,62 +26,18 @@ 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 payload : %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
device = models.Device{
IMEI: deviceInfo.IMEI,
IMSI: deviceInfo.IMSI,
DeviceID: deviceInfo.ExternalDeviceID,
DeviceConfiguration: string(rawData),
}
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 = device.ID
} else {
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.Valid {
// 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)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info"})
deviceInfo, currentDevice, err := device.SaveDeviceInfoToDB(deviceInfo, rawData)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if device.CurrentContractID != nil {
deviceContract, _, err := contract.GetContractByID(*device.CurrentContractID)
if currentDevice.CurrentContractID != nil {
deviceContract, _, err := contract.GetContractByID(*currentDevice.CurrentContractID)
if err != nil {
log.Printf("SaveDeviceInfo - GetContractByID error : %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch device contract"})
@@ -99,7 +53,7 @@ func SaveDeviceInfo(c *gin.Context) {
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))
err = blockchain.NewService(config.AppConfig.Blockchain).AddIOTData(context.Background(), shared.CovertUintToByte32(deviceContract.ID), shared.CovertUintToByte32(currentDevice.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"})
@@ -108,6 +62,7 @@ func SaveDeviceInfo(c *gin.Context) {
}
}
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})
}

View File

@@ -11,20 +11,20 @@ import (
)
func GetNotifications(c *gin.Context) {
// Get the User ID and Time from query parameters
userIDStr := c.DefaultQuery("user_id", "")
// Get the Company ID and Time from query parameters
companyIDStr := c.DefaultQuery("company_id", "")
timeStr := c.DefaultQuery("time", "")
if userIDStr == "" || timeStr == "" {
log.Printf("GetNotifications Error: User ID and Time are required")
c.JSON(http.StatusBadRequest, gin.H{"error": "User ID and Time are required"})
if companyIDStr == "" || timeStr == "" {
log.Printf("GetNotifications Error: Company ID and Time are required")
c.JSON(http.StatusBadRequest, gin.H{"error": "Company ID and Time are required"})
return
}
// Convert string to int for UserID
userID, err := strconv.Atoi(userIDStr)
userID, err := strconv.Atoi(companyIDStr)
if err != nil {
log.Printf("GetNotifications Error: Invalid User ID: %v", err)
log.Printf("GetNotifications Error: Invalid Company ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid User ID"})
return
}
@@ -37,8 +37,8 @@ func GetNotifications(c *gin.Context) {
}
afterTime := time.Unix(timestamp, 0)
log.Printf("This is the User ID: %v and Time: %v", userID, afterTime)
notifications, st, err := notification.GetNotificationsForUserID(userID, afterTime)
log.Printf("This is the Company ID: %v and Time: %v", userID, afterTime)
notifications, st, err := notification.GetNotificationsForCompanyID(userID, afterTime)
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})