first handler writing to database

This commit is contained in:
2023-12-01 18:57:56 +01:00
parent aa062a79ab
commit ca004dce51
8 changed files with 219 additions and 39 deletions

View File

@ -3,15 +3,20 @@ package database
import "time"
import "gorm.io/gorm"
type VariableType struct {
Label string `json:"label"`
Variable string `json:"variable"`
Unit string `json:"unit"`
Value interface{} `json:"value,omitempty"`
}
type Measurement struct {
Time time.Time `gorm:"not null;primary_key"`
Application string `gorm:"not null"`
SensorType string `gorm:"not null"`
Sensor string `gorm:"not null"`
Variable string `gorm:"not null"`
Unit string
Value float32 `gorm:"not null"`
Device string
Attributes map[string]string `gorm:"serializer:json;type:jsonb"`
Values map[string]VariableType `gorm:"serializer:json;type:jsonb"`
}
type Application struct {
@ -20,21 +25,21 @@ type Application struct {
Attributes map[string]string `gorm:"serializer:json;type:jsonb"`
}
type SensorType struct {
type DeviceType struct {
gorm.Model
Label string `gorm:"not null"`
Variable string `gorm:"not null"`
Unit string
ModelIdentifier string
Attributes map[string]string `gorm:"serializer:json;type:jsonb"`
}
type Sensor struct {
type Device struct {
gorm.Model
Label string `gorm:"not null"`
ApplicationID int `gorm:"not null"`
Application Application
SensorTypeID int `gorm:"not null"`
SensorType SensorType
DeviceTypeID int `gorm:"not null"`
DeviceType DeviceType
Attributes map[string]string `gorm:"serializer:json;type:jsonb"`
}

View File

@ -1,28 +1,45 @@
package database
import (
"log"
"time"
//"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func Test() {
dsn := ""
db, err := gorm.Open(postgres.Open(dsn))
log.Printf("Database: %s, %s", db, err)
db.AutoMigrate(&Application{})
db.AutoMigrate(&SensorType{})
db.AutoMigrate(&Sensor{})
db.AutoMigrate(&Measurement{})
n := time.Now()
m := Measurement { Time: n, Application: "bla", SensorType: "bla", Sensor: "bla", Variable: "bla", Unit: "bla", Value: 1.0 }
db.Create(&m)
m = Measurement { Time: n, Application: "bla", SensorType: "bla", Sensor: "bla", Variable: "bla", Unit: "bla", Value: 1.0 }
db.Create(&m)
type DatabaseHandle struct {
initialized bool
dbh *gorm.DB
}
func NewDatabaseHandle(dsn string) *DatabaseHandle {
var db DatabaseHandle
conn, err := gorm.Open(postgres.Open(dsn))
if err != nil {
log.Printf("Unable to open database connection: %s", err)
db.initialized = false
} else {
db.dbh = conn
db.initialized = true
log.Println("Database connection opened")
}
return &db
}
func (dbh *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
if ! dbh.initialized {
log.Println("Database connection not initialized, can not store, measurement lost")
return
}
result := dbh.dbh.Create(measurement)
if result.Error != nil {
log.Printf("Unable to insert measurement: %s", result.Error)
return
}
log.Println("Successfully stored measurement")
}

View File

@ -0,0 +1,55 @@
package database
import (
"log"
//"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func Migrate() {
dsn := ""
db, err := gorm.Open(postgres.Open(dsn))
if err != nil {
log.Fatalf("Unable to open database connection: %s", err)
}
db.AutoMigrate(&Application{})
log.Println("Application created")
db.AutoMigrate(&DeviceType{})
log.Println("DeviceType created")
db.AutoMigrate(&Device{})
log.Println("Device created")
db.AutoMigrate(&Measurement{})
log.Println("Measurement created")
log.Println("Remember to call create_hypertable on measurements, sowhat I can't do that for you.")
/*
m := Measurement {
Time: time.Now(),
Application: "app",
Attributes: nil,
Values: []SensorType {
{ Variable: "Temperature", Unit: "Degree Celsius", Value: 1.0 },
{ Variable: "Temperature", Unit: "Degree Celsius", Value: 3.0 },
},
}
db.Create(&m)
m = Measurement {
Time: time.Now(),
Application: "app",
Attributes: nil,
Values: []SensorType {
{ Variable: "Temperature", Unit: "Degree Celsius", Value: 10.0 },
{ Variable: "Temperature", Unit: "Degree Celsius", Value: 30.0 },
},
}
db.Create(&m)
*/
}