2023-09-12 18:28:18 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
2023-10-19 07:57:48 +02:00
|
|
|
"context"
|
2023-09-12 18:28:18 +02:00
|
|
|
"encoding/json"
|
2023-09-13 21:54:43 +02:00
|
|
|
"log"
|
2023-10-19 07:57:48 +02:00
|
|
|
"math/big"
|
2023-09-12 18:28:18 +02:00
|
|
|
"net/http"
|
2023-09-19 08:15:42 +02:00
|
|
|
"strconv"
|
2023-10-19 07:57:48 +02:00
|
|
|
"time"
|
2023-09-12 18:28:18 +02:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-10-19 07:57:48 +02:00
|
|
|
"gitlab.com/pactual1/backend/config"
|
2023-09-22 11:38:32 +02:00
|
|
|
"gitlab.com/pactual1/backend/database/contract"
|
|
|
|
|
"gitlab.com/pactual1/backend/database/device"
|
2023-09-12 18:28:18 +02:00
|
|
|
"gitlab.com/pactual1/backend/models"
|
2023-10-19 07:57:48 +02:00
|
|
|
"gitlab.com/pactual1/backend/services/blockchain"
|
2023-09-12 18:28:18 +02:00
|
|
|
"gitlab.com/pactual1/backend/shared"
|
|
|
|
|
)
|
|
|
|
|
|
2023-09-19 05:09:33 +02:00
|
|
|
func SaveDeviceInfo(c *gin.Context) {
|
2023-09-12 18:28:18 +02:00
|
|
|
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"})
|
2023-10-16 19:43:35 +02:00
|
|
|
log.Printf("Invalid json payload : %v", err)
|
2023-09-12 18:28:18 +02:00
|
|
|
return
|
|
|
|
|
}
|
2023-09-13 21:54:43 +02:00
|
|
|
|
2023-10-16 19:43:35 +02:00
|
|
|
deviceInfo, currentDevice, err := device.SaveDeviceInfoToDB(deviceInfo, rawData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
2023-09-13 21:54:43 +02:00
|
|
|
return
|
|
|
|
|
}
|
2023-10-19 07:57:48 +02:00
|
|
|
|
2023-10-16 19:43:35 +02:00
|
|
|
if currentDevice.CurrentContractID != nil {
|
|
|
|
|
deviceContract, _, err := contract.GetContractByID(*currentDevice.CurrentContractID)
|
2023-10-19 07:57:48 +02:00
|
|
|
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)
|
2023-10-24 05:48:19 +02:00
|
|
|
if deviceContract.BlockchainSecret == "" {
|
|
|
|
|
deviceContract.BlockchainSecret = shared.GenerateRandomString(BlockchainSecretLength)
|
|
|
|
|
_, _, err := contract.UpdateContract(deviceContract)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("SaveDeviceInfo Update Contract error: %v", err)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not update contract secret"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
deviceInfoEncryptedStr, err := shared.NewEncryptionClient(deviceContract.BlockchainSecret).Encrypt(string(deviceInfoBytes))
|
2023-10-19 07:57:48 +02:00
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("SaveDeviceInfo - Enrypt error : %v", err)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt device info"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 19:43:35 +02:00
|
|
|
err = blockchain.NewService(config.AppConfig.Blockchain).AddIOTData(context.Background(), shared.CovertUintToByte32(deviceContract.ID), shared.CovertUintToByte32(currentDevice.ID), big.NewInt(time.Now().Unix()), []byte(deviceInfoEncryptedStr))
|
2023-10-19 07:57:48 +02:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 19:43:35 +02:00
|
|
|
|
2023-09-27 08:04:50 +02:00
|
|
|
log.Printf("Successfully received and saved device info: %v", deviceInfo)
|
2023-09-13 21:54:43 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved device info", "data": deviceInfo})
|
2023-09-12 18:28:18 +02:00
|
|
|
}
|
2023-09-19 08:15:42 +02:00
|
|
|
|
|
|
|
|
func GetDeviceData(c *gin.Context) {
|
2023-09-27 08:04:50 +02:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 07:57:48 +02:00
|
|
|
contract, st, err := contract.GetContractByID(uint(contractID))
|
2023-09-27 08:04:50 +02:00
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(st, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deviceConnectedToContract := false
|
2023-09-22 08:28:50 +02:00
|
|
|
for _, contractDeviceID := range contract.DeviceIDs {
|
|
|
|
|
if deviceID == uint64(contractDeviceID) {
|
|
|
|
|
deviceConnectedToContract = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !deviceConnectedToContract {
|
2023-09-27 08:04:50 +02:00
|
|
|
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
|
|
|
|
|
}
|
2023-09-22 08:28:50 +02:00
|
|
|
|
2023-09-22 11:38:32 +02:00
|
|
|
featureCollection, st, err := device.GetDeviceInfoForContract(deviceID, contract)
|
2023-09-19 08:15:42 +02:00
|
|
|
|
2023-09-22 11:38:32 +02:00
|
|
|
if err != nil {
|
2023-09-27 08:04:50 +02:00
|
|
|
c.JSON(st, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-19 08:15:42 +02:00
|
|
|
|
2023-09-27 08:04:50 +02:00
|
|
|
// Respond with the GeoJSON feature collection
|
2023-10-03 18:26:57 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"data": featureCollection})
|
2023-09-19 08:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetDevicesByContract(c *gin.Context) {
|
|
|
|
|
// Get the contract ID from query parameter
|
|
|
|
|
contractIDStr := c.DefaultQuery("contract_id", "")
|
|
|
|
|
if contractIDStr == "" {
|
|
|
|
|
log.Printf("GetDevicesByContract Error: Contract ID is required")
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Contract ID is required"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert string to uint
|
|
|
|
|
contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("GetDevicesByContract Error: Invalid Contract ID: %v", err)
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Contract ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("This is the ID: %v", contractID)
|
2023-09-22 11:38:32 +02:00
|
|
|
devices, st, err := device.GetDevicesForContract(contractID)
|
2023-09-19 08:15:42 +02:00
|
|
|
|
2023-09-22 11:38:32 +02:00
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(st, gin.H{"error": err.Error()})
|
2023-09-19 08:15:42 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Respond with the devices
|
2023-10-13 11:48:14 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"data": models.ConvertDeviceToResponse(devices)})
|
2023-09-19 08:15:42 +02:00
|
|
|
}
|