fix: gnu-cc, missing humidity table, restrict repository select

This commit is contained in:
Markus Pesch 2020-06-27 21:12:00 +02:00
parent 92a776f6ce
commit 8c1bd57ad5
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
9 changed files with 69 additions and 15 deletions

View File

@ -48,6 +48,7 @@ bin/linux/arm/5/${EXECUTABLE}: bindata
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@} go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/linux/arm/7/${EXECUTABLE}: bindata bin/linux/arm/7/${EXECUTABLE}: bindata
CC=arm-linux-gnueabihf-gcc \
CGO_ENABLED=1 \ CGO_ENABLED=1 \
GOOS=linux \ GOOS=linux \
GOARCH=arm \ GOARCH=arm \

View File

@ -83,7 +83,7 @@ func preRunError(cmd *cobra.Command, args []string) error {
Name: hostname, Name: hostname,
CreationDate: postgresTimeStamp, CreationDate: postgresTimeStamp,
}, },
DSN: "sqlite3:///var/log/flucky/sqlite.db?cache=shared&mode=memory&FKSupport=True", DSN: "sqlite3:///var/log/flucky/sqlite.db?cache=shared&mode=memory&foreign_keys=on",
} }
err = config.Write(&cnf, configFile) err = config.Write(&cnf, configFile)

View File

@ -164,6 +164,8 @@ func addSensor(cmd *cobra.Command, args []string) error {
return err return err
} }
sensor.DeviceID = cnf.Device.ID
dsnURL, err := url.Parse(cnf.DSN) dsnURL, err := url.Parse(cnf.DSN)
if err != nil { if err != nil {
return err return err
@ -290,7 +292,7 @@ func listSensors(cmd *cobra.Command, args []string) error {
} }
// add sensor entry to list // add sensor entry to list
sensors, err := repo.GetSensors() sensors, err := repo.GetSensorsByDeviceID(cnf.Device.ID)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,10 +2,10 @@ package daemon
import ( import (
"context" "context"
"fmt"
"net/url" "net/url"
"os" "os"
"os/signal" "os/signal"
"syscall"
"git.cryptic.systems/volker.raschek/flucky/pkg/config" "git.cryptic.systems/volker.raschek/flucky/pkg/config"
"git.cryptic.systems/volker.raschek/flucky/pkg/repository" "git.cryptic.systems/volker.raschek/flucky/pkg/repository"
@ -29,17 +29,39 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
return err return err
} }
repoSensors, err := repo.GetSensors() // Add
if err != nil { repoDevice, err := repo.GetDevice(cnf.Device.ID)
switch {
case err != nil:
return err return err
case repoDevice == nil:
err = repo.AddDevices(cnf.Device)
if err != nil {
return err
}
repoDevice, err = repo.GetDevice(cnf.Device.ID)
if err != nil {
return err
}
}
repoSensors, err := repo.GetSensorsByDeviceID(repoDevice.ID)
switch {
case err != nil:
return err
case repoSensors == nil, len(repoSensors) <= 0:
return fmt.Errorf("No sensors found")
} }
sensors := make([]sensor.Sensor, 0) sensors := make([]sensor.Sensor, 0)
for _, repoSensor := range repoSensors { for _, repoSensor := range repoSensors {
if !repoSensor.Enabled { if !repoSensor.Enabled || repoSensor.DeviceID != repoDevice.ID {
continue continue
} }
flogger.Debug("Found sensor %v", repoSensor.GetName())
sensor, err := sensor.New(repoSensor) sensor, err := sensor.New(repoSensor)
if err != nil { if err != nil {
return err return err
@ -48,7 +70,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
} }
interruptChannel := make(chan os.Signal, 1) interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Kill, syscall.SIGTERM) signal.Notify(interruptChannel, os.Interrupt, os.Kill)
// Collection // Collection
parentCtx := context.Background() parentCtx := context.Background()
@ -82,7 +104,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
measuredValues = append(measuredValues, measuredValue) measuredValues = append(measuredValues, measuredValue)
if cap(measuredValues) == len(measuredValues) { if cap(measuredValues) == len(measuredValues) {
flogger.Debug("Flush cache") flogger.Debug("Flush cache with %v values", len(measuredValues))
err := repo.AddMeasuredValues(measuredValues...) err := repo.AddMeasuredValues(measuredValues...)
if err != nil { if err != nil {
flogger.Error("%v", err) flogger.Error("%v", err)
@ -90,16 +112,26 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
measuredValues = make([]*types.MeasuredValue, 0, 10) measuredValues = make([]*types.MeasuredValue, 0, 10)
} }
case <-interruptChannel: case signal := <-interruptChannel:
cancel() cancel()
close(measuredValueChannel) close(measuredValueChannel)
flogger.Info("Stopping daemon: Received process signal %v", signal.String())
flogger.Debug("Flush cache with %v remaining values", len(measuredValues))
err := repo.AddMeasuredValues(measuredValues...) err := repo.AddMeasuredValues(measuredValues...)
if err != nil { if err != nil {
flogger.Error("%v", err) flogger.Error("%v", err)
} }
break flogger.Debug("Close repository")
err = repo.Close()
if err != nil {
flogger.Error("%v", err)
}
return nil
} }
} }
} }

View File

@ -16,6 +16,7 @@ import (
// Database is a general interface for a database backend like postgres, oracle // Database is a general interface for a database backend like postgres, oracle
// or sqlite // or sqlite
type Database interface { type Database interface {
Close() error
DeleteDevices(ctx context.Context, deviceIDs ...string) error DeleteDevices(ctx context.Context, deviceIDs ...string) error
DeleteSensors(ctx context.Context, sensorIDs ...string) error DeleteSensors(ctx context.Context, sensorIDs ...string) error
InsertDevices(ctx context.Context, devices ...*types.Device) error InsertDevices(ctx context.Context, devices ...*types.Device) error
@ -120,5 +121,4 @@ func New(dsnURL *url.URL, flogger logger.Logger) (Database, error) {
} }
return database, nil return database, nil
} }

View File

@ -17,6 +17,12 @@ type Postgres struct {
queries map[string]string queries map[string]string
} }
// Close closes the database and prevents new queries from starting. Close then
// waits for all queries that have started processing on the server to finish.
func (postgres *Postgres) Close() error {
return postgres.dbo.Close()
}
// DeleteDevices from the database // DeleteDevices from the database
func (postgres *Postgres) DeleteDevices(ctx context.Context, deviceIDs ...string) error { func (postgres *Postgres) DeleteDevices(ctx context.Context, deviceIDs ...string) error {
queryFile := "deleteDevice.sql" queryFile := "deleteDevice.sql"
@ -228,7 +234,7 @@ func (postgres *Postgres) Scheme(ctx context.Context) error {
for _, query := range []string{ for _, query := range []string{
postgres.queries["createTableDevices.sql"], postgres.queries["createTableDevices.sql"],
postgres.queries["createTableSensors.sql"], postgres.queries["createTableSensors.sql"],
postgres.queries["createTableHumidites.sql"], postgres.queries["createTableHumidities.sql"],
postgres.queries["createTablePressures.sql"], postgres.queries["createTablePressures.sql"],
postgres.queries["createTableTemperatures.sql"], postgres.queries["createTableTemperatures.sql"],
} { } {

View File

@ -16,6 +16,12 @@ type SQLite struct {
queries map[string]string queries map[string]string
} }
// Close closes the database and prevents new queries from starting. Close then
// waits for all queries that have started processing on the server to finish.
func (sqlite *SQLite) Close() error {
return sqlite.dbo.Close()
}
// DeleteDevices from the database // DeleteDevices from the database
func (sqlite *SQLite) DeleteDevices(ctx context.Context, deviceIDs ...string) error { func (sqlite *SQLite) DeleteDevices(ctx context.Context, deviceIDs ...string) error {
queryFile := "deleteDevice.sql" queryFile := "deleteDevice.sql"
@ -227,7 +233,7 @@ func (sqlite *SQLite) Scheme(ctx context.Context) error {
for _, query := range []string{ for _, query := range []string{
sqlite.queries["createTableDevices.sql"], sqlite.queries["createTableDevices.sql"],
sqlite.queries["createTableSensors.sql"], sqlite.queries["createTableSensors.sql"],
sqlite.queries["createTableHumidites.sql"], sqlite.queries["createTableHumidities.sql"],
sqlite.queries["createTablePressures.sql"], sqlite.queries["createTablePressures.sql"],
sqlite.queries["createTableTemperatures.sql"], sqlite.queries["createTableTemperatures.sql"],
} { } {

View File

@ -31,6 +31,13 @@ func (repo *Repository) AddSensors(sensors ...*types.Sensor) error {
return repo.database.InsertSensors(context.Background(), sensors...) return repo.database.InsertSensors(context.Background(), sensors...)
} }
// Close closes the repository and prevents new queries from starting. Close
// then waits for all queries that have started processing on the server to
// finish.
func (repo *Repository) Close() error {
return repo.database.Close()
}
// DisableSensorsByNames disable all sensors which match bei their name // DisableSensorsByNames disable all sensors which match bei their name
func (repo *Repository) DisableSensorsByNames(sensorNames ...string) error { func (repo *Repository) DisableSensorsByNames(sensorNames ...string) error {
sensors, err := repo.GetSensors() sensors, err := repo.GetSensors()

View File

@ -278,7 +278,7 @@ func testBackend(t *testing.T, repo *repository.Repository) {
{ {
ID: "2e5a297a-3da0-46ae-89d2-0fcab0f1d5f7", ID: "2e5a297a-3da0-46ae-89d2-0fcab0f1d5f7",
Value: 32, Value: 32,
ValueType: "temperature", ValueType: "humidity",
Date: *timeNow(require), Date: *timeNow(require),
SensorID: "8c74397f-8e60-4c9d-960d-3197747cef9a", SensorID: "8c74397f-8e60-4c9d-960d-3197747cef9a",
CreationDate: *timeNow(require), CreationDate: *timeNow(require),
@ -287,7 +287,7 @@ func testBackend(t *testing.T, repo *repository.Repository) {
{ {
ID: "d69f1b62-0c6c-4058-b42c-4a2821bd220c", ID: "d69f1b62-0c6c-4058-b42c-4a2821bd220c",
Value: 38, Value: 38,
ValueType: "temperature", ValueType: "pressure",
Date: *timeNow(require), Date: *timeNow(require),
SensorID: "8c74397f-8e60-4c9d-960d-3197747cef9a", SensorID: "8c74397f-8e60-4c9d-960d-3197747cef9a",
CreationDate: *timeNow(require), CreationDate: *timeNow(require),