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
2025-03-05 11:10:09 +01:00
err := s . ctx . DB . Select ( & sensors , "SELECT id, name, type, mqtt_broker_id, mqtt_topic, mqtt_path, 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
2025-03-05 11:10:09 +01:00
func ( s * SensorsService ) Create ( name string , sensorType * string , mqttBrokerId * int , mqttTopic * string , mqttPath * string ) ( * models . SensorItem , error ) {
2022-08-28 12:30:37 +02:00
authKey , err := generateRandomString ( 32 )
if err != nil {
return nil , err
}
2025-03-05 11:10:09 +01:00
if sensorType == nil {
sensorType = new ( string )
* sensorType = "rest"
}
2024-03-29 20:49:08 +01:00
item := models . SensorItem {
2025-03-05 11:10:09 +01:00
Name : name ,
AuthKey : authKey ,
Type : sensorType ,
MqttBrokerId : mqttBrokerId ,
MqttTopic : mqttTopic ,
MqttPath : mqttPath ,
2022-08-28 11:56:03 +02:00
}
2025-03-05 11:10:09 +01:00
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 )
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
}
2025-03-05 11:10:09 +01:00
s . ctx . Services . MQTTBrokers . EnsureListeners ( )
2022-08-28 11:56:03 +02:00
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
2025-03-05 11:10:09 +01:00
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 )
2022-08-28 11:56:03 +02:00
if err != nil {
return nil , err
}
return & item , nil
}
2025-03-05 11:10:09 +01:00
func ( s * SensorsService ) Update ( id int64 , name string , sensorType * string , mqttBrokerId * int , mqttTopic * string , mqttPath * string ) ( * models . SensorItem , error ) {
2022-08-28 11:56:03 +02:00
item , err := s . GetById ( id )
if err != nil {
return nil , err
}
2025-03-05 11:10:09 +01:00
if sensorType == nil {
sensorType = new ( string )
* sensorType = "rest"
}
2022-08-28 11:56:03 +02:00
item . Name = name
2025-03-05 11:10:09 +01:00
item . Type = sensorType
item . MqttBrokerId = mqttBrokerId
item . MqttTopic = mqttTopic
item . MqttPath = mqttPath
2022-08-28 11:56:03 +02:00
2025-03-05 11:10:09 +01:00
_ , 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 )
2022-08-28 11:56:03 +02:00
if err != nil {
return nil , err
}
2025-03-05 11:10:09 +01:00
s . ctx . Services . MQTTBrokers . EnsureListeners ( )
2022-08-28 11:56:03 +02:00
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 )
2025-03-05 11:10:09 +01:00
s . ctx . Services . MQTTBrokers . EnsureListeners ( )
2022-08-28 21:47:36 +02:00
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
}