Initial commit
This commit is contained in:
commit
68cb6923c0
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${fileDirname}",
|
||||
// "args": ["-c", "/etc/activitycollect/config.json"]
|
||||
}
|
||||
]
|
||||
}
|
16
.vscode/tasks.json
vendored
Normal file
16
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||
// for the documentation about the tasks.json format
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"label": "Go build",
|
||||
"type": "shell",
|
||||
"command": "make all",
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
48
Makefile
Normal file
48
Makefile
Normal file
@ -0,0 +1,48 @@
|
||||
PROJECT_NAME := gasmeter
|
||||
|
||||
PREFIX ?= /usr/bin
|
||||
|
||||
CONFIG_DIR := /etc/$(PROJECT_NAME)
|
||||
SYSTEM_DIR := /usr/lib/systemd/system
|
||||
|
||||
CONFIG_FILE := config/config.json
|
||||
BIN_FILE := bin/$(PROJECT_NAME)
|
||||
UNIT_FILE := $(PROJECT_NAME).service
|
||||
README_FILE := README.md
|
||||
|
||||
.PHONY: all
|
||||
all:
|
||||
mkdir -p bin
|
||||
go clean
|
||||
go mod tidy
|
||||
go build -o $(BIN_FILE)
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
go clean
|
||||
rm -rf bin
|
||||
|
||||
.PHONY: install
|
||||
install: all
|
||||
@if [ -f $(CONFIG_DIR)/$(notdir $(CONFIG_FILE)) ]; then \
|
||||
echo "$(CONFIG_DIR)/$(notdir $(CONFIG_FILE)) already exists - skipping..."; \
|
||||
else \
|
||||
install -d $(CONFIG_DIR); \
|
||||
install -m 0644 $(CONFIG_FILE) $(CONFIG_DIR); \
|
||||
echo "install -d $(CONFIG_DIR)"; \
|
||||
echo "install -m 0644 $(CONFIG_FILE) $(CONFIG_DIR)"; \
|
||||
fi
|
||||
install -d $(PREFIX)
|
||||
install -m 0755 $(BIN_FILE) $(PREFIX)
|
||||
install -d $(SYSTEM_DIR)
|
||||
install -m 0644 $(UNIT_FILE) $(SYSTEM_DIR)
|
||||
|
||||
.PHONY: uninstall
|
||||
uninstall:
|
||||
rm -rf $(CONFIG_DIR)
|
||||
rm -rf $(SYSTEM_DIR)/$(UNIT_FILE)
|
||||
rm -rf $(PREFIX)/$(PROJECT_NAME)
|
||||
|
||||
.PHONY: package
|
||||
package: all
|
||||
tar cvzf $(PROJECT_NAME).tar.gz $(CONFIG_FILE) $(BIN_FILE) $(UNIT_FILE) $(README_FILE)
|
40
README.md
Normal file
40
README.md
Normal file
@ -0,0 +1,40 @@
|
||||
# Gasmeter
|
||||
|
||||
Collect volume and power data of gas meter and store them in InfluxDB
|
||||
|
||||
## Installation
|
||||
|
||||
```shell
|
||||
make install
|
||||
```
|
||||
|
||||
Default install location for the executable is `/usr/bin`. This can be
|
||||
modyfied by changing the `PREFIX` variable.
|
||||
|
||||
```shell
|
||||
PREFIX=/usr/local/bin make install
|
||||
```
|
||||
|
||||
## Uninstallation
|
||||
|
||||
```shell
|
||||
make uninstall
|
||||
```
|
||||
|
||||
When `PREFIX` was modyfied for installation it needs to be changed for
|
||||
uninstalling as well.
|
||||
|
||||
```shell
|
||||
PREFIX=/usr/local/bin make uninstall
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Default configuration file is installed in `/etc/gasmeter/config.json`
|
||||
|
||||
## systemd service
|
||||
|
||||
```shell
|
||||
systemctl enable gasmeter.service
|
||||
systemctl start gasmeter.service
|
||||
```
|
11
config/config.json
Normal file
11
config/config.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"data_storage": "/var/lib/gasmeter/data.json",
|
||||
"influxdb_host": "localhost",
|
||||
"influxdb_port": 8086,
|
||||
"influxdb_token": "",
|
||||
"trigger_level": 4500.0,
|
||||
"trigger_hysterese": 100.0,
|
||||
"step_width": 0.001,
|
||||
"zustandszahl": 0.9636,
|
||||
"brennwert_hs": 11.218
|
||||
}
|
10
gasmeter.service
Normal file
10
gasmeter.service
Normal file
@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=gasmeter service
|
||||
After=multi-user.target
|
||||
|
||||
[Service]
|
||||
Type=idle
|
||||
ExecStart=/usr/bin/gasmeter -c /etc/gasmeter/config.json
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
17
go.mod
Normal file
17
go.mod
Normal file
@ -0,0 +1,17 @@
|
||||
module chicks
|
||||
|
||||
go 1.19
|
||||
|
||||
require periph.io/x/host/v3 v3.8.0
|
||||
|
||||
require (
|
||||
github.com/influxdata/influxdb-client-go/v2 v2.12.1
|
||||
periph.io/x/conn/v3 v3.7.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/deepmap/oapi-codegen v1.8.2 // indirect
|
||||
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
|
||||
)
|
86
go.sum
Normal file
86
go.sum
Normal file
@ -0,0 +1,86 @@
|
||||
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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/deepmap/oapi-codegen v1.8.2 h1:SegyeYGcdi0jLLrpbCMoJxnUUn8GBXHsvr4rbzjuhfU=
|
||||
github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs=
|
||||
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
||||
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
|
||||
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGSh1pIozq4ZwCfxS0WA5JGXguxk94ar/4c87Y=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/influxdata/influxdb-client-go/v2 v2.12.1 h1:RrjoDNyBGFYvjKfjmtIyYAn6GY/SrtocSo4RPlt+Lng=
|
||||
github.com/influxdata/influxdb-client-go/v2 v2.12.1/go.mod h1:YteV91FiQxRdccyJ2cHvj2f/5sq4y4Njqu1fQzsQCOU=
|
||||
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU=
|
||||
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
|
||||
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
|
||||
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/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg=
|
||||
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
|
||||
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
|
||||
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
|
||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
periph.io/x/conn/v3 v3.7.0 h1:f1EXLn4pkf7AEWwkol2gilCNZ0ElY+bxS4WE2PQXfrA=
|
||||
periph.io/x/conn/v3 v3.7.0/go.mod h1:ypY7UVxgDbP9PJGwFSVelRRagxyXYfttVh7hJZUHEhg=
|
||||
periph.io/x/host/v3 v3.8.0 h1:T5ojZ2wvnZHGPS4h95N2ZpcCyHnsvH3YRZ1UUUiv5CQ=
|
||||
periph.io/x/host/v3 v3.8.0/go.mod h1:rzOLH+2g9bhc6pWZrkCrmytD4igwQ2vxFw6Wn6ZOlLY=
|
237
main.go
Normal file
237
main.go
Normal file
@ -0,0 +1,237 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"periph.io/x/conn/v3/i2c"
|
||||
"periph.io/x/conn/v3/i2c/i2creg"
|
||||
"periph.io/x/conn/v3/mmr"
|
||||
"periph.io/x/host/v3"
|
||||
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
DataStorage string `json:"data_storage"`
|
||||
InfluxdbHost string `json:"influxdb_host"`
|
||||
InfluxdbPort int `json:"influxdb_port"`
|
||||
InfluxdbToken string `json:"influxdb_token"`
|
||||
TriggerLevel float64 `json:"trigger_level"`
|
||||
TriggerHysterese float64 `json:"trigger_hysterese"`
|
||||
StepWidth float64 `json:"step_width"`
|
||||
Zustandszahl float64 `json:"zustandszahl"`
|
||||
BrennwertHS float64 `json:"brennwert_hs"`
|
||||
}
|
||||
|
||||
type data struct {
|
||||
Unit string `json:"unit"`
|
||||
Value float64 `json:"value"`
|
||||
}
|
||||
|
||||
type sample struct {
|
||||
Volume data `json:"volume"`
|
||||
Power data `json:"power"`
|
||||
}
|
||||
|
||||
var (
|
||||
logger log.Logger = *log.Default()
|
||||
config_path string
|
||||
config_cache = config{
|
||||
DataStorage: "",
|
||||
InfluxdbHost: "",
|
||||
InfluxdbPort: 0,
|
||||
InfluxdbToken: "",
|
||||
TriggerLevel: 0.0,
|
||||
TriggerHysterese: 0.0,
|
||||
StepWidth: 0.0,
|
||||
Zustandszahl: 0.0,
|
||||
BrennwertHS: 0.0,
|
||||
}
|
||||
)
|
||||
|
||||
func read_config() {
|
||||
data, err := os.ReadFile(config_path)
|
||||
if err != nil {
|
||||
logger.Printf("Unable to read %s", config_path)
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(data, &config_cache)
|
||||
if err != nil {
|
||||
logger.Print("Unable to evaluate config data")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// file, err := os.OpenFile("log.txt", os.O_WRONLY|os.O_CREATE, 0644)
|
||||
// if err != nil {
|
||||
// logger.Println(err)
|
||||
// }
|
||||
// defer file.Close()
|
||||
// logger.SetOutput(file)
|
||||
|
||||
logger.SetPrefix("Gas: ")
|
||||
logger.Println("Starting")
|
||||
|
||||
flag.StringVar(&config_path, "c", "./config/config.json", "Specify path to find the config file. Default is ./config/config.json")
|
||||
flag.Parse()
|
||||
read_config()
|
||||
|
||||
trigger_state := false
|
||||
old_state := false
|
||||
|
||||
var gas sample
|
||||
|
||||
// Create data storage directory
|
||||
dir := path.Dir(config_cache.DataStorage)
|
||||
err := os.MkdirAll(dir, os.ModePerm)
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
// Try reading stored value
|
||||
data, err := os.ReadFile(config_cache.DataStorage)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
gas.Volume.Unit = "m³"
|
||||
gas.Volume.Value = 0.0
|
||||
gas.Power.Unit = "kW/h"
|
||||
gas.Power.Value = 0.0
|
||||
} else {
|
||||
err = json.Unmarshal(data, &gas)
|
||||
if err != nil {
|
||||
logger.Print(err)
|
||||
}
|
||||
}
|
||||
last_value := gas.Volume.Value
|
||||
|
||||
// prepare influx connection
|
||||
influxdb_url := "http://" + config_cache.InfluxdbHost + ":" + strconv.Itoa(config_cache.InfluxdbPort)
|
||||
client := influxdb2.NewClient(influxdb_url, config_cache.InfluxdbToken)
|
||||
// always close client at the end
|
||||
defer client.Close()
|
||||
|
||||
// get non-blocking write client
|
||||
writeAPI := client.WriteAPIBlocking("tkl", "home")
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Int I2C system
|
||||
if _, err := host.Init(); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
bus, err := i2creg.Open("")
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
defer bus.Close()
|
||||
i2c_dev := i2c.Dev{
|
||||
Bus: bus,
|
||||
Addr: 0x0d,
|
||||
}
|
||||
mem_dev := mmr.Dev8{
|
||||
Conn: &i2c_dev,
|
||||
Order: binary.BigEndian,
|
||||
}
|
||||
err = mem_dev.WriteUint8(0x09, 0x1d)
|
||||
if err != nil {
|
||||
logger.Fatal("unable to write to reg 0x09")
|
||||
}
|
||||
|
||||
for { // ever
|
||||
var raw [6]uint8
|
||||
var i uint8
|
||||
for i = 0; i < 6; i++ {
|
||||
res, err := mem_dev.ReadUint8(i)
|
||||
if err != nil {
|
||||
logger.Print("unable to read from reg 0")
|
||||
continue
|
||||
}
|
||||
raw[i] = res
|
||||
}
|
||||
var mag_x int16
|
||||
mag_x = int16(raw[0])
|
||||
mag_x |= int16(raw[1]) << 8
|
||||
fmag_x := float64(mag_x)
|
||||
|
||||
var mag_y int16
|
||||
mag_y = int16(raw[2])
|
||||
mag_y |= int16(raw[3]) << 8
|
||||
fmag_y := float64(mag_y)
|
||||
|
||||
var mag_z int16
|
||||
mag_z = int16(raw[4])
|
||||
mag_z |= int16(raw[5]) << 8
|
||||
fmag_z := float64(mag_z)
|
||||
|
||||
mag := math.Sqrt(fmag_x*fmag_x + fmag_y*fmag_y + fmag_z*fmag_z)
|
||||
logger.Printf("mag: %f\n\n", mag)
|
||||
|
||||
old_state = trigger_state
|
||||
if mag > config_cache.TriggerLevel+config_cache.TriggerHysterese {
|
||||
trigger_state = true
|
||||
} else if mag < config_cache.TriggerLevel-config_cache.TriggerHysterese {
|
||||
trigger_state = false
|
||||
}
|
||||
|
||||
if !old_state && trigger_state {
|
||||
gas.Volume.Value += config_cache.StepWidth
|
||||
}
|
||||
logger.Printf("Volume: %f %s\n", gas.Volume.Value, gas.Volume.Unit)
|
||||
gas.Power.Value = gas.Volume.Value * config_cache.BrennwertHS * config_cache.Zustandszahl
|
||||
logger.Printf("Power: %f %s\n", gas.Power.Value, gas.Power.Unit)
|
||||
|
||||
if last_value != gas.Volume.Value {
|
||||
last_value = gas.Volume.Value
|
||||
|
||||
file, err := os.OpenFile(config_cache.DataStorage, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
} else {
|
||||
res, err := json.Marshal(gas)
|
||||
if err != nil {
|
||||
logger.Print(err)
|
||||
} else {
|
||||
file.Write(res)
|
||||
}
|
||||
file.Close()
|
||||
}
|
||||
|
||||
point := influxdb2.NewPointWithMeasurement("gas")
|
||||
point.AddTag("sensor", "gasmeter")
|
||||
point.AddField("volume", gas.Volume.Value)
|
||||
point.AddField("unit", gas.Volume.Unit)
|
||||
|
||||
point.SetTime(time.Now())
|
||||
err = writeAPI.WritePoint(ctx, point)
|
||||
if err != nil {
|
||||
logger.Print(err.Error())
|
||||
}
|
||||
point = influxdb2.NewPointWithMeasurement("gas")
|
||||
point.AddTag("sensor", "gasmeter")
|
||||
point.AddField("power", gas.Power.Value)
|
||||
point.AddField("unit", gas.Power.Unit)
|
||||
|
||||
point.SetTime(time.Now())
|
||||
err = writeAPI.WritePoint(ctx, point)
|
||||
if err != nil {
|
||||
logger.Print(err.Error())
|
||||
}
|
||||
err = writeAPI.Flush(ctx)
|
||||
if err != nil {
|
||||
logger.Print(err.Error())
|
||||
continue
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Millisecond * 1000)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user