Compare commits

...

7 Commits

Author SHA1 Message Date
bd3e9ff43d
fix: golangci-lint and gosec warnings 2021-05-16 23:02:46 +02:00
a745311b7b
fix: rename completion file 2021-05-16 22:09:19 +02:00
05d1cea40b
fix 2021-04-10 17:44:20 +02:00
c22decf1a9
fix 2021-04-10 17:40:49 +02:00
d59c738aa3
fix 2021-04-10 17:37:08 +02:00
df0745c449
fix 2021-04-10 17:33:34 +02:00
1e4f5a9dfd
WIP 2021-04-10 17:29:18 +02:00
21 changed files with 216 additions and 155 deletions

View File

@ -13,6 +13,18 @@ steps:
- pull_request
- tag
- name: trigger
image: plugins/downstream
settings:
server: https://drone.cryptic.systems
token:
from_secret: drone_token
params:
- VERSION=${DRONE_COMMIT_HASH}
fork: true
repositories:
- flucky/PKGBUILD@master
# steps:
# - name: test-unit
# image: docker.io/volkerraschek/build-image:latest

29
.golangci.yml Normal file
View File

@ -0,0 +1,29 @@
run:
skip-dirs:
- it
timeout: 10m
tests: true
linters:
disable-all: true
enable:
# Default
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
# Additionally linters
- bodyclose
- misspell
- nilerr
- rowserrcheck
- sqlclosecheck
- unparam
- whitespace

View File

@ -103,6 +103,18 @@ PHONY+=test/coverage
test/coverage: test/unit
go tool cover -html=coverage.txt
# GOLANGCI-LINT
# ==============================================================================
PHONY+=golangci-lint
golangci-lint:
golangci-lint run --concurrency=$(shell nproc)
# GOSEC
# ==============================================================================
PHONY+=gosec
gosec:
gosec $(shell pwd)/...
# PHONY
# ==============================================================================
# Declare the contents of the PHONY variable as phony. We keep that information

View File

@ -11,15 +11,7 @@ import (
"github.com/spf13/cobra"
)
var (
importSensors bool
importHumidities bool
importPressures bool
importTemperatures bool
)
func InitCmd(cmd *cobra.Command) error {
importCmd := &cobra.Command{
Use: "import",
Args: cobra.RangeArgs(1, 2),
@ -34,7 +26,6 @@ import sqlite3:///var/cache/flucky/sqlite3.db postgres://user:password@host:port
}
func importSources(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")

View File

@ -21,7 +21,6 @@ import (
// Execute a
func Execute(version string) error {
rootCmd := &cobra.Command{
Use: "flucky",
PersistentPreRunE: preRunError,
@ -59,13 +58,11 @@ func Execute(version string) error {
}
func preRunError(cmd *cobra.Command, args []string) error {
configFile := cmd.Flag("config").Value.String()
// check if config file exists
if _, err := os.Stat(configFile); os.IsNotExist(err) {
// Time must be truncted for postgres. Postgres currently does not support
// Time must be truncated for postgres. Postgres currently does not support
// nanoseconds which is automatically include into the go time object
postgresTimeStamp := time.Now()
location, err := time.LoadLocation("Europe/Berlin")

View File

@ -98,7 +98,6 @@ flucky sensor rename f98b00ea-a9b2-4e00-924f-113859d0af2d outdoor`,
}
func addSensor(cmd *cobra.Command, args []string) error {
sensor := &types.Sensor{
ID: uuid.NewV4().String(),
Name: args[0],
@ -200,7 +199,6 @@ func addSensor(cmd *cobra.Command, args []string) error {
}
func disableSensor(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
@ -246,7 +244,6 @@ func disableSensor(cmd *cobra.Command, args []string) error {
}
func enableSensor(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
@ -292,7 +289,6 @@ func enableSensor(cmd *cobra.Command, args []string) error {
}
func listSensors(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
@ -335,7 +331,6 @@ func listSensors(cmd *cobra.Command, args []string) error {
}
func removeSensor(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
@ -367,7 +362,6 @@ func removeSensor(cmd *cobra.Command, args []string) error {
}
func renameSensor(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("No config file defined")
@ -410,5 +404,4 @@ func renameSensor(cmd *cobra.Command, args []string) error {
}
return nil
}

View File

@ -97,26 +97,22 @@ func readTemperature(cmd *cobra.Command, args []string) error {
go func() {
for {
select {
case err, open := <-errorChannel:
if !open {
return
}
flogger.Error("%v", err)
err, open := <-errorChannel
if !open {
return
}
flogger.Error("%v", err)
}
}()
measuredValues := make([]*types.MeasuredValue, 0)
LOOP:
for {
select {
case measuredValue, open := <-measuredValueChannel:
if !open {
break LOOP
}
measuredValues = append(measuredValues, measuredValue)
measuredValue, open := <-measuredValueChannel
if !open {
break LOOP
}
measuredValues = append(measuredValues, measuredValue)
}
err = cli.PrintMeasuredValues(measuredValues, os.Stdout)

View File

@ -1,6 +1,8 @@
package main
import (
"log"
"git.cryptic.systems/volker.raschek/flucky/cli"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
@ -14,5 +16,8 @@ var (
)
func main() {
cli.Execute(version)
err := cli.Execute(version)
if err != nil {
log.Println(err)
}
}

View File

@ -9,7 +9,6 @@ import (
)
func PrintMeasuredValues(measuredValues []*types.MeasuredValue, w io.Writer) error {
// declar tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
@ -29,7 +28,6 @@ func PrintMeasuredValues(measuredValues []*types.MeasuredValue, w io.Writer) err
// PrintSensors displays a list with all configured sensors
func PrintSensors(sensors []*types.Sensor, w io.Writer) error {
// declar tabwriter
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)

View File

@ -31,20 +31,21 @@ func Encode(cnf *Config, w io.Writer) error {
// Read the configuration file
func Read(configFile string) (*Config, error) {
/* #nosec */
f, err := os.Open(configFile)
if err != nil {
return nil, fmt.Errorf("Can not open file %v: %v", configFile, err)
}
defer f.Close()
defer func() { _ = f.Close() }()
return Decode(f)
}
// Write the configuration into a file, specified by the configuration filepath
func Write(cnf *Config, configFile string) error {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
configDir := filepath.Dir(configFile)
/* #nosec */
err := os.MkdirAll(configDir, 0775)
if err != nil {
return fmt.Errorf("Failed to create config directory %v: %v", configDir, err)
@ -55,7 +56,7 @@ func Write(cnf *Config, configFile string) error {
if err != nil {
return fmt.Errorf("Failed not create config file %v: %v", configFile, err)
}
defer f.Close()
defer func() { _ = f.Close() }()
return Encode(cnf, f)
}

View File

@ -6,6 +6,7 @@ import (
"net/url"
"os"
"os/signal"
"syscall"
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
"git.cryptic.systems/volker.raschek/flucky/pkg/repository"
@ -15,7 +16,6 @@ import (
)
func Start(cnf *config.Config, cachedEntries uint, flogger logger.Logger) error {
// load data source name (dsn)
dsnURL, err := url.Parse(cnf.DSN)
if err != nil {
@ -79,7 +79,7 @@ func Start(cnf *config.Config, cachedEntries uint, flogger logger.Logger) error
}
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Interrupt, os.Kill)
signal.Notify(interruptChannel, syscall.SIGTERM)
// Collection
parentCtx := context.Background()

View File

@ -1,14 +1,11 @@
package format
import (
"errors"
"math"
"time"
)
var (
errorPraseTime = errors.New("Can not parse time")
TimeFormat = "2006-01-02T15:04:05.999999Z07:00"
)
@ -19,5 +16,4 @@ func FormatedTime() time.Time {
t := time.Now()
l, _ := time.LoadLocation("Europe/Berlin")
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), int(math.Round(float64(t.Nanosecond())/1000000)*1000000), l)
}

View File

@ -67,7 +67,7 @@ func (d *Postgres) AddDevices(ctx context.Context, devices ...*types.Device) err
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
err = d.insertDevices(tx, devices...)
if err != nil {
@ -82,10 +82,9 @@ func (d *Postgres) insertDevices(tx *sql.Tx, devices ...*types.Device) error {
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, device := range devices {
if device.CreationDate.Equal(time.Time{}) {
device.CreationDate = time.Now()
}
@ -110,7 +109,7 @@ func (d *Postgres) AddOrUpdateDevices(ctx context.Context, devices ...*types.Dev
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
err = d.insertOrUpdateDevices(tx, devices...)
if err != nil {
@ -125,10 +124,9 @@ func (d *Postgres) insertOrUpdateDevices(tx *sql.Tx, devices ...*types.Device) e
if err != nil {
return fmt.Errorf("Failed to prepare statement: %w", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, device := range devices {
if device.CreationDate.Equal(time.Time{}) {
device.CreationDate = time.Now()
}
@ -163,7 +161,7 @@ func (d *Postgres) AddMeasuredValues(ctx context.Context, measuredValues ...*typ
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
for measuredValueType, measuredValues := range splittedMeasuredValues {
var queryFile string
@ -193,10 +191,9 @@ func (d *Postgres) insertMeasuredValues(tx *sql.Tx, query string, measuredValues
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, measuredValue := range measuredValues {
if measuredValue.CreationDate.Equal(time.Time{}) {
measuredValue.CreationDate = time.Now()
}
@ -233,7 +230,7 @@ func (d *Postgres) AddOrUpdateMeasuredValues(ctx context.Context, measuredValues
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
for measuredValueType, measuredValues := range splittedMeasuredValues {
var queryFile string
@ -263,10 +260,9 @@ func (d *Postgres) insertOrUpdateMeasuredValues(tx *sql.Tx, query string, measur
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, measuredValue := range measuredValues {
if measuredValue.CreationDate.Equal(time.Time{}) {
measuredValue.CreationDate = time.Now()
}
@ -294,16 +290,15 @@ func (d *Postgres) AddSensors(ctx context.Context, sensors ...*types.Sensor) err
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(postgres.InsertSensorSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensor := range sensors {
if sensor.CreationDate.Equal(time.Time{}) {
sensor.CreationDate = time.Now()
}
@ -337,7 +332,7 @@ func (d *Postgres) AddOrUpdateSensors(ctx context.Context, sensors ...*types.Sen
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
err = d.insertOrUpdateSensors(tx, sensors...)
if err != nil {
@ -352,10 +347,9 @@ func (d *Postgres) insertOrUpdateSensors(tx *sql.Tx, sensors ...*types.Sensor) e
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensor := range sensors {
if sensor.CreationDate.Equal(time.Time{}) {
sensor.CreationDate = time.Now()
}
@ -416,7 +410,7 @@ func (d *Postgres) GetDeviceByID(ctx context.Context, id string) (*types.Device,
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
devices, err := d.selectDevices(tx, postgres.SelectDeviceByIDSQL, id)
if err != nil {
@ -441,7 +435,7 @@ func (d *Postgres) GetDeviceByName(ctx context.Context, name string) (*types.Dev
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
devices, err := d.selectDevices(tx, postgres.SelectDeviceByNameSQL, name)
if err != nil {
@ -466,7 +460,7 @@ func (d *Postgres) GetDevices(ctx context.Context) ([]*types.Device, error) {
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
devices, err := d.selectDevices(tx, postgres.SelectDevicesSQL)
if err != nil {
@ -486,12 +480,13 @@ func (d *Postgres) selectDevices(tx *sql.Tx, query string, args ...interface{})
if err != nil {
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
rows, err := stmt.Query(args...)
if err != nil {
return nil, fmt.Errorf("Failed to query statement: %v", err)
}
defer func() { _ = rows.Close() }()
devices := make([]*types.Device, 0)
for rows.Next() {
@ -509,6 +504,10 @@ func (d *Postgres) selectDevices(tx *sql.Tx, query string, args ...interface{})
devices = append(devices, device)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to scan rows: %v", err)
}
return devices, nil
}
@ -518,7 +517,7 @@ func (d *Postgres) GetHumidityByID(ctx context.Context, id string) (*types.Measu
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, postgres.SelectHumidityByIDSQL, id)
if err != nil {
@ -547,7 +546,7 @@ func (d *Postgres) GetHumidities(ctx context.Context) ([]*types.MeasuredValue, e
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, postgres.SelectHumiditiesSQL)
if err != nil {
@ -571,16 +570,18 @@ func (d *Postgres) selectMeasuredValue(tx *sql.Tx, query string, args ...interfa
if err != nil {
return nil, err
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
rows, err := stmt.Query(args...)
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
measuredValues := make([]*types.MeasuredValue, 0)
for rows.Next() {
measuredValue := new(types.MeasuredValue)
err := rows.Scan(
&measuredValue.ID,
&measuredValue.Value,
@ -589,14 +590,17 @@ func (d *Postgres) selectMeasuredValue(tx *sql.Tx, query string, args ...interfa
&measuredValue.CreationDate,
&measuredValue.UpdateDate,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to scan row: %v", err)
}
measuredValues = append(measuredValues, measuredValue)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to scan rows: %v", err)
}
return measuredValues, nil
}
@ -606,7 +610,7 @@ func (d *Postgres) GetPressureByID(ctx context.Context, id string) (*types.Measu
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, postgres.SelectPressureByIDSQL, id)
if err != nil {
@ -635,7 +639,7 @@ func (d *Postgres) GetPressures(ctx context.Context) ([]*types.MeasuredValue, er
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, postgres.SelectPressuresSQL)
if err != nil {
@ -660,7 +664,7 @@ func (d *Postgres) GetSensorByID(ctx context.Context, id string) (*types.Sensor,
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
sensors, err := d.selectSensors(tx, postgres.SelectSensorByIDSQL, id)
if err != nil {
@ -685,7 +689,7 @@ func (d *Postgres) GetSensors(ctx context.Context) ([]*types.Sensor, error) {
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
sensors, err := d.selectSensors(tx, postgres.SelectSensorsSQL)
if err != nil {
@ -706,7 +710,7 @@ func (d *Postgres) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...strin
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
cachedSensors := make([]*types.Sensor, 0)
for i := range deviceIDs {
@ -732,6 +736,7 @@ func (d *Postgres) GetSensorsByModels(ctx context.Context, sensorModels ...strin
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer func() { _ = tx.Rollback() }()
cachedSensors := make([]*types.Sensor, 0)
for i := range sensorModels {
@ -757,7 +762,7 @@ func (d *Postgres) GetSensorsByNames(ctx context.Context, sensorNames ...string)
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
cachedSensors := make([]*types.Sensor, 0)
for i := range sensorNames {
@ -782,12 +787,13 @@ func (d *Postgres) selectSensors(tx *sql.Tx, query string, args ...interface{})
if err != nil {
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
rows, err := stmt.Query(args...)
if err != nil {
return nil, fmt.Errorf("Failed to query statement: %v", err)
}
defer func() { _ = rows.Close() }()
sensors := make([]*types.Sensor, 0)
for rows.Next() {
@ -814,6 +820,10 @@ func (d *Postgres) selectSensors(tx *sql.Tx, query string, args ...interface{})
sensors = append(sensors, sensor)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to scan rows: %v", err)
}
return sensors, nil
}
@ -823,7 +833,7 @@ func (d *Postgres) GetTemperatureByID(ctx context.Context, id string) (*types.Me
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, postgres.SelectTemperatureByIDSQL, id)
if err != nil {
@ -852,7 +862,7 @@ func (d *Postgres) GetTemperatures(ctx context.Context) ([]*types.MeasuredValue,
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, postgres.SelectTemperaturesSQL)
if err != nil {
@ -878,7 +888,7 @@ func (d *Postgres) Import(ctx context.Context, src Repository) error {
if err != nil {
return err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
devices, err := src.GetDevices(ctx)
if err != nil {
@ -928,13 +938,13 @@ func (d *Postgres) RemoveDevicesByIDs(ctx context.Context, deviceIDs ...string)
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(postgres.DeleteDeviceByIDSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, deviceID := range deviceIDs {
_, err = stmt.Exec(deviceID)
@ -952,13 +962,13 @@ func (d *Postgres) RemoveDevicesByNames(ctx context.Context, names ...string) er
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(postgres.DeleteDeviceByNameSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, deviceID := range names {
_, err = stmt.Exec(deviceID)
@ -976,13 +986,13 @@ func (d *Postgres) RemoveSensorsByIDs(ctx context.Context, sensorIDs ...string)
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(postgres.DeleteSensorByIDSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensorID := range sensorIDs {
_, err = stmt.Exec(sensorID)
@ -1000,13 +1010,13 @@ func (d *Postgres) RemoveSensorsByNames(ctx context.Context, sensorIDs ...string
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(postgres.DeleteSensorByNameSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensorID := range sensorIDs {
_, err = stmt.Exec(sensorID)
@ -1024,13 +1034,13 @@ func (d *Postgres) UpdateDevices(ctx context.Context, devices ...*types.Device)
if err != nil {
return err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(postgres.UpdateDeviceSQL)
if err != nil {
return err
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, device := range devices {
now := time.Now()
@ -1057,13 +1067,13 @@ func (d *Postgres) UpdateSensors(ctx context.Context, sensors ...*types.Sensor)
if err != nil {
return err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(postgres.UpdateSensorSQL)
if err != nil {
return err
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensor := range sensors {
now := time.Now()

View File

@ -47,6 +47,7 @@ func NewSQLite(opts SQLiteOpts) (Repository, error) {
// Create directory if not exist
if _, err := os.Stat(filepath.Dir(opts.DatabaseURL.Path)); os.IsNotExist(err) {
/* #nosec */
err := os.MkdirAll(filepath.Dir(opts.DatabaseURL.Path), 0755)
if err != nil {
return nil, err
@ -89,7 +90,7 @@ func (d *SQLite) AddDevices(ctx context.Context, devices ...*types.Device) error
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
err = d.insertDevices(tx, devices...)
if err != nil {
@ -104,10 +105,16 @@ func (d *SQLite) insertDevices(tx *sql.Tx, devices ...*types.Device) error {
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, device := range devices {
_, err = stmt.Exec(&device.ID, &device.Name, &device.Location, &device.CreationDate, &device.UpdateDate)
_, err = stmt.Exec(
&device.ID,
&device.Name,
&device.Location,
&device.CreationDate,
&device.UpdateDate,
)
if err != nil {
return fmt.Errorf("Failed to execute statement: %v", err)
}
@ -125,7 +132,7 @@ func (d *SQLite) AddOrUpdateDevices(ctx context.Context, devices ...*types.Devic
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
err = d.insertOrUpdateDevices(tx, devices...)
if err != nil {
@ -140,10 +147,16 @@ func (d *SQLite) insertOrUpdateDevices(tx *sql.Tx, devices ...*types.Device) err
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, device := range devices {
_, err = stmt.Exec(&device.ID, &device.Name, &device.Location, &device.CreationDate, &device.UpdateDate)
_, err = stmt.Exec(
&device.ID,
&device.Name,
&device.Location,
&device.CreationDate,
&device.UpdateDate,
)
if err != nil {
return fmt.Errorf("Failed to execute statement: %v", err)
}
@ -170,7 +183,7 @@ func (d *SQLite) AddMeasuredValues(ctx context.Context, measuredValues ...*types
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
for measuredValueType, measuredValues := range splittedMeasuredValues {
var query string
@ -200,7 +213,7 @@ func (d *SQLite) insertMeasuredValues(tx *sql.Tx, query string, measuredValues .
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, measuredValue := range measuredValues {
_, err := stmt.Exec(
@ -211,7 +224,6 @@ func (d *SQLite) insertMeasuredValues(tx *sql.Tx, query string, measuredValues .
&measuredValue.CreationDate,
&measuredValue.UpdateDate,
)
if err != nil {
return fmt.Errorf("Failed to execute statement: %v", err)
}
@ -238,7 +250,7 @@ func (d *SQLite) AddOrUpdateMeasuredValues(ctx context.Context, measuredValues .
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
for measuredValueType, measuredValues := range splittedMeasuredValues {
var query string
@ -268,7 +280,7 @@ func (d *SQLite) insertOrUpdateMeasuredValues(tx *sql.Tx, query string, measured
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, measuredValue := range measuredValues {
_, err := stmt.Exec(
@ -297,13 +309,13 @@ func (d *SQLite) AddSensors(ctx context.Context, sensors ...*types.Sensor) error
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(sqlite3.InsertSensorSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensor := range sensors {
_, err = stmt.Exec(
@ -338,7 +350,7 @@ func (d *SQLite) AddOrUpdateSensors(ctx context.Context, sensors ...*types.Senso
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
err = d.insertOrUpdateSensors(tx, sensors...)
if err != nil {
@ -353,7 +365,7 @@ func (d *SQLite) insertOrUpdateSensors(tx *sql.Tx, sensors ...*types.Sensor) err
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensor := range sensors {
_, err = stmt.Exec(
@ -420,7 +432,7 @@ func (d *SQLite) GetDeviceByID(ctx context.Context, id string) (*types.Device, e
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
devices, err := d.selectDevices(tx, sqlite3.SelectDeviceByIDSQL, id)
if err != nil {
@ -448,7 +460,7 @@ func (d *SQLite) GetDeviceByName(ctx context.Context, name string) (*types.Devic
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
devices, err := d.selectDevices(tx, sqlite3.SelectDeviceByNameSQL, name)
if err != nil {
@ -476,7 +488,7 @@ func (d *SQLite) GetDevices(ctx context.Context) ([]*types.Device, error) {
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
devices, err := d.selectDevices(tx, sqlite3.SelectDevicesSQL)
if err != nil {
@ -496,13 +508,13 @@ func (d *SQLite) selectDevices(tx *sql.Tx, query string, args ...interface{}) ([
if err != nil {
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
rows, err := stmt.Query(args...)
if err != nil {
return nil, fmt.Errorf("Failed to query statement: %v", err)
}
defer rows.Close()
defer func() { _ = rows.Close() }()
devices := make([]*types.Device, 0)
for rows.Next() {
@ -520,6 +532,10 @@ func (d *SQLite) selectDevices(tx *sql.Tx, query string, args ...interface{}) ([
devices = append(devices, device)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to scan rows: %v", err)
}
return devices, nil
}
@ -532,7 +548,7 @@ func (d *SQLite) GetHumidityByID(ctx context.Context, id string) (*types.Measure
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectHumidityByIDSQL, id)
if err != nil {
@ -564,7 +580,7 @@ func (d *SQLite) GetHumidities(ctx context.Context) ([]*types.MeasuredValue, err
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectHumiditiesSQL)
if err != nil {
@ -588,17 +604,18 @@ func (d *SQLite) selectMeasuredValue(tx *sql.Tx, query string, args ...interface
if err != nil {
return nil, err
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
rows, err := stmt.Query(args...)
if err != nil {
return nil, err
}
defer rows.Close()
defer func() { _ = rows.Close() }()
measuredValues := make([]*types.MeasuredValue, 0)
for rows.Next() {
measuredValue := new(types.MeasuredValue)
err := rows.Scan(
&measuredValue.ID,
&measuredValue.Value,
@ -607,7 +624,6 @@ func (d *SQLite) selectMeasuredValue(tx *sql.Tx, query string, args ...interface
&measuredValue.CreationDate,
&measuredValue.UpdateDate,
)
if err != nil {
return nil, err
}
@ -615,6 +631,10 @@ func (d *SQLite) selectMeasuredValue(tx *sql.Tx, query string, args ...interface
measuredValues = append(measuredValues, measuredValue)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to scan rows: %v", err)
}
return measuredValues, nil
}
@ -627,7 +647,7 @@ func (d *SQLite) GetPressureByID(ctx context.Context, id string) (*types.Measure
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectPressureByIDSQL, id)
if err != nil {
@ -659,7 +679,7 @@ func (d *SQLite) GetPressures(ctx context.Context) ([]*types.MeasuredValue, erro
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectPressuresSQL)
if err != nil {
@ -687,7 +707,7 @@ func (d *SQLite) GetSensorByID(ctx context.Context, id string) (*types.Sensor, e
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
sensors, err := d.selectSensors(tx, sqlite3.SelectSensorByIDSQL, id)
if err != nil {
@ -715,7 +735,7 @@ func (d *SQLite) GetSensors(ctx context.Context) ([]*types.Sensor, error) {
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
sensors, err := d.selectSensors(tx, sqlite3.SelectSensorsSQL)
if err != nil {
@ -739,7 +759,7 @@ func (d *SQLite) GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...string)
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
cachedSensors := make([]*types.Sensor, 0)
for i := range deviceIDs {
@ -768,7 +788,7 @@ func (d *SQLite) GetSensorsByModels(ctx context.Context, sensorModels ...string)
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
cachedSensors := make([]*types.Sensor, 0)
for i := range sensorModels {
@ -797,7 +817,7 @@ func (d *SQLite) GetSensorsByNames(ctx context.Context, sensorNames ...string) (
if err != nil {
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
cachedSensors := make([]*types.Sensor, 0)
for i := range sensorNames {
@ -822,13 +842,13 @@ func (d *SQLite) selectSensors(tx *sql.Tx, query string, args ...interface{}) ([
if err != nil {
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
rows, err := stmt.Query(args...)
if err != nil {
return nil, fmt.Errorf("Failed to query statement: %v", err)
}
defer rows.Close()
defer func() { _ = rows.Close() }()
sensors := make([]*types.Sensor, 0)
for rows.Next() {
@ -855,6 +875,10 @@ func (d *SQLite) selectSensors(tx *sql.Tx, query string, args ...interface{}) ([
sensors = append(sensors, sensor)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to scan rows: %v", err)
}
return sensors, nil
}
@ -867,7 +891,7 @@ func (d *SQLite) GetTemperatureByID(ctx context.Context, id string) (*types.Meas
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectTemperatureByIDSQL, id)
if err != nil {
@ -899,7 +923,7 @@ func (d *SQLite) GetTemperatures(ctx context.Context) ([]*types.MeasuredValue, e
if err != nil {
return nil, err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
measuredValues, err := d.selectMeasuredValue(tx, sqlite3.SelectTemperaturesSQL)
if err != nil {
@ -928,7 +952,7 @@ func (d *SQLite) Import(ctx context.Context, src Repository) error {
if err != nil {
return err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
devices, err := src.GetDevices(ctx)
if err != nil {
@ -981,13 +1005,13 @@ func (d *SQLite) RemoveDevicesByIDs(ctx context.Context, deviceIDs ...string) er
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(sqlite3.DeleteDeviceByIDSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, deviceID := range deviceIDs {
_, err = stmt.Exec(deviceID)
@ -1008,13 +1032,13 @@ func (d *SQLite) RemoveDevicesByNames(ctx context.Context, names ...string) erro
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(sqlite3.DeleteDeviceByNameSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, deviceID := range names {
_, err = stmt.Exec(deviceID)
@ -1035,13 +1059,13 @@ func (d *SQLite) RemoveSensorsByIDs(ctx context.Context, sensorIDs ...string) er
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(sqlite3.DeleteSensorByIDSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensorID := range sensorIDs {
_, err = stmt.Exec(sensorID)
@ -1062,13 +1086,13 @@ func (d *SQLite) RemoveSensorsByNames(ctx context.Context, names ...string) erro
if err != nil {
return fmt.Errorf("Failed to begin new transaction: %v", err)
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(sqlite3.DeleteSensorByNameSQL)
if err != nil {
return fmt.Errorf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensorID := range names {
_, err = stmt.Exec(sensorID)
@ -1089,13 +1113,13 @@ func (d *SQLite) UpdateDevices(ctx context.Context, devices ...*types.Device) er
if err != nil {
return err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(sqlite3.UpdateDeviceSQL)
if err != nil {
return err
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, device := range devices {
_, err := stmt.Exec(
@ -1122,13 +1146,13 @@ func (d *SQLite) UpdateSensors(ctx context.Context, sensors ...*types.Sensor) er
if err != nil {
return err
}
defer tx.Rollback()
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare(sqlite3.UpdateSensorSQL)
if err != nil {
return err
}
defer stmt.Close()
defer func() { _ = stmt.Close() }()
for _, sensor := range sensors {
_, err := stmt.Exec(

View File

@ -21,7 +21,6 @@ type BME280 struct {
// Read measured values
func (bme280 *BME280) Read() ([]*types.MeasuredValue, error) {
// Lock multiple access
bme280.mutex.Lock()
defer bme280.mutex.Unlock()

View File

@ -18,7 +18,6 @@ type DHT11 struct {
// Read measured values
func (dht11 *DHT11) Read() ([]*types.MeasuredValue, error) {
// Lock multiple access
dht11.mutex.Lock()
defer dht11.mutex.Unlock()

View File

@ -18,7 +18,6 @@ type DHT22 struct {
// Read measured values
func (dht22 *DHT22) Read() ([]*types.MeasuredValue, error) {
// Lock multiple access
dht22.mutex.Lock()
defer dht22.mutex.Unlock()

View File

@ -22,7 +22,6 @@ type DS18B20 struct {
// Read measured values
func (ds18b20 *DS18B20) Read() ([]*types.MeasuredValue, error) {
// Lock multiple access
ds18b20.mutex.Lock()
defer ds18b20.mutex.Unlock()
@ -36,6 +35,7 @@ func (ds18b20 *DS18B20) Read() ([]*types.MeasuredValue, error) {
return nil, fmt.Errorf("Socket path not found: %v", socketPath)
}
/* #nosec */
data, err := ioutil.ReadFile(socketPath)
if err != nil {
return nil, fmt.Errorf("Can not read data from sensor %v", ds18b20.Name)

View File

@ -14,7 +14,7 @@ var (
// FilterMeasuredValuesByTypes filters measured values by type
func FilterMeasuredValuesByTypes(ctx context.Context, inChannel <-chan *types.MeasuredValue, measuredValueTypes ...types.MeasuredValueType) <-chan *types.MeasuredValue {
outChannel := make(chan *types.MeasuredValue, 0)
outChannel := make(chan *types.MeasuredValue, 1)
go func() {
LOOP:
for {
@ -40,7 +40,7 @@ func FilterMeasuredValuesByTypes(ctx context.Context, inChannel <-chan *types.Me
// FilterMeasuredValuesBySensorIDs filters measured values by sensor id
func FilterMeasuredValuesBySensorIDs(ctx context.Context, inChannel <-chan *types.MeasuredValue, sensorIDs ...string) <-chan *types.MeasuredValue {
outChannel := make(chan *types.MeasuredValue, 0)
outChannel := make(chan *types.MeasuredValue, 1)
go func() {
LOOP:
for {
@ -68,8 +68,8 @@ func FilterMeasuredValuesBySensorIDs(ctx context.Context, inChannel <-chan *type
// closed. The returned channels will be closed
func ReadPipeline(ctx context.Context, sensors ...Sensor) (<-chan *types.MeasuredValue, <-chan error) {
var (
errorChannel = make(chan error, 0)
measuredValueChannel = make(chan *types.MeasuredValue, 0)
errorChannel = make(chan error, 1)
measuredValueChannel = make(chan *types.MeasuredValue, 1)
)
go func() {
@ -102,8 +102,8 @@ func ReadPipeline(ctx context.Context, sensors ...Sensor) (<-chan *types.Measure
// the context has been closed
func ReadTickingPipeline(ctx context.Context, sensors ...Sensor) (<-chan *types.MeasuredValue, <-chan error) {
var (
errorChannel = make(chan error, 0)
measuredValueChannel = make(chan *types.MeasuredValue, 0)
errorChannel = make(chan error, 1)
measuredValueChannel = make(chan *types.MeasuredValue, 1)
)
for i := range sensors {

View File

@ -9,7 +9,7 @@ import (
// or temperature.
type MeasuredValue struct {
ID string `json:"id" xml:"id"`
Value float64 `json:"value,string" xml:"value,string"`
Value float64 `json:"value,string" xml:"value"`
ValueType MeasuredValueType `json:"value_type" xml:"value_type"`
Date time.Time `json:"date" xml:"date"`
SensorID string `json:"sensor_id" xml:"sensor_id"`
@ -21,6 +21,6 @@ type MeasuredValueType string
const (
Humidity MeasuredValueType = "humidity"
Pressure = "pressure"
Temperature = "temperature"
Pressure MeasuredValueType = "pressure"
Temperature MeasuredValueType = "temperature"
)