Compare commits
62 Commits
Author | SHA1 | Date | |
---|---|---|---|
ef9266b010
|
|||
6c03bc078b
|
|||
8c1bd57ad5
|
|||
92a776f6ce
|
|||
9ee9742aff
|
|||
ec7cad4555
|
|||
fa45125b49
|
|||
83fae430b9
|
|||
0384bbd574
|
|||
7e018280fb
|
|||
a49546bd6d
|
|||
940e04371c
|
|||
4931c63c10
|
|||
a1c28a0a2e
|
|||
10069568f9
|
|||
43e9d00dcb
|
|||
11717679bc
|
|||
d0e238e64a
|
|||
8f1c7b10f7
|
|||
fb916c94ae
|
|||
fb8d4dd5eb
|
|||
84d052184e
|
|||
0f9b62fb81
|
|||
c679b29051
|
|||
17dda6a987
|
|||
cc317e0b6c
|
|||
8cc232adc1
|
|||
671a3eb748
|
|||
6f45c2957a
|
|||
546139492b
|
|||
3581424bd2
|
|||
c266117785
|
|||
0765bd29d1
|
|||
0261203395
|
|||
12246aae0c
|
|||
002f3e9e25
|
|||
40857249c4
|
|||
b595cf1ac8
|
|||
f1a4ade402
|
|||
2cd2188dcb
|
|||
95fb1f6745
|
|||
ca4269fff8
|
|||
a507bef108
|
|||
f0bba4a202
|
|||
111dc0e4f1
|
|||
5f48d29dae
|
|||
39b3a495ac
|
|||
b125bf432c
|
|||
dbef4f8241
|
|||
afe55b3d33
|
|||
25dd99cd3d
|
|||
86f598bc7d
|
|||
a7722e028a
|
|||
ea83d43cd6
|
|||
bacddcfbf9
|
|||
4e531b8734
|
|||
4ed014c0df
|
|||
036ff28175
|
|||
04bc3baffe
|
|||
6223f4e79b
|
|||
f19cc3249a
|
|||
837f6c1cb2
|
1
.dockerignore
Normal file
1
.dockerignore
Normal file
@ -0,0 +1 @@
|
||||
bin
|
25
.drone.yml
Normal file
25
.drone.yml
Normal file
@ -0,0 +1,25 @@
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: amd64
|
||||
|
||||
steps:
|
||||
- name: build-linux-amd64
|
||||
image: docker.io/volkerraschek/build-image:latest
|
||||
commands:
|
||||
- make bin/linux/amd64/flucky
|
||||
when:
|
||||
event:
|
||||
- push
|
||||
- pull_request
|
||||
- tag
|
||||
|
||||
# steps:
|
||||
# - name: test-unit
|
||||
# image: docker.io/volkerraschek/build-image:latest
|
||||
# commands:
|
||||
# - make test/unit
|
||||
# when:
|
||||
# event:
|
||||
# - push
|
||||
# - pull_request
|
||||
# - tag
|
15
.env_dev
15
.env_dev
@ -1,15 +0,0 @@
|
||||
SERVER_PORT=80
|
||||
DATABASE_DRIVER=postgres
|
||||
DATABASE_NAME=postgres
|
||||
DATABASE_SCHEMA=public
|
||||
DATABASE_USER=postgres
|
||||
DATABASE_PASSWORD=postgres
|
||||
|
||||
PG_HOST=flucky_db
|
||||
PG_INTERN_PORT=5432
|
||||
PG_EXTERN_PORT=5433
|
||||
PG_NAME=public
|
||||
PG_USER=postgres
|
||||
PG_PASSWORD=postgres
|
||||
|
||||
TZ=Europe/Berlin
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -11,3 +11,6 @@ flucky.tar.xz
|
||||
|
||||
# directories
|
||||
.vscode/
|
||||
|
||||
# coverage files
|
||||
coverage*
|
22
.travis.yml
22
.travis.yml
@ -1,22 +0,0 @@
|
||||
language: go
|
||||
|
||||
services:
|
||||
- docker
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- stage: build
|
||||
name: go-build
|
||||
script: make container/all
|
||||
|
||||
deploy:
|
||||
- provider: script
|
||||
script: make container/release
|
||||
# skip_cleanup: true
|
||||
on:
|
||||
tags: true
|
||||
|
||||
notifications:
|
||||
email:
|
||||
on_success: change
|
||||
on_failure: change
|
20
Dockerfile
Normal file
20
Dockerfile
Normal file
@ -0,0 +1,20 @@
|
||||
# ARGs
|
||||
# ==================================
|
||||
ARG BASE_IMAGE
|
||||
ARG BUILD_IMAGE
|
||||
ARG EXECUTABLE
|
||||
ARG EXECUTABLE_TARGET
|
||||
ARG GOPROXY
|
||||
ARG GOPRIVATE
|
||||
ARG VERSION
|
||||
|
||||
# BUILD
|
||||
# ==================================
|
||||
FROM ${BUILD_IMAGE} AS build
|
||||
COPY . /workspace
|
||||
RUN make ${EXECUTABLE_TARGET} VERSION=${VERSION} GOPROXY=${GOPROXY} GOPRIVATE=${GOPRIVATE}
|
||||
|
||||
# TARGET
|
||||
# ==================================
|
||||
FROM ${BASE_IMAGE}
|
||||
COPY --from=build /workspace/${EXECUTABLE_TARGET} /usr/bin/${EXECUTABLE}
|
258
Makefile
258
Makefile
@ -1,140 +1,184 @@
|
||||
# UID/GID
|
||||
# UID or GID is the UNIX user ID or group ID of the user who executes
|
||||
# make. If the UID or GID is not passed as a make variable, an attempt
|
||||
# is made to determine it.
|
||||
UID?=$(shell id --user)
|
||||
GID?=$(shell id --group)
|
||||
|
||||
# VERSION
|
||||
# If no version is specified as a parameter of make, the last git hash
|
||||
# value is taken.
|
||||
VERSION:=$(or ${TRAVIS_TAG}, $(shell git rev-parse --short HEAD)-git)
|
||||
VERSION?=$(shell git describe --abbrev=0)+$(shell date +'%Y%m%d%H%I%S')
|
||||
|
||||
# GOPROXY settings
|
||||
# If no GOPROXY environment variable available, the pre-defined GOPROXY from go
|
||||
# env to download and validate go modules is used. Exclude downloading and
|
||||
# validation of all private modules which are pre-defined in GOPRIVATE. If no
|
||||
# GOPRIVATE variable defined, the variable of go env is used.
|
||||
GOPROXY?=$(shell go env GOPROXY)
|
||||
GOPRIVATE?=$(shell go env GOPRIVATE)
|
||||
|
||||
# CONTAINER_RUNTIME
|
||||
# The CONTAINER_RUNTIME variable will be used to specified the path to a
|
||||
# container runtime. This is needed to start and run a container images.
|
||||
CONTAINER_RUNTIME?=$(shell which docker)
|
||||
|
||||
# BUILD_IMAGE
|
||||
BUILD_IMAGE:=volkerraschek/build-image:latest
|
||||
|
||||
# GH_USER/GITHUB_TOKEN
|
||||
# It's the user name from github.com and his token. This token and the username
|
||||
# can be set over encrypted environment variables in the ci/cd pipeline
|
||||
GITHUB_USER=volker-raschek
|
||||
GITHUB_TOKEN?=""
|
||||
CONTAINER_IMAGE_VERSION?=latest
|
||||
|
||||
# EXECUTABLE
|
||||
# Executable binary which should be compiled for different architecures
|
||||
EXECUTABLE:=flucky
|
||||
EXECUTABLE=flucky
|
||||
|
||||
# UNIX_EXECUTABLES
|
||||
# Define all executables for different architectures and operation systems
|
||||
UNIX_EXECUTABLES := \
|
||||
darwin/amd64/$(EXECUTABLE) \
|
||||
freebsd/amd64/$(EXECUTABLE) \
|
||||
linux/amd64/$(EXECUTABLE) \
|
||||
linux/arm/5/$(EXECUTABLE) \
|
||||
linux/arm/7/$(EXECUTABLE) \
|
||||
# BINARIES
|
||||
# ==============================================================================
|
||||
EXECUTABLE_TARGETS:= \
|
||||
bin/linux/amd64/${EXECUTABLE} \
|
||||
bin/linux/arm/5/${EXECUTABLE} \
|
||||
bin/linux/arm/7/${EXECUTABLE} \
|
||||
bin/tmp/${EXECUTABLE}
|
||||
|
||||
# EXECUTABLE_TARGETS
|
||||
# Include the relative paths to all executables
|
||||
EXECUTABLE_TARGETS=$(UNIX_EXECUTABLES:%=bin/%)
|
||||
${EXECUTABLE}: bin/tmp/${EXECUTABLE}
|
||||
|
||||
# COMPRSSED_EXECUTABLES
|
||||
# Append to all defined executables the compression extentions to detect the
|
||||
# different make steps which compress the binary
|
||||
COMPRESSED_EXECUTABLES=$(UNIX_EXECUTABLES:%=%.tar.bz2) $(UNIX_EXECUTABLES:%=%.tar.gz) $(UNIX_EXECUTABLES:%=%.tar.xz)
|
||||
COMPRESSED_EXECUTABLE_TARGETS=$(COMPRESSED_EXECUTABLES:%=bin/%)
|
||||
bin/linux/amd64/${EXECUTABLE}: bindata
|
||||
CGO_ENABLED=1 \
|
||||
GOOS=linux \
|
||||
GOARCH=amd64 \
|
||||
GOPROXY=${GOPROXY} \
|
||||
GOPRIVATE=${GOPRIVATE} \
|
||||
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
|
||||
|
||||
# PHONY
|
||||
# To avoid a conflict with an existing file and a defined make step
|
||||
.PHONY: clean container/${EXECUTABLE_TARGETS} container/test release remote test
|
||||
|
||||
all: ${EXECUTABLE}
|
||||
|
||||
$(EXECUTABLE): bindata
|
||||
go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
|
||||
bin/tmp/${EXECUTABLE}: bindata
|
||||
go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
|
||||
# arm
|
||||
bin/linux/arm/5/${EXECUTABLE}: bindata
|
||||
GOARM=5 GOARCH=arm go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
CGO_ENABLED=1 \
|
||||
GOOS=linux \
|
||||
GOARCH=arm \
|
||||
GOARM=5 \
|
||||
GOPROXY=${GOPROXY} \
|
||||
GOPRIVATE=${GOPRIVATE} \
|
||||
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
|
||||
|
||||
bin/linux/arm/7/${EXECUTABLE}: bindata
|
||||
GOARM=7 GOARCH=arm go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
CC=arm-linux-gnueabihf-gcc \
|
||||
CGO_ENABLED=1 \
|
||||
GOOS=linux \
|
||||
GOARCH=arm \
|
||||
GOARM=7 \
|
||||
GOPROXY=${GOPROXY} \
|
||||
GOPRIVATE=${GOPRIVATE} \
|
||||
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
|
||||
|
||||
# 386
|
||||
bin/darwin/386/$(EXECUTABLE): bindata
|
||||
GOARCH=386 GOOS=darwin go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
bin/tmp/${EXECUTABLE}: bindata
|
||||
CGO_ENABLED=1 \
|
||||
GOPROXY=${GOPROXY} \
|
||||
GOPRIVATE=${GOPRIVATE} \
|
||||
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
|
||||
|
||||
bin/linux/386/$(EXECUTABLE): bindata
|
||||
GOARCH=386 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
# GO-BINDATA
|
||||
# ==============================================================================
|
||||
BINDATA_TARGETS := \
|
||||
pkg/repository/db/bindataSQL.go \
|
||||
|
||||
# amd64
|
||||
bin/freebsd/amd64/$(EXECUTABLE): bindata
|
||||
GOARCH=amd64 GOOS=freebsd go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
|
||||
bin/darwin/amd64/$(EXECUTABLE): bindata
|
||||
GOARCH=amd64 GOOS=darwin go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
|
||||
bin/linux/amd64/$(EXECUTABLE): bindata
|
||||
GOARCH=amd64 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o "$@"
|
||||
|
||||
bindata:
|
||||
go-bindata -pkg db -o ./pkg/db/bindataSQL.go ./pkg/db/sql/***
|
||||
go-bindata -pkg goldenfiles -o ./test/goldenfiles/bindata.go ./test/goldenfiles/json/***
|
||||
|
||||
%.tar.bz2: %
|
||||
tar --create --bzip2 --file "$@" "$<"
|
||||
|
||||
%.tar.gz: %
|
||||
tar --create --gunzip --file "$@" "$<"
|
||||
|
||||
%.tar.xz: %
|
||||
tar --create --xz --file "$@" "$<"
|
||||
|
||||
|
||||
test: ${EXECUTABLE}
|
||||
go test -v ./pkg/...
|
||||
|
||||
|
||||
release: clean
|
||||
$(MAKE) $(COMPRESSED_EXECUTABLE_TARGETS)
|
||||
github-release release \
|
||||
--user ${GITHUB_USER} \
|
||||
--repo ${EXECUTABLE} \
|
||||
--tag ${VERSION}
|
||||
|
||||
$(foreach FILE,$(COMPRESSED_EXECUTABLES),github-release upload --user ${GITHUB_USER} --repo ${EXECUTABLE} --tag ${VERSION} --name $(subst /,-,$(FILE)) --file bin/$(FILE) --replace;)
|
||||
PHONY+=bindata
|
||||
bindata: clean ${BINDATA_TARGETS}
|
||||
|
||||
pkg/repository/db/bindataSQL.go:
|
||||
go-bindata -pkg db -o ./pkg/repository/db/bindataSQL.go pkg/repository/db/postgres/*** pkg/repository/db/sqlite3/***
|
||||
|
||||
# CLEAN
|
||||
# ==============================================================================
|
||||
PHONY+=clean
|
||||
clean:
|
||||
rm ${EXECUTABLE} || true
|
||||
rm --recursive --force bin/ || true
|
||||
rm --force ${BINDATA_TARGETS} || true
|
||||
rm --force --recursive bin/ || true
|
||||
|
||||
# TEST
|
||||
# ==============================================================================
|
||||
PHONY+=test/unit
|
||||
test/unit: bindata
|
||||
go test -v -race -coverprofile=coverage.txt -covermode=atomic -timeout 600s -count=1 ./pkg/...
|
||||
|
||||
container/all:
|
||||
$(MAKE) container-run COMMAND=all
|
||||
PHONY+=test/integration
|
||||
test/integration:
|
||||
go test -v -count=1 -timeout 600s ./it/...
|
||||
|
||||
test/coverage: test/unit
|
||||
go tool cover -html=coverage.txt
|
||||
|
||||
container/test:
|
||||
$(MAKE) container-run COMMAND=test
|
||||
# CONTAINER IMAGE STEPS
|
||||
# ==============================================================================
|
||||
PHONY+=container-image/build/amd64
|
||||
container-image/build/amd64:
|
||||
${CONTAINER_RUNTIME} build \
|
||||
--build-arg BASE_IMAGE=docker.io/library/alpine:3.11.2 \
|
||||
--build-arg BUILD_IMAGE=docker.io/volkerraschek/build-image:latest \
|
||||
--build-arg EXECUTABLE=${EXECUTABLE} \
|
||||
--build-arg EXECUTABLE_TARGET=bin/linux/amd64/${EXECUTABLE} \
|
||||
--build-arg GOPROXY=${GOPROXY} \
|
||||
--build-arg GOPRIVATE=${GOPRIVATE} \
|
||||
--build-arg VERSION=${VERSION} \
|
||||
--file Dockerfile \
|
||||
--no-cache \
|
||||
--tag docker.io/volkerraschek/flucky:${CONTAINER_IMAGE_VERSION} \
|
||||
--tag volkerraschek/flucky:${CONTAINER_IMAGE_VERSION} \
|
||||
.
|
||||
|
||||
container/release:
|
||||
$(MAKE) container-run COMMAND=release
|
||||
PHONY+=container-image/push/amd64
|
||||
container-image/push/amd64: container-image/build/amd64
|
||||
${CONTAINER_RUNTIME} login docker.io \
|
||||
--username ${CONTAINER_IMAGE_REGISTRY_USER} \
|
||||
--password ${CONTAINER_IMAGE_REGISTRY_PASSWORD}
|
||||
${CONTAINER_RUNTIME} push docker.io/volkerraschek/flucky:${CONTAINER_IMAGE_VERSION}
|
||||
|
||||
# CONTAINER STEPS - BINARY
|
||||
# ==============================================================================
|
||||
# current os
|
||||
PHONY+=container-run/${EXECUTABLE}
|
||||
container-run/${EXECUTABLE}:
|
||||
$(MAKE) container-run COMMAND=${@:container-run/%=%}
|
||||
|
||||
# build all binaries for any operating system
|
||||
PHONY+=container-run/all
|
||||
container-run/all:
|
||||
$(MAKE) container-run COMMAND=${@:container-run/%=%}
|
||||
|
||||
PHONY+=${EXECUTABLE_TARGETS:%=container-run/%}
|
||||
${EXECUTABLE_TARGETS:%=container-run/%}:
|
||||
$(MAKE) container-run COMMAND=${@:container-run/%=%}
|
||||
|
||||
# CONTAINER STEPS - GO-BINDATA
|
||||
# ==============================================================================
|
||||
PHONY+=container-run/bindata
|
||||
container-run/bindata:
|
||||
$(MAKE) container-run COMMAND=${@:container-run/%=%}
|
||||
|
||||
# CONTAINER STEPS - CLEAN
|
||||
# ==============================================================================
|
||||
PHONY+=container-run/clean
|
||||
container-run/clean:
|
||||
$(MAKE) container-run COMMAND=${@:container-run/%=%}
|
||||
|
||||
# CONTAINER STEPS - TEST
|
||||
# ==============================================================================
|
||||
PHONY+=container-run/test/unit
|
||||
container-run/test/unit:
|
||||
$(MAKE) container-run COMMAND=${@:container-run/%=%}
|
||||
|
||||
# GENERAL CONTAINER COMMAND
|
||||
# ==============================================================================
|
||||
PHONY+=container-run
|
||||
container-run:
|
||||
${CONTAINER_RUNTIME} run \
|
||||
--rm \
|
||||
--volume ${PWD}:/workspace \
|
||||
${BUILD_IMAGE} \
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock \
|
||||
docker.io/volkerraschek/build-image:latest \
|
||||
make ${COMMAND} \
|
||||
UID=${UID} \
|
||||
GID=${GID} \
|
||||
VERSION=${VERSION} \
|
||||
GITHUB_TOKEN=${GITHUB_TOKEN}
|
||||
GOPROXY=${GOPROXY} \
|
||||
GOPRIVATE=${GOPRIVATE} \
|
||||
VERSION=${VERSION:v%=%}
|
||||
|
||||
remote: bin/linux/arm/7/${EXECUTABLE}
|
||||
scp bin/linux/arm/7/${EXECUTABLE} ${FLUCKY_REMOTE}:/usr/local/bin
|
||||
ssh ${FLUCKY_REMOTE} "chmod +x /usr/local/bin/flucky"
|
||||
# REMOTE
|
||||
# ==============================================================================
|
||||
PHONY+=${FLUCKY_REMOTE:%=remote/%}
|
||||
remote/${FLUCKY_REMOTE}: bin/linux/arm/7/${EXECUTABLE}
|
||||
scp bin/linux/arm/7/${EXECUTABLE} root@${FLUCKY_REMOTE}:/usr/local/bin/${EXECUTABLE}
|
||||
# ssh root@${FLUCKY_REMOTE} 'mkdir --parent /etc/bash_completion.d || true'
|
||||
# ssh root@${FLUCKY_REMOTE} 'flucky completion bash > /etc/bash_completion.d/flucky.sh && chmod +x /etc/bash_completion.d/flucky.sh'
|
||||
# ssh root@${FLUCKY_REMOTE} 'flucky completion zsh > /etc/bash_completion.d/flucky.zsh && chmod +x /etc/bash_completion.d/flucky.zsh'
|
||||
ssh root@${FLUCKY_REMOTE} 'chmod +x /usr/local/bin/${EXECUTABLE}'
|
||||
|
||||
# PHONY
|
||||
# ==============================================================================
|
||||
# Declare the contents of the PHONY variable as phony. We keep that information
|
||||
# in a variable so we can use it in if_changed.
|
||||
.PHONY: ${PHONY}
|
93
README.md
93
README.md
@ -1,72 +1,71 @@
|
||||
# flucky
|
||||
|
||||
[](https://travis-ci.com/go-flucky/flucky)
|
||||
[](https://goreportcard.com/report/github.com/volker-raschek/flucky)
|
||||
[](https://drone.cryptic.systems/volker.raschek/flucky)
|
||||
[](https://hub.docker.com/r/volkerraschek/flucky)
|
||||
|
||||
Flucky is a lightweight program written in go for reading data from sensors, for
|
||||
example with a banana or raspberry pi. In addition, flucky provides a REST-API
|
||||
to receive from other flucky installations measured values. The measured values
|
||||
can be saved in a logfile or into a database. Currently is only postgres
|
||||
supported.
|
||||
to receive from other flucky installations measured values. All received
|
||||
measured values, no matter if they come directly from flucky or from a server
|
||||
instance of flucky with the provided REST-API, can be stored into a database.
|
||||
|
||||
## Supported and planned sensors
|
||||
|
||||
| Name | Measured values | Supported |
|
||||
| ------- | --------------------------------------- | --------- |
|
||||
| BME280 | humidity, pressure, temperature | Yes |
|
||||
| DHT11 | temperature | Yes |
|
||||
| DHT22 | temperature | Yes |
|
||||
| DS18B20 | temperature | Yes |
|
||||
| SDS011 | fine dust | Planning |
|
||||
|
||||
## Supported and planned backends
|
||||
|
||||
| Backend | Supported |
|
||||
| --------------- | --------------- |
|
||||
| flucky | WIP |
|
||||
| Logfile | CSV, JSON, XML |
|
||||
| MySQL, MariaDB | Planning |
|
||||
| PostgreSQL | Yes |
|
||||
| SQLite | Planning |
|
||||
|
||||
## Installation
|
||||
|
||||
Flucky can be installed over multiple ways, for example with your local package
|
||||
manager from your linux distribution. At the moment it's only the rpm package
|
||||
format supported. Alternatively the source code can be compiled to get the
|
||||
flucky binary.
|
||||
Flucky can be installed over multiple ways. For example over a specific linux
|
||||
distributions package or as container. The following table lists all
|
||||
repositories where flucky can be otained from, but flucky can already be
|
||||
compiled from source code.
|
||||
|
||||
### RPM-Package
|
||||
|
||||
To install flucky over a RPM package you can download a specific version from
|
||||
github.com or configure the RPM-Mirror.
|
||||
|
||||
#### RPM-Mirror
|
||||
|
||||
```bash
|
||||
$ cat > /etc/yum.repos.d/flucky.rpm <<EOF
|
||||
[flucky]
|
||||
name=flucky
|
||||
baseurl=http://rpm-mirror.cryptic.systems
|
||||
gpgcheck=0
|
||||
EOF
|
||||
$ yum update && yum install --assumeyes flucky
|
||||
```
|
||||
|
||||
#### Download github.com
|
||||
|
||||
```bash
|
||||
$ VERSION=v0.1.0 \
|
||||
curl --location https://github.com/volker-raschek/flucky/releases/${VERSION}/flucky.rpm -O flucky-${VERSION}.rpm
|
||||
$ yum install --assumeyes ./flucky-${VERSION}.rpm
|
||||
```
|
||||
| Distribution | Repo's |
|
||||
| --------------------------- | ------------------------------------------------------------------------------------------- |
|
||||
| Arch Linux | [armv7](https://arch.cryptic.systems/armv7), [x86_64](https://arch.cryptic.systems/x86_64) |
|
||||
| Debian, Ubuntu, Linux Mint | Currently not supported |
|
||||
| RHEL, Fedora, Centos | Planning |
|
||||
| Container Image | [hub.docker.com](https://hub.docker.com/repository/docker/volkerraschek/flucky) |
|
||||
|
||||
### Compiling the source code
|
||||
|
||||
An additional ways to install flucky is to compile the source code. There are
|
||||
two different ways to compile flucky from scratch. The easier ways is
|
||||
to use the pre-defined container image in the Makefile, which has included all
|
||||
dependancies to compile flucky. Alternatively, if all dependencies are met,
|
||||
flucky can also be compiled without the container image. Both variants are
|
||||
briefly described.
|
||||
two different ways to compile flucky from scratch. The easier ways is to use the
|
||||
pre-defined container image, which has included all dependencies to compile
|
||||
flucky. Alternatively, if all dependencies are met, flucky can also be compiled
|
||||
without the container image. Both variants are briefly described.
|
||||
|
||||
#### Compiling the source code via container image
|
||||
|
||||
To compile flucky via container image it's necessary, that a container runtime
|
||||
is installed. In the Makefile is predefined docker, but it's can be also used
|
||||
podman. Execute `make container-go-build` to start the compiling process.
|
||||
is installed. In the `Makefile` is predefined docker, but it's can be also used
|
||||
podman. Execute `make container-run/flucky` to start the compiling process.
|
||||
|
||||
```bash
|
||||
$ make container-go-build
|
||||
make container-run COMMAND=go-build
|
||||
$ make container-run/flucky
|
||||
make container-run COMMAND=flucky
|
||||
make[1]: Directory „/home/markus/workspace/flucky“ is entered
|
||||
/usr/bin/docker run \
|
||||
--rm \
|
||||
--volume /home/markus/workspace/flucky:/workspace \
|
||||
volkerraschek/build-image:latest \
|
||||
make go-build \
|
||||
UID=1000 \
|
||||
GID=1000 \
|
||||
VERSION=60ee044-git \
|
||||
GOOS=linux \
|
||||
GOARCH=amd64
|
||||
@ -82,10 +81,6 @@ go: finding github.com/satori/go.uuid v1.2.0
|
||||
|
||||
#### Compiling the source code without container image
|
||||
|
||||
Make sure you have installed go >= v1.12. Execute `make go-build` to compile
|
||||
Make sure you have installed go >= v1.12. Execute `make flucky` to compile
|
||||
flucky without a container-image. There should be a similar output as when
|
||||
compiling flucky via the container image.
|
||||
|
||||
## Enhancements
|
||||
|
||||
+ Provide flucky additionally as AUR and deb package
|
||||
|
48
cli/daemon/daemon.go
Normal file
48
cli/daemon/daemon.go
Normal file
@ -0,0 +1,48 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/daemon"
|
||||
"git.cryptic.systems/volker.raschek/go-logger"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// InitCmd initialize all daemon subcommands
|
||||
func InitCmd(cmd *cobra.Command) error {
|
||||
daemonCmd := &cobra.Command{
|
||||
Use: "daemon",
|
||||
Short: "Read continuously data from all enabled sensors",
|
||||
Example: "flucky daemon",
|
||||
RunE: run,
|
||||
}
|
||||
|
||||
daemonCmd.Flags().Bool("compression", true, "Compress measured values")
|
||||
daemonCmd.Flags().Uint("cached-values", 500, "Number of cached values before saveing into the backend")
|
||||
daemonCmd.Flags().Float64("round", 0.5, "Round values. The value 0 deactivates the function")
|
||||
cmd.AddCommand(daemonCmd)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func run(cmd *cobra.Command, args []string) error {
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined: %v", err)
|
||||
}
|
||||
|
||||
// logLevel, err := cmd.Flags().GetString("loglevel")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("No loglevel defined: %v", err)
|
||||
// }
|
||||
|
||||
flogger := logger.NewLogger(logger.LogLevelDebug)
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return daemon.Start(cnf, flogger)
|
||||
}
|
135
cli/root.go
Normal file
135
cli/root.go
Normal file
@ -0,0 +1,135 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/cli/daemon"
|
||||
"git.cryptic.systems/volker.raschek/flucky/cli/sensor"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
"github.com/Masterminds/semver"
|
||||
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Execute a
|
||||
func Execute(version *semver.Version) error {
|
||||
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "flucky",
|
||||
// Short: "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
|
||||
PersistentPreRunE: preRunError,
|
||||
Version: version.String(),
|
||||
}
|
||||
|
||||
defaultConfigFile, err := getDefaultConfigFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rootCmd.PersistentFlags().String("config", defaultConfigFile, "Config file")
|
||||
rootCmd.PersistentFlags().String("loglevel", "info", "Set the Loglevel. Possible values: debug, info, warn, error, fatal")
|
||||
|
||||
subCommands := []func(cmd *cobra.Command) error{
|
||||
daemon.InitCmd,
|
||||
sensor.InitCmd,
|
||||
}
|
||||
|
||||
for _, subCommand := range subCommands {
|
||||
if err := subCommand(rootCmd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = rootCmd.Execute()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to determine the hostname: %v", err)
|
||||
}
|
||||
|
||||
// Time must be truncted 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")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
postgresTimeStamp = time.Date(postgresTimeStamp.Year(), postgresTimeStamp.Month(), postgresTimeStamp.Day(), postgresTimeStamp.Hour(), postgresTimeStamp.Minute(), postgresTimeStamp.Second(), int(math.Round(float64(postgresTimeStamp.Nanosecond())/1000000)*1000000), location)
|
||||
|
||||
// default cache directory
|
||||
defaultCacheDir, err := getDefaultCacheDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Default configuration
|
||||
dsn := fmt.Sprintf("sqlite3://%v/sqlite.db?cache=shared&mode=memory&foreign_keys=on", defaultCacheDir)
|
||||
cnf := config.Config{
|
||||
Device: &types.Device{
|
||||
ID: uuid.NewV4().String(),
|
||||
Name: hostname,
|
||||
CreationDate: postgresTimeStamp,
|
||||
},
|
||||
DSN: dsn,
|
||||
}
|
||||
|
||||
err = config.Write(&cnf, configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getDefaultConfigFile returns the default path to the configuration file of
|
||||
// flucky
|
||||
func getDefaultConfigFile() (string, error) {
|
||||
u, err := user.Current()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Can not read current user: %v", err)
|
||||
}
|
||||
|
||||
switch u.Uid {
|
||||
case "0":
|
||||
return "/etc/flucky/config.json", nil
|
||||
default:
|
||||
return filepath.Join(u.HomeDir, ".config/flucky/config.json"), nil
|
||||
}
|
||||
}
|
||||
|
||||
// getDefaultCacheDir returns the default path to the cache directory where
|
||||
// flucky stores his measured values.
|
||||
func getDefaultCacheDir() (string, error) {
|
||||
u, err := user.Current()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Can not read current user: %v", err)
|
||||
}
|
||||
|
||||
switch u.Uid {
|
||||
case "0":
|
||||
return "/var/cache/flucky", nil
|
||||
default:
|
||||
return filepath.Join(u.HomeDir, ".cache/flucky"), nil
|
||||
}
|
||||
}
|
370
cli/sensor/sensor.go
Normal file
370
cli/sensor/sensor.go
Normal file
@ -0,0 +1,370 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/cli"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/repository"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
"git.cryptic.systems/volker.raschek/go-logger"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// InitCmd initialize all sensor subcommands
|
||||
func InitCmd(cmd *cobra.Command) error {
|
||||
sensorCmd := &cobra.Command{
|
||||
Use: "sensor",
|
||||
Short: "Manage Sensors",
|
||||
}
|
||||
|
||||
addSensorCmd := &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add Sensor",
|
||||
Aliases: []string{"append"},
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: `flucky sensor add --gpio GPIO14 indoor DHT11
|
||||
flucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20
|
||||
flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`,
|
||||
RunE: addSensor,
|
||||
}
|
||||
addSensorCmd.Flags().Bool("enabled", true, "Enable new sensor")
|
||||
addSensorCmd.Flags().String("gpio", "", "Defines the GPIO port")
|
||||
addSensorCmd.Flags().Uint8("i2c-address", 0, "Defines the I2C address on the I2C bus")
|
||||
addSensorCmd.Flags().Int("i2c-bus", 0, "Defines the I2C bus")
|
||||
addSensorCmd.Flags().String("location", "", "Location of the sensor")
|
||||
addSensorCmd.Flags().String("tick-duration", "1m", "Specifies how often measured values should be read from the sensor")
|
||||
addSensorCmd.Flags().String("wire-id", "", "Defines the Wire-ID")
|
||||
|
||||
disableSensorCmd := &cobra.Command{
|
||||
Use: "disable",
|
||||
Short: "Disable Sensor",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Example: "flucky sensor disable outdoor",
|
||||
RunE: disableSensor,
|
||||
}
|
||||
|
||||
enableSensorCmd := &cobra.Command{
|
||||
Use: "enable",
|
||||
Short: "Enable Sensor",
|
||||
Example: "flucky sensor enable outdoor",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: enableSensor,
|
||||
}
|
||||
|
||||
listSensorCmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List Sensors",
|
||||
Aliases: []string{"ls"},
|
||||
RunE: listSensors,
|
||||
}
|
||||
|
||||
removeSensorCmd := &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "Remove Sensor",
|
||||
Aliases: []string{"rm"},
|
||||
Example: "flucky sensor remove outdoor",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: removeSensor,
|
||||
}
|
||||
|
||||
renameSensorCmd := &cobra.Command{
|
||||
Use: "rename",
|
||||
Short: "Rename Sensor",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: `flucky sensor rename indoor outdoor
|
||||
flucky sensor rename f98b00ea-a9b2-4e00-924f-113859d0af2d outdoor`,
|
||||
RunE: renameSensor,
|
||||
}
|
||||
|
||||
for _, subCommand := range []*cobra.Command{
|
||||
addSensorCmd,
|
||||
disableSensorCmd,
|
||||
enableSensorCmd,
|
||||
listSensorCmd,
|
||||
removeSensorCmd,
|
||||
renameSensorCmd,
|
||||
} {
|
||||
sensorCmd.AddCommand(subCommand)
|
||||
}
|
||||
|
||||
cmd.AddCommand(sensorCmd)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func addSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
sensor := &types.Sensor{
|
||||
ID: uuid.NewV4().String(),
|
||||
Name: args[0],
|
||||
Model: args[1],
|
||||
}
|
||||
|
||||
sensorLocation, err := cmd.Flags().GetString("location")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sensor.Location = sensorLocation
|
||||
|
||||
sensorEnabled, err := cmd.Flags().GetBool("enabled")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sensor.Enabled = sensorEnabled
|
||||
|
||||
sensorGPIO, err := cmd.Flags().GetString("gpio")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(sensorGPIO) > 0 {
|
||||
sensor.GPIONumber = sensorGPIO
|
||||
}
|
||||
|
||||
sensorI2CAddress, err := cmd.Flags().GetUint8("i2c-address")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if sensorI2CAddress > 0 {
|
||||
sensor.I2CAddress = &sensorI2CAddress
|
||||
}
|
||||
|
||||
sensorI2CBus, err := cmd.Flags().GetInt("i2c-bus")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if sensorI2CBus > 0 {
|
||||
sensor.I2CBus = &sensorI2CBus
|
||||
}
|
||||
|
||||
sensorTickDuration, err := cmd.Flags().GetString("tick-duration")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sensor.TickDuration = sensorTickDuration
|
||||
|
||||
sensorWireID, err := cmd.Flags().GetString("wire-id")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(sensorWireID) > 0 {
|
||||
sensor.WireID = &sensorWireID
|
||||
}
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sensor.DeviceID = cnf.Device.ID
|
||||
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// loglevel, err := cmd.Flags().GetString("loglevel")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("No loglevel defined")
|
||||
// }
|
||||
|
||||
flogger := logger.NewLogger(logger.LogLevelDebug)
|
||||
|
||||
repo, err := repository.New(dsnURL, flogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// add sensor entry to list
|
||||
err = repo.AddSensors(sensor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
// err = config.Write(cnf, configFile)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func disableSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// loglevel, err := cmd.Flags().GetString("loglevel")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("No loglevel defined")
|
||||
// }
|
||||
|
||||
flogger := logger.NewLogger(logger.LogLevelDebug)
|
||||
|
||||
repo, err := repository.New(dsnURL, flogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return repo.DisableSensorsByNames(args...)
|
||||
}
|
||||
|
||||
func enableSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// loglevel, err := cmd.Flags().GetString("loglevel")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("No loglevel defined")
|
||||
// }
|
||||
|
||||
flogger := logger.NewLogger(logger.LogLevelDebug)
|
||||
|
||||
repo, err := repository.New(dsnURL, flogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return repo.EnableSensorsByNames(args...)
|
||||
}
|
||||
|
||||
func listSensors(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// loglevel, err := cmd.Flags().GetString("loglevel")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("No loglevel defined")
|
||||
// }
|
||||
|
||||
flogger := logger.NewLogger(logger.LogLevelDebug)
|
||||
|
||||
repo, err := repository.New(dsnURL, flogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// add sensor entry to list
|
||||
sensors, err := repo.GetSensorsByDeviceID(cnf.Device.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = cli.PrintSensors(sensors, os.Stdout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// loglevel, err := cmd.Flags().GetString("loglevel")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("No loglevel defined")
|
||||
// }
|
||||
|
||||
flogger := logger.NewLogger(logger.LogLevelDebug)
|
||||
|
||||
repo, err := repository.New(dsnURL, flogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return repo.RemoveSensorsByNames(args...)
|
||||
}
|
||||
|
||||
func renameSensor(cmd *cobra.Command, args []string) error {
|
||||
|
||||
configFile, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
return fmt.Errorf("No config file defined")
|
||||
}
|
||||
|
||||
cnf, err := config.Read(configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// loglevel, err := cmd.Flags().GetString("loglevel")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("No loglevel defined")
|
||||
// }
|
||||
|
||||
flogger := logger.NewLogger(logger.LogLevelDebug)
|
||||
|
||||
repo, err := repository.New(dsnURL, flogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return repo.RenameSensors(args[0], args[1])
|
||||
}
|
80
cmd/cmd.go
80
cmd/cmd.go
@ -1,80 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-flucky/flucky/cmd/compression"
|
||||
"github.com/go-flucky/flucky/cmd/convert"
|
||||
"github.com/go-flucky/flucky/cmd/daemon"
|
||||
"github.com/go-flucky/flucky/cmd/humidity"
|
||||
"github.com/go-flucky/flucky/cmd/pressure"
|
||||
"github.com/go-flucky/flucky/cmd/rgbled"
|
||||
"github.com/go-flucky/flucky/cmd/sensor"
|
||||
"github.com/go-flucky/flucky/cmd/temperature"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var configFile string
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "flucky",
|
||||
Short: "flucky - operate with differen sensors, his values and remote servers to synchronize measured values",
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
// check if config file exists
|
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not locate the hostname: %v", err)
|
||||
}
|
||||
|
||||
// Time must be truncted for postgres
|
||||
// Postgres currently does not support nanoseconds which is automatically
|
||||
// include into the go time object
|
||||
t := time.Now()
|
||||
l, _ := time.LoadLocation("Europe/Berlin")
|
||||
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), int(math.Round(float64(t.Nanosecond())/1000000)*1000000), l)
|
||||
|
||||
cnf := config.Configuration{
|
||||
Device: &types.Device{
|
||||
DeviceID: uuid.NewV4().String(),
|
||||
DeviceName: hostname,
|
||||
Logfile: "/var/log/flucky/logfile.csv",
|
||||
CreationDate: t,
|
||||
},
|
||||
}
|
||||
|
||||
err = config.Write(&cnf, configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func Execute(version string) {
|
||||
rootCmd.Version = version
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
|
||||
|
||||
compression.InitCmd(rootCmd, &configFile)
|
||||
convert.InitCmd(rootCmd, &configFile)
|
||||
daemon.InitCmd(rootCmd, &configFile)
|
||||
// db.InitCmd(rootCmd, &configFile)
|
||||
humidity.InitCmd(rootCmd, &configFile)
|
||||
pressure.InitCmd(rootCmd, &configFile)
|
||||
rgbled.InitCmd(rootCmd, &configFile)
|
||||
sensor.InitCmd(rootCmd, &configFile)
|
||||
temperature.InitCmd(rootCmd, &configFile)
|
||||
rootCmd.Execute()
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package compression
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var compression bool
|
||||
var configFile *string
|
||||
|
||||
var compressionCmd = &cobra.Command{
|
||||
Use: "compression",
|
||||
Short: "Compress a logfile",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Example: "flucky compression /var/log/flucky/logfile.csv",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
measuredValueLogfile := logfile.New(args[0])
|
||||
measuredValues, err := measuredValueLogfile.Read()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues = logfile.Compression(measuredValues)
|
||||
|
||||
err = measuredValueLogfile.Write(measuredValues)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
cmd.AddCommand(compressionCmd)
|
||||
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var compression bool
|
||||
var configFile *string
|
||||
|
||||
var convertCmd = &cobra.Command{
|
||||
Use: "convert",
|
||||
Short: "Convert logfiles into other markup language",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: "flucky convert /var/log/flucky/logfile.json /var/log/flucky/logfile.csv",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
measuredValuesInput := logfile.New(args[0])
|
||||
measuredValues, err := measuredValuesInput.Read()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if compression {
|
||||
measuredValues = logfile.Compression(measuredValues)
|
||||
}
|
||||
|
||||
measuredValuesOutput := logfile.New(args[1])
|
||||
err = measuredValuesOutput.Write(measuredValues)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
cmd.AddCommand(convertCmd)
|
||||
convertCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values")
|
||||
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/daemon"
|
||||
"github.com/go-flucky/flucky/pkg/logger"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cleanCacheInterval string
|
||||
var compression bool
|
||||
var configFile *string
|
||||
var round float64
|
||||
var temperatureUnit string
|
||||
|
||||
var daemonCmd = &cobra.Command{
|
||||
Use: "daemon",
|
||||
Short: "Read continuously data from all enabled sensors",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
duration, err := time.ParseDuration(cleanCacheInterval)
|
||||
if err != nil {
|
||||
log.Fatalf("Can not parse clean cache interval into duration time: %v", err)
|
||||
}
|
||||
|
||||
logger := logger.NewDefaultLogger(logger.LogLevelDebug)
|
||||
|
||||
daemon.Start(cnf, duration, compression, round, logger)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
cmd.AddCommand(daemonCmd)
|
||||
daemonCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values")
|
||||
daemonCmd.Flags().StringVar(&cleanCacheInterval, "clean-cache-interval", "5m", "Minute intervall to clean cache and write measured values into logfile")
|
||||
daemonCmd.Flags().Float64Var(&round, "round", 0.5, "Round values. The value 0 deactivates the function")
|
||||
}
|
49
cmd/db/db.go
49
cmd/db/db.go
@ -1,49 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
database "github.com/go-flucky/flucky/pkg/db"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var configFile *string
|
||||
|
||||
var dbCmd = &cobra.Command{
|
||||
Use: "db",
|
||||
Short: "Operates with the configured database",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
postgresDB, err := database.New(database.DBOTypePostgres, "localhost", "5432", "postgres", "postgres", "postgres")
|
||||
if err != nil {
|
||||
log.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
devices := []*types.Device{
|
||||
&types.Device{
|
||||
DeviceID: "1684df26-bc72-4435-a4f9-74b24bdb286c",
|
||||
DeviceName: "raspberr-pi",
|
||||
},
|
||||
&types.Device{
|
||||
DeviceID: "1684df26-bc72-4435-a4f9-74b24bdb286c",
|
||||
DeviceName: "raspberr-pi",
|
||||
},
|
||||
}
|
||||
|
||||
if err := postgresDB.InsertDevices(ctx, devices); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
|
||||
cmd.AddCommand(dbCmd)
|
||||
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package humidity
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var compression bool
|
||||
var configFile *string
|
||||
var round float64
|
||||
|
||||
var humidityCmd = &cobra.Command{
|
||||
Use: "humidity",
|
||||
Short: "Operates with humidity values",
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
|
||||
cmd.AddCommand(humidityCmd)
|
||||
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
package humidity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var listTemperatureCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List humidity values from different or specified sensors by arguments",
|
||||
Example: fmt.Sprintf("flucky humidity logs"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
logfile := logfile.New(cnf.Device.Logfile)
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
if err := rgbled.Logfile(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues, err := logfile.Read()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if err := rgbled.Off(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeHumidity, measuredValues)
|
||||
|
||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
humidityCmd.AddCommand(listTemperatureCmd)
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package humidity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/go-flucky/flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var logs bool
|
||||
|
||||
var readHumidityCmd = &cobra.Command{
|
||||
Use: "read",
|
||||
Short: "Reading air pressure values from different or specified sensors by arguments",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// fetch all temperature sensors or sensors by args
|
||||
sensors := make([]sensor.Sensor, 0)
|
||||
if len(args) == 0 {
|
||||
sensors = cnf.GetHumiditySensors(config.ENABLED)
|
||||
} else {
|
||||
sensors = cnf.GetHumiditySensorsByName(args)
|
||||
}
|
||||
|
||||
if len(sensors) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
if err := rgbled.Run(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
measuredValues, err := sensor.Read(ctx, sensors)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeHumidity, measuredValues)
|
||||
|
||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||
|
||||
if logs {
|
||||
measuredValuesLogfile := logfile.New(cnf.Device.Logfile)
|
||||
err := logfile.Append(measuredValuesLogfile, compression, round, measuredValues)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
rgbled.Off(rgbLEDs)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
humidityCmd.AddCommand(readHumidityCmd)
|
||||
readHumidityCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature")
|
||||
readHumidityCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures")
|
||||
readHumidityCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function")
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
package pressure
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var listTemperatureCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "Reading temperature values from different or specified sensors by arguments",
|
||||
Example: fmt.Sprintf("flucky pressure logs"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
logfile := logfile.New(cnf.Device.Logfile)
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
if err := rgbled.Logfile(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues, err := logfile.Read()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if err := rgbled.Off(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypePressure, measuredValues)
|
||||
|
||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
pressureCmd.AddCommand(listTemperatureCmd)
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package pressure
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var compression bool
|
||||
var configFile *string
|
||||
var round float64
|
||||
|
||||
var pressureCmd = &cobra.Command{
|
||||
Use: "pressure",
|
||||
Short: "List air pressure values from different or specified sensors by arguments",
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
|
||||
cmd.AddCommand(pressureCmd)
|
||||
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package pressure
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/go-flucky/flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var logs bool
|
||||
|
||||
var readPressureCmd = &cobra.Command{
|
||||
Use: "read",
|
||||
Short: "Operates with air pressure values",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// fetch all temperature sensors or sensors by args
|
||||
sensors := make([]sensor.Sensor, 0)
|
||||
if len(args) == 0 {
|
||||
sensors = cnf.GetPressureSensors(config.ENABLED)
|
||||
} else {
|
||||
sensors = cnf.GetPressureSensorsByName(args)
|
||||
}
|
||||
|
||||
if len(sensors) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
if err := rgbled.Run(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
measuredValues, err := sensor.Read(ctx, sensors)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypePressure, measuredValues)
|
||||
|
||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||
|
||||
if logs {
|
||||
measuredValuesLogfile := logfile.New(cnf.Device.Logfile)
|
||||
err := logfile.Append(measuredValuesLogfile, compression, round, measuredValues)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
rgbled.Off(rgbLEDs)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
pressureCmd.AddCommand(readPressureCmd)
|
||||
readPressureCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature")
|
||||
readPressureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures")
|
||||
readPressureCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function")
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var enabled bool
|
||||
var location string
|
||||
|
||||
var addRgbLedCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add a RGB-LED",
|
||||
Aliases: []string{"append"},
|
||||
Args: cobra.ExactArgs(4),
|
||||
Example: fmt.Sprintf(`flucky rgb-led add <name> <gpio-for-blue> <gpio-for-green> <gpio-for-red>
|
||||
flucky rgb-led add my-led GPIO13 GPIO17 GPIO26`),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// determine gpio port
|
||||
gpioBlue, err := types.StringToGPIO(args[1])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
gpioGreen, err := types.StringToGPIO(args[2])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
gpioRed, err := types.StringToGPIO(args[3])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// create new sensor struct
|
||||
rgbLED := &types.RGBLED{
|
||||
RGBLEDName: args[0],
|
||||
RGBLEDLocation: location,
|
||||
RGBLEDEnabled: enabled,
|
||||
ActionMapping: types.DefaultActionMapping,
|
||||
BaseColorsToGPIO: map[types.BaseColor]*types.GPIO{
|
||||
types.BaseColorBlue: &gpioBlue,
|
||||
types.BaseColorGreen: &gpioGreen,
|
||||
types.BaseColorRed: &gpioRed,
|
||||
},
|
||||
}
|
||||
|
||||
// // add sensor entry to list
|
||||
err = cnf.AddRGBLED(rgbLED)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rgbLedCmd.AddCommand(addRgbLedCmd)
|
||||
|
||||
addRgbLedCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
|
||||
addRgbLedCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location")
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var disableRgbLedCmd = &cobra.Command{
|
||||
Use: "disable",
|
||||
Short: "Disable a RGB-LED",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Example: `flucky rgb-led disable <name/uuid>
|
||||
flucky rgb-led disable my-led
|
||||
flucky rgb-led disable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// disable sensor entry to list
|
||||
err = cnf.DisableRGBLED(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rgbLedCmd.AddCommand(disableRgbLedCmd)
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var enableRgbLedCmd = &cobra.Command{
|
||||
Use: "enable",
|
||||
Short: "Enable a RGB-LED",
|
||||
Example: `flucky rgb-led enable <name/uuid>
|
||||
flucky rgb-led enable my-led
|
||||
flucky rgb-led enable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// disable sensor entry to list
|
||||
err = cnf.EnableRGBLED(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rgbLedCmd.AddCommand(enableRgbLedCmd)
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var listRgbLedCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List RGB-LEDs",
|
||||
Aliases: []string{"ls"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// print sensors on stdout
|
||||
cli.PrintRGBLEDs(cnf, os.Stdout)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rgbLedCmd.AddCommand(listRgbLedCmd)
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var offRgbLedCmd = &cobra.Command{
|
||||
Use: "off",
|
||||
Short: "Turn a RGB-LED color off",
|
||||
Example: `flucky rgb-led off <name/uuid> <blue>
|
||||
flucky rgb-led off my-led`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
rgbLEDs := make([]rgbled.RGBLED, 0)
|
||||
if len(args) != 0 {
|
||||
rgbLEDs = cnf.GetRGBLEDsByName(args)
|
||||
} else {
|
||||
rgbLEDs = cnf.GetRGBLEDs(config.ENABLED)
|
||||
}
|
||||
|
||||
err = rgbled.Off(rgbLEDs)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rgbLedCmd.AddCommand(offRgbLedCmd)
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var onRgbLedCmd = &cobra.Command{
|
||||
Use: "on",
|
||||
Short: "Turn a RGB-LED color on",
|
||||
Example: `flucky rgb-led on <names/uuids> <blue/green/purple/red/turquoise/white/yellow>
|
||||
flucky rgb-led on my-led blue
|
||||
flucky rgb-led on my-led my-sweet-led white
|
||||
flucky rgb-led on 1c5b9424-f6e9-4a37-be5c-77e531e94aab red`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
rgbLEDs := make([]rgbled.RGBLED, 0)
|
||||
if len(args) > 1 {
|
||||
rgbLEDs = cnf.GetRGBLEDsByName(args[0 : len(args)-1])
|
||||
} else {
|
||||
rgbLEDs = cnf.GetRGBLEDs(config.ENABLED)
|
||||
}
|
||||
|
||||
color, err := types.StringToLEDColor(args[len(args)-1])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
err = rgbled.CustomColor(rgbLEDs, color)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rgbLedCmd.AddCommand(onRgbLedCmd)
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var removeRgbLedCmd = &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "Remove a RGB-LED",
|
||||
Example: `flucky rgb-led remove <name/uuid>
|
||||
flucky rgb-led remove my-led
|
||||
flucky rgb-led remove 9f8abfc5-91f3-480c-a42d-b990b6f89e5d`,
|
||||
Aliases: []string{"rm"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// // add remote entry to list
|
||||
err = cnf.RemoveRGBLED(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rgbLedCmd.AddCommand(removeRgbLedCmd)
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var renameRgbLedCmd = &cobra.Command{
|
||||
Use: "rename",
|
||||
Short: "Rename a RGB-LED",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: `flucky rgb-led disable <name/uuid> <new-name>
|
||||
flucky rgb-led disable my-led my-sweet-led
|
||||
flucky rgb-led disable 9f8abfc5-91f3-480c-a42d-b990b6f89e5d my-sweet-led`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// rename sensor
|
||||
err = cnf.RenameRGBLED(args[0], args[1])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rgbLedCmd.AddCommand(renameRgbLedCmd)
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package rgbled
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var configFile *string
|
||||
|
||||
var rgbLedCmd = &cobra.Command{
|
||||
Use: "rgb-led",
|
||||
Short: "Manage RGB-LEDs",
|
||||
}
|
||||
|
||||
// InitCmd da
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
|
||||
cmd.AddCommand(rgbLedCmd)
|
||||
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var enabled bool
|
||||
var gpioIn string
|
||||
var i2cAddress uint8
|
||||
var i2cBus int
|
||||
var location string
|
||||
var wireID string
|
||||
|
||||
var addSensorCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add Sensor",
|
||||
Aliases: []string{"append"},
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: `flucky sensor add --gpio GPIO14 indoor DHT11
|
||||
flucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20
|
||||
flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// determine sensor model
|
||||
sensorModel, err := types.SelectSensorModel(args[1])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// create new sensor struct
|
||||
sensor := &types.Sensor{
|
||||
SensorName: args[0],
|
||||
SensorModel: sensorModel,
|
||||
SensorLocation: location,
|
||||
SensorEnabled: enabled,
|
||||
}
|
||||
|
||||
// determine gpio port if set
|
||||
if gpioIn != "" &&
|
||||
i2cAddress == 0 &&
|
||||
i2cBus == 0 &&
|
||||
wireID == "" {
|
||||
gpio, err := types.StringToGPIO(gpioIn)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
sensor.GPIONumber = &gpio
|
||||
}
|
||||
|
||||
// set i2c connection settings
|
||||
if gpioIn == "" &&
|
||||
i2cAddress != 0 &&
|
||||
i2cBus != 0 &&
|
||||
wireID == "" {
|
||||
sensor.I2CAddress = &i2cAddress
|
||||
sensor.I2CBus = &i2cBus
|
||||
}
|
||||
|
||||
// set wire connection settings
|
||||
if gpioIn == "" &&
|
||||
i2cAddress == 0 &&
|
||||
i2cBus == 0 &&
|
||||
wireID != "" {
|
||||
sensor.WireID = &wireID
|
||||
}
|
||||
|
||||
// add sensor entry to list
|
||||
err = cnf.AddSensor(sensor)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(addSensorCmd)
|
||||
|
||||
addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable Sensor")
|
||||
addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "GPIO")
|
||||
addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "I2C-Address")
|
||||
addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "I2C-Bus")
|
||||
addSensorCmd.Flags().StringVar(&location, "location", "", "Sensor location")
|
||||
addSensorCmd.Flags().StringVar(&wireID, "wire-id", "", "Wire-ID")
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var disableSensorCmd = &cobra.Command{
|
||||
Use: "disable",
|
||||
Short: "Disable Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Example: "flucky sensor disable outdoor",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// disable sensor entry to list
|
||||
err = cnf.DisableSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(disableSensorCmd)
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var enableSensorCmd = &cobra.Command{
|
||||
Use: "enable",
|
||||
Short: "Enable Sensor",
|
||||
Example: "flucky sensor enable outdoor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// disable sensor entry to list
|
||||
err = cnf.EnableSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(enableSensorCmd)
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var listSensorCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List Sensors",
|
||||
Aliases: []string{"ls"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// print sensors on stdout
|
||||
err = cli.PrintSensors(cnf, os.Stdout)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(listSensorCmd)
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rmSensorCmd = &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "Remove Sensor",
|
||||
Example: "flucky sensor rm outdoor",
|
||||
Aliases: []string{"rm"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// // add remote entry to list
|
||||
err = cnf.RemoveSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(rmSensorCmd)
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var renameSensorCmd = &cobra.Command{
|
||||
Use: "rename",
|
||||
Short: "Rename Sensor",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: fmt.Sprintf("flucky sensor rename indoor outdoor\nflucky sensor rename f98b00ea-a9b2-4e00-924f-113859d0af2d outdoor"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// rename sensor
|
||||
err = cnf.RenameSensor(args[0], args[1])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(cnf, *configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(renameSensorCmd)
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var configFile *string
|
||||
|
||||
var sensorCmd = &cobra.Command{
|
||||
Use: "sensor",
|
||||
Short: "Manage Sensors",
|
||||
}
|
||||
|
||||
// InitCmd da
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
|
||||
cmd.AddCommand(sensorCmd)
|
||||
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var listTemperatureCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List temperature values from different or specified sensors by arguments",
|
||||
Example: fmt.Sprintf("flucky temperature logs"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
logfile := logfile.New(cnf.Device.Logfile)
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
if err := rgbled.Logfile(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues, err := logfile.Read()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if err := rgbled.Off(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues)
|
||||
|
||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(listTemperatureCmd)
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/cli"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var logs bool
|
||||
|
||||
var readTemperatureCmd = &cobra.Command{
|
||||
Use: "read",
|
||||
Short: "Reading temperature values from different or specified sensors by arguments",
|
||||
Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
cnf, err := config.Read(*configFile)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// fetch all temperature sensors or sensors by args
|
||||
sensors := make([]sensor.Sensor, 0)
|
||||
if len(args) == 0 {
|
||||
sensors = cnf.GetTemperatureSensors(config.ENABLED)
|
||||
} else {
|
||||
sensors = cnf.GetTemperatureSensorsByName(args)
|
||||
}
|
||||
|
||||
if len(sensors) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
if err := rgbled.Run(rgbLEDs); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
measuredValues, err := sensor.Read(ctx, sensors)
|
||||
if err != nil {
|
||||
rgbled.Error(rgbLEDs)
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues)
|
||||
|
||||
// print temperatures on stdout
|
||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||
|
||||
if logs {
|
||||
measuredValuesLogfile := logfile.New(cnf.Device.Logfile)
|
||||
err := logfile.Append(measuredValuesLogfile, compression, round, measuredValues)
|
||||
if err != nil {
|
||||
rgbled.Error(rgbLEDs)
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
rgbled.Off(rgbLEDs)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(readTemperatureCmd)
|
||||
readTemperatureCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature")
|
||||
readTemperatureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures")
|
||||
readTemperatureCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function")
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var compression bool
|
||||
var configFile *string
|
||||
var round float64
|
||||
|
||||
var temperatureCmd = &cobra.Command{
|
||||
Use: "temperature",
|
||||
Short: "Operates with temperature values",
|
||||
Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"),
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func InitCmd(cmd *cobra.Command, cnfFile *string) {
|
||||
configFile = cnfFile
|
||||
cmd.AddCommand(temperatureCmd)
|
||||
|
||||
}
|
@ -3,14 +3,12 @@ services:
|
||||
flucky-db:
|
||||
container_name: postgres
|
||||
environment:
|
||||
- PGTZ=${TZ}
|
||||
- POSTGRES_PASSWORD=${PG_PASSWORD}
|
||||
- POSTGRES_USER=${PG_USER}
|
||||
- POSTGRES_DB=${PG_NAME}
|
||||
- TZ=${TZ}
|
||||
- PGTZ=Europe/Berlin
|
||||
- POSTGRES_PASSWORD=postgres
|
||||
- TZ=Europe/Berlin
|
||||
image: postgres:11.5-alpine
|
||||
ports:
|
||||
- ${PG_EXTERN_PORT}:${PG_INTERN_PORT}/tcp
|
||||
- 5432:5432
|
||||
restart: always
|
||||
volumes:
|
||||
- /etc/localtime:/etc/localtime:ro
|
||||
|
30
go.mod
30
go.mod
@ -1,20 +1,24 @@
|
||||
module github.com/go-flucky/flucky
|
||||
module git.cryptic.systems/volker.raschek/flucky
|
||||
|
||||
go 1.12
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
git.cryptic.systems/volker.raschek/go-dht v0.1.2
|
||||
git.cryptic.systems/volker.raschek/go-logger v0.1.0
|
||||
github.com/Masterminds/semver v1.5.0
|
||||
github.com/Microsoft/go-winio v0.4.14 // indirect
|
||||
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375
|
||||
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce
|
||||
github.com/d2r2/go-i2c v0.0.0-20191123181816-73a8a799d6bc
|
||||
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e
|
||||
github.com/go-flucky/go-dht v0.1.1
|
||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/lib/pq v1.2.0
|
||||
github.com/docker/distribution v2.7.1+incompatible // indirect
|
||||
github.com/docker/docker v1.13.1
|
||||
github.com/docker/go-connections v0.4.0
|
||||
github.com/docker/go-units v0.4.0 // indirect
|
||||
github.com/lib/pq v1.7.0
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
github.com/spf13/cobra v0.0.3
|
||||
github.com/spf13/pflag v1.0.3 // indirect
|
||||
github.com/stianeikeland/go-rpio v4.2.0+incompatible
|
||||
github.com/stretchr/testify v1.3.0
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
periph.io/x/periph v3.4.0+incompatible
|
||||
github.com/spf13/cobra v1.0.0
|
||||
github.com/stretchr/testify v1.6.1
|
||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
|
||||
)
|
||||
|
177
go.sum
177
go.sum
@ -1,36 +1,185 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
git.cryptic.systems/volker.raschek/go-dht v0.1.2 h1:kGmfpaVUETQhSELCIrKXMjKwuUhQkRUz/7VbLYiTRJA=
|
||||
git.cryptic.systems/volker.raschek/go-dht v0.1.2/go.mod h1:FUMwxa4cD+ATHPztXJntlO22I0DBTUPtXxfRF0JxXy8=
|
||||
git.cryptic.systems/volker.raschek/go-logger v0.1.0 h1:JHBDesKBZaXjc2AlqYms1T3dGIX0oNIOBWl4cnVFWIo=
|
||||
git.cryptic.systems/volker.raschek/go-logger v0.1.0/go.mod h1:GqeuxFj64SAolfj5kpbWup6E1vv37SaH5S+4wa40Tqs=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
|
||||
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
|
||||
github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU=
|
||||
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
|
||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
|
||||
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
|
||||
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
||||
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375 h1:vdUOwcZdV+bBfGUUh5oPPWSzw9p+lBnNSuGgQwGpCH4=
|
||||
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375/go.mod h1:3iz1WHlYJU9b4NJei+Q8G7DN3K05arcCMlOQ+qNCDjo=
|
||||
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce h1:Dog7PLNz1fPaXqHPOHonpERqsF57Oh4X76pM80T1GDY=
|
||||
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce/go.mod h1:AwxDPnsgIpy47jbGXZHA9Rv7pDkOJvQbezPuK1Y+nNk=
|
||||
github.com/d2r2/go-i2c v0.0.0-20191123181816-73a8a799d6bc h1:HLRSIWzUGMLCq4ldt0W1GLs3nnAxa5EGoP+9qHgh6j0=
|
||||
github.com/d2r2/go-i2c v0.0.0-20191123181816-73a8a799d6bc/go.mod h1:AwxDPnsgIpy47jbGXZHA9Rv7pDkOJvQbezPuK1Y+nNk=
|
||||
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e h1:ZG3JBA6rPRl0xxQ+nNSfO7tor8w+CNCTs05DNJQYbLM=
|
||||
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e/go.mod h1:oA+9PUt8F1aKJ6o4YU1T120i7sgo1T6/1LWEEBy0BSs=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-flucky/go-dht v0.1.1 h1:dPz9F5D7oUaTd0GUGTvT4lBH2R043h1bkmhTlpQax1Y=
|
||||
github.com/go-flucky/go-dht v0.1.1/go.mod h1:Yk/cct+/u+eCS7pB/kc0tc7MrVXdFI4W15MJCj5FRUc=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
|
||||
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
|
||||
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
|
||||
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
|
||||
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
|
||||
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
|
||||
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
|
||||
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
|
||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY=
|
||||
github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
|
||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
|
||||
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
|
||||
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
|
||||
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
|
||||
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
||||
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
|
||||
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
|
||||
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
|
||||
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
|
||||
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
|
||||
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
|
||||
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
|
||||
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
|
||||
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||
github.com/stianeikeland/go-rpio v4.2.0+incompatible h1:CUOlIxdJdT+H1obJPsmg8byu7jMSECLfAN9zynm5QGo=
|
||||
github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8=
|
||||
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
|
||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
||||
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
|
||||
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco=
|
||||
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM=
|
||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b h1:ag/x1USPSsqHud38I9BAC88qdNLDHHtQ4mlgQIZPPNA=
|
||||
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
periph.io/x/periph v3.4.0+incompatible h1:5gzxE4ryPq52cdqSw0mErR6pyJK8cBF2qdUAcOWh0bo=
|
||||
periph.io/x/periph v3.4.0+incompatible/go.mod h1:EWr+FCIU2dBWz5/wSWeiIUJTriYv9v2j2ENBmgYyy7Y=
|
||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
periph.io/x/periph v3.6.4+incompatible h1:8FyXTbu9lcMVofz8mf+cj1pzTLN4V6EuPY2EF+DoJF4=
|
||||
periph.io/x/periph v3.6.4+incompatible/go.mod h1:EWr+FCIU2dBWz5/wSWeiIUJTriYv9v2j2ENBmgYyy7Y=
|
||||
|
23
main.go
23
main.go
@ -3,25 +3,24 @@ package main
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/user"
|
||||
|
||||
"github.com/go-flucky/flucky/cmd"
|
||||
"git.cryptic.systems/volker.raschek/flucky/cli"
|
||||
"github.com/Masterminds/semver"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var version string
|
||||
var (
|
||||
version string
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
user, err := user.Current()
|
||||
|
||||
sversion, err := semver.NewVersion(version)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if user.Uid != "0" {
|
||||
log.Println("you need to be root to run this command")
|
||||
log.Printf("The sematic versioning is invalid: %v", version)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cmd.Execute(version)
|
||||
cli.Execute(sversion)
|
||||
}
|
||||
|
120
pkg/cli/cli.go
120
pkg/cli/cli.go
@ -5,74 +5,19 @@ import (
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
)
|
||||
|
||||
// GetSensorsByMeasuredValues returns commulated list of sensors by measured values
|
||||
func GetSensorsByMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration) []*types.Sensor {
|
||||
sensors := []*types.Sensor{}
|
||||
for _, measuredValue := range measuredValues {
|
||||
duplicated := false
|
||||
foundSensor := &types.Sensor{}
|
||||
for _, cnfSensor := range cnf.Sensors {
|
||||
if measuredValue.SensorID == cnfSensor.SensorID {
|
||||
foundSensor = cnfSensor
|
||||
|
||||
// compare if id has already been added to list
|
||||
for _, sensor := range sensors {
|
||||
if cnfSensor.SensorID == sensor.SensorID {
|
||||
duplicated = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if duplicated {
|
||||
continue
|
||||
}
|
||||
if foundSensor != nil {
|
||||
sensors = append(sensors, foundSensor)
|
||||
continue
|
||||
} else {
|
||||
sensors = append(sensors, &types.Sensor{SensorID: measuredValue.SensorID})
|
||||
}
|
||||
}
|
||||
return sensors
|
||||
}
|
||||
|
||||
// PrintRGBLEDs displays a list with all configured RGBLEDs
|
||||
func PrintRGBLEDs(cnf *config.Configuration, w io.Writer) {
|
||||
|
||||
// declare tabwriter
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
||||
|
||||
// headline
|
||||
fmt.Fprintln(tw, "name\tlocation\tblue\tgreen\tred\tenabled\taction")
|
||||
|
||||
for _, rgbled := range cnf.RGBLEDs {
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\t", rgbled.RGBLEDName, rgbled.RGBLEDLocation, *rgbled.BaseColorsToGPIO[types.BaseColorBlue], *rgbled.BaseColorsToGPIO[types.BaseColorGreen], *rgbled.BaseColorsToGPIO[types.BaseColorRed], rgbled.RGBLEDEnabled)
|
||||
|
||||
for action, color := range rgbled.ActionMapping {
|
||||
fmt.Fprintf(tw, "%v=%v,", action, color)
|
||||
}
|
||||
|
||||
fmt.Fprintf(tw, "\n")
|
||||
}
|
||||
|
||||
tw.Flush()
|
||||
}
|
||||
|
||||
// PrintSensors displays a list with all configured sensors
|
||||
func PrintSensors(cnf *config.Configuration, w io.Writer) error {
|
||||
func PrintSensors(sensors []*types.Sensor, w io.Writer) error {
|
||||
|
||||
// declar tabwriter
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
||||
|
||||
fmt.Fprint(tw, "name\tlocation\ttype\twire-id\ti2c-bus\ti2c-address\tgpio\tenabled\n")
|
||||
fmt.Fprint(tw, "name\tlocation\ttype\twire-id\ti2c-bus\ti2c-address\tgpio\ttick-duration\tenabled\n")
|
||||
|
||||
for _, sensor := range cnf.Sensors {
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\t", sensor.SensorName, sensor.SensorLocation, sensor.SensorModel)
|
||||
for _, sensor := range sensors {
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\t", sensor.Name, sensor.Location, sensor.Model)
|
||||
|
||||
if sensor.WireID != nil {
|
||||
fmt.Fprintf(tw, "%v\t", *sensor.WireID)
|
||||
@ -87,66 +32,17 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error {
|
||||
}
|
||||
|
||||
if sensor.I2CAddress != nil {
|
||||
fmt.Fprintf(tw, "%v\t", *sensor.I2CAddress)
|
||||
fmt.Fprintf(tw, "%#v\t", *sensor.I2CAddress)
|
||||
} else {
|
||||
fmt.Fprintf(tw, "\t")
|
||||
}
|
||||
|
||||
if sensor.GPIONumber != nil {
|
||||
fmt.Fprintf(tw, "%v\t", *sensor.GPIONumber)
|
||||
} else {
|
||||
fmt.Fprintf(tw, "\t")
|
||||
}
|
||||
fmt.Fprintf(tw, "%v\t", sensor.GPIONumber)
|
||||
|
||||
fmt.Fprintf(tw, "%v\n", sensor.SensorEnabled)
|
||||
fmt.Fprintf(tw, "%v\t%v\n", sensor.TickDuration, sensor.Enabled)
|
||||
}
|
||||
|
||||
tw.Flush()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintMeasuredValues displays a list of measured values
|
||||
func PrintMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration, w io.Writer) {
|
||||
|
||||
sensors := GetSensorsByMeasuredValues(measuredValues, cnf)
|
||||
|
||||
// sort measured values for every sensor
|
||||
orderedMeasuredValues := make(map[string][]*types.MeasuredValue)
|
||||
for _, measuredValue := range measuredValues {
|
||||
orderedMeasuredValues[measuredValue.SensorID] = append(orderedMeasuredValues[measuredValue.SensorID], measuredValue)
|
||||
}
|
||||
|
||||
// declare tabwriter
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
||||
|
||||
// headlines
|
||||
for i, sensor := range sensors {
|
||||
fmt.Fprintf(tw, "%v\t", sensor.Name())
|
||||
if i == len(sensors)-1 {
|
||||
fmt.Fprintf(tw, "\n")
|
||||
}
|
||||
}
|
||||
|
||||
// find sensor with maximum temperature values
|
||||
maxLength := 0
|
||||
for _, orderedMeasuredValue := range orderedMeasuredValues {
|
||||
if len(orderedMeasuredValue) > maxLength {
|
||||
maxLength = len(orderedMeasuredValue)
|
||||
}
|
||||
}
|
||||
|
||||
// body
|
||||
for i := 0; i < maxLength; i++ {
|
||||
for _, sensor := range sensors {
|
||||
if len(orderedMeasuredValues[sensor.SensorID]) > i {
|
||||
fmt.Fprintf(tw, "%3.3f\t", orderedMeasuredValues[sensor.SensorID][i].Value)
|
||||
} else {
|
||||
fmt.Fprint(tw, "\t")
|
||||
}
|
||||
|
||||
}
|
||||
fmt.Fprint(tw, "\n")
|
||||
}
|
||||
tw.Flush()
|
||||
}
|
||||
|
@ -1,59 +1,191 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"time"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/internal/format"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
var validUUID = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
|
||||
var (
|
||||
validUUID = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
|
||||
)
|
||||
|
||||
// Read the configuration file
|
||||
func Read(configFile string) (*Configuration, error) {
|
||||
|
||||
fc := &Configuration{}
|
||||
|
||||
f, err := os.Open(configFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can not open file %v: %v", configFile, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
jsonDecoder := json.NewDecoder(f)
|
||||
if err := jsonDecoder.Decode(&fc); err != nil {
|
||||
return nil, fmt.Errorf("Can not unmarshal JSON: %v", err)
|
||||
// Config represent the configuration
|
||||
type Config struct {
|
||||
Device *types.Device `json:"device"`
|
||||
Sensors []*types.Sensor `json:"sensors"`
|
||||
DSN string `json:"dsn"`
|
||||
DSNFallback string `json:"dsn_fallback"`
|
||||
}
|
||||
|
||||
return fc, nil
|
||||
// AddSensor add a new sensor
|
||||
func (cnf *Config) AddSensor(sensor *types.Sensor) error {
|
||||
|
||||
// Verify that a device is configured
|
||||
if cnf.Device == nil {
|
||||
return fmt.Errorf("No device configured")
|
||||
}
|
||||
|
||||
// Write the configuration into a file, specified by the configuration filepath
|
||||
func Write(cfg *Configuration, configFile string) error {
|
||||
// Define a new UUID if the current UUID is invalid
|
||||
if !validUUID.MatchString(sensor.ID) {
|
||||
sensor.ID = uuid.NewV4().String()
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
||||
configDir := filepath.Dir(configFile)
|
||||
err := os.MkdirAll(configDir, os.ModeDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create config directory %v: %v", configDir, err)
|
||||
// Verify that the sensor has a valid name
|
||||
if len(sensor.Name) <= 0 {
|
||||
return fmt.Errorf("No sensor name defined")
|
||||
}
|
||||
|
||||
// check if sensor name and sensor uuid already exists
|
||||
for _, cnfSensor := range cnf.Sensors {
|
||||
if cnfSensor.Name == sensor.Name ||
|
||||
cnfSensor.ID == sensor.ID {
|
||||
return fmt.Errorf("Sensor with same name or id already exist")
|
||||
}
|
||||
|
||||
if cnfSensor.WireID != nil &&
|
||||
sensor.WireID != nil &&
|
||||
*cnfSensor.WireID == *sensor.WireID {
|
||||
return fmt.Errorf("Sensor with same wire id already exist")
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.Create(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not write config file: %v", err)
|
||||
// check if sensor has a valid tick time
|
||||
if _, err := time.ParseDuration(sensor.TickDuration); err != nil {
|
||||
return fmt.Errorf("Failed to parse tick duration: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
encoder := json.NewEncoder(f)
|
||||
encoder.SetIndent("", " ")
|
||||
err = encoder.Encode(cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error in encoding struct to json: %v", err)
|
||||
// check if sensor has a valid device id
|
||||
if sensor.DeviceID != cnf.Device.ID {
|
||||
sensor.DeviceID = cnf.Device.ID
|
||||
}
|
||||
|
||||
// overwrite creation date
|
||||
sensor.CreationDate = format.FormatedTime()
|
||||
|
||||
// check if wire socket is available
|
||||
if sensor.WireID != nil {
|
||||
socketPath := filepath.Join("/sys/bus/w1/devices", *sensor.WireID, "/w1_slave")
|
||||
if _, err := os.Stat(socketPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("Wire socket not found: %v", socketPath)
|
||||
}
|
||||
}
|
||||
|
||||
cnf.Sensors = append(cnf.Sensors, sensor)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DisableSensor disables a sensor by its name or its unique UUID
|
||||
func (cnf *Config) DisableSensor(name string) error {
|
||||
found := false
|
||||
|
||||
for _, sensor := range cnf.Sensors {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
sensor.Name == name {
|
||||
sensor.Enabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.ID == name {
|
||||
sensor.Enabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found sensor %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// EnableSensor enables a sensor by its name or its unique UUID
|
||||
func (cnf *Config) EnableSensor(name string) error {
|
||||
found := false
|
||||
|
||||
for _, sensor := range cnf.Sensors {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
sensor.Name == name {
|
||||
sensor.Enabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.ID == name {
|
||||
sensor.Enabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found sensor %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSensorByID returns a sensor matched by his id. If no sensor has this id,
|
||||
// the function returns nil
|
||||
func (cnf *Config) GetSensorByID(id string) *types.Sensor {
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if sensor.ID == id {
|
||||
return sensor
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveSensor deletes a sensor by its name or its unique UUID, If definitive
|
||||
// is set to true, the sensor will not only be removed in the configuration file
|
||||
// but also in the backend.
|
||||
func (cnf *Config) RemoveSensor(name string) error {
|
||||
|
||||
for i, sensor := range cnf.Sensors {
|
||||
// remove machted name
|
||||
if !validUUID.MatchString(name) &&
|
||||
sensor.Name == name {
|
||||
cnf.Sensors = append(cnf.Sensors[:i], cnf.Sensors[i+1:]...)
|
||||
|
||||
return nil
|
||||
}
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.ID == name {
|
||||
cnf.Sensors = append(cnf.Sensors[:i], cnf.Sensors[i+1:]...)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Can not find sensor %v", name)
|
||||
}
|
||||
|
||||
// RenameSensor renamed a sensor
|
||||
func (cnf *Config) RenameSensor(oldName string, newName string) error {
|
||||
for _, cnfSensor := range cnf.Sensors {
|
||||
if cnfSensor.Name == oldName {
|
||||
cnfSensor.Name = newName
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("No sensor %v found", oldName)
|
||||
}
|
||||
|
31
pkg/config/config_test.go
Normal file
31
pkg/config/config_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package config_test
|
||||
|
||||
// func TestAddRemoveSensor(t *testing.T) {
|
||||
// require := require.New(t)
|
||||
|
||||
// // Test: No device configured
|
||||
// cnf := new(config.Config)
|
||||
// err := cnf.AddSensor(&types.Sensor{ID: "1aa32c9a-b505-456d-868b-0403344f4cdf"})
|
||||
// require.Error(err)
|
||||
|
||||
// // wireID := "sdfsdff"
|
||||
// // i2cBus := 1
|
||||
// // i2cAddress := 78
|
||||
|
||||
// cnf.Device = &types.Device{ID: "d6176a06-2b0b-41af-a85c-913e8f61c35d"}
|
||||
// testCases := map[*types.Sensor]error{
|
||||
// {ID: "1aa32c9a-b505-456d-868b-0403344f4cdf", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d"}: fmt.Errorf("No sensor name defined"),
|
||||
// {ID: "1aa32c9a-b505-456d-868b-0403344f4cdf", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test01"}: fmt.Errorf("Failed to parse tick duration: time: invalid duration "),
|
||||
// {ID: "1aa32c9a-b505-456d-868b-0403344f4cdf", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test01", TickDuration: "5s"}: nil,
|
||||
// {ID: "1aa32c9a-b505-456d-868b-0403344f4cdf", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test01", TickDuration: "5s"}: fmt.Errorf("Sensor with same name or id already exist"),
|
||||
// // {ID: "f90cfc18-f141-4cfd-a8d2-fb40082de5cc", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test01", TickDuration: "5s"}: fmt.Errorf("Sensor with same name or id already exist"),
|
||||
// // {ID: "860a9922-62cb-4c9b-b5af-5fa783cebe9d", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test02", TickDuration: "5s", WireID: &wireID}: fmt.Errorf("Wire socket not found: /sys/bus/w1/devices/sdfsdff/w1_slave"),
|
||||
// // {ID: "9be8989c-b2a1-4401-a82f-d6989ec226fe", DeviceID: "d6176a06-2b0b-41af-a85c-913e8f61c35d", Name: "Test02", TickDuration: "5s"}: nil,
|
||||
// }
|
||||
|
||||
// for sensor, expectedErr := range testCases {
|
||||
// err := cnf.AddSensor(sensor)
|
||||
// require.Equal(expectedErr, err)
|
||||
// }
|
||||
|
||||
// }
|
@ -1,582 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/internal/format"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/go-flucky/flucky/pkg/sensor"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
var humiditySensorModels = map[types.SensorModel]types.SensorModel{
|
||||
types.BME280: types.BME280,
|
||||
types.DHT11: types.DHT11,
|
||||
types.DHT22: types.DHT22,
|
||||
}
|
||||
|
||||
var pressureSensorModels = map[types.SensorModel]types.SensorModel{
|
||||
types.BME280: types.BME280,
|
||||
}
|
||||
|
||||
var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
|
||||
types.BME280: types.BME280,
|
||||
types.DHT11: types.DHT11,
|
||||
types.DHT22: types.DHT22,
|
||||
types.DS18B20: types.DS18B20,
|
||||
}
|
||||
|
||||
// Configuration of flucky
|
||||
type Configuration struct {
|
||||
Device *types.Device `json:"device"`
|
||||
RGBLEDs []*types.RGBLED `json:"rgb_leds"`
|
||||
Sensors []*types.Sensor `json:"sensors"`
|
||||
}
|
||||
|
||||
// AddRGBLED add a new RGBLED
|
||||
func (c *Configuration) AddRGBLED(rgbLED *types.RGBLED) error {
|
||||
|
||||
// check if RGBLEDID is a valid UUID string
|
||||
if !validUUID.MatchString(rgbLED.RGBLEDID) {
|
||||
rgbLED.RGBLEDID = uuid.NewV4().String()
|
||||
}
|
||||
|
||||
// check if sensor name and sensor uuid already exists
|
||||
for _, l := range c.RGBLEDs {
|
||||
if l.RGBLEDName == rgbLED.RGBLEDName {
|
||||
return fmt.Errorf("RGBLED %v already exists", rgbLED.RGBLEDName)
|
||||
}
|
||||
|
||||
if l.RGBLEDID == rgbLED.RGBLEDID {
|
||||
return fmt.Errorf("RGBLED %v with UUID %v already exists", rgbLED.RGBLEDName, rgbLED.RGBLEDID)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// check if sensor has a valid device id
|
||||
if rgbLED.DeviceID != c.Device.DeviceID {
|
||||
rgbLED.DeviceID = c.Device.DeviceID
|
||||
}
|
||||
|
||||
// overwrite creation date
|
||||
rgbLED.CreationDate = time.Now()
|
||||
|
||||
// check
|
||||
c.RGBLEDs = append(c.RGBLEDs, rgbLED)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddSensor add a new sensor
|
||||
func (c *Configuration) AddSensor(sensor *types.Sensor) error {
|
||||
|
||||
// check if sensorID is a valid UUID string
|
||||
if !validUUID.MatchString(sensor.SensorID) {
|
||||
sensor.SensorID = uuid.NewV4().String()
|
||||
}
|
||||
|
||||
// check if sensor name and sensor uuid already exists
|
||||
for _, s := range c.Sensors {
|
||||
if s.SensorName == sensor.SensorName {
|
||||
return fmt.Errorf("Sensor %v already exists", s.SensorName)
|
||||
}
|
||||
|
||||
if s.SensorID == sensor.SensorID {
|
||||
return fmt.Errorf("Sensor %v with UUID %v already exists", s.SensorName, s.SensorID)
|
||||
}
|
||||
|
||||
if sensor.WireID != nil {
|
||||
if *s.WireID == *sensor.WireID {
|
||||
return fmt.Errorf("Sensor with 1wire-id %v already exists as %v", *s.WireID, s.SensorName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check if sensor has a valid device id
|
||||
if sensor.DeviceID != c.Device.DeviceID {
|
||||
sensor.DeviceID = c.Device.DeviceID
|
||||
}
|
||||
|
||||
// overwrite creation date
|
||||
sensor.CreationDate = format.FormatedTime()
|
||||
|
||||
//TODO: check if wire sensor exists in /dev/bus/w1/devices
|
||||
|
||||
// check
|
||||
c.Sensors = append(c.Sensors, sensor)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DisableRGBLED enables a rgb led by its name or its unique UUID
|
||||
func (c *Configuration) DisableRGBLED(name string) error {
|
||||
found := false
|
||||
|
||||
for _, rgbled := range c.RGBLEDs {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
rgbled.RGBLEDName == name {
|
||||
rgbled.RGBLEDEnabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// disable sensor matched by uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
rgbled.RGBLEDID == name {
|
||||
rgbled.RGBLEDEnabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found RGB-LED %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DisableSensor disables a sensor by its name or its unique UUID
|
||||
func (c *Configuration) DisableSensor(name string) error {
|
||||
found := false
|
||||
|
||||
for _, sensor := range c.Sensors {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
sensor.SensorName == name {
|
||||
sensor.SensorEnabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.SensorID == name {
|
||||
sensor.SensorEnabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found sensor %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableRGBLED enables a rgb led by its name or its unique UUID
|
||||
func (c *Configuration) EnableRGBLED(name string) error {
|
||||
found := false
|
||||
|
||||
for _, rgbled := range c.RGBLEDs {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
rgbled.RGBLEDName == name {
|
||||
rgbled.RGBLEDEnabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// disable sensor matched by uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
rgbled.RGBLEDID == name {
|
||||
rgbled.RGBLEDEnabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found RGB-LED %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableSensor enables a sensor by its name or its unique UUID
|
||||
func (c *Configuration) EnableSensor(name string) error {
|
||||
found := false
|
||||
|
||||
for _, sensor := range c.Sensors {
|
||||
|
||||
// disable sensor matched after name
|
||||
if !validUUID.MatchString(name) &&
|
||||
sensor.SensorName == name {
|
||||
sensor.SensorEnabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.SensorID == name {
|
||||
sensor.SensorEnabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not found sensor %v", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetHumiditySensors returns a list of humidity sensors
|
||||
func (c *Configuration) GetHumiditySensors(option Option) []sensor.Sensor {
|
||||
sensors := c.getHumiditySensors()
|
||||
|
||||
cachedSensors := make([]*types.Sensor, 0)
|
||||
|
||||
switch option {
|
||||
case ENABLED:
|
||||
for _, sensor := range sensors {
|
||||
if sensor.SensorEnabled {
|
||||
cachedSensors = append(cachedSensors, sensor)
|
||||
}
|
||||
}
|
||||
return c.convertSensors(cachedSensors)
|
||||
case DISABLED:
|
||||
for _, sensor := range sensors {
|
||||
if !sensor.SensorEnabled {
|
||||
cachedSensors = append(cachedSensors, sensor)
|
||||
}
|
||||
}
|
||||
return c.convertSensors(cachedSensors)
|
||||
default:
|
||||
return c.convertSensors(cachedSensors)
|
||||
}
|
||||
}
|
||||
|
||||
// GetHumiditySensorsByName returns a list of humidity sensors by name,
|
||||
// uuid or wire-id
|
||||
func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Sensor {
|
||||
configHumiditySensors := make(map[string]*types.Sensor, 0)
|
||||
|
||||
for _, name := range names {
|
||||
for _, s := range c.getHumiditySensors() {
|
||||
switch name {
|
||||
case s.SensorID:
|
||||
configHumiditySensors[s.SensorID] = s
|
||||
case s.SensorName:
|
||||
configHumiditySensors[s.SensorID] = s
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
humiditySensors := make([]*types.Sensor, 0)
|
||||
for _, cs := range configHumiditySensors {
|
||||
humiditySensors = append(humiditySensors, cs)
|
||||
}
|
||||
|
||||
return c.convertSensors(humiditySensors)
|
||||
}
|
||||
|
||||
// GetPressureSensors returns a list of pressure sensors
|
||||
func (c *Configuration) GetPressureSensors(option Option) []sensor.Sensor {
|
||||
sensors := c.getPressureSensors()
|
||||
|
||||
cachedSensors := make([]*types.Sensor, 0)
|
||||
|
||||
switch option {
|
||||
case ENABLED:
|
||||
for _, sensor := range sensors {
|
||||
if sensor.SensorEnabled {
|
||||
cachedSensors = append(cachedSensors, sensor)
|
||||
}
|
||||
}
|
||||
return c.convertSensors(cachedSensors)
|
||||
case DISABLED:
|
||||
for _, sensor := range sensors {
|
||||
if !sensor.SensorEnabled {
|
||||
cachedSensors = append(cachedSensors, sensor)
|
||||
}
|
||||
}
|
||||
return c.convertSensors(cachedSensors)
|
||||
default:
|
||||
return c.convertSensors(cachedSensors)
|
||||
}
|
||||
}
|
||||
|
||||
// GetPressureSensorsByName returns a list of pressure sensors by name,
|
||||
// uuid or wire-id
|
||||
func (c *Configuration) GetPressureSensorsByName(names []string) []sensor.Sensor {
|
||||
configPressureSensors := make(map[string]*types.Sensor, 0)
|
||||
|
||||
for _, name := range names {
|
||||
for _, s := range c.getPressureSensors() {
|
||||
switch name {
|
||||
case s.SensorID:
|
||||
configPressureSensors[s.SensorID] = s
|
||||
case s.SensorName:
|
||||
configPressureSensors[s.SensorID] = s
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pressureSensors := make([]*types.Sensor, 0)
|
||||
for _, cs := range configPressureSensors {
|
||||
pressureSensors = append(pressureSensors, cs)
|
||||
}
|
||||
|
||||
return c.convertSensors(pressureSensors)
|
||||
}
|
||||
|
||||
func (c *Configuration) GetRGBLEDs(option Option) []rgbled.RGBLED {
|
||||
rgbLEDs := c.RGBLEDs
|
||||
|
||||
switch option {
|
||||
case ENABLED:
|
||||
for i, rgbLED := range c.RGBLEDs {
|
||||
if !rgbLED.RGBLEDEnabled {
|
||||
rgbLEDs = append(rgbLEDs[:i], rgbLEDs[i+1:]...)
|
||||
}
|
||||
}
|
||||
return c.convertRGBLEDs(rgbLEDs)
|
||||
case DISABLED:
|
||||
for i, rgbLED := range c.RGBLEDs {
|
||||
if rgbLED.RGBLEDEnabled {
|
||||
rgbLEDs = append(rgbLEDs[:i], rgbLEDs[i+1:]...)
|
||||
}
|
||||
}
|
||||
return c.convertRGBLEDs(rgbLEDs)
|
||||
default:
|
||||
return c.convertRGBLEDs(rgbLEDs)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Configuration) GetRGBLEDsByName(names []string) []rgbled.RGBLED {
|
||||
configRGBLEDs := make(map[string]*types.RGBLED, 0)
|
||||
|
||||
for _, name := range names {
|
||||
for _, led := range c.RGBLEDs {
|
||||
switch name {
|
||||
case led.RGBLEDID:
|
||||
configRGBLEDs[led.RGBLEDID] = led
|
||||
case led.RGBLEDName:
|
||||
configRGBLEDs[led.RGBLEDID] = led
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rgbLEDs := make([]*types.RGBLED, 0)
|
||||
for _, rgbLED := range configRGBLEDs {
|
||||
rgbLEDs = append(rgbLEDs, rgbLED)
|
||||
}
|
||||
|
||||
return c.convertRGBLEDs(rgbLEDs)
|
||||
}
|
||||
|
||||
// GetSensors returns a list of humidity sensors
|
||||
func (c *Configuration) GetSensors(option Option) []sensor.Sensor {
|
||||
cachedSensors := make([]*types.Sensor, 0)
|
||||
|
||||
switch option {
|
||||
case ENABLED:
|
||||
for _, sensor := range c.Sensors {
|
||||
if sensor.SensorEnabled {
|
||||
cachedSensors = append(cachedSensors, sensor)
|
||||
}
|
||||
}
|
||||
return c.convertSensors(cachedSensors)
|
||||
case DISABLED:
|
||||
for _, sensor := range c.Sensors {
|
||||
if !sensor.SensorEnabled {
|
||||
cachedSensors = append(cachedSensors, sensor)
|
||||
}
|
||||
}
|
||||
return c.convertSensors(cachedSensors)
|
||||
default:
|
||||
return c.convertSensors(cachedSensors)
|
||||
}
|
||||
}
|
||||
|
||||
// GetTemperatureSensors returns a list of temperature sensors
|
||||
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.Sensor {
|
||||
sensors := c.getTemperatureSensors()
|
||||
|
||||
cachedSensors := make([]*types.Sensor, 0)
|
||||
|
||||
switch option {
|
||||
case ENABLED:
|
||||
for _, sensor := range sensors {
|
||||
if sensor.SensorEnabled {
|
||||
cachedSensors = append(cachedSensors, sensor)
|
||||
}
|
||||
}
|
||||
return c.convertSensors(cachedSensors)
|
||||
case DISABLED:
|
||||
for _, sensor := range sensors {
|
||||
if !sensor.SensorEnabled {
|
||||
cachedSensors = append(cachedSensors, sensor)
|
||||
}
|
||||
}
|
||||
return c.convertSensors(cachedSensors)
|
||||
default:
|
||||
return c.convertSensors(cachedSensors)
|
||||
}
|
||||
}
|
||||
|
||||
// GetTemperatureSensorsByName returns a list of temperature sensors by name,
|
||||
// uuid or wire-id
|
||||
func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Sensor {
|
||||
configTemperatureSensors := make(map[string]*types.Sensor, 0)
|
||||
|
||||
for _, name := range names {
|
||||
for _, s := range c.getTemperatureSensors() {
|
||||
switch name {
|
||||
case s.SensorID:
|
||||
configTemperatureSensors[s.SensorID] = s
|
||||
case s.SensorName:
|
||||
configTemperatureSensors[s.SensorID] = s
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
temperatureSensors := make([]*types.Sensor, 0)
|
||||
for _, cs := range configTemperatureSensors {
|
||||
temperatureSensors = append(temperatureSensors, cs)
|
||||
}
|
||||
|
||||
return c.convertSensors(temperatureSensors)
|
||||
}
|
||||
|
||||
// RemoveRGBLED deletes a LED by its name or its unique UUID
|
||||
func (c *Configuration) RemoveRGBLED(name string) error {
|
||||
for i, rgbLED := range c.RGBLEDs {
|
||||
// remove machted name
|
||||
if !validUUID.MatchString(name) &&
|
||||
rgbLED.RGBLEDName == name {
|
||||
c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
rgbLED.RGBLEDID == name {
|
||||
c.RGBLEDs = append(c.RGBLEDs[:i], c.RGBLEDs[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Can not find RGBLED %v", name)
|
||||
}
|
||||
|
||||
// RemoveSensor deletes a sensor by its name or its unique UUID
|
||||
func (c *Configuration) RemoveSensor(name string) error {
|
||||
for i, sensor := range c.Sensors {
|
||||
// remove machted name
|
||||
if !validUUID.MatchString(name) &&
|
||||
sensor.SensorName == name {
|
||||
c.Sensors = append(c.Sensors[:i], c.Sensors[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
// remove machted uuid
|
||||
if validUUID.MatchString(name) &&
|
||||
sensor.SensorID == name {
|
||||
c.Sensors = append(c.Sensors[:i], c.Sensors[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Can not find sensor %v", name)
|
||||
}
|
||||
|
||||
// RenameRGBLED renames a sensor identified by the name or the UUID
|
||||
func (c *Configuration) RenameRGBLED(oldName, newName string) error {
|
||||
for _, rgbled := range c.RGBLEDs {
|
||||
if rgbled.RGBLEDName == oldName ||
|
||||
rgbled.RGBLEDID == oldName {
|
||||
rgbled.RGBLEDName = newName
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Could not find rgb-led %v to replace into with %v", oldName, newName)
|
||||
}
|
||||
|
||||
// RenameSensor renames a sensor identified by the name or the UUID
|
||||
func (c *Configuration) RenameSensor(oldName, newName string) error {
|
||||
for _, sensor := range c.Sensors {
|
||||
if sensor.SensorName == oldName ||
|
||||
sensor.SensorID == oldName {
|
||||
sensor.SensorName = newName
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Could not find remote %v to replace into with %v", oldName, newName)
|
||||
}
|
||||
|
||||
func (c *Configuration) convertSensors(sensors []*types.Sensor) []sensor.Sensor {
|
||||
cachedSensors := make([]sensor.Sensor, 0)
|
||||
|
||||
for _, s := range sensors {
|
||||
switch s.SensorModel {
|
||||
case types.BME280:
|
||||
cachedSensors = append(cachedSensors, &sensor.BME280{
|
||||
Sensor: s,
|
||||
})
|
||||
case types.DHT11:
|
||||
cachedSensors = append(cachedSensors, &sensor.DHT11{
|
||||
Sensor: s,
|
||||
})
|
||||
case types.DHT22:
|
||||
cachedSensors = append(cachedSensors, &sensor.DHT22{
|
||||
Sensor: s,
|
||||
})
|
||||
case types.DS18B20:
|
||||
cachedSensors = append(cachedSensors, &sensor.DS18B20{
|
||||
Sensor: s,
|
||||
})
|
||||
}
|
||||
}
|
||||
return cachedSensors
|
||||
}
|
||||
|
||||
func (c *Configuration) convertRGBLEDs(rgbLEDs []*types.RGBLED) []rgbled.RGBLED {
|
||||
leds := make([]rgbled.RGBLED, 0)
|
||||
|
||||
for _, rgbLED := range rgbLEDs {
|
||||
leds = append(leds, &rgbled.DefaultRGBLED{
|
||||
RGBLED: rgbLED,
|
||||
})
|
||||
}
|
||||
|
||||
return leds
|
||||
}
|
||||
|
||||
func (c *Configuration) getHumiditySensors() []*types.Sensor {
|
||||
humiditySensors := make([]*types.Sensor, 0)
|
||||
for _, s := range c.Sensors {
|
||||
if _, ok := humiditySensorModels[s.SensorModel]; ok {
|
||||
humiditySensors = append(humiditySensors, s)
|
||||
}
|
||||
}
|
||||
return humiditySensors
|
||||
}
|
||||
|
||||
func (c *Configuration) getPressureSensors() []*types.Sensor {
|
||||
pressureSensors := make([]*types.Sensor, 0)
|
||||
for _, s := range c.Sensors {
|
||||
if _, ok := pressureSensorModels[s.SensorModel]; ok {
|
||||
pressureSensors = append(pressureSensors, s)
|
||||
}
|
||||
}
|
||||
return pressureSensors
|
||||
}
|
||||
|
||||
func (c *Configuration) getTemperatureSensors() []*types.Sensor {
|
||||
temperatureSensors := make([]*types.Sensor, 0)
|
||||
for _, s := range c.Sensors {
|
||||
if _, ok := temperatureSensorModels[s.SensorModel]; ok {
|
||||
temperatureSensors = append(temperatureSensors, s)
|
||||
}
|
||||
}
|
||||
return temperatureSensors
|
||||
}
|
61
pkg/config/io.go
Normal file
61
pkg/config/io.go
Normal file
@ -0,0 +1,61 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Decode a configuration from a reader
|
||||
func Decode(r io.Reader) (*Config, error) {
|
||||
cnf := new(Config)
|
||||
jsonDecoder := json.NewDecoder(r)
|
||||
if err := jsonDecoder.Decode(&cnf); err != nil {
|
||||
return nil, fmt.Errorf("Can not unmarshal JSON: %v", err)
|
||||
}
|
||||
return cnf, nil
|
||||
}
|
||||
|
||||
// Encode a configuration to a writer
|
||||
func Encode(cnf *Config, w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
err := encoder.Encode(cnf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error encoding config to json: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read the configuration file
|
||||
func Read(configFile string) (*Config, error) {
|
||||
f, err := os.Open(configFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can not open file %v: %v", configFile, err)
|
||||
}
|
||||
defer 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)
|
||||
err := os.MkdirAll(configDir, 0775)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create config directory %v: %v", configDir, err)
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.Create(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed not create config file %v: %v", configFile, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return Encode(cnf, f)
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package config
|
||||
|
||||
type Option int
|
||||
|
||||
const (
|
||||
// ALL specified enabled and disabled items
|
||||
ALL Option = iota + 1
|
||||
|
||||
// ENABLED items
|
||||
ENABLED
|
||||
|
||||
// DISABLED items
|
||||
DISABLED
|
||||
)
|
@ -2,129 +2,136 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/db"
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/logger"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/go-flucky/flucky/pkg/sensor"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/config"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/repository"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/sensor"
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
"git.cryptic.systems/volker.raschek/go-logger"
|
||||
)
|
||||
|
||||
var (
|
||||
postgresHost = "markus-pc.trier.cryptic.systems"
|
||||
postgresPort = "5432"
|
||||
postgresDatabase = "postgres"
|
||||
postgresUser = "postgres"
|
||||
postgresPassword = "postgres"
|
||||
)
|
||||
func Start(cnf *config.Config, flogger logger.Logger) error {
|
||||
|
||||
// Start the daemon
|
||||
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, logger logger.Logger) {
|
||||
measuredValueChannel := make(chan *types.MeasuredValue, 0)
|
||||
|
||||
// Info
|
||||
logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
|
||||
logger.Info("Use compression: %v", compression)
|
||||
logger.Info("Round: %v", round)
|
||||
// load data source name (dsn)
|
||||
dsnURL, err := url.Parse(cnf.DSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ticker := time.Tick(cleanCacheInterval)
|
||||
repo, err := repository.New(dsnURL, flogger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||
// Add
|
||||
repoDevice, err := repo.GetDevice(cnf.Device.ID)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case repoDevice == nil:
|
||||
err = repo.AddDevices(cnf.Device)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
errorChannel := make(chan error, 0)
|
||||
measuredValuesChannel := make(chan []*types.MeasuredValue, 0)
|
||||
repoDevice, err = repo.GetDevice(cnf.Device.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
childContext, cancel := context.WithCancel(ctx)
|
||||
repoSensors, err := repo.GetSensorsByDeviceID(repoDevice.ID)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case repoSensors == nil, len(repoSensors) <= 0:
|
||||
return fmt.Errorf("No sensors found")
|
||||
}
|
||||
|
||||
measuredValuesLogfile := logfile.New(cnf.Device.Logfile)
|
||||
sensors := make([]sensor.Sensor, 0)
|
||||
for _, repoSensor := range repoSensors {
|
||||
if !repoSensor.Enabled || repoSensor.DeviceID != repoDevice.ID {
|
||||
continue
|
||||
}
|
||||
|
||||
measuredValuesCache := make([]*types.MeasuredValue, 0)
|
||||
flogger.Debug("Found sensor %v", repoSensor.GetName())
|
||||
|
||||
go sensor.ReadContinuously(childContext, cnf.GetSensors(config.ENABLED), measuredValuesChannel, errorChannel)
|
||||
sensor, err := sensor.New(repoSensor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sensors = append(sensors, sensor)
|
||||
}
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
interruptChannel := make(chan os.Signal, 1)
|
||||
signal.Notify(interruptChannel, os.Interrupt, os.Kill)
|
||||
|
||||
// Collection
|
||||
parentCtx := context.Background()
|
||||
ctx, cancel := context.WithCancel(parentCtx)
|
||||
|
||||
for _, s := range sensors {
|
||||
go func(sensor sensor.Sensor) {
|
||||
for {
|
||||
|
||||
err := rgbled.Run(rgbLEDs)
|
||||
if err != nil {
|
||||
logger.Error("Can not turn on green info light: %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case err, _ := <-errorChannel:
|
||||
|
||||
logger.Error("%v", err)
|
||||
|
||||
err = rgbled.Error(rgbLEDs)
|
||||
if err != nil {
|
||||
logger.Error("Can not turn on red info light: %v", err)
|
||||
}
|
||||
|
||||
time.Sleep(time.Second * 2)
|
||||
|
||||
case <-ticker:
|
||||
err := rgbled.Logfile(rgbLEDs)
|
||||
if err != nil {
|
||||
logger.Error("Can not turn on blue info light: %v", err)
|
||||
}
|
||||
|
||||
// err = logfile.Append(measuredValuesLogfile, compression, round, measuredValuesCache)
|
||||
|
||||
postgres, err := db.New(db.DBOTypePostgres, postgresHost, postgresPort, postgresDatabase, postgresUser, postgresPassword)
|
||||
if err != nil {
|
||||
|
||||
err = rgbled.Error(rgbLEDs)
|
||||
if err != nil {
|
||||
logger.Error("Can not turn on red info light: %v", err)
|
||||
}
|
||||
|
||||
cancel()
|
||||
logger.Error("Can not open database connection: %v", err)
|
||||
}
|
||||
|
||||
postgresCtx := context.Background()
|
||||
err = postgres.InsertMeasuredValues(postgresCtx, measuredValuesCache)
|
||||
if err != nil {
|
||||
|
||||
err = rgbled.Error(rgbLEDs)
|
||||
if err != nil {
|
||||
logger.Error("Can not turn on red info light: %v", err)
|
||||
}
|
||||
|
||||
cancel()
|
||||
logger.Error("Can not save caches measured values in database: %v", err)
|
||||
}
|
||||
|
||||
measuredValuesCache = make([]*types.MeasuredValue, 0)
|
||||
|
||||
case measuredValues, _ := <-measuredValuesChannel:
|
||||
measuredValuesCache = append(measuredValuesCache, measuredValues...)
|
||||
|
||||
case killSignal := <-interrupt:
|
||||
logger.Warn("Daemon was interruped by system signal %v\n", killSignal)
|
||||
|
||||
cancel()
|
||||
|
||||
err := rgbled.Error(rgbLEDs)
|
||||
if err != nil {
|
||||
logger.Error("Can not turn on red info light: %v", err)
|
||||
}
|
||||
|
||||
logger.Warn("Save remaining data from the cache")
|
||||
err = logfile.Append(measuredValuesLogfile, compression, round, measuredValuesCache)
|
||||
if err != nil {
|
||||
logger.Fatal("%v", err)
|
||||
}
|
||||
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-sensor.GetTicker().C:
|
||||
measuredValues, err := sensor.Read()
|
||||
if err != nil {
|
||||
flogger.Error("%v", err)
|
||||
continue
|
||||
}
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValueChannel <- measuredValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}(s)
|
||||
}
|
||||
|
||||
measuredValues := make([]*types.MeasuredValue, 0, 10)
|
||||
for {
|
||||
select {
|
||||
case measuredValue := <-measuredValueChannel:
|
||||
flogger.Debug("%v\t%v\t%v", measuredValue.ID, measuredValue.ValueType, measuredValue.Value)
|
||||
measuredValues = append(measuredValues, measuredValue)
|
||||
|
||||
if cap(measuredValues) == len(measuredValues) {
|
||||
flogger.Debug("Flush cache with %v values", len(measuredValues))
|
||||
err := repo.AddMeasuredValues(measuredValues...)
|
||||
if err != nil {
|
||||
flogger.Error("%v", err)
|
||||
}
|
||||
measuredValues = make([]*types.MeasuredValue, 0, 10)
|
||||
}
|
||||
|
||||
case signal := <-interruptChannel:
|
||||
cancel()
|
||||
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...)
|
||||
if err != nil {
|
||||
flogger.Error("%v", err)
|
||||
}
|
||||
|
||||
flogger.Debug("Close repository")
|
||||
err = repo.Close()
|
||||
if err != nil {
|
||||
flogger.Error("%v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
36
pkg/db/db.go
36
pkg/db/db.go
@ -1,36 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type DBOType string
|
||||
|
||||
func (dboType DBOType) String() string {
|
||||
return string(dboType)
|
||||
}
|
||||
|
||||
const (
|
||||
DBOTypePostgres DBOType = "postgres"
|
||||
DBOTypeOracle = "oracle"
|
||||
)
|
||||
|
||||
func New(dboType DBOType, host string, port string, database string, user string, password string) (Database, error) {
|
||||
connStr := fmt.Sprintf("%v://%v:%v@%v:%v/%v?sslmode=disable", dboType.String(), user, password, host, port, database)
|
||||
newDBO, err := sql.Open(dboType.String(), connStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch dboType {
|
||||
case "postgres":
|
||||
return &Postgres{
|
||||
dbo: newDBO,
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Unknown Database Type")
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
errorBeginTransaction = errors.New("Can not start new transaction")
|
||||
errorGetAsset = errors.New("Can not get asset from go-bindata")
|
||||
errorRowNotFound = errors.New("Can not find row by given ID")
|
||||
errorPrepareStatement = errors.New("Can not prepare sql statement")
|
||||
errorRollbackTransaction = errors.New("Can not rollback transaction")
|
||||
errorScanRow = errors.New("Can not scan row")
|
||||
errorStatementExecute = errors.New("Can not execute statement")
|
||||
errorStatementQuery = errors.New("Can not query statement")
|
||||
errorUnknownMeasuredValueType = errors.New("Unknown measured value type")
|
||||
)
|
@ -1,39 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
type Database interface {
|
||||
|
||||
// Close DB Connction
|
||||
Close() error
|
||||
|
||||
// Delete
|
||||
DeleteDevices(ctx context.Context, devices []*types.Device) error
|
||||
DeleteMeasuredValues(ctx context.Context, measuredValues []*types.MeasuredValue) error
|
||||
DeleteSensors(ctx context.Context, sensors []*types.Sensor) error
|
||||
|
||||
// Insert
|
||||
InsertDevices(ctx context.Context, devices []*types.Device) error
|
||||
InsertMeasuredValues(ctx context.Context, measuredValues []*types.MeasuredValue) error
|
||||
InsertSensors(ctx context.Context, sensors []*types.Sensor) error
|
||||
|
||||
// Select
|
||||
SelectDeviceByID(ctx context.Context, id string) (*types.Device, error)
|
||||
SelectHumidities(ctx context.Context) ([]*types.MeasuredValue, error)
|
||||
SelectHumidityByID(ctx context.Context, id string) (*types.MeasuredValue, error)
|
||||
SelectMeasuredValuesByIDAndType(ctx context.Context, id string, valueType types.MeasuredValueType) (*types.MeasuredValue, error)
|
||||
SelectPressures(ctx context.Context) ([]*types.MeasuredValue, error)
|
||||
SelectPressureByID(ctx context.Context, id string) (*types.MeasuredValue, error)
|
||||
SelectSensorByID(ctx context.Context, id string) (*types.Sensor, error)
|
||||
SelectTemperatures(ctx context.Context) ([]*types.MeasuredValue, error)
|
||||
SelectTemperatureByID(ctx context.Context, id string) (*types.MeasuredValue, error)
|
||||
|
||||
// Update
|
||||
UpdateDevices(ctx context.Context, devices []*types.Device) error
|
||||
UpdateMeasuredValues(ctx context.Context, measuredValues []*types.MeasuredValue) error
|
||||
UpdateSensors(ctx context.Context, sensots []*types.Sensor) error
|
||||
}
|
@ -1,481 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
dbo *sql.DB
|
||||
}
|
||||
|
||||
func (p *Postgres) Close() error {
|
||||
return p.Close()
|
||||
}
|
||||
|
||||
func (p *Postgres) DeleteDevices(ctx context.Context, devices []*types.Device) error {
|
||||
asset := "pkg/db/sql/psql/deleteDevice.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, device := range devices {
|
||||
_, err := stmt.ExecContext(ctx, &device.DeviceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorStatementExecute, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) DeleteSensors(ctx context.Context, sensors []*types.Sensor) error {
|
||||
asset := "pkg/db/sql/psql/deleteSensor.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, sensor := range sensors {
|
||||
_, err := stmt.ExecContext(ctx, &sensor.SensorID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorStatementExecute, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) DeleteMeasuredValues(ctx context.Context, measuredValues []*types.MeasuredValue) error {
|
||||
|
||||
deleteMeasuredValue := func(ctx context.Context, query string, measuredValues []*types.MeasuredValue) error {
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
_, err := stmt.ExecContext(ctx, &measuredValue.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorStatementExecute, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
sortedMeasuredValueTypes := make(map[types.MeasuredValueType][]*types.MeasuredValue)
|
||||
for _, measuredValue := range measuredValues {
|
||||
if _, ok := sortedMeasuredValueTypes[measuredValue.ValueType]; !ok {
|
||||
sortedMeasuredValueTypes[measuredValue.ValueType] = make([]*types.MeasuredValue, 0)
|
||||
}
|
||||
sortedMeasuredValueTypes[measuredValue.ValueType] = append(sortedMeasuredValueTypes[measuredValue.ValueType], measuredValue)
|
||||
}
|
||||
|
||||
assetFunc := func(queryFile string) (string, error) {
|
||||
queryBytes, err := Asset(queryFile)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
return string(queryBytes), nil
|
||||
}
|
||||
|
||||
for measuredValueType, sortedMeasuredValues := range sortedMeasuredValueTypes {
|
||||
switch measuredValueType {
|
||||
case types.MeasuredValueTypeHumidity:
|
||||
query, err := assetFunc("pkg/db/sql/psql/deleteHumidity.sql")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := deleteMeasuredValue(ctx, query, sortedMeasuredValues); err != nil {
|
||||
return err
|
||||
}
|
||||
case types.MeasuredValueTypePressure:
|
||||
query, err := assetFunc("pkg/db/sql/psql/deletePressure.sql")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := deleteMeasuredValue(ctx, query, sortedMeasuredValues); err != nil {
|
||||
return err
|
||||
}
|
||||
case types.MeasuredValueTypeTemperature:
|
||||
query, err := assetFunc("pkg/db/sql/psql/deleteTemperature.sql")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := deleteMeasuredValue(ctx, query, sortedMeasuredValues); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) InsertDevices(ctx context.Context, devices []*types.Device) error {
|
||||
asset := "pkg/db/sql/psql/insertDevice.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, device := range devices {
|
||||
_, err := stmt.ExecContext(ctx, &device.DeviceID, &device.DeviceName, &device.DeviceLocation, &device.CreationDate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorStatementExecute, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) InsertMeasuredValues(ctx context.Context, measuredValues []*types.MeasuredValue) error {
|
||||
|
||||
sortedMeasuredValueTypes := make(map[types.MeasuredValueType][]*types.MeasuredValue)
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
if _, ok := sortedMeasuredValueTypes[measuredValue.ValueType]; !ok {
|
||||
sortedMeasuredValueTypes[measuredValue.ValueType] = make([]*types.MeasuredValue, 0)
|
||||
}
|
||||
sortedMeasuredValueTypes[measuredValue.ValueType] = append(sortedMeasuredValueTypes[measuredValue.ValueType], measuredValue)
|
||||
}
|
||||
|
||||
for measuredValueType, sortedMeasuredValues := range sortedMeasuredValueTypes {
|
||||
switch measuredValueType {
|
||||
case types.MeasuredValueTypeHumidity:
|
||||
if err := p.insertHumidity(ctx, sortedMeasuredValues); err != nil {
|
||||
return err
|
||||
}
|
||||
case types.MeasuredValueTypePressure:
|
||||
if err := p.insertPressure(ctx, sortedMeasuredValues); err != nil {
|
||||
return err
|
||||
}
|
||||
case types.MeasuredValueTypeTemperature:
|
||||
if err := p.insertTemperature(ctx, sortedMeasuredValues); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) insertHumidity(ctx context.Context, measuredValues []*types.MeasuredValue) error {
|
||||
|
||||
asset := "pkg/db/sql/psql/insertHumidity.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
|
||||
if measuredValue.ValueType != types.MeasuredValueTypeHumidity {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := stmt.ExecContext(ctx, &measuredValue.ID, &measuredValue.Value, &measuredValue.FromDate, &measuredValue.TillDate, &measuredValue.SensorID, &measuredValue.CreationDate, &measuredValue.UpdateDate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorStatementExecute, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) insertPressure(ctx context.Context, measuredValues []*types.MeasuredValue) error {
|
||||
|
||||
asset := "pkg/db/sql/psql/insertPressure.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
|
||||
if measuredValue.ValueType != types.MeasuredValueTypePressure {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := stmt.ExecContext(ctx, &measuredValue.ID, &measuredValue.Value, &measuredValue.FromDate, &measuredValue.TillDate, &measuredValue.SensorID, &measuredValue.CreationDate, &measuredValue.UpdateDate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorStatementExecute, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) insertTemperature(ctx context.Context, measuredValues []*types.MeasuredValue) error {
|
||||
|
||||
asset := "pkg/db/sql/psql/insertTemperature.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
|
||||
if measuredValue.ValueType != types.MeasuredValueTypeTemperature {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := stmt.ExecContext(ctx, &measuredValue.ID, &measuredValue.Value, &measuredValue.FromDate, &measuredValue.TillDate, &measuredValue.SensorID, &measuredValue.CreationDate, &measuredValue.UpdateDate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorStatementExecute, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) InsertSensors(ctx context.Context, sensors []*types.Sensor) error {
|
||||
|
||||
asset := "pkg/db/sql/psql/insertSensor.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, sensor := range sensors {
|
||||
|
||||
_, err := stmt.ExecContext(ctx, &sensor.SensorID, &sensor.SensorName, &sensor.SensorLocation, &sensor.WireID, &sensor.I2CBus, &sensor.I2CAddress, &sensor.GPIONumber, &sensor.SensorModel, &sensor.SensorEnabled, &sensor.DeviceID, &sensor.CreationDate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorStatementExecute, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectDeviceByID(ctx context.Context, id string) (*types.Device, error) {
|
||||
asset := "pkg/db/sql/psql/selectDeviceByID.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
|
||||
row := stmt.QueryRowContext(ctx, id)
|
||||
if row == nil {
|
||||
return nil, errorRowNotFound
|
||||
}
|
||||
|
||||
device := new(types.Device)
|
||||
err = row.Scan(&device.DeviceID, &device.DeviceName, &device.DeviceLocation, &device.CreationDate)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorScanRow, err)
|
||||
}
|
||||
|
||||
return device, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectMeasuredValuesByIDAndType(ctx context.Context, id string, valueType types.MeasuredValueType) (*types.MeasuredValue, error) {
|
||||
switch valueType {
|
||||
case types.MeasuredValueTypeHumidity:
|
||||
return p.SelectHumidityByID(ctx, id)
|
||||
case types.MeasuredValueTypePressure:
|
||||
return p.SelectPressureByID(ctx, id)
|
||||
case types.MeasuredValueTypeTemperature:
|
||||
return p.SelectTemperatureByID(ctx, id)
|
||||
default:
|
||||
return nil, fmt.Errorf("%v: %v", errorUnknownMeasuredValueType, valueType)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectHumidities(ctx context.Context) ([]*types.MeasuredValue, error) {
|
||||
queryFile := "pkg/db/sql/psql/selectHumidities.sql"
|
||||
measuredValues, err := p.selectMeasuredValues(ctx, types.MeasuredValueTypeHumidity, queryFile, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectHumidityByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
||||
queryFile := "pkg/db/sql/psql/selectHumidityByID.sql"
|
||||
args := []interface{}{id}
|
||||
measuredValues, err := p.selectMeasuredValues(ctx, types.MeasuredValueTypeHumidity, queryFile, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(measuredValues) == 0 {
|
||||
return nil, fmt.Errorf("%v: %v", errorRowNotFound, id)
|
||||
}
|
||||
|
||||
return measuredValues[0], nil
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectPressures(ctx context.Context) ([]*types.MeasuredValue, error) {
|
||||
queryFile := "pkg/db/sql/psql/selectPressures.sql"
|
||||
measuredValues, err := p.selectMeasuredValues(ctx, types.MeasuredValueTypePressure, queryFile, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectPressureByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
||||
queryFile := "pkg/db/sql/psql/selectPressureByID.sql"
|
||||
args := []interface{}{id}
|
||||
measuredValues, err := p.selectMeasuredValues(ctx, types.MeasuredValueTypePressure, queryFile, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(measuredValues) == 0 {
|
||||
return nil, fmt.Errorf("%v: %v", errorRowNotFound, id)
|
||||
}
|
||||
|
||||
return measuredValues[0], nil
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectSensorByID(ctx context.Context, id string) (*types.Sensor, error) {
|
||||
asset := "pkg/db/sql/psql/selectSensorByID.sql"
|
||||
queryBytes, err := Asset(asset)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
|
||||
row := stmt.QueryRowContext(ctx, id)
|
||||
if row == nil {
|
||||
return nil, errorRowNotFound
|
||||
}
|
||||
|
||||
sensor := new(types.Sensor)
|
||||
err = row.Scan(&sensor.SensorID, &sensor.SensorName, &sensor.SensorLocation, &sensor.WireID, &sensor.I2CBus, &sensor.I2CAddress, &sensor.GPIONumber, &sensor.SensorModel, &sensor.SensorEnabled, &sensor.DeviceID, &sensor.CreationDate)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorScanRow, err)
|
||||
}
|
||||
|
||||
return sensor, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectTemperatures(ctx context.Context) ([]*types.MeasuredValue, error) {
|
||||
queryFile := "pkg/db/sql/psql/selectTemperatures.sql"
|
||||
measuredValues, err := p.selectMeasuredValues(ctx, types.MeasuredValueTypeTemperature, queryFile, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) SelectTemperatureByID(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
||||
queryFile := "pkg/db/sql/psql/selectTemperatureByID.sql"
|
||||
args := []interface{}{id}
|
||||
measuredValues, err := p.selectMeasuredValues(ctx, types.MeasuredValueTypeTemperature, queryFile, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(measuredValues) == 0 {
|
||||
return nil, fmt.Errorf("%v: %v", errorRowNotFound, id)
|
||||
}
|
||||
|
||||
return measuredValues[0], nil
|
||||
}
|
||||
|
||||
func (p *Postgres) selectMeasuredValues(ctx context.Context, measuredValueType types.MeasuredValueType, queryFile string, queryArgs []interface{}) ([]*types.MeasuredValue, error) {
|
||||
queryBytes, err := Asset(queryFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorGetAsset, err)
|
||||
}
|
||||
query := string(queryBytes)
|
||||
|
||||
stmt, err := p.dbo.PrepareContext(ctx, query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorPrepareStatement, err)
|
||||
}
|
||||
|
||||
rows, err := stmt.QueryContext(ctx, queryArgs...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorStatementQuery, err)
|
||||
}
|
||||
|
||||
measuredValues := make([]*types.MeasuredValue, 0)
|
||||
for rows.Next() {
|
||||
measuredValue := new(types.MeasuredValue)
|
||||
measuredValue.ValueType = measuredValueType
|
||||
rows.Scan(&measuredValue.ID, &measuredValue.Value, &measuredValue.FromDate, &measuredValue.TillDate, &measuredValue.SensorID, &measuredValue.CreationDate, &measuredValue.UpdateDate)
|
||||
measuredValues = append(measuredValues, measuredValue)
|
||||
}
|
||||
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) UpdateDevices(ctx context.Context, devices []*types.Device) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) UpdateMeasuredValues(ctx context.Context, measuredValues []*types.MeasuredValue) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) UpdateSensors(ctx context.Context, sensots []*types.Sensor) error {
|
||||
return nil
|
||||
}
|
@ -1,273 +0,0 @@
|
||||
package db_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/db"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/go-flucky/flucky/test/goldenfiles"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type test struct {
|
||||
Name string
|
||||
Test func(*testing.T)
|
||||
}
|
||||
|
||||
var (
|
||||
database db.Database
|
||||
|
||||
postgresContainerImage string = "docker.io/postgres/postgres"
|
||||
postgresHost string = "localhost"
|
||||
postgresPort string = "5432"
|
||||
postgresUser string = "postgres"
|
||||
postgresPassword string = "postgres"
|
||||
postgresDatabase string = "postgres"
|
||||
|
||||
goldenDevicesFilePath string = "test/goldenfiles/json/goldenDevices.json"
|
||||
goldenSensorsFilePath string = "test/goldenfiles/json/goldenSensors.json"
|
||||
goldenMeasuredValuesFilePath string = "test/goldenfiles/json/goldenMeasuredValues.json"
|
||||
goldenPressuresFilePath string = "test/goldenfiles/json/goldenPressures.json"
|
||||
goldenHumiditiesFilePath string = "test/goldenfiles/json/goldenHumidities.json"
|
||||
goldenTemperaturesFilePath string = "test/goldenfiles/json/goldenTemperatures.json"
|
||||
|
||||
goldenDevices []*types.Device
|
||||
goldenSensors []*types.Sensor
|
||||
goldenMeasuredValues []*types.MeasuredValue
|
||||
goldenPressures []*types.MeasuredValue
|
||||
goldenHumidites []*types.MeasuredValue
|
||||
goldenTemperatures []*types.MeasuredValue
|
||||
)
|
||||
|
||||
func load(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
d, err := goldenfiles.GetGoldenDevices(goldenDevicesFilePath)
|
||||
require.NoError(err)
|
||||
goldenDevices = d
|
||||
|
||||
s, err := goldenfiles.GetGoldenSensors(goldenSensorsFilePath)
|
||||
require.NoError(err)
|
||||
goldenSensors = s
|
||||
|
||||
hum, err := goldenfiles.GetGoldenMeasuredValues(goldenHumiditiesFilePath)
|
||||
require.NoError(err)
|
||||
goldenHumidites = hum
|
||||
|
||||
mv, err := goldenfiles.GetGoldenMeasuredValues(goldenMeasuredValuesFilePath)
|
||||
require.NoError(err)
|
||||
goldenMeasuredValues = mv
|
||||
|
||||
pres, err := goldenfiles.GetGoldenMeasuredValues(goldenPressuresFilePath)
|
||||
require.NoError(err)
|
||||
goldenPressures = pres
|
||||
|
||||
temp, err := goldenfiles.GetGoldenMeasuredValues(goldenTemperaturesFilePath)
|
||||
require.NoError(err)
|
||||
goldenTemperatures = temp
|
||||
|
||||
}
|
||||
|
||||
func TestPostgres(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
load(t)
|
||||
|
||||
db, err := db.New(db.DBOTypePostgres, postgresHost, postgresPort, postgresDatabase, postgresUser, postgresPassword)
|
||||
database = db
|
||||
require.Nil(err)
|
||||
|
||||
tests := []*test{
|
||||
&test{
|
||||
Name: "insertDevices",
|
||||
Test: testInsertDevices,
|
||||
},
|
||||
&test{
|
||||
Name: "insertSensors",
|
||||
Test: testInsertSensors,
|
||||
},
|
||||
&test{
|
||||
Name: "insertHumidity",
|
||||
Test: testInsertHumidity,
|
||||
},
|
||||
&test{
|
||||
Name: "insertPressure",
|
||||
Test: testInsertPressure,
|
||||
},
|
||||
&test{
|
||||
Name: "insertTemperatures",
|
||||
Test: testInsertTemperatures,
|
||||
},
|
||||
&test{
|
||||
Name: "deleteHumidities",
|
||||
Test: testDeleteHumidity,
|
||||
},
|
||||
&test{
|
||||
Name: "deletePressures",
|
||||
Test: testDeletePressures,
|
||||
},
|
||||
&test{
|
||||
Name: "deleteTemperatures",
|
||||
Test: testDeleteTemperatures,
|
||||
},
|
||||
&test{
|
||||
Name: "insertMeasuredValues",
|
||||
Test: testInsertMeasuredValues,
|
||||
},
|
||||
&test{
|
||||
Name: "deleteMeasuredValues",
|
||||
Test: testDeleteMeasuredValues,
|
||||
},
|
||||
&test{
|
||||
Name: "deleteSensors",
|
||||
Test: testDeleteSensors,
|
||||
},
|
||||
&test{
|
||||
Name: "deleteDevices",
|
||||
Test: testDeleteDevices,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.Name, test.Test)
|
||||
}
|
||||
}
|
||||
|
||||
func testInsertDevices(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.InsertDevices(ctx, goldenDevices)
|
||||
require.NoError(err)
|
||||
for _, goldenDevice := range goldenDevices {
|
||||
testDevice, err := database.SelectDeviceByID(ctx, goldenDevice.DeviceID)
|
||||
require.NoError(err)
|
||||
goldenfiles.CompareMeasuredValues(t, goldenDevice, testDevice)
|
||||
}
|
||||
}
|
||||
|
||||
func testInsertSensors(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.InsertSensors(ctx, goldenSensors)
|
||||
require.NoError(err)
|
||||
for _, goldenSensor := range goldenSensors {
|
||||
testSensor, err := database.SelectSensorByID(ctx, goldenSensor.SensorID)
|
||||
require.NoError(err)
|
||||
goldenfiles.CompareMeasuredValues(t, goldenSensor, testSensor)
|
||||
}
|
||||
}
|
||||
|
||||
func testInsertHumidity(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.InsertMeasuredValues(ctx, goldenHumidites)
|
||||
require.NoError(err)
|
||||
for _, goldenHumidity := range goldenHumidites {
|
||||
testHumidity, err := database.SelectHumidityByID(ctx, goldenHumidity.ID)
|
||||
require.NoError(err)
|
||||
goldenfiles.CompareMeasuredValues(t, []*types.MeasuredValue{testHumidity}, []*types.MeasuredValue{testHumidity})
|
||||
}
|
||||
}
|
||||
|
||||
func testInsertMeasuredValues(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.InsertMeasuredValues(ctx, goldenMeasuredValues)
|
||||
require.NoError(err)
|
||||
for _, goldenMeasuredValue := range goldenMeasuredValues {
|
||||
testMeasuredValue, err := database.SelectMeasuredValuesByIDAndType(ctx, goldenMeasuredValue.ID, goldenMeasuredValue.ValueType)
|
||||
require.NoError(err)
|
||||
goldenfiles.CompareMeasuredValues(t, []*types.MeasuredValue{goldenMeasuredValue}, []*types.MeasuredValue{testMeasuredValue})
|
||||
}
|
||||
}
|
||||
|
||||
func testInsertPressure(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.InsertMeasuredValues(ctx, goldenPressures)
|
||||
require.NoError(err)
|
||||
for _, goldenPressure := range goldenPressures {
|
||||
testPressure, err := database.SelectPressureByID(ctx, goldenPressure.ID)
|
||||
require.NoError(err)
|
||||
goldenfiles.CompareMeasuredValues(t, []*types.MeasuredValue{testPressure}, []*types.MeasuredValue{testPressure})
|
||||
}
|
||||
}
|
||||
|
||||
func testInsertTemperatures(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.InsertMeasuredValues(ctx, goldenTemperatures)
|
||||
require.NoError(err)
|
||||
for _, goldenTemperature := range goldenTemperatures {
|
||||
testTemperature, err := database.SelectTemperatureByID(ctx, goldenTemperature.ID)
|
||||
require.NoError(err)
|
||||
goldenfiles.CompareMeasuredValues(t, []*types.MeasuredValue{goldenTemperature}, []*types.MeasuredValue{testTemperature})
|
||||
}
|
||||
}
|
||||
|
||||
func testDeleteDevices(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.DeleteDevices(ctx, goldenDevices)
|
||||
require.NoError(err)
|
||||
for _, goldenDevice := range goldenDevices {
|
||||
_, err := database.SelectDeviceByID(ctx, goldenDevice.DeviceID)
|
||||
require.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testDeleteSensors(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.DeleteSensors(ctx, goldenSensors)
|
||||
require.NoError(err)
|
||||
for _, goldenSensor := range goldenSensors {
|
||||
_, err := database.SelectDeviceByID(ctx, goldenSensor.SensorID)
|
||||
require.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testDeleteHumidity(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.DeleteMeasuredValues(ctx, goldenHumidites)
|
||||
require.NoError(err)
|
||||
for _, goldenHumidity := range goldenHumidites {
|
||||
_, err := database.SelectHumidityByID(ctx, goldenHumidity.ID)
|
||||
require.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testDeleteMeasuredValues(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.DeleteMeasuredValues(ctx, goldenMeasuredValues)
|
||||
require.NoError(err)
|
||||
for _, goldenMeasuredValue := range goldenMeasuredValues {
|
||||
_, err := database.SelectPressureByID(ctx, goldenMeasuredValue.ID)
|
||||
require.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testDeletePressures(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.DeleteMeasuredValues(ctx, goldenPressures)
|
||||
require.NoError(err)
|
||||
for _, goldenPressure := range goldenPressures {
|
||||
_, err := database.SelectPressureByID(ctx, goldenPressure.ID)
|
||||
require.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testDeleteTemperatures(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
err := database.DeleteMeasuredValues(ctx, goldenTemperatures)
|
||||
require.NoError(err)
|
||||
for _, goldenTemperature := range goldenTemperatures {
|
||||
_, err := database.SelectTemperatureByID(ctx, goldenTemperature.ID)
|
||||
require.Error(err)
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
DELETE FROM humidities
|
||||
WHERE humidity_id = $1;
|
@ -1,2 +0,0 @@
|
||||
DELETE FROM pressures
|
||||
WHERE pressure_id = $1;
|
@ -1,2 +0,0 @@
|
||||
DELETE FROM temperatures
|
||||
WHERE temperature_id = $1;
|
@ -1,10 +0,0 @@
|
||||
INSERT INTO humidities (
|
||||
humidity_id,
|
||||
humidity_value,
|
||||
humidity_from_date,
|
||||
humidity_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7);
|
@ -1,10 +0,0 @@
|
||||
INSERT INTO pressures (
|
||||
pressure_id,
|
||||
pressure_value,
|
||||
pressure_from_date,
|
||||
pressure_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7);
|
@ -1,10 +0,0 @@
|
||||
INSERT INTO temperatures (
|
||||
temperature_id,
|
||||
temperature_value,
|
||||
temperature_from_date,
|
||||
temperature_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7);
|
@ -1,146 +0,0 @@
|
||||
DROP TABLE IF EXISTS devices CASCADE;
|
||||
DROP TABLE IF EXISTS sensors CASCADE;
|
||||
DROP TABLE IF EXISTS humidities CASCADE;
|
||||
DROP TABLE IF EXISTS pressures CASCADE;
|
||||
DROP TABLE IF EXISTS temperatures CASCADE;
|
||||
|
||||
|
||||
-- +----------------------------------------+
|
||||
-- | TABLES |
|
||||
-- +----------------------------------------+
|
||||
CREATE TABLE IF NOT EXISTS devices(
|
||||
device_id CHAR(36) CONSTRAINT pk_devices PRIMARY KEY,
|
||||
device_name VARCHAR(32) NOT NULL,
|
||||
device_location VARCHAR(32),
|
||||
device_last_contact TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sensors (
|
||||
sensor_id CHAR(36) CONSTRAINT pk_sensors PRIMARY KEY,
|
||||
sensor_name VARCHAR(32) NOT NULL,
|
||||
sensor_location VARCHAR(32) NOT NULL,
|
||||
wire_id VARCHAR(15),
|
||||
i2c_bus VARCHAR(255),
|
||||
i2c_address VARCHAR(12),
|
||||
gpio_number VARCHAR(6),
|
||||
sensor_model VARCHAR(16) NOT NULL,
|
||||
sensor_enabled BOOLEAN DEFAULT TRUE NOT NULL,
|
||||
sensor_last_contact TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
device_id CHAR(36),
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS humidities (
|
||||
humidity_id CHAR(36) CONSTRAINT pk_humidities PRIMARY KEY,
|
||||
humidity_value NUMERIC(9,3) NOT NULL,
|
||||
humidity_from_date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
humidity_till_date TIMESTAMP WITH TIME ZONE,
|
||||
sensor_id CHAR(36),
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS pressures (
|
||||
pressure_id CHAR(36) CONSTRAINT pk_pressures PRIMARY KEY,
|
||||
pressure_value NUMERIC(10,3) NOT NULL,
|
||||
pressure_from_date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
pressure_till_date TIMESTAMP WITH TIME ZONE,
|
||||
sensor_id CHAR(36),
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS temperatures (
|
||||
temperature_id CHAR(36) CONSTRAINT pk_temperatures PRIMARY KEY,
|
||||
temperature_value NUMERIC(5,3) NOT NULL,
|
||||
temperature_from_date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
temperature_till_date TIMESTAMP WITH TIME ZONE,
|
||||
sensor_id CHAR(36),
|
||||
creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
-- +----------------------------------------+
|
||||
-- | FOREIGN-KEYS |
|
||||
-- +----------------------------------------+
|
||||
ALTER TABLE sensors
|
||||
ADD FOREIGN KEY (device_id)
|
||||
REFERENCES devices(device_id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE humidities
|
||||
ADD FOREIGN KEY (sensor_id)
|
||||
REFERENCES sensors(sensor_id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE pressures
|
||||
ADD FOREIGN KEY (sensor_id)
|
||||
REFERENCES sensors(sensor_id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE temperatures
|
||||
ADD FOREIGN KEY (sensor_id)
|
||||
REFERENCES sensors(sensor_id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
||||
|
||||
-- +----------------------------------------+
|
||||
-- | Trigger-Functions |
|
||||
-- +----------------------------------------+
|
||||
CREATE OR REPLACE FUNCTION device_last_contact()
|
||||
RETURNS trigger AS
|
||||
$BODY$
|
||||
BEGIN
|
||||
UPDATE devices
|
||||
SET device_last_contact = CURRENT_TIMESTAMP
|
||||
WHERE device_id = NEW.device_id;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$BODY$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE OR REPLACE FUNCTION sensor_last_contact()
|
||||
RETURNS trigger AS
|
||||
$BODY$
|
||||
BEGIN
|
||||
UPDATE sensors
|
||||
SET sensor_last_contact = CURRENT_TIMESTAMP,
|
||||
sensor_enabled = true
|
||||
WHERE sensor_id = NEW.sensor_id;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$BODY$ LANGUAGE plpgsql;
|
||||
|
||||
-- +----------------------------------------+
|
||||
-- | Trigger |
|
||||
-- +----------------------------------------+
|
||||
DROP TRIGGER IF EXISTS ai_humidities ON humidities;
|
||||
DROP TRIGGER IF EXISTS ai_pressure ON pressures;
|
||||
DROP TRIGGER IF EXISTS ai_temperatures ON temperatures;
|
||||
|
||||
CREATE TRIGGER au_sensors
|
||||
AFTER UPDATE
|
||||
ON sensors
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE device_last_contact();
|
||||
|
||||
CREATE TRIGGER ai_humidities
|
||||
AFTER INSERT
|
||||
ON humidities
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE sensor_last_contact();
|
||||
|
||||
CREATE TRIGGER ai_pressures
|
||||
AFTER INSERT
|
||||
ON pressures
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE sensor_last_contact();
|
||||
|
||||
CREATE TRIGGER ai_temperatures
|
||||
AFTER INSERT
|
||||
ON temperatures
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE sensor_last_contact();
|
@ -1,10 +0,0 @@
|
||||
SELECT
|
||||
humidity_id,
|
||||
humidity_value,
|
||||
humidity_from_date,
|
||||
humidity_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
humidities;
|
@ -1,12 +0,0 @@
|
||||
SELECT
|
||||
humidity_id,
|
||||
humidity_value,
|
||||
humidity_from_date,
|
||||
humidity_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
humidities
|
||||
WHERE
|
||||
humidity_id = $1;
|
@ -1,12 +0,0 @@
|
||||
SELECT
|
||||
pressure_id,
|
||||
pressure_value,
|
||||
pressure_from_date,
|
||||
pressure_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
pressures
|
||||
WHERE
|
||||
pressure_id = $1;
|
@ -1,10 +0,0 @@
|
||||
SELECT
|
||||
pressure_id,
|
||||
pressure_value,
|
||||
pressure_from_date,
|
||||
pressure_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
pressures;
|
@ -1,12 +0,0 @@
|
||||
SELECT
|
||||
temperature_id,
|
||||
temperature_value,
|
||||
temperature_from_date,
|
||||
temperature_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
temperatures
|
||||
WHERE
|
||||
temperature_id = $1;
|
@ -1,10 +0,0 @@
|
||||
SELECT
|
||||
temperature_id,
|
||||
temperature_value,
|
||||
temperature_from_date,
|
||||
temperature_till_date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
temperatures;
|
@ -1,16 +0,0 @@
|
||||
package collect
|
||||
|
||||
func Errors(errorChannel <-chan error) []error {
|
||||
errorList := make([]error, 0)
|
||||
for {
|
||||
select {
|
||||
case err, more := <-errorChannel:
|
||||
if more {
|
||||
errorList = append(errorList, err)
|
||||
continue
|
||||
}
|
||||
default:
|
||||
return errorList
|
||||
}
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package collect
|
||||
|
||||
import (
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
func MeasuredValues(measuredValuesChannel <-chan []*types.MeasuredValue) []*types.MeasuredValue {
|
||||
cachedMeasuredValues := make([]*types.MeasuredValue, 0)
|
||||
for {
|
||||
select {
|
||||
case measuredValues, more := <-measuredValuesChannel:
|
||||
if more {
|
||||
cachedMeasuredValues = append(cachedMeasuredValues, measuredValues...)
|
||||
continue
|
||||
}
|
||||
default:
|
||||
return cachedMeasuredValues
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package prittyprint
|
||||
|
||||
import "fmt"
|
||||
|
||||
func FormatErrors(errors []error) error {
|
||||
if len(errors) > 0 {
|
||||
errMsg := ""
|
||||
for i, err := range errors {
|
||||
if i == 0 {
|
||||
errMsg = fmt.Sprintf("%v", err.Error())
|
||||
} else {
|
||||
errMsg = fmt.Sprintf("%v\n%v", errMsg, err.Error())
|
||||
}
|
||||
}
|
||||
return fmt.Errorf(errMsg)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,129 +0,0 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/internal/format"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
type csvLogfile struct {
|
||||
logfile string
|
||||
}
|
||||
|
||||
func (cl *csvLogfile) Read() ([]*types.MeasuredValue, error) {
|
||||
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
||||
}
|
||||
|
||||
f, err := os.Open(cl.logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
r := csv.NewReader(f)
|
||||
records, err := r.ReadAll()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
||||
}
|
||||
|
||||
measuredValues := make([]*types.MeasuredValue, 0)
|
||||
|
||||
for _, record := range records {
|
||||
|
||||
// ValueType
|
||||
valueType, err := types.SelectMeasuredValueType(record[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
|
||||
}
|
||||
|
||||
// Value
|
||||
value, err := strconv.ParseFloat(record[2], 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[2], err)
|
||||
}
|
||||
|
||||
// Times
|
||||
times := make([]time.Time, 0)
|
||||
for _, i := range []int{3, 4} {
|
||||
time, err := time.Parse(format.TimeFormat, record[i])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[i], err)
|
||||
}
|
||||
times = append(times, time)
|
||||
}
|
||||
|
||||
measuredValue := &types.MeasuredValue{
|
||||
ID: record[0],
|
||||
ValueType: *valueType,
|
||||
Value: value,
|
||||
FromDate: times[0],
|
||||
TillDate: times[1],
|
||||
SensorID: record[5],
|
||||
}
|
||||
|
||||
// Creation date
|
||||
creationDate, err := time.Parse(format.TimeFormat, record[6])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[6], err)
|
||||
}
|
||||
measuredValue.CreationDate = creationDate
|
||||
|
||||
if record[7] != "null" {
|
||||
updateDate, err := time.Parse(format.TimeFormat, record[7])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
|
||||
}
|
||||
measuredValue.UpdateDate = &updateDate
|
||||
}
|
||||
|
||||
measuredValues = append(measuredValues, measuredValue)
|
||||
|
||||
}
|
||||
|
||||
return measuredValues, nil
|
||||
|
||||
}
|
||||
|
||||
func (cl *csvLogfile) Write(measuredValues []*types.MeasuredValue) error {
|
||||
f, err := os.Create(cl.logfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
writeCreationDate(measuredValues)
|
||||
|
||||
w := csv.NewWriter(f)
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
|
||||
record := []string{
|
||||
measuredValue.ID,
|
||||
fmt.Sprintf("%v", measuredValue.ValueType),
|
||||
fmt.Sprintf("%v", measuredValue.Value),
|
||||
measuredValue.FromDate.Format(format.TimeFormat),
|
||||
measuredValue.TillDate.Format(format.TimeFormat),
|
||||
measuredValue.SensorID,
|
||||
}
|
||||
|
||||
record = append(record, measuredValue.CreationDate.Format(format.TimeFormat))
|
||||
|
||||
if measuredValue.UpdateDate != nil {
|
||||
record = append(record, measuredValue.UpdateDate.Format(format.TimeFormat))
|
||||
} else {
|
||||
record = append(record, "null")
|
||||
}
|
||||
|
||||
w.Write(record)
|
||||
}
|
||||
|
||||
w.Flush()
|
||||
|
||||
return nil
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package logfile
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
errorLogfileCreate = errors.New("Can not create logfile")
|
||||
errorLogfileDecode = errors.New("Can not decode from reader")
|
||||
errorLogfileEncode = errors.New("Can not encode from writer")
|
||||
errorLogfileMarshal = errors.New("Can not marshal values")
|
||||
errorLogfileNotFound = errors.New("Can not find logfile")
|
||||
errorLogfileOpen = errors.New("Can not open logfile")
|
||||
errorLogfileRead = errors.New("Can not read from given reader")
|
||||
errorLogfileUnmarshal = errors.New("Can not unmarshal values")
|
||||
errorLogfileWrite = errors.New("Can not write with given writer")
|
||||
|
||||
errorParseFloat = errors.New("Can not parse float")
|
||||
errorParseMeasurementUnit = errors.New("Can not parse mesaurement unit")
|
||||
errorParseTime = errors.New("Can not parse time")
|
||||
|
||||
errorNoValidHumidityID = errors.New("No valid humidity id detected or available")
|
||||
errorNoValidMesuredValue = errors.New("No mesured value detected or available")
|
||||
errorNoValidSensorID = errors.New("No sensor id detected or available")
|
||||
errorNoValidTemperatureID = errors.New("No valid temperature id detected or available")
|
||||
errorNoValidTime = errors.New("No time detected or available")
|
||||
errorNoValidTimePeriods = errors.New("No valid time periods")
|
||||
|
||||
errorTypeSwitch = errors.New("Can not detect type via type switch")
|
||||
)
|
@ -1,11 +0,0 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
// Logfile is an interface for various logfiles
|
||||
type Logfile interface {
|
||||
Read() ([]*types.MeasuredValue, error)
|
||||
Write(measuredValues []*types.MeasuredValue) error
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
type jsonLogfile struct {
|
||||
logfile string
|
||||
}
|
||||
|
||||
func (jl *jsonLogfile) Read() ([]*types.MeasuredValue, error) {
|
||||
|
||||
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.logfile)
|
||||
}
|
||||
|
||||
f, err := os.Open(jl.logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v %v: %v", errorLogfileOpen, jl.logfile, err)
|
||||
}
|
||||
|
||||
measuredValues := make([]*types.MeasuredValue, 0)
|
||||
|
||||
if err := json.NewDecoder(f).Decode(&measuredValues); err != nil {
|
||||
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, jl.logfile, err)
|
||||
}
|
||||
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
func (jl *jsonLogfile) Write(measuredValues []*types.MeasuredValue) error {
|
||||
|
||||
if _, err := os.Stat(filepath.Dir(jl.logfile)); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(filepath.Dir(jl.logfile), 755); err != nil {
|
||||
return fmt.Errorf("Directory for the logfile can not be created: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
writeCreationDate(measuredValues)
|
||||
|
||||
f, err := os.Create(jl.logfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v %v: %v", errorLogfileCreate, jl.logfile, err)
|
||||
}
|
||||
|
||||
jsonEncoder := json.NewEncoder(f)
|
||||
jsonEncoder.SetIndent("", " ")
|
||||
err = jsonEncoder.Encode(measuredValues)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v %v: %v", errorLogfileEncode, jl.logfile, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
@ -1,132 +0,0 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"math"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/internal/format"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
// var validUUID = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
|
||||
|
||||
// Append adds an array of several measured values to a logfile
|
||||
func Append(logfile Logfile, compression bool, round float64, measuredValues []*types.MeasuredValue) error {
|
||||
|
||||
if round != 0 {
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValue.Value = math.Round(measuredValue.Value/round) * round
|
||||
}
|
||||
}
|
||||
|
||||
allMeasuredValues, err := logfile.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
||||
|
||||
if compression {
|
||||
allMeasuredValues = Compression(allMeasuredValues)
|
||||
}
|
||||
|
||||
err = logfile.Write(allMeasuredValues)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Compression the measured values. The system checks whether the measured values
|
||||
// of the same type correspond to those of the predecessor. If this is the case,
|
||||
// the current value is discarded and the validity date of the previous value is
|
||||
// set to that of the current value. This means that no information is lost.
|
||||
// Only the validity period of the measured value is increased.
|
||||
func Compression(measuredValues []*types.MeasuredValue) []*types.MeasuredValue {
|
||||
compressedMeasuredValues := make([]*types.MeasuredValue, 0)
|
||||
lastMeasuredValuesBySensors := make(map[string]map[types.MeasuredValueType]*types.MeasuredValue, 0)
|
||||
|
||||
// Sort all measured values according to the start time of the validity date
|
||||
// in order to successfully implement the subsequent compression.
|
||||
sort.SliceStable(measuredValues, func(i int, j int) bool {
|
||||
return measuredValues[i].FromDate.Before(measuredValues[j].TillDate)
|
||||
})
|
||||
|
||||
now := format.FormatedTime()
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
if _, ok := lastMeasuredValuesBySensors[measuredValue.SensorID]; !ok {
|
||||
lastMeasuredValuesBySensors[measuredValue.SensorID] = make(map[types.MeasuredValueType]*types.MeasuredValue, 0)
|
||||
}
|
||||
|
||||
if _, ok := lastMeasuredValuesBySensors[measuredValue.SensorID][measuredValue.ValueType]; !ok {
|
||||
lastMeasuredValuesBySensors[measuredValue.SensorID][measuredValue.ValueType] = measuredValue
|
||||
continue
|
||||
}
|
||||
|
||||
if lastMeasuredValuesBySensors[measuredValue.SensorID][measuredValue.ValueType].Value == measuredValue.Value {
|
||||
lastMeasuredValuesBySensors[measuredValue.SensorID][measuredValue.ValueType].TillDate = measuredValue.TillDate
|
||||
lastMeasuredValuesBySensors[measuredValue.SensorID][measuredValue.ValueType].UpdateDate = &now
|
||||
} else if lastMeasuredValuesBySensors[measuredValue.SensorID][measuredValue.ValueType].Value != measuredValue.Value {
|
||||
compressedMeasuredValues = append(compressedMeasuredValues, lastMeasuredValuesBySensors[measuredValue.SensorID][measuredValue.ValueType])
|
||||
delete(lastMeasuredValuesBySensors[measuredValue.SensorID], measuredValue.ValueType)
|
||||
lastMeasuredValuesBySensors[measuredValue.SensorID][measuredValue.ValueType] = measuredValue
|
||||
}
|
||||
}
|
||||
|
||||
// Copy all remaining entries from the map into the cache array
|
||||
for _, lastMeasuredValuesBySensor := range lastMeasuredValuesBySensors {
|
||||
for _, measuredValueType := range types.MeasuredValueTypes {
|
||||
if measuredValue, ok := lastMeasuredValuesBySensor[measuredValueType]; ok {
|
||||
compressedMeasuredValues = append(compressedMeasuredValues, measuredValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort all measured values again to include the measured values from the
|
||||
// cache.
|
||||
sort.SliceStable(compressedMeasuredValues, func(i int, j int) bool {
|
||||
return compressedMeasuredValues[i].FromDate.Before(compressedMeasuredValues[j].FromDate)
|
||||
})
|
||||
|
||||
return compressedMeasuredValues
|
||||
}
|
||||
|
||||
// New returns a log file with basic functions for reading and writing data. The
|
||||
// file extension of the logfile is taken into account to format the logfile
|
||||
// into the correct format.
|
||||
func New(logfile string) Logfile {
|
||||
|
||||
ext := filepath.Ext(logfile)
|
||||
|
||||
switch ext {
|
||||
case ".csv":
|
||||
return &csvLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
case ".json":
|
||||
return &jsonLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
case ".xml":
|
||||
return &xmlLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
default:
|
||||
return &jsonLogfile{
|
||||
logfile: logfile,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func writeCreationDate(measuredValues []*types.MeasuredValue) error {
|
||||
for _, measuredValue := range measuredValues {
|
||||
now := format.FormatedTime()
|
||||
measuredValue.CreationDate = now
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1 +0,0 @@
|
||||
package logfile_test
|
@ -1,19 +0,0 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
// MeasuredValues is an XML Wrapper for an array of measured values
|
||||
type MeasuredValues struct {
|
||||
XMLName xml.Name `xml:"measured_values"`
|
||||
MeasuredValues []*MeasuredValue `xml:"measured_value"`
|
||||
}
|
||||
|
||||
// MeasuredValue is an XML Wrapper for the original measured value struct
|
||||
type MeasuredValue struct {
|
||||
XMLName xml.Name `xml:"measured_value"`
|
||||
*types.MeasuredValue
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
package logfile
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
)
|
||||
|
||||
type xmlLogfile struct {
|
||||
logfile string
|
||||
}
|
||||
|
||||
func (xl *xmlLogfile) GetLogfile() string {
|
||||
return xl.logfile
|
||||
}
|
||||
|
||||
func (xl *xmlLogfile) Read() ([]*types.MeasuredValue, error) {
|
||||
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
||||
}
|
||||
|
||||
f, err := os.Open(xl.logfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
measuredValues := new(MeasuredValues)
|
||||
|
||||
if err := xml.NewDecoder(f).Decode(&measuredValues); err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
||||
}
|
||||
|
||||
cachedMeasuredValues := make([]*types.MeasuredValue, 0)
|
||||
for _, measuredValue := range measuredValues.MeasuredValues {
|
||||
cachedMeasuredValues = append(cachedMeasuredValues, measuredValue.MeasuredValue)
|
||||
}
|
||||
|
||||
return cachedMeasuredValues, nil
|
||||
}
|
||||
|
||||
func (xl *xmlLogfile) Write(measuredValues []*types.MeasuredValue) error {
|
||||
f, err := os.Create(xl.logfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
writeCreationDate(measuredValues)
|
||||
|
||||
cachedMeasuredValues := new(MeasuredValues)
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
cachedMeasuredValue := &MeasuredValue{
|
||||
MeasuredValue: measuredValue,
|
||||
}
|
||||
|
||||
cachedMeasuredValues.MeasuredValues = append(cachedMeasuredValues.MeasuredValues, cachedMeasuredValue)
|
||||
}
|
||||
|
||||
bytes, err := xml.MarshalIndent(cachedMeasuredValues, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorLogfileMarshal, err)
|
||||
}
|
||||
|
||||
_, err = f.Write(bytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v: %v", errorLogfileWrite, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type defaultLogger struct {
|
||||
logLevel LogLevel
|
||||
mutex *sync.Mutex
|
||||
stdout io.Writer
|
||||
stderr io.Writer
|
||||
}
|
||||
|
||||
func (dl *defaultLogger) Debug(f string, v ...interface{}) {
|
||||
dl.log(LogLevelDebug, f, v...)
|
||||
}
|
||||
|
||||
func (dl *defaultLogger) Info(f string, v ...interface{}) {
|
||||
dl.log(LogLevelInfo, f, v...)
|
||||
}
|
||||
|
||||
func (dl *defaultLogger) Warn(f string, v ...interface{}) {
|
||||
dl.log(LogLevelWarn, f, v...)
|
||||
}
|
||||
|
||||
func (dl *defaultLogger) Error(f string, v ...interface{}) {
|
||||
dl.log(LogLevelError, f, v...)
|
||||
}
|
||||
|
||||
func (dl *defaultLogger) Fatal(f string, v ...interface{}) {
|
||||
dl.log(LogLevelFatal, f, v...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func (dl *defaultLogger) log(ll LogLevel, f string, v ...interface{}) {
|
||||
|
||||
if dl.logLevel > ll {
|
||||
return
|
||||
}
|
||||
|
||||
layout := "2006/01/02 15:04:05"
|
||||
dw := dl.stdout
|
||||
prefix := ""
|
||||
switch ll {
|
||||
case LogLevelDebug:
|
||||
prefix = "DEBUG"
|
||||
case LogLevelInfo:
|
||||
prefix = "INFO"
|
||||
case LogLevelWarn:
|
||||
prefix = "WARN"
|
||||
case LogLevelError:
|
||||
prefix = "ERROR"
|
||||
dw = dl.stderr
|
||||
case LogLevelFatal:
|
||||
prefix = "FATAL"
|
||||
dw = dl.stderr
|
||||
}
|
||||
dl.mutex.Lock()
|
||||
fmt.Fprintf(dw, "%v %v: %v\n", time.Now().Format(layout), prefix, fmt.Sprintf(f, v...))
|
||||
dl.mutex.Unlock()
|
||||
}
|
||||
|
||||
func NewDefaultLogger(logLevel LogLevel) Logger {
|
||||
return &defaultLogger{
|
||||
logLevel: logLevel,
|
||||
mutex: new(sync.Mutex),
|
||||
stdout: os.Stdout,
|
||||
stderr: os.Stderr,
|
||||
}
|
||||
}
|
||||
|
||||
func NewCustomLogger(loglevel LogLevel, out io.Writer, err io.Writer) Logger {
|
||||
return &defaultLogger{
|
||||
logLevel: loglevel,
|
||||
mutex: new(sync.Mutex),
|
||||
stdout: out,
|
||||
stderr: err,
|
||||
}
|
||||
}
|
||||
|
||||
func NewSilentLogger() Logger {
|
||||
return &defaultLogger{
|
||||
logLevel: LogLevelDebug,
|
||||
mutex: new(sync.Mutex),
|
||||
stdout: ioutil.Discard,
|
||||
stderr: ioutil.Discard,
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
package logger_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/logger"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
require := require.New(t)
|
||||
stdoutBuffer := new(bytes.Buffer)
|
||||
stderrBuffer := new(bytes.Buffer)
|
||||
|
||||
// LogLevelDebug
|
||||
l := logger.NewCustomLogger(logger.LogLevelDebug, stdoutBuffer, stderrBuffer)
|
||||
l.Debug("DEBUG")
|
||||
require.NotEmpty(stdoutBuffer.Bytes())
|
||||
require.Empty(stderrBuffer.Bytes())
|
||||
|
||||
stdoutBuffer.Reset()
|
||||
stderrBuffer.Reset()
|
||||
|
||||
// LogLevelInfo
|
||||
l = logger.NewCustomLogger(logger.LogLevelInfo, stdoutBuffer, stderrBuffer)
|
||||
|
||||
l.Debug("DEBUG")
|
||||
require.Empty(stdoutBuffer.Bytes())
|
||||
require.Empty(stderrBuffer.Bytes())
|
||||
|
||||
stdoutBuffer.Reset()
|
||||
stderrBuffer.Reset()
|
||||
|
||||
l.Info("INFO")
|
||||
require.NotEmpty(stdoutBuffer.Bytes())
|
||||
require.Empty(stderrBuffer.Bytes())
|
||||
|
||||
stringArray := strings.Split(stdoutBuffer.String(), ": ")
|
||||
require.Equal(2, len(stringArray))
|
||||
require.Equal("INFO\n", stringArray[1])
|
||||
|
||||
stdoutBuffer.Reset()
|
||||
stderrBuffer.Reset()
|
||||
|
||||
// LogLevelWarn
|
||||
l = logger.NewCustomLogger(logger.LogLevelWarn, stdoutBuffer, stderrBuffer)
|
||||
l.Warn("WARN")
|
||||
require.NotEmpty(stdoutBuffer.Bytes())
|
||||
require.Empty(stderrBuffer.Bytes())
|
||||
|
||||
stringArray = strings.Split(stdoutBuffer.String(), ": ")
|
||||
require.Equal(2, len(stringArray))
|
||||
require.Equal("WARN\n", stringArray[1])
|
||||
|
||||
stdoutBuffer.Reset()
|
||||
stderrBuffer.Reset()
|
||||
|
||||
// LogLevelError
|
||||
l = logger.NewCustomLogger(logger.LogLevelError, stdoutBuffer, stderrBuffer)
|
||||
l.Error("ERROR")
|
||||
require.Empty(stdoutBuffer.Bytes())
|
||||
require.NotEmpty(stderrBuffer.Bytes())
|
||||
|
||||
stringArray = strings.Split(stderrBuffer.String(), ": ")
|
||||
require.Equal(2, len(stringArray))
|
||||
require.Equal("ERROR\n", stringArray[1])
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package logger
|
||||
|
||||
type Logger interface {
|
||||
Debug(string, ...interface{})
|
||||
Info(string, ...interface{})
|
||||
Warn(string, ...interface{})
|
||||
Error(string, ...interface{})
|
||||
Fatal(string, ...interface{})
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package logger
|
||||
|
||||
type LogLevel int
|
||||
|
||||
const (
|
||||
LogLevelDebug LogLevel = iota + 1
|
||||
LogLevelInfo
|
||||
LogLevelWarn
|
||||
LogLevelError
|
||||
LogLevelFatal
|
||||
)
|
124
pkg/repository/db/db.go
Normal file
124
pkg/repository/db/db.go
Normal file
@ -0,0 +1,124 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
"git.cryptic.systems/volker.raschek/go-logger"
|
||||
)
|
||||
|
||||
// Database is a general interface for a database backend like postgres, oracle
|
||||
// or sqlite
|
||||
type Database interface {
|
||||
Close() error
|
||||
DeleteDevices(ctx context.Context, deviceIDs ...string) error
|
||||
DeleteSensors(ctx context.Context, sensorIDs ...string) error
|
||||
InsertDevices(ctx context.Context, devices ...*types.Device) error
|
||||
InsertMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error
|
||||
InsertSensors(ctx context.Context, sensors ...*types.Sensor) error
|
||||
Scheme(ctx context.Context) error
|
||||
SelectDevice(ctx context.Context, deviceID string) (*types.Device, error)
|
||||
SelectDevices(ctx context.Context) ([]*types.Device, error)
|
||||
SelectHumidity(ctx context.Context, id string) (*types.MeasuredValue, error)
|
||||
SelectHumidities(ctx context.Context) ([]*types.MeasuredValue, error)
|
||||
SelectPressure(ctx context.Context, id string) (*types.MeasuredValue, error)
|
||||
SelectPressures(ctx context.Context) ([]*types.MeasuredValue, error)
|
||||
SelectSensor(ctx context.Context, sensorID string) (*types.Sensor, error)
|
||||
SelectSensors(ctx context.Context) ([]*types.Sensor, error)
|
||||
SelectTemperature(ctx context.Context, id string) (*types.MeasuredValue, error)
|
||||
SelectTemperatures(ctx context.Context) ([]*types.MeasuredValue, error)
|
||||
UpdateDevices(ctx context.Context, devices ...*types.Device) error
|
||||
UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error
|
||||
}
|
||||
|
||||
// New returns a new database backend interface
|
||||
func New(dsnURL *url.URL, flogger logger.Logger) (Database, error) {
|
||||
|
||||
// Check of nil pointer
|
||||
for _, parameter := range []interface{}{
|
||||
dsnURL,
|
||||
flogger,
|
||||
} {
|
||||
if parameter == nil {
|
||||
return nil, fmt.Errorf("Parameter does not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
// Load Queryfiles
|
||||
queries := make(map[string]string, 0)
|
||||
for _, asset := range AssetNames() {
|
||||
|
||||
if !strings.Contains(asset, dsnURL.Scheme) {
|
||||
continue
|
||||
}
|
||||
|
||||
body, err := Asset(asset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
queryFile := filepath.Base(asset)
|
||||
|
||||
queries[queryFile] = string(body)
|
||||
}
|
||||
|
||||
var (
|
||||
database Database
|
||||
err error
|
||||
)
|
||||
|
||||
switch dsnURL.Scheme {
|
||||
case "postgres":
|
||||
|
||||
// postgres://[user]:[password]@[host]:[port]/[path]?[query]
|
||||
newDBO, err := sql.Open(dsnURL.Scheme, dsnURL.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
database = &Postgres{
|
||||
dbo: newDBO,
|
||||
flogger: flogger,
|
||||
queries: queries,
|
||||
}
|
||||
|
||||
case "sqlite3":
|
||||
|
||||
// Create directory where the db file will be created if not exists.
|
||||
if _, err := os.Stat(filepath.Dir(dsnURL.Path)); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(filepath.Dir(dsnURL.Path), 0755)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// sqlite3:///[path]?[query] flucky dsn
|
||||
// file:///[path]?[query] sql-lib dsn
|
||||
newDBO, err := sql.Open(dsnURL.Scheme, fmt.Sprintf("file://%v?%v", dsnURL.Path, dsnURL.RawQuery))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
database = &SQLite{
|
||||
dbo: newDBO,
|
||||
flogger: flogger,
|
||||
queries: queries,
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("Unsupported database scheme: %v", dsnURL.Scheme)
|
||||
}
|
||||
|
||||
// Initialize database scheme if not exists
|
||||
err = database.Scheme(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return database, nil
|
||||
}
|
746
pkg/repository/db/postgres.go
Normal file
746
pkg/repository/db/postgres.go
Normal file
@ -0,0 +1,746 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
||||
"git.cryptic.systems/volker.raschek/go-logger"
|
||||
)
|
||||
|
||||
// Postgres implementation
|
||||
type Postgres struct {
|
||||
dbo *sql.DB
|
||||
flogger logger.Logger
|
||||
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
|
||||
func (postgres *Postgres) DeleteDevices(ctx context.Context, deviceIDs ...string) error {
|
||||
queryFile := "deleteDevice.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, deviceID := range deviceIDs {
|
||||
_, err = stmt.Exec(deviceID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// DeleteSensors from the database
|
||||
func (postgres *Postgres) DeleteSensors(ctx context.Context, sensorIDs ...string) error {
|
||||
queryFile := "deleteSensor.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, sensorID := range sensorIDs {
|
||||
_, err = stmt.Exec(sensorID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// InsertDevices into the database
|
||||
func (postgres *Postgres) InsertDevices(ctx context.Context, devices ...*types.Device) error {
|
||||
queryFile := "insertDevice.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, device := range devices {
|
||||
_, err = stmt.Exec(&device.ID, &device.Name, &device.Location, &device.CreationDate, &device.UpdateDate)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("Failed to execute statement: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// InsertMeasuredValues into the database
|
||||
func (postgres *Postgres) InsertMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error {
|
||||
splittedMeasuredValues := make(map[string][]*types.MeasuredValue, 0)
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
if _, ok := splittedMeasuredValues[measuredValue.ValueType]; !ok {
|
||||
splittedMeasuredValues[measuredValue.ValueType] = make([]*types.MeasuredValue, 0)
|
||||
}
|
||||
splittedMeasuredValues[measuredValue.ValueType] = append(splittedMeasuredValues[measuredValue.ValueType], measuredValue)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
// General insert function
|
||||
insert := func(tx *sql.Tx, queryFile string, measuredValues []*types.MeasuredValue) error {
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
_, err := stmt.Exec(
|
||||
&measuredValue.ID,
|
||||
&measuredValue.Value,
|
||||
&measuredValue.Date,
|
||||
&measuredValue.SensorID,
|
||||
&measuredValue.CreationDate,
|
||||
&measuredValue.UpdateDate,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to execute statement: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
for measuredValueType, measuredValues := range splittedMeasuredValues {
|
||||
var queryFile string
|
||||
|
||||
switch measuredValueType {
|
||||
case "humidity":
|
||||
queryFile = "insertHumidity.sql"
|
||||
case "pressure":
|
||||
queryFile = "insertPressure.sql"
|
||||
case "temperature":
|
||||
queryFile = "insertTemperature.sql"
|
||||
default:
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("Measured value type %v not supported", measuredValueType)
|
||||
}
|
||||
|
||||
err := insert(tx, queryFile, measuredValues)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// InsertSensors into the database
|
||||
func (postgres *Postgres) InsertSensors(ctx context.Context, sensors ...*types.Sensor) error {
|
||||
queryFile := "insertSensor.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, sensor := range sensors {
|
||||
_, err = stmt.Exec(
|
||||
&sensor.ID,
|
||||
&sensor.Name,
|
||||
&sensor.Location,
|
||||
&sensor.WireID,
|
||||
&sensor.I2CBus,
|
||||
&sensor.I2CAddress,
|
||||
&sensor.GPIONumber,
|
||||
&sensor.Model,
|
||||
&sensor.Enabled,
|
||||
&sensor.TickDuration,
|
||||
&sensor.DeviceID,
|
||||
&sensor.CreationDate,
|
||||
&sensor.UpdateDate,
|
||||
)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("Failed to execute statement: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Scheme creates all required tables if not exist
|
||||
func (postgres *Postgres) Scheme(ctx context.Context) error {
|
||||
for _, query := range []string{
|
||||
postgres.queries["createTableDevices.sql"],
|
||||
postgres.queries["createTableSensors.sql"],
|
||||
postgres.queries["createTableHumidities.sql"],
|
||||
postgres.queries["createTablePressures.sql"],
|
||||
postgres.queries["createTableTemperatures.sql"],
|
||||
} {
|
||||
_, err := postgres.dbo.ExecContext(ctx, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SelectDevice from database
|
||||
func (postgres *Postgres) SelectDevice(ctx context.Context, id string) (*types.Device, error) {
|
||||
queryFile := "selectDevice.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
devices, err := postgres.selectDevices(tx, query, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(devices) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return devices[0], nil
|
||||
}
|
||||
|
||||
// SelectDevices from the database
|
||||
func (postgres *Postgres) SelectDevices(ctx context.Context) ([]*types.Device, error) {
|
||||
queryFile := "selectDevices.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
devices, err := postgres.selectDevices(tx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
||||
}
|
||||
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
func (postgres *Postgres) selectDevices(tx *sql.Tx, query string, args ...interface{}) ([]*types.Device, error) {
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
rows, err := stmt.Query(args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to query statement: %v", err)
|
||||
}
|
||||
|
||||
devices := make([]*types.Device, 0)
|
||||
for rows.Next() {
|
||||
device := new(types.Device)
|
||||
err = rows.Scan(
|
||||
&device.ID,
|
||||
&device.Name,
|
||||
&device.Location,
|
||||
&device.CreationDate,
|
||||
&device.UpdateDate,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to scan row: %v", err)
|
||||
}
|
||||
devices = append(devices, device)
|
||||
}
|
||||
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
// SelectHumidity returns humidity from the database
|
||||
func (postgres *Postgres) SelectHumidity(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
||||
queryFile := "selectHumidity.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues, err := postgres.selectMeasuredValue(tx, query, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if measuredValues == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValue.ValueType = "humidity"
|
||||
}
|
||||
|
||||
return measuredValues[0], nil
|
||||
}
|
||||
|
||||
// SelectHumidities returns humidities from the database
|
||||
func (postgres *Postgres) SelectHumidities(ctx context.Context) ([]*types.MeasuredValue, error) {
|
||||
queryFile := "selectHumidities.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues, err := postgres.selectMeasuredValue(tx, query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValue.ValueType = "humidity"
|
||||
}
|
||||
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
func (postgres *Postgres) selectMeasuredValue(tx *sql.Tx, query string, args ...interface{}) ([]*types.MeasuredValue, error) {
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
rows, err := stmt.Query(args...)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues := make([]*types.MeasuredValue, 0)
|
||||
for rows.Next() {
|
||||
measuredValue := new(types.MeasuredValue)
|
||||
err := rows.Scan(
|
||||
&measuredValue.ID,
|
||||
&measuredValue.Value,
|
||||
&measuredValue.SensorID,
|
||||
&measuredValue.Date,
|
||||
&measuredValue.CreationDate,
|
||||
&measuredValue.UpdateDate,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues = append(measuredValues, measuredValue)
|
||||
}
|
||||
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
// SelectPressure returns pressure from the database
|
||||
func (postgres *Postgres) SelectPressure(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
||||
queryFile := "selectPressure.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues, err := postgres.selectMeasuredValue(tx, query, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if measuredValues == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValue.ValueType = "pressure"
|
||||
}
|
||||
|
||||
return measuredValues[0], nil
|
||||
}
|
||||
|
||||
// SelectPressures returns pressure from the database
|
||||
func (postgres *Postgres) SelectPressures(ctx context.Context) ([]*types.MeasuredValue, error) {
|
||||
queryFile := "selectPressures.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues, err := postgres.selectMeasuredValue(tx, query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValue.ValueType = "pressure"
|
||||
}
|
||||
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
// SelectSensor from database
|
||||
func (postgres *Postgres) SelectSensor(ctx context.Context, id string) (*types.Sensor, error) {
|
||||
queryFile := "selectSensor.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
sensors, err := postgres.selectSensors(tx, query, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
||||
}
|
||||
|
||||
if len(sensors) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return sensors[0], nil
|
||||
}
|
||||
|
||||
// SelectSensors from the database
|
||||
func (postgres *Postgres) SelectSensors(ctx context.Context) ([]*types.Sensor, error) {
|
||||
queryFile := "selectSensors.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to begin new transaction: %v", err)
|
||||
}
|
||||
|
||||
sensors, err := postgres.selectSensors(tx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to commit transaction: %v", err)
|
||||
}
|
||||
|
||||
return sensors, nil
|
||||
}
|
||||
|
||||
func (postgres *Postgres) selectSensors(tx *sql.Tx, query string, args ...interface{}) ([]*types.Sensor, error) {
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
rows, err := stmt.Query(args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to query statement: %v", err)
|
||||
}
|
||||
|
||||
sensors := make([]*types.Sensor, 0)
|
||||
for rows.Next() {
|
||||
sensor := new(types.Sensor)
|
||||
err = rows.Scan(
|
||||
&sensor.ID,
|
||||
&sensor.Name,
|
||||
&sensor.Location,
|
||||
&sensor.WireID,
|
||||
&sensor.I2CBus,
|
||||
&sensor.I2CAddress,
|
||||
&sensor.GPIONumber,
|
||||
&sensor.Model,
|
||||
&sensor.Enabled,
|
||||
&sensor.TickDuration,
|
||||
&sensor.DeviceID,
|
||||
&sensor.CreationDate,
|
||||
&sensor.UpdateDate,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to scan row: %v", err)
|
||||
}
|
||||
sensors = append(sensors, sensor)
|
||||
}
|
||||
|
||||
return sensors, nil
|
||||
}
|
||||
|
||||
// SelectTemperature returns temperatures from the database
|
||||
func (postgres *Postgres) SelectTemperature(ctx context.Context, id string) (*types.MeasuredValue, error) {
|
||||
queryFile := "selectTemperature.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues, err := postgres.selectMeasuredValue(tx, query, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if measuredValues == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValue.ValueType = "temperatures"
|
||||
}
|
||||
|
||||
return measuredValues[0], nil
|
||||
}
|
||||
|
||||
// SelectTemperatures returns temperatures from the database
|
||||
func (postgres *Postgres) SelectTemperatures(ctx context.Context) ([]*types.MeasuredValue, error) {
|
||||
queryFile := "selectTemperatures.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
measuredValues, err := postgres.selectMeasuredValue(tx, query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, measuredValue := range measuredValues {
|
||||
measuredValue.ValueType = "temperatures"
|
||||
}
|
||||
|
||||
return measuredValues, nil
|
||||
}
|
||||
|
||||
// UpdateDevices updates a device in the database
|
||||
func (postgres *Postgres) UpdateDevices(ctx context.Context, devices ...*types.Device) error {
|
||||
queryFile := "updateDevice.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, device := range devices {
|
||||
now := time.Now()
|
||||
device.UpdateDate = &now
|
||||
|
||||
_, err := stmt.Exec(
|
||||
&device.Name,
|
||||
&device.Location,
|
||||
&device.CreationDate,
|
||||
&device.UpdateDate,
|
||||
&device.ID,
|
||||
)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// UpdateSensors updates a sensor in the database
|
||||
func (postgres *Postgres) UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error {
|
||||
queryFile := "updateSensor.sql"
|
||||
query, present := postgres.queries[queryFile]
|
||||
if !present {
|
||||
return fmt.Errorf("SQLite-Backend: File %v not found", queryFile)
|
||||
}
|
||||
|
||||
tx, err := postgres.dbo.BeginTx(ctx, &sql.TxOptions{ReadOnly: false})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(query)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, sensor := range sensors {
|
||||
now := time.Now()
|
||||
sensor.UpdateDate = &now
|
||||
_, err := stmt.Exec(
|
||||
&sensor.Name,
|
||||
&sensor.Location,
|
||||
&sensor.WireID,
|
||||
&sensor.I2CBus,
|
||||
&sensor.I2CAddress,
|
||||
&sensor.GPIONumber,
|
||||
&sensor.Model,
|
||||
&sensor.Enabled,
|
||||
&sensor.TickDuration,
|
||||
&sensor.DeviceID,
|
||||
&sensor.CreationDate,
|
||||
&sensor.UpdateDate,
|
||||
&sensor.ID,
|
||||
)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
7
pkg/repository/db/postgres/createTableDevices.sql
Normal file
7
pkg/repository/db/postgres/createTableDevices.sql
Normal file
@ -0,0 +1,7 @@
|
||||
CREATE TABLE IF NOT EXISTS devices (
|
||||
device_id CHAR(36) CONSTRAINT pk_devices PRIMARY KEY,
|
||||
device_name VARCHAR(64) NOT NULL,
|
||||
device_location VARCHAR(64),
|
||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP
|
||||
);
|
14
pkg/repository/db/postgres/createTableHumidities.sql
Normal file
14
pkg/repository/db/postgres/createTableHumidities.sql
Normal file
@ -0,0 +1,14 @@
|
||||
CREATE TABLE IF NOT EXISTS humidities (
|
||||
id CHAR(36) CONSTRAINT pk_humidities PRIMARY KEY,
|
||||
value NUMERIC(10,3) NOT NULL,
|
||||
date TIMESTAMP NOT NULL,
|
||||
sensor_id CHAR(36) NOT NULL,
|
||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP
|
||||
);
|
||||
|
||||
ALTER TABLE humidities
|
||||
ADD FOREIGN KEY (sensor_id)
|
||||
REFERENCES sensors(sensor_id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
14
pkg/repository/db/postgres/createTablePressures.sql
Normal file
14
pkg/repository/db/postgres/createTablePressures.sql
Normal file
@ -0,0 +1,14 @@
|
||||
CREATE TABLE IF NOT EXISTS pressures (
|
||||
id CHAR(36) CONSTRAINT pk_pressures PRIMARY KEY,
|
||||
value NUMERIC(10,3) NOT NULL,
|
||||
date TIMESTAMP NOT NULL,
|
||||
sensor_id CHAR(36) NOT NULL,
|
||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP
|
||||
);
|
||||
|
||||
ALTER TABLE pressures
|
||||
ADD FOREIGN KEY (sensor_id)
|
||||
REFERENCES sensors(sensor_id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
21
pkg/repository/db/postgres/createTableSensors.sql
Normal file
21
pkg/repository/db/postgres/createTableSensors.sql
Normal file
@ -0,0 +1,21 @@
|
||||
CREATE TABLE IF NOT EXISTS sensors (
|
||||
sensor_id CHAR(36) CONSTRAINT pk_sensors PRIMARY KEY,
|
||||
sensor_name VARCHAR(64) NOT NULL,
|
||||
sensor_location VARCHAR(64),
|
||||
wire_id VARCHAR(64),
|
||||
i2c_bus VARCHAR(255),
|
||||
i2c_address VARCHAR(12),
|
||||
gpio_number VARCHAR(6),
|
||||
sensor_model VARCHAR(16) NOT NULL,
|
||||
sensor_enabled BOOLEAN DEFAULT TRUE NOT NULL,
|
||||
tick_duration VARCHAR(6) NOT NULL,
|
||||
device_id CHAR(36) NOT NULL,
|
||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP
|
||||
);
|
||||
|
||||
ALTER TABLE sensors
|
||||
ADD FOREIGN KEY (device_id)
|
||||
REFERENCES devices(device_id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
14
pkg/repository/db/postgres/createTableTemperatures.sql
Normal file
14
pkg/repository/db/postgres/createTableTemperatures.sql
Normal file
@ -0,0 +1,14 @@
|
||||
CREATE TABLE IF NOT EXISTS temperatures (
|
||||
id CHAR(36) CONSTRAINT pk_temperatures PRIMARY KEY,
|
||||
value NUMERIC(10,3) NOT NULL,
|
||||
date TIMESTAMP NOT NULL,
|
||||
sensor_id CHAR(36) NOT NULL,
|
||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
update_date TIMESTAMP
|
||||
);
|
||||
|
||||
ALTER TABLE temperatures
|
||||
ADD FOREIGN KEY (sensor_id)
|
||||
REFERENCES sensors(sensor_id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE;
|
@ -2,6 +2,7 @@ INSERT INTO devices (
|
||||
device_id,
|
||||
device_name,
|
||||
device_location,
|
||||
creation_date
|
||||
creation_date,
|
||||
update_date
|
||||
)
|
||||
VALUES ($1, $2, $3, $4);
|
||||
VALUES ($1, $2, $3, $4, $5);
|
9
pkg/repository/db/postgres/insertHumidity.sql
Normal file
9
pkg/repository/db/postgres/insertHumidity.sql
Normal file
@ -0,0 +1,9 @@
|
||||
INSERT INTO humidities (
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6);
|
9
pkg/repository/db/postgres/insertPressure.sql
Normal file
9
pkg/repository/db/postgres/insertPressure.sql
Normal file
@ -0,0 +1,9 @@
|
||||
INSERT INTO pressures (
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6);
|
16
pkg/repository/db/postgres/insertSensor.sql
Normal file
16
pkg/repository/db/postgres/insertSensor.sql
Normal file
@ -0,0 +1,16 @@
|
||||
INSERT INTO sensors (
|
||||
sensor_id,
|
||||
sensor_name,
|
||||
sensor_location,
|
||||
wire_id,
|
||||
i2c_bus,
|
||||
i2c_address,
|
||||
gpio_number,
|
||||
sensor_model,
|
||||
sensor_enabled,
|
||||
tick_duration,
|
||||
device_id,
|
||||
creation_date,
|
||||
update_date
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13);
|
9
pkg/repository/db/postgres/insertTemperature.sql
Normal file
9
pkg/repository/db/postgres/insertTemperature.sql
Normal file
@ -0,0 +1,9 @@
|
||||
INSERT INTO temperatures (
|
||||
id,
|
||||
value,
|
||||
date,
|
||||
sensor_id,
|
||||
creation_date,
|
||||
update_date
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6);
|
@ -2,7 +2,8 @@ SELECT
|
||||
device_id,
|
||||
device_name,
|
||||
device_location,
|
||||
creation_date
|
||||
creation_date,
|
||||
update_date
|
||||
FROM
|
||||
devices
|
||||
WHERE device_id = $1;
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user