Added temperature exceded event
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/services/messaging"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
@@ -99,3 +100,74 @@ func GetDeviceInfoForContract(deviceID uint64, contract models.Contract) (models
|
||||
return featureCollection, 0, nil
|
||||
|
||||
}
|
||||
|
||||
func SaveDeviceInfoToDB(deviceInfo models.DeviceInfo, rawData []byte) (models.DeviceInfo, models.Device, error) {
|
||||
deviceInfo.RawJSON = string(rawData)
|
||||
deviceInfo.X = deviceInfo.AccInfo.X
|
||||
deviceInfo.Y = deviceInfo.AccInfo.Y
|
||||
deviceInfo.Z = deviceInfo.AccInfo.Z
|
||||
|
||||
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) {
|
||||
newDevice := models.Device{
|
||||
IMEI: deviceInfo.IMEI,
|
||||
IMSI: deviceInfo.IMSI,
|
||||
DeviceID: deviceInfo.ExternalDeviceID,
|
||||
DeviceConfiguration: string(rawData),
|
||||
}
|
||||
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
|
||||
return deviceInfo, device, fmt.Errorf("CREATE -Device DB Error: %v", err)
|
||||
}
|
||||
deviceInfo.DeviceID = newDevice.ID
|
||||
} else {
|
||||
return deviceInfo, device, fmt.Errorf("CREATE -Device DB Error: %v", err)
|
||||
}
|
||||
} else {
|
||||
if device.DeletedAt.Valid {
|
||||
if err := shared.GetDb().Exec("UPDATE devices SET deleted_at = NULL, company_id = NULL WHERE id = ?", device.ID).Error; err != nil {
|
||||
return deviceInfo, device, fmt.Errorf("UNDELETE -Device DB Error: %v", err)
|
||||
}
|
||||
}
|
||||
deviceInfo.DeviceID = device.ID
|
||||
}
|
||||
|
||||
if err := shared.GetDb().Create(&deviceInfo).Error; err != nil {
|
||||
return deviceInfo, device, fmt.Errorf("SaveDeviceInfo CREATE -DeviceInfo DB Error: %v", err)
|
||||
}
|
||||
|
||||
|
||||
go func(deviceInfo models.DeviceInfo) {
|
||||
var contract models.Contract
|
||||
if err := shared.GetDb().Where("device_ids @> ARRAY[?]::integer[]", deviceInfo.DeviceID).First(&contract).Error; err != nil {
|
||||
log.Printf("could not find associated contract: %v", err)
|
||||
|
||||
}
|
||||
|
||||
messagingChannel:= messaging.GetMessagingChannel()
|
||||
|
||||
if deviceInfo.Temperature > contract.MaxTemp || deviceInfo.Temperature < contract.MinTemp {
|
||||
|
||||
notification := models.Notification{
|
||||
Title: "Temperature Alert",
|
||||
NotificationType: "Temperature",
|
||||
Text: fmt.Sprintf("Device %s has a temperature outside the permitted range.", deviceInfo.ExternalDeviceID),
|
||||
CompanyID: int(contract.BuyerID),
|
||||
}
|
||||
|
||||
sellerNotification := models.Notification{
|
||||
Title: "Temperature Alert",
|
||||
NotificationType: "Temperature",
|
||||
Text: fmt.Sprintf("Device %s has a temperature outside the permitted range.", deviceInfo.ExternalDeviceID),
|
||||
CompanyID: int(contract.SellerID),
|
||||
}
|
||||
|
||||
messagingChannel <- notification
|
||||
messagingChannel <- sellerNotification
|
||||
}
|
||||
}(deviceInfo)
|
||||
|
||||
|
||||
|
||||
return deviceInfo, device, nil
|
||||
}
|
||||
@@ -12,18 +12,18 @@ import (
|
||||
)
|
||||
|
||||
|
||||
func GetNotificationsForUserID(userID int, afterTime time.Time) ([]models.GetNotificationsResponse, int, error) {
|
||||
func GetNotificationsForCompanyID(companyID int, afterTime time.Time) ([]models.GetNotificationsResponse, int, error) {
|
||||
// Create a slice to hold the notifications
|
||||
var notifications []models.Notification
|
||||
var customNotifications []models.GetNotificationsResponse
|
||||
|
||||
// Fetch notifications from the database based on UserID and creation time
|
||||
if err := shared.GetDb().Where("user_id = ? AND created_at > ?", userID, afterTime).Find(¬ifications).Error; err != nil {
|
||||
if err := shared.GetDb().Where("company_id = ? AND created_at > ?", companyID, afterTime).Find(¬ifications).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("GetNotificationsForUserID Error: No notifications found: %v", err)
|
||||
log.Printf("GetNotificationsForCompanyID Error: No notifications found: %v", err)
|
||||
return customNotifications, http.StatusNotFound, err
|
||||
} else {
|
||||
log.Printf("GetNotificationsForUserID Error: Database error: %v", err)
|
||||
log.Printf("GetNotificationsForCompanyID Error: Database error: %v", err)
|
||||
return customNotifications, http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user