package services import ( "basic-sensor-receiver/models" "crypto/rand" "math/big" ) type SensorsService struct { ctx *Context } func (s *SensorsService) GetList() ([]models.SensorItem, error) { sensors := []models.SensorItem{} err := s.ctx.DB.Select(&sensors, "SELECT id, name, type, mqtt_broker_id, mqtt_topic, mqtt_path, auth_key, last_contact_at FROM sensors") if err != nil { return nil, err } return sensors, nil } func (s *SensorsService) Create(name string, sensorType *string, mqttBrokerId *int, mqttTopic *string, mqttPath *string) (*models.SensorItem, error) { authKey, err := generateRandomString(32) if err != nil { return nil, err } if sensorType == nil { sensorType = new(string) *sensorType = "rest" } item := models.SensorItem{ Name: name, AuthKey: authKey, Type: sensorType, MqttBrokerId: mqttBrokerId, MqttTopic: mqttTopic, MqttPath: mqttPath, } res, err := s.ctx.DB.Exec("INSERT INTO sensors (name, auth_key, type, mqtt_broker_id, mqtt_topic, mqtt_path) VALUES (?, ?, ?, ?, ?, ?)", item.Name, item.AuthKey, item.Type, item.MqttBrokerId, item.MqttTopic, item.MqttPath) if err != nil { return nil, err } item.Id, err = res.LastInsertId() if err != nil { return nil, err } s.ctx.Services.MQTTBrokers.EnsureListeners() return &item, nil } func (s *SensorsService) GetById(id int64) (*models.SensorItem, error) { item := models.SensorItem{} err := s.ctx.DB.Get(&item, "SELECT id, name, auth_key, last_contact_at, type, mqtt_broker_id, mqtt_topic, mqtt_path FROM sensors WHERE id = $1", id) if err != nil { return nil, err } return &item, nil } func (s *SensorsService) Update(id int64, name string, sensorType *string, mqttBrokerId *int, mqttTopic *string, mqttPath *string) (*models.SensorItem, error) { item, err := s.GetById(id) if err != nil { return nil, err } if sensorType == nil { sensorType = new(string) *sensorType = "rest" } item.Name = name item.Type = sensorType item.MqttBrokerId = mqttBrokerId item.MqttTopic = mqttTopic item.MqttPath = mqttPath _, err = s.ctx.DB.Exec("UPDATE sensors SET name = ?, type = ?, mqtt_broker_id = ?, mqtt_topic = ?, mqtt_path = ? WHERE id = ?", item.Name, item.Type, item.MqttBrokerId, item.MqttTopic, item.MqttPath, item.Id) if err != nil { return nil, err } s.ctx.Services.MQTTBrokers.EnsureListeners() return item, nil } 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) s.ctx.Services.MQTTBrokers.EnsureListeners() return err } 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 }