2022-08-13 23:33:50 +02:00
|
|
|
package services
|
|
|
|
|
|
2022-08-28 12:30:37 +02:00
|
|
|
import (
|
2024-03-29 20:49:08 +01:00
|
|
|
"basic-sensor-receiver/models"
|
2022-08-28 12:30:37 +02:00
|
|
|
"crypto/rand"
|
|
|
|
|
"math/big"
|
|
|
|
|
)
|
2022-08-28 11:56:03 +02:00
|
|
|
|
2022-08-13 23:33:50 +02:00
|
|
|
type SensorsService struct {
|
|
|
|
|
ctx *Context
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:49:08 +01:00
|
|
|
func (s *SensorsService) GetList() ([]models.SensorItem, error) {
|
2024-04-01 17:03:45 +02:00
|
|
|
sensors := []models.SensorItem{}
|
2022-08-13 23:33:50 +02:00
|
|
|
|
2024-04-01 17:03:45 +02:00
|
|
|
err := s.ctx.DB.Select(&sensors, "SELECT id, name, auth_key, last_contact_at FROM sensors")
|
2022-08-13 23:33:50 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sensors, nil
|
|
|
|
|
}
|
2022-08-28 11:56:03 +02:00
|
|
|
|
2024-03-29 20:49:08 +01:00
|
|
|
func (s *SensorsService) Create(name string) (*models.SensorItem, error) {
|
2022-08-28 12:30:37 +02:00
|
|
|
authKey, err := generateRandomString(32)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:49:08 +01:00
|
|
|
item := models.SensorItem{
|
2022-08-28 11:56:03 +02:00
|
|
|
Name: name,
|
2022-08-28 12:30:37 +02:00
|
|
|
AuthKey: authKey,
|
2022-08-28 11:56:03 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-28 12:30:37 +02:00
|
|
|
res, err := s.ctx.DB.Exec("INSERT INTO sensors (name, auth_key) VALUES (?, ?)", item.Name, item.AuthKey)
|
2022-08-28 11:56:03 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.Id, err = res.LastInsertId()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:49:08 +01:00
|
|
|
func (s *SensorsService) GetById(id int64) (*models.SensorItem, error) {
|
|
|
|
|
item := models.SensorItem{}
|
2022-08-28 11:56:03 +02:00
|
|
|
|
2024-04-01 17:03:45 +02:00
|
|
|
err := s.ctx.DB.Get(&item, "SELECT id, name, auth_key, last_contact_at FROM sensors WHERE id = $1", id)
|
2022-08-28 11:56:03 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:49:08 +01:00
|
|
|
func (s *SensorsService) Update(id int64, name string) (*models.SensorItem, error) {
|
2022-08-28 11:56:03 +02:00
|
|
|
item, err := s.GetById(id)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.Name = name
|
|
|
|
|
|
|
|
|
|
_, err = s.ctx.DB.Exec("UPDATE sensors SET name = ? WHERE id = ?", item.Name, item.Id)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item, nil
|
|
|
|
|
}
|
2022-08-28 12:30:37 +02:00
|
|
|
|
2022-08-28 21:47:36 +02:00
|
|
|
func (s *SensorsService) DeleteById(id int64) error {
|
|
|
|
|
item, err := s.GetById(id)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = s.ctx.DB.Exec("DELETE FROM sensor_values WHERE sensor_id = ?", item.Id)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = s.ctx.DB.Exec("DELETE FROM sensors WHERE id = ?", item.Id)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 12:30:37 +02:00
|
|
|
var randomKeySource = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
|
|
|
|
|
|
func generateRandomString(length int) (string, error) {
|
|
|
|
|
key := make([]byte, length)
|
|
|
|
|
|
|
|
|
|
for i := range key {
|
|
|
|
|
value, err := rand.Int(rand.Reader, big.NewInt(int64(len(randomKeySource))))
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key[i] = randomKeySource[value.Int64()]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string(key), nil
|
|
|
|
|
}
|