Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e700239764 | |||
| e8153e2953 | |||
| d72e88ab7b | |||
| 7d0ce52c27 | |||
| d3df73ae0f | |||
| afc71dc507 | |||
| 22a42bfef0 | |||
| ff0c806efa | |||
| ed326eb8ef | |||
| 1b6989b5d9 | |||
| e995a252e1 | |||
| e17e4d1a0a | |||
| ee403d1e35 | |||
| bc49047499 | |||
| 1c15ff5911 | |||
| bdcb8e6f73 | |||
| 5cc8a0d7db | |||
| a31a0b4829 | |||
|
|
959d1e3c1f | ||
|
|
bda5aa9238 | ||
|
|
4e5da3f322 | ||
|
|
826576516f | ||
|
|
933c7cc307 | ||
|
|
9471f0b5ac | ||
|
|
4d774937df | ||
|
|
3702654ec3 | ||
|
|
7d63422555 | ||
|
|
fa6fc5fb34 | ||
|
|
86d865ce1b | ||
|
|
516dd6b611 | ||
|
|
df421fcccb | ||
|
|
a050189e12 | ||
|
|
7bcdc3276f | ||
|
|
9f9e25a796 | ||
|
|
c4a095a094 | ||
|
|
bf4716e8df | ||
|
|
574e832152 | ||
|
|
2a84799832 | ||
|
|
8adf3c8261 | ||
|
|
b3245aba9b | ||
|
|
dac2149fbd | ||
|
|
e501f6d125 | ||
|
|
341f26f197 | ||
|
|
5e28643a76 | ||
|
|
b35132451c | ||
|
|
6b0aeae504 | ||
|
|
950c77886a | ||
|
|
51221d37d0 | ||
|
|
d02baa4e8d | ||
|
|
41b3db0457 | ||
|
|
b6c488b7d4 | ||
|
|
c4b09b7a14 | ||
|
|
e201779341 | ||
|
|
321d19c5c2 | ||
|
|
56c417fd0e | ||
|
|
d3576031bb | ||
|
|
f6c3b38976 |
202
.drone.yml
Normal file
202
.drone.yml
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: ci
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
event:
|
||||||
|
- pull_request
|
||||||
|
- push
|
||||||
|
- tag
|
||||||
|
|
||||||
|
steps:
|
||||||
|
# - name: lint
|
||||||
|
# image: cache.coadcorp.com/library/golang:1.26
|
||||||
|
# environment:
|
||||||
|
# GOFLAGS: -mod=mod
|
||||||
|
# GOBIN: /usr/local/bin
|
||||||
|
# commands:
|
||||||
|
# - go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.64.8
|
||||||
|
# - golangci-lint version
|
||||||
|
# - golangci-lint run --timeout=5m
|
||||||
|
|
||||||
|
# - name: test
|
||||||
|
# image: cache.coadcorp.com/library/golang:1.26
|
||||||
|
# environment:
|
||||||
|
# GOFLAGS: -mod=mod
|
||||||
|
# commands:
|
||||||
|
# - go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
|
||||||
|
|
||||||
|
- name: build
|
||||||
|
image: cache.coadcorp.com/library/golang:1.26
|
||||||
|
environment:
|
||||||
|
GOFLAGS: -mod=mod
|
||||||
|
commands:
|
||||||
|
- CGO_ENABLED=0 go build -v ./cmd/invertergui
|
||||||
|
|
||||||
|
- name: build-linux-binaries
|
||||||
|
image: cache.coadcorp.com/library/golang:1.26
|
||||||
|
environment:
|
||||||
|
GOFLAGS: -mod=mod
|
||||||
|
commands:
|
||||||
|
- mkdir -p dist
|
||||||
|
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o dist/invertergui-linux-amd64 ./cmd/invertergui
|
||||||
|
- CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -v -o dist/invertergui-linux-arm64 ./cmd/invertergui
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
- tag
|
||||||
|
|
||||||
|
- name: docker-build-validate
|
||||||
|
image: gcr.io/kaniko-project/executor:v1.24.0
|
||||||
|
commands:
|
||||||
|
- /kaniko/executor --context "${DRONE_WORKSPACE}" --dockerfile "${DRONE_WORKSPACE}/Dockerfile" --no-push --destination registry.coadcorp.com/nathan/invertergui:pr-${DRONE_BUILD_NUMBER}
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- pull_request
|
||||||
|
|
||||||
|
- name: docker-publish-commit-amd64
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
repo: "registry.coadcorp.com/nathan/invertergui"
|
||||||
|
dockerfile: Dockerfile.publish.amd64
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
tags:
|
||||||
|
- "${DRONE_COMMIT_SHA}-linux-amd64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
|
||||||
|
- name: docker-publish-commit-arm64
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
repo: "registry.coadcorp.com/nathan/invertergui"
|
||||||
|
dockerfile: Dockerfile.publish.arm64
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
tags:
|
||||||
|
- "${DRONE_COMMIT_SHA}-linux-arm64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
|
||||||
|
- name: docker-manifest-commit
|
||||||
|
image: plugins/manifest
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
target: "registry.coadcorp.com/nathan/invertergui:${DRONE_COMMIT_SHA}"
|
||||||
|
template: "registry.coadcorp.com/nathan/invertergui:${DRONE_COMMIT_SHA}-OS-ARCH"
|
||||||
|
platforms:
|
||||||
|
- "linux/amd64"
|
||||||
|
- "linux/arm64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
|
||||||
|
- name: docker-publish-latest-amd64
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
repo: "registry.coadcorp.com/nathan/invertergui"
|
||||||
|
dockerfile: Dockerfile.publish.amd64
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
tags:
|
||||||
|
- "latest-linux-amd64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
branch:
|
||||||
|
- main
|
||||||
|
- master
|
||||||
|
|
||||||
|
- name: docker-publish-latest-arm64
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
repo: "registry.coadcorp.com/nathan/invertergui"
|
||||||
|
dockerfile: Dockerfile.publish.arm64
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
tags:
|
||||||
|
- "latest-linux-arm64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
branch:
|
||||||
|
- main
|
||||||
|
- master
|
||||||
|
|
||||||
|
- name: docker-manifest-latest
|
||||||
|
image: plugins/manifest
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
target: "registry.coadcorp.com/nathan/invertergui:latest"
|
||||||
|
template: "registry.coadcorp.com/nathan/invertergui:latest-OS-ARCH"
|
||||||
|
platforms:
|
||||||
|
- "linux/amd64"
|
||||||
|
- "linux/arm64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
branch:
|
||||||
|
- main
|
||||||
|
- master
|
||||||
|
|
||||||
|
- name: docker-publish-release-amd64
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
repo: "registry.coadcorp.com/nathan/invertergui"
|
||||||
|
dockerfile: Dockerfile.publish.amd64
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
tags:
|
||||||
|
- "${DRONE_TAG}-linux-amd64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- tag
|
||||||
|
|
||||||
|
- name: docker-publish-release-arm64
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
repo: "registry.coadcorp.com/nathan/invertergui"
|
||||||
|
dockerfile: Dockerfile.publish.arm64
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
tags:
|
||||||
|
- "${DRONE_TAG}-linux-arm64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- tag
|
||||||
|
|
||||||
|
- name: docker-manifest-release
|
||||||
|
image: plugins/manifest
|
||||||
|
settings:
|
||||||
|
registry: registry.coadcorp.com
|
||||||
|
username: nathan
|
||||||
|
password:
|
||||||
|
from_secret: registry_password
|
||||||
|
target: "registry.coadcorp.com/nathan/invertergui:${DRONE_TAG}"
|
||||||
|
template: "registry.coadcorp.com/nathan/invertergui:${DRONE_TAG}-OS-ARCH"
|
||||||
|
platforms:
|
||||||
|
- "linux/amd64"
|
||||||
|
- "linux/arm64"
|
||||||
|
when:
|
||||||
|
event:
|
||||||
|
- tag
|
||||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -22,3 +22,9 @@ _testmain.go
|
|||||||
*.exe
|
*.exe
|
||||||
*.test
|
*.test
|
||||||
*.prof
|
*.prof
|
||||||
|
|
||||||
|
vendor/
|
||||||
|
|
||||||
|
# Python cache files (for Home Assistant custom component)
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
run:
|
|
||||||
deadline: 10m
|
|
||||||
|
|
||||||
linters:
|
|
||||||
enable-all: false
|
|
||||||
enable:
|
|
||||||
# Enabled by default in golangci-lint v1.13.2
|
|
||||||
- deadcode
|
|
||||||
- errcheck
|
|
||||||
- govet
|
|
||||||
- ineffassign
|
|
||||||
- typecheck
|
|
||||||
- varcheck
|
|
||||||
# Disabled by default in golangci-lint v1.13.2
|
|
||||||
- dupl
|
|
||||||
- goconst
|
|
||||||
- gofmt
|
|
||||||
- golint
|
|
||||||
- unconvert
|
|
||||||
# The following result in high memory usage (>1GB)
|
|
||||||
# https://github.com/golangci/golangci-lint/issues/337
|
|
||||||
- staticcheck
|
|
||||||
- structcheck
|
|
||||||
- gosimple
|
|
||||||
- unused
|
|
||||||
|
|
||||||
issues:
|
|
||||||
max-per-linter: 0
|
|
||||||
max-same-issues: 0
|
|
||||||
28
.travis.yml
28
.travis.yml
@@ -1,28 +0,0 @@
|
|||||||
sudo: false
|
|
||||||
|
|
||||||
language: go
|
|
||||||
|
|
||||||
env:
|
|
||||||
- GO111MODULE=on
|
|
||||||
|
|
||||||
go:
|
|
||||||
- 1.14.x
|
|
||||||
|
|
||||||
git:
|
|
||||||
depth: 1
|
|
||||||
|
|
||||||
install: true
|
|
||||||
|
|
||||||
notifications:
|
|
||||||
email: false
|
|
||||||
|
|
||||||
before_script:
|
|
||||||
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.26.0
|
|
||||||
- go mod vendor
|
|
||||||
|
|
||||||
script:
|
|
||||||
- golangci-lint run
|
|
||||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
|
|
||||||
|
|
||||||
after_success:
|
|
||||||
- bash <(curl -s https://codecov.io/bash)
|
|
||||||
10
.vscode/settings.json
vendored
10
.vscode/settings.json
vendored
@@ -10,5 +10,13 @@
|
|||||||
"[go]": {
|
"[go]": {
|
||||||
"editor.insertSpaces": false,
|
"editor.insertSpaces": false,
|
||||||
"editor.tabSize": 4
|
"editor.tabSize": 4
|
||||||
}
|
},
|
||||||
|
"cSpell.words": [
|
||||||
|
"diebietse",
|
||||||
|
"ghaction",
|
||||||
|
"ghcr",
|
||||||
|
"golangci",
|
||||||
|
"invertergui",
|
||||||
|
"semver"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
32
Dockerfile
32
Dockerfile
@@ -1,14 +1,24 @@
|
|||||||
FROM golang:alpine as builder
|
ARG BUILDPLATFORM=linux/amd64
|
||||||
RUN apk add git
|
FROM --platform=${BUILDPLATFORM} golang:1.26-alpine as builder
|
||||||
|
|
||||||
|
ARG TARGETOS=linux
|
||||||
|
ARG TARGETARCH=amd64
|
||||||
|
ARG TARGETVARIANT
|
||||||
|
|
||||||
RUN mkdir /build
|
RUN mkdir /build
|
||||||
COPY . /build/
|
COPY . /build/
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
RUN go build -o invertergui ./cmd/invertergui
|
RUN set -eux; \
|
||||||
FROM alpine
|
GOARM=""; \
|
||||||
RUN adduser -S -D -H -h /app inverteruser
|
if [ "${TARGETARCH}" = "arm" ] && [ -n "${TARGETVARIANT:-}" ]; then GOARM="${TARGETVARIANT#v}"; fi; \
|
||||||
RUN addgroup inverteruser dialout
|
CGO_ENABLED=0 GOOS="${TARGETOS}" GOARCH="${TARGETARCH}" GOARM="${GOARM}" go build -o invertergui ./cmd/invertergui
|
||||||
USER inverteruser
|
|
||||||
COPY --from=builder /build/invertergui /app/
|
FROM scratch
|
||||||
WORKDIR /app
|
|
||||||
ENTRYPOINT [ "./invertergui" ]
|
# Group ID 20 is dialout, needed for tty read/write access
|
||||||
CMD []
|
USER 3000:20
|
||||||
|
ENV READ_ONLY=false
|
||||||
|
ENV CONTROL_ALLOWED_PANEL_MODES=""
|
||||||
|
COPY --from=builder /build/invertergui /bin/
|
||||||
|
ENTRYPOINT [ "/bin/invertergui" ]
|
||||||
|
EXPOSE 8080
|
||||||
|
|||||||
9
Dockerfile.publish.amd64
Normal file
9
Dockerfile.publish.amd64
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
FROM scratch
|
||||||
|
|
||||||
|
# Group ID 20 is dialout, needed for tty read/write access
|
||||||
|
USER 3000:20
|
||||||
|
ENV READ_ONLY=false
|
||||||
|
ENV CONTROL_ALLOWED_PANEL_MODES=""
|
||||||
|
COPY dist/invertergui-linux-amd64 /bin/invertergui
|
||||||
|
ENTRYPOINT ["/bin/invertergui"]
|
||||||
|
EXPOSE 8080
|
||||||
9
Dockerfile.publish.arm64
Normal file
9
Dockerfile.publish.arm64
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
FROM scratch
|
||||||
|
|
||||||
|
# Group ID 20 is dialout, needed for tty read/write access
|
||||||
|
USER 3000:20
|
||||||
|
ENV READ_ONLY=false
|
||||||
|
ENV CONTROL_ALLOWED_PANEL_MODES=""
|
||||||
|
COPY dist/invertergui-linux-arm64 /bin/invertergui
|
||||||
|
ENTRYPOINT ["/bin/invertergui"]
|
||||||
|
EXPOSE 8080
|
||||||
9
Makefile
9
Makefile
@@ -44,13 +44,10 @@ test:
|
|||||||
docker:
|
docker:
|
||||||
docker build --tag invertergui .
|
docker build --tag invertergui .
|
||||||
|
|
||||||
statik:
|
|
||||||
statik -f -p=frontend -src=./frontend/root
|
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
docker run --rm -it \
|
docker run --rm -it \
|
||||||
-w /src -v $(shell pwd):/src \
|
-w /src -v $(shell pwd):/src \
|
||||||
golangci/golangci-lint:v1.26 golangci-lint run \
|
golangci/golangci-lint:v1.56 golangci-lint run \
|
||||||
-v -c .golangci.yml
|
-v -c .golangci.yml
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@@ -59,3 +56,7 @@ clean:
|
|||||||
vendor:
|
vendor:
|
||||||
go mod tidy
|
go mod tidy
|
||||||
go mod vendor
|
go mod vendor
|
||||||
|
|
||||||
|
.PHONY: upgrade-vendor
|
||||||
|
upgrade-vendor:
|
||||||
|
go get -u ./...
|
||||||
|
|||||||
436
README.md
436
README.md
@@ -1,24 +1,83 @@
|
|||||||
# Inverter GUI
|
# Inverter GUI
|
||||||
|
|
||||||
[](https://travis-ci.org/diebietse/invertergui)
|
[Repository](https://git.coadcorp.com/nathan/invertergui) | [Container Image](https://registry.coadcorp.com/nathan/invertergui)
|
||||||
|
|
||||||
The invertergui allows the monitoring of a [Victron Multiplus](https://www.victronenergy.com/inverters-chargers/multiplus-12v-24v-48v-800va-3kva) via the [MK3/MK2 USB](https://www.victronenergy.com/accessories/interface-mk3-usb) or the MK2 RS232.
|
The invertergui allows the monitoring of a [Victron Multiplus](https://www.victronenergy.com/inverters-chargers/multiplus-12v-24v-48v-800va-3kva) via the [MK3/MK2 USB](https://www.victronenergy.com/accessories/interface-mk3-usb) or the MK2 RS232.
|
||||||
|
|
||||||
The [`diebietse/invertergui`](https://hub.docker.com/r/diebietse/invertergui) docker image is a build of this repository.
|
The [`registry.coadcorp.com/nathan/invertergui`](https://registry.coadcorp.com/nathan/invertergui) container image is a build of this repository.
|
||||||
|
|
||||||
|
The code has been updated to support more of the protocol published by Victron at https://www.victronenergy.com/upload/documents/Technical-Information-Interfacing-with-VE-Bus-products-MK2-Protocol-3-14.pdf
|
||||||
|
|
||||||
|
## Acknowledgements
|
||||||
|
|
||||||
|
This project is based on the original open source `invertergui` project by Hendrik van Wyk and contributors:
|
||||||
|
|
||||||
|
- Original repository: https://github.com/diebietse/invertergui
|
||||||
|
- Home Assistant `victron-mk3-hass` inspiration: https://github.com/j9brown/victron-mk3-hass
|
||||||
|
|
||||||
## Demo
|
## Demo
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
```console
|
```console
|
||||||
docker run --name invertergui --device /dev/ttyUSB0:/dev/ttyUSB0 -p 8080:8080 diebietse/invertergui
|
docker run --name invertergui --device /dev/ttyUSB0:/dev/ttyUSB0 -p 8080:8080 registry.coadcorp.com/nathan/invertergui:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
This project makes use of [Go Modules](https://github.com/golang/go/wiki/Modules). The minimum version for Go is 1.11.x.
|
This project makes use of [Go Modules](https://github.com/golang/go/wiki/Modules). The minimum supported version for Go is 1.22
|
||||||
|
|
||||||
|
## Driver API: Metadata + Safe Transactions
|
||||||
|
|
||||||
|
The MK2 driver now includes a metadata and transaction safety layer via the
|
||||||
|
`mk2driver.MetadataControl` interface:
|
||||||
|
|
||||||
|
- Register metadata lookup (`RegisterMetadata`, `ListRegisterMetadata`)
|
||||||
|
- Generic register reads by kind/id (`ReadRegister`)
|
||||||
|
- Transactional writes with retry and verify (`WriteRegister`)
|
||||||
|
|
||||||
|
`WriteRegister` supports:
|
||||||
|
|
||||||
|
- `ReadBeforeWrite`
|
||||||
|
- `VerifyAfterWrite`
|
||||||
|
- configurable retry count and delay
|
||||||
|
|
||||||
|
This layer is additive and does not replace existing `WriteSetting`, `WriteRAMVar`,
|
||||||
|
or panel control APIs.
|
||||||
|
|
||||||
|
## Advanced Control + Orchestration Features
|
||||||
|
|
||||||
|
The codebase now includes:
|
||||||
|
|
||||||
|
- Full MK2 command-path coverage in driver APIs for:
|
||||||
|
- `0x0E` device state read/write
|
||||||
|
- register read by id (`0x30`, `0x31`)
|
||||||
|
- selected read/write flows (`0x32`, `0x33`, `0x34`, `0x35`)
|
||||||
|
- RAM var metadata/info (`0x36`)
|
||||||
|
- write-by-id flows (`0x37`, `0x38`)
|
||||||
|
- Register metadata with `unit`, `min/max`, `scale`, `writable`, and `safety_class`.
|
||||||
|
- Transaction-safe writes with read-before-write, verify-after-write, retry/backoff, and timeout classes.
|
||||||
|
- Snapshot/diff/restore register workflows with rollback on partial restore failure.
|
||||||
|
- Alarm engine with LED/state alarms + command-failure alarms, including debounce and clear behavior.
|
||||||
|
- Venus-like derived operating state model: `Off`, `Inverter`, `Charger`, `Passthru`, `Fault`.
|
||||||
|
- Historical counters for energy and availability:
|
||||||
|
- `energy_in_wh`, `energy_out_wh`
|
||||||
|
- battery charge/discharge Wh
|
||||||
|
- uptime seconds
|
||||||
|
- fault count + last fault timestamp
|
||||||
|
- Multi-device orchestration fields and topics:
|
||||||
|
- `device_id`, `instance_id`, `phase`, `phase_group`
|
||||||
|
- per-device topics and phase-group fanout topics
|
||||||
|
- Command arbitration/policy layer:
|
||||||
|
- single serialized write path
|
||||||
|
- lockout windows
|
||||||
|
- source tagging (`ui`, `mqtt`, `automation`)
|
||||||
|
- max current guardrail + mode rate limit + maintenance/read-only profiles
|
||||||
|
- Venus-compatible MQTT mode (`N/...` + optional `W/...`) for HA/Node-RED/VRM-style workflows.
|
||||||
|
- Guide-style Victron ESS MQTT paths (`settings/0/Settings/CGwacs/*`) with `victron/N/...` and `victron/W/...` prefix compatibility.
|
||||||
|
- Structured diagnostics bundle topics with protocol traces, recent command history, and health score.
|
||||||
|
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
@@ -28,22 +87,109 @@ Usage:
|
|||||||
|
|
||||||
Application Options:
|
Application Options:
|
||||||
--address= The IP/DNS and port of the machine that the application is running on. (default: :8080) [$ADDRESS]
|
--address= The IP/DNS and port of the machine that the application is running on. (default: :8080) [$ADDRESS]
|
||||||
|
--read_only Disable all write operations and run in monitoring-only mode. [$READ_ONLY]
|
||||||
--data.source= Set the source of data for the inverter gui. "serial", "tcp" or "mock" (default: serial) [$DATA_SOURCE]
|
--data.source= Set the source of data for the inverter gui. "serial", "tcp" or "mock" (default: serial) [$DATA_SOURCE]
|
||||||
--data.host= Host to connect when source is set to tcp. (default: localhost:8139) [$DATA_HOST]
|
--data.host= Host to connect when source is set to tcp. (default: localhost:8139) [$DATA_HOST]
|
||||||
--data.device= TTY device to use when source is set to serial. (default: /dev/ttyUSB0) [$DATA_DEVICE]
|
--data.device= TTY device to use when source is set to serial. (default: /dev/ttyUSB0) [$DATA_DEVICE]
|
||||||
--cli.enabled Enable CLI output. [$CLI_ENABLED]
|
--cli.enabled Enable CLI output. [$CLI_ENABLED]
|
||||||
--mqtt.enabled Enable MQTT publishing. [$MQTT_ENABLED]
|
--mqtt.enabled Enable MQTT publishing. [$MQTT_ENABLED]
|
||||||
--mqtt.broker= Set the host port and scheme of the MQTT broker. (default: tcp://localhost:1883) [$MQTT_BROKER]
|
--mqtt.broker= Set the host port and scheme of the MQTT broker. (default: tcp://localhost:1883) [$MQTT_BROKER]
|
||||||
--mqtt.client_id= Set the client ID for the MQTT connection. (default: interter-gui) [$MQTT_CLIENT_ID]
|
--mqtt.client_id= Set the client ID for the MQTT connection. (default: inverter-gui) [$MQTT_CLIENT_ID]
|
||||||
--mqtt.topic= Set the MQTT topic updates published to. (default: invertergui/updates) [$MQTT_TOPIC]
|
--mqtt.topic= Set the MQTT topic updates published to. (default: invertergui/updates) [$MQTT_TOPIC]
|
||||||
|
--mqtt.command_topic= Set the MQTT topic that receives write commands for Victron settings/RAM variables. (default: invertergui/settings/set) [$MQTT_COMMAND_TOPIC]
|
||||||
|
--mqtt.status_topic= Set the MQTT topic where write command status updates are published. (default: invertergui/settings/status) [$MQTT_STATUS_TOPIC]
|
||||||
|
--mqtt.device_id= Set logical device ID used for per-device orchestration topics. (default: invertergui) [$MQTT_DEVICE_ID]
|
||||||
|
--mqtt.history_size= Number of samples retained for rolling history summaries. (default: 120) [$MQTT_HISTORY_SIZE]
|
||||||
|
--mqtt.instance_id= Device instance ID for multi-device orchestration and Venus compatibility. (default: 0) [$MQTT_INSTANCE_ID]
|
||||||
|
--mqtt.phase= Electrical phase label for this instance (L1/L2/L3). (default: L1) [$MQTT_PHASE]
|
||||||
|
--mqtt.phase_group= Grouping key for parallel/3-phase system aggregation topics. (default: default) [$MQTT_PHASE_GROUP]
|
||||||
|
--mqtt.ha.enabled Enable Home Assistant MQTT discovery integration. [$MQTT_HA_ENABLED]
|
||||||
|
--mqtt.ha.discovery_prefix= Set Home Assistant MQTT discovery prefix. (default: homeassistant) [$MQTT_HA_DISCOVERY_PREFIX]
|
||||||
|
--mqtt.ha.node_id= Set Home Assistant node ID used for discovery topics and unique IDs. (default: invertergui) [$MQTT_HA_NODE_ID]
|
||||||
|
--mqtt.ha.device_name= Set Home Assistant device display name. (default: Victron Inverter) [$MQTT_HA_DEVICE_NAME]
|
||||||
|
--mqtt.venus.enabled Enable Venus-style MQTT compatibility topics (N/W model). [$MQTT_VENUS_ENABLED]
|
||||||
|
--mqtt.venus.portal_id= Set Venus portal ID segment used in N/W topics. (default: invertergui) [$MQTT_VENUS_PORTAL_ID]
|
||||||
|
--mqtt.venus.service= Set Venus service segment used in N/W topics. (default: vebus/257) [$MQTT_VENUS_SERVICE]
|
||||||
|
--mqtt.venus.subscribe_writes Subscribe to Venus W/... topics and map to MK2 commands. [$MQTT_VENUS_SUBSCRIBE_WRITES]
|
||||||
|
--mqtt.venus.topic_prefix= Optional topic prefix before Venus N/W topics, e.g. victron. [$MQTT_VENUS_TOPIC_PREFIX]
|
||||||
|
--mqtt.venus.guide_compat Enable guide-style settings/0/Settings/CGwacs compatibility paths. [$MQTT_VENUS_GUIDE_COMPAT]
|
||||||
--mqtt.username= Set the MQTT username [$MQTT_USERNAME]
|
--mqtt.username= Set the MQTT username [$MQTT_USERNAME]
|
||||||
--mqtt.password= Set the MQTT password [$MQTT_PASSWORD]
|
--mqtt.password= Set the MQTT password [$MQTT_PASSWORD]
|
||||||
|
--mqtt.password-file= Path to a file containing the MQTT password [$MQTT_PASSWORD_FILE]
|
||||||
|
--control.profile= Write policy profile: normal, maintenance, or read_only. (default: normal) [$CONTROL_PROFILE]
|
||||||
|
--control.max_current_limit= Optional max AC current limit guardrail in amps (0 disables). (default: 0) [$CONTROL_MAX_CURRENT_LIMIT]
|
||||||
|
--control.mode_change_min_interval= Minimum time between mode changes. (default: 3s) [$CONTROL_MODE_CHANGE_MIN_INTERVAL]
|
||||||
|
--control.lockout_window= Post-command lockout window for command arbitration. (default: 0s) [$CONTROL_LOCKOUT_WINDOW]
|
||||||
|
--control.allowed_panel_modes= Comma-separated allowlist of remote panel modes. Supported values: on, off, charger_only, inverter_only. Empty allows all modes. [$CONTROL_ALLOWED_PANEL_MODES]
|
||||||
--loglevel= The log level to generate logs at. ("panic", "fatal", "error", "warn", "info", "debug", "trace") (default: info) [$LOGLEVEL]
|
--loglevel= The log level to generate logs at. ("panic", "fatal", "error", "warn", "info", "debug", "trace") (default: info) [$LOGLEVEL]
|
||||||
|
|
||||||
Help Options:
|
Help Options:
|
||||||
-h, --help Show this help message
|
-h, --help Show this help message
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Read-Only Mode
|
||||||
|
|
||||||
|
Set `READ_ONLY=true` (or `--read_only`) to disable all write operations.
|
||||||
|
When read-only mode is enabled, the app still monitors and publishes telemetry, but it will not send commands to the Victron device.
|
||||||
|
|
||||||
|
This affects:
|
||||||
|
|
||||||
|
- MQTT command handling (`--mqtt.command_topic` commands are ignored)
|
||||||
|
- Web UI control actions (`POST /api/remote-panel/state` and `POST /api/remote-panel/standby`)
|
||||||
|
|
||||||
|
### Remote Panel Mode Allowlist
|
||||||
|
|
||||||
|
Set `CONTROL_ALLOWED_PANEL_MODES` (or `--control.allowed_panel_modes`) to restrict which
|
||||||
|
remote panel modes can be selected from Web UI, MQTT, Home Assistant, and Venus-compatible
|
||||||
|
write paths.
|
||||||
|
|
||||||
|
Supported values:
|
||||||
|
|
||||||
|
- `on`
|
||||||
|
- `off`
|
||||||
|
- `charger_only`
|
||||||
|
- `inverter_only`
|
||||||
|
|
||||||
|
Use a comma-separated list. Empty means all modes are allowed.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
- `CONTROL_ALLOWED_PANEL_MODES=off,charger_only`
|
||||||
|
|
||||||
|
Example `docker-compose.yml` snippet:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
services:
|
||||||
|
invertergui:
|
||||||
|
image: registry.coadcorp.com/nathan/invertergui:latest
|
||||||
|
environment:
|
||||||
|
READ_ONLY: "true"
|
||||||
|
CONTROL_ALLOWED_PANEL_MODES: "off,charger_only"
|
||||||
|
devices:
|
||||||
|
- "/dev/ttyUSB0:/dev/ttyUSB0"
|
||||||
|
command: ["--mqtt.enabled", "--mqtt.broker=tcp://192.168.1.1:1883", "--loglevel=info"]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Home Assistant Guide-Style ESS Control
|
||||||
|
|
||||||
|
To mimic the Victron community ESS control approach (MQTT `N/...` state + `W/...` writes under `settings/0/Settings/CGwacs/...`):
|
||||||
|
|
||||||
|
1. Start invertergui with:
|
||||||
|
- `MQTT_VENUS_ENABLED=true`
|
||||||
|
- `MQTT_VENUS_GUIDE_COMPAT=true`
|
||||||
|
- `MQTT_VENUS_TOPIC_PREFIX=victron`
|
||||||
|
- `MQTT_VENUS_PORTAL_ID=invertergui` (or your chosen portal id)
|
||||||
|
2. In Home Assistant:
|
||||||
|
- Use the included custom integration (`custom_components/victron_mk2_mqtt`) which now exposes ESS-style entities and service calls.
|
||||||
|
- Or use `homeassistant/packages/invertergui_mqtt.yaml`, which includes guide-style MQTT entities:
|
||||||
|
- `number.victron_ess_grid_setpoint`
|
||||||
|
- `number.victron_ess_max_charge_power`
|
||||||
|
- `number.victron_ess_max_discharge_power`
|
||||||
|
- `switch.victron_ess_optimized_mode`
|
||||||
|
|
||||||
|
Compatibility note:
|
||||||
|
- MK2/VE.Bus does not expose every Venus ESS feature one-to-one. This project maps ESS-style commands onto available MK2 controls (mode/current-limit/policy-safe behavior) to provide similar Home Assistant control flow.
|
||||||
|
|
||||||
## Port 8080
|
## Port 8080
|
||||||
|
|
||||||
The default HTTP server port is hosted on port 8080. This exposes the HTTP server that hosts the:
|
The default HTTP server port is hosted on port 8080. This exposes the HTTP server that hosts the:
|
||||||
@@ -79,6 +225,20 @@ Battery Power: -0.659 W
|
|||||||
Battery Charge: 100.000 %
|
Battery Charge: 100.000 %
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The web UI also includes a **Remote Panel Control** section for:
|
||||||
|
|
||||||
|
- Remote Panel Mode (`on`, `off`, `charger_only`, `inverter_only`)
|
||||||
|
- Remote Panel Current Limit (AC input current limit in amps)
|
||||||
|
- Remote Panel Standby (prevent sleep while turned off)
|
||||||
|
|
||||||
|
The combined mode + current limit action maps to the same behavior as
|
||||||
|
`set_remote_panel_state` in `victron-mk3`.
|
||||||
|
|
||||||
|
The backing HTTP API endpoints are:
|
||||||
|
|
||||||
|
- `GET/POST /api/remote-panel/state`
|
||||||
|
- `GET/POST /api/remote-panel/standby`
|
||||||
|
|
||||||
### Munin
|
### Munin
|
||||||
|
|
||||||
The Munin plugin location is at /munin (http://localhost:8080/munin).
|
The Munin plugin location is at /munin (http://localhost:8080/munin).
|
||||||
@@ -278,16 +438,276 @@ The MQTT client will publish updates to the given broker at the set topic.
|
|||||||
```bash
|
```bash
|
||||||
--mqtt.enabled Enable MQTT publishing. [$MQTT_ENABLED]
|
--mqtt.enabled Enable MQTT publishing. [$MQTT_ENABLED]
|
||||||
--mqtt.broker= Set the host port and scheme of the MQTT broker. (default: tcp://localhost:1883) [$MQTT_BROKER]
|
--mqtt.broker= Set the host port and scheme of the MQTT broker. (default: tcp://localhost:1883) [$MQTT_BROKER]
|
||||||
--mqtt.client_id= Set the client ID for the MQTT connection. (default: interter-gui) [$MQTT_CLIENT_ID]
|
--mqtt.client_id= Set the client ID for the MQTT connection. (default: inverter-gui) [$MQTT_CLIENT_ID]
|
||||||
--mqtt.topic= Set the MQTT topic updates published to. (default: invertergui/updates) [$MQTT_TOPIC]
|
--mqtt.topic= Set the MQTT topic updates published to. (default: invertergui/updates) [$MQTT_TOPIC]
|
||||||
|
--mqtt.command_topic= Set the MQTT topic that receives write commands for Victron settings/RAM variables. (default: invertergui/settings/set) [$MQTT_COMMAND_TOPIC]
|
||||||
|
--mqtt.status_topic= Set the MQTT topic where write command status updates are published. (default: invertergui/settings/status) [$MQTT_STATUS_TOPIC]
|
||||||
|
--mqtt.device_id= Set logical device ID used for per-device orchestration topics. (default: invertergui) [$MQTT_DEVICE_ID]
|
||||||
|
--mqtt.history_size= Number of samples retained for rolling history summaries. (default: 120) [$MQTT_HISTORY_SIZE]
|
||||||
|
--mqtt.ha.enabled Enable Home Assistant MQTT discovery integration. [$MQTT_HA_ENABLED]
|
||||||
|
--mqtt.ha.discovery_prefix= Set Home Assistant MQTT discovery prefix. (default: homeassistant) [$MQTT_HA_DISCOVERY_PREFIX]
|
||||||
|
--mqtt.ha.node_id= Set Home Assistant node ID used for discovery topics and unique IDs. (default: invertergui) [$MQTT_HA_NODE_ID]
|
||||||
|
--mqtt.ha.device_name= Set Home Assistant device display name. (default: Victron Inverter) [$MQTT_HA_DEVICE_NAME]
|
||||||
|
--mqtt.venus.enabled Enable Venus-style MQTT compatibility topics (N/W model). [$MQTT_VENUS_ENABLED]
|
||||||
|
--mqtt.venus.portal_id= Set Venus portal ID segment used in N/W topics. (default: invertergui) [$MQTT_VENUS_PORTAL_ID]
|
||||||
|
--mqtt.venus.service= Set Venus service segment used in N/W topics. (default: vebus/257) [$MQTT_VENUS_SERVICE]
|
||||||
|
--mqtt.venus.subscribe_writes Subscribe to Venus W/... topics and map to MK2 commands. [$MQTT_VENUS_SUBSCRIBE_WRITES]
|
||||||
--mqtt.username= Set the MQTT username [$MQTT_USERNAME]
|
--mqtt.username= Set the MQTT username [$MQTT_USERNAME]
|
||||||
--mqtt.password= Set the MQTT password [$MQTT_PASSWORD]
|
--mqtt.password= Set the MQTT password [$MQTT_PASSWORD]
|
||||||
|
--mqtt.password-file= Path to a file containing the MQTT password [$MQTT_PASSWORD_FILE]
|
||||||
|
```
|
||||||
|
|
||||||
|
Related global option:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
--read_only Disable all write operations and run in monitoring-only mode. [$READ_ONLY]
|
||||||
```
|
```
|
||||||
|
|
||||||
The MQTT client can be enabled by setting the environment variable `MQTT_ENABLED=true` or flag `--mqtt.enabled`.
|
The MQTT client can be enabled by setting the environment variable `MQTT_ENABLED=true` or flag `--mqtt.enabled`.
|
||||||
All MQTT configuration can be done via flags or as environment variables.
|
All MQTT configuration can be done via flags or as environment variables.
|
||||||
The URI for the broker can be configured format should be `scheme://host:port`, where "scheme" is one of "tcp", "ssl", or "ws".
|
The URI for the broker can be configured format should be `scheme://host:port`, where "scheme" is one of "tcp", "ssl", or "ws".
|
||||||
|
|
||||||
|
When `--mqtt.command_topic` is configured, the application subscribes to that topic and accepts JSON write commands.
|
||||||
|
The recommended command for inverter control follows the same model used by `victron-mk3`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"request_id": "optional-correlation-id",
|
||||||
|
"kind": "panel_state",
|
||||||
|
"switch": "on",
|
||||||
|
"current_limit": 16.5
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`switch` supports `charger_only`, `inverter_only`, `on`, and `off` (or numeric values `1..4`).
|
||||||
|
`current_limit` is in amps and optional. If omitted, only the switch state is changed.
|
||||||
|
To update only the current limit (while preserving the last known mode), send:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"kind": "panel_state",
|
||||||
|
"current_limit": 12.0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If no prior mode is known (for example on a fresh broker state), this command is rejected until a mode command is sent once.
|
||||||
|
|
||||||
|
Standby can be controlled with:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"kind": "standby",
|
||||||
|
"standby": true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Low-level writes are still supported:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"kind": "setting",
|
||||||
|
"id": 15,
|
||||||
|
"value": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`kind` supports `panel_state`, `setting`, and `ram_var` (with aliases for each).
|
||||||
|
The result is published to `--mqtt.status_topic` with status `ok` or `error`.
|
||||||
|
|
||||||
|
### MQTT Device Orchestration Topics
|
||||||
|
|
||||||
|
For multi-device deployments on one MQTT broker, `invertergui` now also publishes
|
||||||
|
device-scoped orchestration topics under:
|
||||||
|
|
||||||
|
- `{topic-root}/devices/{device_id}/state`
|
||||||
|
- `{topic-root}/devices/{device_id}/history/summary`
|
||||||
|
- `{topic-root}/devices/{device_id}/alarms/active`
|
||||||
|
|
||||||
|
Set `--mqtt.device_id` (or `MQTT_DEVICE_ID`) per inverter instance so each instance
|
||||||
|
publishes to a unique device path.
|
||||||
|
|
||||||
|
Rolling history depth for the summary window is set by `--mqtt.history_size`.
|
||||||
|
|
||||||
|
### Venus-Style MQTT Compatibility
|
||||||
|
|
||||||
|
Enable Venus-compatible topics with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
--mqtt.venus.enabled
|
||||||
|
```
|
||||||
|
|
||||||
|
When enabled, `invertergui` publishes Venus-style notifications to:
|
||||||
|
|
||||||
|
- `N/{portal_id}/{service}/...`
|
||||||
|
|
||||||
|
Defaults:
|
||||||
|
|
||||||
|
- `portal_id`: `invertergui` (`--mqtt.venus.portal_id`)
|
||||||
|
- `service`: `vebus/257` (`--mqtt.venus.service`)
|
||||||
|
|
||||||
|
Published paths include common VE.Bus style values such as:
|
||||||
|
|
||||||
|
- `Dc/0/Voltage`, `Dc/0/Current`, `Dc/0/Power`, `Soc`
|
||||||
|
- `Ac/ActiveIn/L1/V`, `Ac/ActiveIn/L1/I`, `Ac/ActiveIn/L1/F`, `Ac/ActiveIn/L1/P`
|
||||||
|
- `Ac/Out/L1/V`, `Ac/Out/L1/I`, `Ac/Out/L1/F`, `Ac/Out/L1/P`
|
||||||
|
- alarm paths (`Alarms/LowBattery`, `Alarms/HighTemperature`, `Alarms/Overload`, `Alarms/Communication`)
|
||||||
|
|
||||||
|
Optional write compatibility can be enabled with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
--mqtt.venus.subscribe_writes
|
||||||
|
```
|
||||||
|
|
||||||
|
This subscribes to:
|
||||||
|
|
||||||
|
- `W/{portal_id}/{service}/#`
|
||||||
|
|
||||||
|
and maps supported write paths to MK2 control operations:
|
||||||
|
|
||||||
|
- `Mode` -> remote panel mode
|
||||||
|
- `Ac/ActiveIn/CurrentLimit` -> remote panel current limit
|
||||||
|
- `Settings/Standby` / `RemotePanel/Standby` -> standby control
|
||||||
|
|
||||||
|
### Home Assistant
|
||||||
|
|
||||||
|
Enable Home Assistant auto-discovery with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
--mqtt.ha.enabled
|
||||||
|
```
|
||||||
|
|
||||||
|
When enabled, `invertergui` publishes retained discovery payloads and availability under:
|
||||||
|
|
||||||
|
- `{topic-root}/homeassistant/availability` (`online`/`offline`)
|
||||||
|
- `{discovery_prefix}/sensor/{node_id}/.../config`
|
||||||
|
- `{discovery_prefix}/binary_sensor/{node_id}/.../config`
|
||||||
|
- `{discovery_prefix}/select/{node_id}/remote_panel_mode/config` (if command topic is configured)
|
||||||
|
- `{discovery_prefix}/number/{node_id}/remote_panel_current_limit/config` (if command topic is configured)
|
||||||
|
- `{discovery_prefix}/switch/{node_id}/remote_panel_standby/config` (if command topic is configured)
|
||||||
|
|
||||||
|
The discovered entities include battery/input/output sensors, a data-valid diagnostic binary sensor,
|
||||||
|
plus remote panel controls for:
|
||||||
|
|
||||||
|
- `Remote Panel Mode` (`on`, `off`, `charger_only`, `inverter_only`)
|
||||||
|
- `Remote Panel Current Limit` (AC input current limit in amps)
|
||||||
|
- `Remote Panel Standby` (prevent device sleep while off)
|
||||||
|
|
||||||
|
The combined mode + current limit behavior is provided through the `panel_state` MQTT command kind,
|
||||||
|
which mirrors `victron_mk3.set_remote_panel_state`.
|
||||||
|
|
||||||
|
### Home Assistant Custom Component (MQTT)
|
||||||
|
|
||||||
|
This repository also includes a custom Home Assistant integration at:
|
||||||
|
|
||||||
|
- `custom_components/victron_mk2_mqtt`
|
||||||
|
|
||||||
|
This component is useful if you want HA entities/services that are explicitly tied to
|
||||||
|
`invertergui` MQTT topics, instead of relying only on MQTT auto-discovery entities.
|
||||||
|
|
||||||
|
If you use this custom component, you can disable `--mqtt.ha.enabled` in `invertergui`
|
||||||
|
to avoid duplicate entities created by MQTT discovery.
|
||||||
|
|
||||||
|
Install via HACS:
|
||||||
|
|
||||||
|
1. Add the integration repository in Home Assistant:
|
||||||
|
[](https://my.home-assistant.io/redirect/hacs_repository/?owner=nathan&repository=invertergui&category=integration)
|
||||||
|
2. Install `Victron MK2 MQTT` from HACS.
|
||||||
|
3. Restart Home Assistant.
|
||||||
|
4. Add the YAML configuration shown below.
|
||||||
|
|
||||||
|
If you are not mirroring this repo to GitHub, use the manual install method below.
|
||||||
|
|
||||||
|
Manual install (alternative):
|
||||||
|
|
||||||
|
```text
|
||||||
|
<home-assistant-config>/custom_components/victron_mk2_mqtt
|
||||||
|
```
|
||||||
|
|
||||||
|
Then add YAML config:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
victron_mk2_mqtt:
|
||||||
|
name: Victron Inverter
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
command_topic: invertergui/settings/set
|
||||||
|
status_topic: invertergui/settings/status
|
||||||
|
# topic_root is optional; defaults to state_topic root (for example "invertergui")
|
||||||
|
# topic_root: invertergui
|
||||||
|
```
|
||||||
|
|
||||||
|
Provided entities include:
|
||||||
|
|
||||||
|
- Telemetry sensors (battery/input/output voltage/current/frequency and derived power)
|
||||||
|
- `Remote Panel Mode` (`charger_only`, `inverter_only`, `on`, `off`)
|
||||||
|
- `Remote Panel Current Limit` (A)
|
||||||
|
- `Remote Panel Standby`
|
||||||
|
- Diagnostic entities (`Data Valid`, `Last Command Error`)
|
||||||
|
|
||||||
|
Service exposed by the integration:
|
||||||
|
|
||||||
|
- `victron_mk2_mqtt.set_remote_panel_state`
|
||||||
|
|
||||||
|
Example service call:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
service: victron_mk2_mqtt.set_remote_panel_state
|
||||||
|
data:
|
||||||
|
mode: on
|
||||||
|
current_limit: 16.0
|
||||||
|
```
|
||||||
|
|
||||||
|
### Home Assistant MQTT-Only Dashboard (No Duplicate Entities)
|
||||||
|
|
||||||
|
If you want the same control/telemetry experience but only via MQTT (without duplicate
|
||||||
|
entities from discovery/custom integrations), use the packaged Home Assistant files:
|
||||||
|
|
||||||
|
- MQTT entity + control package: `homeassistant/packages/invertergui_mqtt.yaml`
|
||||||
|
- Lovelace dashboard: `homeassistant/dashboards/invertergui_mqtt_dashboard.yaml`
|
||||||
|
|
||||||
|
The package assumes default topics (`invertergui/updates`, `invertergui/settings/set`,
|
||||||
|
`invertergui/settings/status`). If you use custom MQTT topics, update those values in
|
||||||
|
`homeassistant/packages/invertergui_mqtt.yaml`.
|
||||||
|
|
||||||
|
Recommended for this mode:
|
||||||
|
|
||||||
|
- Disable MQTT discovery output from `invertergui` (`--mqtt.ha.enabled=false`)
|
||||||
|
- Do not enable the `victron_mk2_mqtt` custom component at the same time
|
||||||
|
|
||||||
|
1. Ensure HA packages are enabled (if not already):
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
homeassistant:
|
||||||
|
packages: !include_dir_named packages
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Copy package file to your HA config:
|
||||||
|
|
||||||
|
```text
|
||||||
|
<home-assistant-config>/packages/invertergui_mqtt.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Copy dashboard file to your HA config:
|
||||||
|
|
||||||
|
```text
|
||||||
|
<home-assistant-config>/dashboards/invertergui_mqtt_dashboard.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Register the dashboard (YAML mode example):
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
lovelace:
|
||||||
|
mode: storage
|
||||||
|
dashboards:
|
||||||
|
invertergui-victron:
|
||||||
|
mode: yaml
|
||||||
|
title: Victron MQTT
|
||||||
|
icon: mdi:flash
|
||||||
|
show_in_sidebar: true
|
||||||
|
filename: dashboards/invertergui_mqtt_dashboard.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
5. Restart Home Assistant.
|
||||||
|
|
||||||
## TTY Device
|
## TTY Device
|
||||||
|
|
||||||
The intertergui application makes use of a serial tty device to monitor the Multiplus.
|
The intertergui application makes use of a serial tty device to monitor the Multiplus.
|
||||||
@@ -349,4 +769,4 @@ The last four lines are optional, but is useful when debugging and logging conne
|
|||||||
|
|
||||||
This repos includes a [Grafana](https://grafana.com/) dashboard in the [grafana folder](./grafana/prometheus-dashboard.json) that you can import. This is useful if you are using prometheus to log your data and want to display it in a nice way.
|
This repos includes a [Grafana](https://grafana.com/) dashboard in the [grafana folder](./grafana/prometheus-dashboard.json) that you can import. This is useful if you are using prometheus to log your data and want to display it in a nice way.
|
||||||
|
|
||||||

|

|
||||||
|
|||||||
@@ -1,12 +1,25 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
Address string `long:"address" env:"ADDRESS" default:":8080" description:"The IP/DNS and port of the machine that the application is running on."`
|
Address string `long:"address" env:"ADDRESS" default:":8080" description:"The IP/DNS and port of the machine that the application is running on."`
|
||||||
Data struct {
|
ReadOnly bool `long:"read_only" env:"READ_ONLY" description:"Disable all write operations and run in monitoring-only mode."`
|
||||||
|
Control struct {
|
||||||
|
Profile string `long:"control.profile" env:"CONTROL_PROFILE" default:"normal" description:"Write policy profile: normal, maintenance, or read_only."`
|
||||||
|
MaxCurrentLimit float64 `long:"control.max_current_limit" env:"CONTROL_MAX_CURRENT_LIMIT" default:"0" description:"Optional max AC current limit guardrail in amps (0 disables)."`
|
||||||
|
ModeChangeMinInterval time.Duration `long:"control.mode_change_min_interval" env:"CONTROL_MODE_CHANGE_MIN_INTERVAL" default:"3s" description:"Minimum time between mode changes."`
|
||||||
|
LockoutWindow time.Duration `long:"control.lockout_window" env:"CONTROL_LOCKOUT_WINDOW" default:"0s" description:"Post-command lockout window for command arbitration."`
|
||||||
|
AllowedPanelModes string `long:"control.allowed_panel_modes" env:"CONTROL_ALLOWED_PANEL_MODES" default:"" description:"Comma-separated allowlist of remote panel modes. Supported values: on, off, charger_only, inverter_only. Empty allows all modes."`
|
||||||
|
}
|
||||||
|
Data struct {
|
||||||
Source string `long:"data.source" env:"DATA_SOURCE" default:"serial" description:"Set the source of data for the inverter gui. \"serial\", \"tcp\" or \"mock\""`
|
Source string `long:"data.source" env:"DATA_SOURCE" default:"serial" description:"Set the source of data for the inverter gui. \"serial\", \"tcp\" or \"mock\""`
|
||||||
Host string `long:"data.host" env:"DATA_HOST" default:"localhost:8139" description:"Host to connect when source is set to tcp."`
|
Host string `long:"data.host" env:"DATA_HOST" default:"localhost:8139" description:"Host to connect when source is set to tcp."`
|
||||||
Device string `long:"data.device" env:"DATA_DEVICE" default:"/dev/ttyUSB0" description:"TTY device to use when source is set to serial."`
|
Device string `long:"data.device" env:"DATA_DEVICE" default:"/dev/ttyUSB0" description:"TTY device to use when source is set to serial."`
|
||||||
@@ -15,21 +28,71 @@ type config struct {
|
|||||||
Enabled bool `long:"cli.enabled" env:"CLI_ENABLED" description:"Enable CLI output."`
|
Enabled bool `long:"cli.enabled" env:"CLI_ENABLED" description:"Enable CLI output."`
|
||||||
}
|
}
|
||||||
MQTT struct {
|
MQTT struct {
|
||||||
Enabled bool `long:"mqtt.enabled" env:"MQTT_ENABLED" description:"Enable MQTT publishing."`
|
Enabled bool `long:"mqtt.enabled" env:"MQTT_ENABLED" description:"Enable MQTT publishing."`
|
||||||
Broker string `long:"mqtt.broker" env:"MQTT_BROKER" default:"tcp://localhost:1883" description:"Set the host port and scheme of the MQTT broker."`
|
Broker string `long:"mqtt.broker" env:"MQTT_BROKER" default:"tcp://localhost:1883" description:"Set the host port and scheme of the MQTT broker."`
|
||||||
ClientID string `long:"mqtt.client_id" env:"MQTT_CLIENT_ID" default:"interter-gui" description:"Set the client ID for the MQTT connection."`
|
ClientID string `long:"mqtt.client_id" env:"MQTT_CLIENT_ID" default:"inverter-gui" description:"Set the client ID for the MQTT connection."`
|
||||||
Topic string `long:"mqtt.topic" env:"MQTT_TOPIC" default:"invertergui/updates" description:"Set the MQTT topic updates published to."`
|
Topic string `long:"mqtt.topic" env:"MQTT_TOPIC" default:"invertergui/updates" description:"Set the MQTT topic updates published to."`
|
||||||
Username string `long:"mqtt.username" env:"MQTT_USERNAME" default:"" description:"Set the MQTT username"`
|
CommandTopic string `long:"mqtt.command_topic" env:"MQTT_COMMAND_TOPIC" default:"invertergui/settings/set" description:"Set the MQTT topic that receives write commands for Victron settings/RAM variables."`
|
||||||
Password string `long:"mqtt.password" env:"MQTT_PASSWORD" default:"" description:"Set the MQTT password"`
|
StatusTopic string `long:"mqtt.status_topic" env:"MQTT_STATUS_TOPIC" default:"invertergui/settings/status" description:"Set the MQTT topic where write command status updates are published."`
|
||||||
|
DeviceID string `long:"mqtt.device_id" env:"MQTT_DEVICE_ID" default:"invertergui" description:"Set the logical device ID used for per-device orchestration topics."`
|
||||||
|
HistorySize int `long:"mqtt.history_size" env:"MQTT_HISTORY_SIZE" default:"120" description:"Number of telemetry samples retained for rolling history summaries."`
|
||||||
|
InstanceID int `long:"mqtt.instance_id" env:"MQTT_INSTANCE_ID" default:"0" description:"Device instance ID for multi-device orchestration and Venus compatibility."`
|
||||||
|
Phase string `long:"mqtt.phase" env:"MQTT_PHASE" default:"L1" description:"Electrical phase label for this instance (L1/L2/L3)."`
|
||||||
|
PhaseGroup string `long:"mqtt.phase_group" env:"MQTT_PHASE_GROUP" default:"default" description:"Grouping key for parallel/3-phase system aggregation topics."`
|
||||||
|
HA struct {
|
||||||
|
Enabled bool `long:"mqtt.ha.enabled" env:"MQTT_HA_ENABLED" description:"Enable Home Assistant MQTT discovery integration."`
|
||||||
|
DiscoveryPrefix string `long:"mqtt.ha.discovery_prefix" env:"MQTT_HA_DISCOVERY_PREFIX" default:"homeassistant" description:"Set Home Assistant MQTT discovery prefix."`
|
||||||
|
NodeID string `long:"mqtt.ha.node_id" env:"MQTT_HA_NODE_ID" default:"invertergui" description:"Set Home Assistant node ID used for discovery topics and unique IDs."`
|
||||||
|
DeviceName string `long:"mqtt.ha.device_name" env:"MQTT_HA_DEVICE_NAME" default:"Victron Inverter" description:"Set Home Assistant device display name."`
|
||||||
|
}
|
||||||
|
Venus struct {
|
||||||
|
Enabled bool `long:"mqtt.venus.enabled" env:"MQTT_VENUS_ENABLED" description:"Enable Venus-style MQTT compatibility topics (N/W topic model)."`
|
||||||
|
PortalID string `long:"mqtt.venus.portal_id" env:"MQTT_VENUS_PORTAL_ID" default:"invertergui" description:"Set Venus portal ID segment used in N/W topics."`
|
||||||
|
Service string `long:"mqtt.venus.service" env:"MQTT_VENUS_SERVICE" default:"vebus/257" description:"Set Venus service segment used in N/W topics."`
|
||||||
|
SubscribeWrites bool `long:"mqtt.venus.subscribe_writes" env:"MQTT_VENUS_SUBSCRIBE_WRITES" description:"Subscribe to Venus write topics and map them to MK2 control commands."`
|
||||||
|
TopicPrefix string `long:"mqtt.venus.topic_prefix" env:"MQTT_VENUS_TOPIC_PREFIX" default:"" description:"Optional topic prefix before Venus N/W topics, for example 'victron'."`
|
||||||
|
GuideCompat bool `long:"mqtt.venus.guide_compat" env:"MQTT_VENUS_GUIDE_COMPAT" description:"Enable guide-style settings/0/Settings/CGwacs compatibility paths for Home Assistant controls."`
|
||||||
|
}
|
||||||
|
Username string `long:"mqtt.username" env:"MQTT_USERNAME" default:"" description:"Set the MQTT username"`
|
||||||
|
Password string `long:"mqtt.password" env:"MQTT_PASSWORD" default:"" description:"Set the MQTT password"`
|
||||||
|
PasswordFile string `long:"mqtt.password-file" env:"MQTT_PASSWORD_FILE" default:"" description:"Path to a file containing the MQTT password"`
|
||||||
}
|
}
|
||||||
Loglevel string `long:"loglevel" env:"LOGLEVEL" default:"info" description:"The log level to generate logs at. (\"panic\", \"fatal\", \"error\", \"warn\", \"info\", \"debug\", \"trace\")"`
|
Loglevel string `long:"loglevel" env:"LOGLEVEL" default:"info" description:"The log level to generate logs at. (\"panic\", \"fatal\", \"error\", \"warn\", \"info\", \"debug\", \"trace\")"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig() (*config, error) {
|
func parseConfig() (*config, error) {
|
||||||
conf := &config{}
|
conf := &config{}
|
||||||
|
// go-flags bool options cannot use struct-tag defaults; keep intended behavior via struct initialization.
|
||||||
|
conf.MQTT.Venus.SubscribeWrites = true
|
||||||
|
conf.MQTT.Venus.GuideCompat = true
|
||||||
parser := flags.NewParser(conf, flags.Default)
|
parser := flags.NewParser(conf, flags.Default)
|
||||||
if _, err := parser.Parse(); err != nil {
|
if _, err := parser.Parse(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := resolvePasswordFile(conf); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolvePasswordFile(conf *config) error {
|
||||||
|
if conf.MQTT.PasswordFile != "" && conf.MQTT.Password != "" {
|
||||||
|
return fmt.Errorf("mqtt.password and mqtt.password-file are mutually exclusive")
|
||||||
|
}
|
||||||
|
if conf.MQTT.PasswordFile != "" {
|
||||||
|
password, err := readPasswordFile(conf.MQTT.PasswordFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
conf.MQTT.Password = password
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readPasswordFile(path string) (string, error) {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not read MQTT password file: %w", err)
|
||||||
|
}
|
||||||
|
return strings.TrimRight(string(data), "\n\r"), nil
|
||||||
|
}
|
||||||
|
|||||||
107
cmd/invertergui/config_test.go
Normal file
107
cmd/invertergui/config_test.go
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const testInlineSecret = "inline-secret"
|
||||||
|
|
||||||
|
func TestReadPasswordFile(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
content string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "plain password",
|
||||||
|
content: "secret",
|
||||||
|
expected: "secret",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "password with trailing newline",
|
||||||
|
content: "secret\n",
|
||||||
|
expected: "secret",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "password with trailing carriage return and newline",
|
||||||
|
content: "secret\r\n",
|
||||||
|
expected: "secret",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty file",
|
||||||
|
content: "",
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "password")
|
||||||
|
if err := os.WriteFile(path, []byte(tt.content), 0o600); err != nil {
|
||||||
|
t.Fatalf("failed to write test file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := readPasswordFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if got != tt.expected {
|
||||||
|
t.Errorf("got %q, want %q", got, tt.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadPasswordFile_NotFound(t *testing.T) {
|
||||||
|
_, err := readPasswordFile("/nonexistent/path/password")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for missing file, got nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolvePassword_MutuallyExclusive(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "password")
|
||||||
|
if err := os.WriteFile(path, []byte("secret"), 0o600); err != nil {
|
||||||
|
t.Fatalf("failed to write test file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conf := &config{}
|
||||||
|
conf.MQTT.Password = testInlineSecret
|
||||||
|
conf.MQTT.PasswordFile = path
|
||||||
|
|
||||||
|
err := resolvePasswordFile(conf)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error when both mqtt.password and mqtt.password-file are set, got nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolvePassword_FromFile(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "password")
|
||||||
|
if err := os.WriteFile(path, []byte("file-secret\n"), 0o600); err != nil {
|
||||||
|
t.Fatalf("failed to write test file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conf := &config{}
|
||||||
|
conf.MQTT.PasswordFile = path
|
||||||
|
|
||||||
|
if err := resolvePasswordFile(conf); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if conf.MQTT.Password != "file-secret" {
|
||||||
|
t.Errorf("got %q, want %q", conf.MQTT.Password, "file-secret")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolvePassword_NoFile(t *testing.T) {
|
||||||
|
conf := &config{}
|
||||||
|
conf.MQTT.Password = testInlineSecret
|
||||||
|
|
||||||
|
if err := resolvePasswordFile(conf); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if conf.MQTT.Password != testInlineSecret {
|
||||||
|
t.Errorf("got %q, want %q", conf.MQTT.Password, testInlineSecret)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,15 +36,17 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/diebietse/invertergui/mk2core"
|
"git.coadcorp.com/nathan/invertergui/mk2core"
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
"github.com/diebietse/invertergui/plugins/cli"
|
"git.coadcorp.com/nathan/invertergui/plugins/cli"
|
||||||
"github.com/diebietse/invertergui/plugins/mqttclient"
|
"git.coadcorp.com/nathan/invertergui/plugins/mqttclient"
|
||||||
"github.com/diebietse/invertergui/plugins/munin"
|
"git.coadcorp.com/nathan/invertergui/plugins/munin"
|
||||||
"github.com/diebietse/invertergui/plugins/prometheus"
|
"git.coadcorp.com/nathan/invertergui/plugins/prometheus"
|
||||||
"github.com/diebietse/invertergui/plugins/webui"
|
"git.coadcorp.com/nathan/invertergui/plugins/webui"
|
||||||
"github.com/diebietse/invertergui/plugins/webui/static"
|
"git.coadcorp.com/nathan/invertergui/plugins/webui/static"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/tarm/serial"
|
"github.com/tarm/serial"
|
||||||
@@ -55,6 +57,7 @@ var log = logrus.WithField("ctx", "inverter-gui")
|
|||||||
func main() {
|
func main() {
|
||||||
conf, err := parseConfig()
|
conf, err := parseConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.WithError(err).Error("Could not parse configuration")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
log.Info("Starting invertergui")
|
log.Info("Starting invertergui")
|
||||||
@@ -63,45 +66,158 @@ func main() {
|
|||||||
log.Fatalf("Could not parse log level: %v", err)
|
log.Fatalf("Could not parse log level: %v", err)
|
||||||
}
|
}
|
||||||
logrus.SetLevel(logLevel)
|
logrus.SetLevel(logLevel)
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"loglevel": conf.Loglevel,
|
||||||
|
"address": conf.Address,
|
||||||
|
"read_only": conf.ReadOnly,
|
||||||
|
"data_source": conf.Data.Source,
|
||||||
|
"data_host": conf.Data.Host,
|
||||||
|
"data_device": conf.Data.Device,
|
||||||
|
"cli_enabled": conf.Cli.Enabled,
|
||||||
|
"mqtt_enabled": conf.MQTT.Enabled,
|
||||||
|
"mqtt_broker": conf.MQTT.Broker,
|
||||||
|
"mqtt_topic": conf.MQTT.Topic,
|
||||||
|
"mqtt_command_topic": conf.MQTT.CommandTopic,
|
||||||
|
"mqtt_status_topic": conf.MQTT.StatusTopic,
|
||||||
|
"mqtt_device_id": conf.MQTT.DeviceID,
|
||||||
|
"mqtt_history_size": conf.MQTT.HistorySize,
|
||||||
|
"mqtt_ha_enabled": conf.MQTT.HA.Enabled,
|
||||||
|
"mqtt_venus_enabled": conf.MQTT.Venus.Enabled,
|
||||||
|
"mqtt_venus_portal": conf.MQTT.Venus.PortalID,
|
||||||
|
"mqtt_venus_service": conf.MQTT.Venus.Service,
|
||||||
|
"mqtt_venus_prefix": conf.MQTT.Venus.TopicPrefix,
|
||||||
|
"mqtt_venus_guide": conf.MQTT.Venus.GuideCompat,
|
||||||
|
"control_profile": conf.Control.Profile,
|
||||||
|
"control_modes": conf.Control.AllowedPanelModes,
|
||||||
|
}).Info("Configuration loaded")
|
||||||
|
|
||||||
mk2, err := getMk2Device(conf.Data.Source, conf.Data.Host, conf.Data.Device)
|
mk2, err := getMk2Device(conf.Data.Source, conf.Data.Host, conf.Data.Device)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not open data source: %v", err)
|
log.Fatalf("Could not open data source: %v", err)
|
||||||
}
|
}
|
||||||
defer mk2.Close()
|
defer mk2.Close()
|
||||||
|
log.Info("MK2 device connection established")
|
||||||
|
|
||||||
core := mk2core.NewCore(mk2)
|
core := mk2core.NewCore(mk2)
|
||||||
|
|
||||||
if conf.Cli.Enabled {
|
if conf.Cli.Enabled {
|
||||||
|
log.Info("CLI plugin enabled")
|
||||||
cli.NewCli(core.NewSubscription())
|
cli.NewCli(core.NewSubscription())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Webgui
|
// Webgui
|
||||||
gui := webui.NewWebGui(core.NewSubscription())
|
var writer mk2driver.SettingsWriter
|
||||||
|
if w, ok := mk2.(mk2driver.SettingsWriter); ok {
|
||||||
|
writer = w
|
||||||
|
log.Info("MK2 data source supports settings writes")
|
||||||
|
} else {
|
||||||
|
log.Warn("MK2 data source does not support settings writes")
|
||||||
|
}
|
||||||
|
if conf.ReadOnly {
|
||||||
|
if writer != nil {
|
||||||
|
log.Warn("READ_ONLY enabled; disabling all write operations")
|
||||||
|
} else {
|
||||||
|
log.Info("READ_ONLY enabled")
|
||||||
|
}
|
||||||
|
writer = nil
|
||||||
|
} else if writer != nil {
|
||||||
|
allowedPanelStates, allowedPanelModeNames, parseErr := parseAllowedPanelModes(conf.Control.AllowedPanelModes)
|
||||||
|
if parseErr != nil {
|
||||||
|
log.WithError(parseErr).Fatal("Invalid control.allowed_panel_modes configuration")
|
||||||
|
}
|
||||||
|
|
||||||
|
policyProfile := mk2driver.WriterProfile(strings.ToLower(strings.TrimSpace(conf.Control.Profile)))
|
||||||
|
if policyProfile == "" {
|
||||||
|
policyProfile = mk2driver.WriterProfileNormal
|
||||||
|
}
|
||||||
|
if policyProfile != mk2driver.WriterProfileNormal &&
|
||||||
|
policyProfile != mk2driver.WriterProfileMaintenance &&
|
||||||
|
policyProfile != mk2driver.WriterProfileReadOnly {
|
||||||
|
log.WithField("profile", conf.Control.Profile).Warn("Unknown control profile; defaulting to normal")
|
||||||
|
policyProfile = mk2driver.WriterProfileNormal
|
||||||
|
}
|
||||||
|
|
||||||
|
var maxCurrentLimit *float64
|
||||||
|
if conf.Control.MaxCurrentLimit > 0 {
|
||||||
|
limit := conf.Control.MaxCurrentLimit
|
||||||
|
maxCurrentLimit = &limit
|
||||||
|
}
|
||||||
|
|
||||||
|
writer = mk2driver.NewManagedWriter(writer, mk2driver.WriterPolicy{
|
||||||
|
Profile: policyProfile,
|
||||||
|
MaxCurrentLimitA: maxCurrentLimit,
|
||||||
|
ModeChangeMinInterval: conf.Control.ModeChangeMinInterval,
|
||||||
|
LockoutWindow: conf.Control.LockoutWindow,
|
||||||
|
AllowedPanelStates: allowedPanelStates,
|
||||||
|
})
|
||||||
|
|
||||||
|
allowedModes := "all"
|
||||||
|
if len(allowedPanelModeNames) > 0 {
|
||||||
|
allowedModes = strings.Join(allowedPanelModeNames, ",")
|
||||||
|
}
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"profile": policyProfile,
|
||||||
|
"max_current_limit": conf.Control.MaxCurrentLimit,
|
||||||
|
"mode_change_min_interval": conf.Control.ModeChangeMinInterval,
|
||||||
|
"lockout_window": conf.Control.LockoutWindow,
|
||||||
|
"allowed_panel_modes": allowedModes,
|
||||||
|
}).Info("Write policy/arbitration layer enabled")
|
||||||
|
}
|
||||||
|
gui := webui.NewWebGui(core.NewSubscription(), writer)
|
||||||
http.Handle("/", static.New())
|
http.Handle("/", static.New())
|
||||||
http.Handle("/ws", http.HandlerFunc(gui.ServeHub))
|
http.Handle("/ws", http.HandlerFunc(gui.ServeHub))
|
||||||
|
http.Handle("/api/remote-panel/state", http.HandlerFunc(gui.ServeRemotePanelState))
|
||||||
|
http.Handle("/api/remote-panel/standby", http.HandlerFunc(gui.ServeRemotePanelStandby))
|
||||||
|
log.Info("Web UI routes registered")
|
||||||
|
|
||||||
// Munin
|
// Munin
|
||||||
mu := munin.NewMunin(core.NewSubscription())
|
mu := munin.NewMunin(core.NewSubscription())
|
||||||
http.Handle("/munin", http.HandlerFunc(mu.ServeMuninHTTP))
|
http.Handle("/munin", http.HandlerFunc(mu.ServeMuninHTTP))
|
||||||
http.Handle("/muninconfig", http.HandlerFunc(mu.ServeMuninConfigHTTP))
|
http.Handle("/muninconfig", http.HandlerFunc(mu.ServeMuninConfigHTTP))
|
||||||
|
log.Info("Munin routes registered")
|
||||||
|
|
||||||
// Prometheus
|
// Prometheus
|
||||||
prometheus.NewPrometheus(core.NewSubscription())
|
prometheus.NewPrometheus(core.NewSubscription())
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
log.Info("Prometheus route registered")
|
||||||
|
|
||||||
// MQTT
|
// MQTT
|
||||||
if conf.MQTT.Enabled {
|
if conf.MQTT.Enabled {
|
||||||
mqttConf := mqttclient.Config{
|
mqttConf := mqttclient.Config{
|
||||||
Broker: conf.MQTT.Broker,
|
Broker: conf.MQTT.Broker,
|
||||||
Topic: conf.MQTT.Topic,
|
Topic: conf.MQTT.Topic,
|
||||||
ClientID: conf.MQTT.ClientID,
|
CommandTopic: conf.MQTT.CommandTopic,
|
||||||
|
StatusTopic: conf.MQTT.StatusTopic,
|
||||||
|
ClientID: conf.MQTT.ClientID,
|
||||||
|
DeviceID: conf.MQTT.DeviceID,
|
||||||
|
HistorySize: conf.MQTT.HistorySize,
|
||||||
|
InstanceID: conf.MQTT.InstanceID,
|
||||||
|
Phase: conf.MQTT.Phase,
|
||||||
|
PhaseGroup: conf.MQTT.PhaseGroup,
|
||||||
|
HomeAssistant: mqttclient.HomeAssistantConfig{
|
||||||
|
Enabled: conf.MQTT.HA.Enabled,
|
||||||
|
DiscoveryPrefix: conf.MQTT.HA.DiscoveryPrefix,
|
||||||
|
NodeID: conf.MQTT.HA.NodeID,
|
||||||
|
DeviceName: conf.MQTT.HA.DeviceName,
|
||||||
|
},
|
||||||
|
Venus: mqttclient.VenusConfig{
|
||||||
|
Enabled: conf.MQTT.Venus.Enabled,
|
||||||
|
PortalID: conf.MQTT.Venus.PortalID,
|
||||||
|
Service: conf.MQTT.Venus.Service,
|
||||||
|
SubscribeWrites: conf.MQTT.Venus.SubscribeWrites,
|
||||||
|
TopicPrefix: conf.MQTT.Venus.TopicPrefix,
|
||||||
|
GuideCompat: conf.MQTT.Venus.GuideCompat,
|
||||||
|
},
|
||||||
Username: conf.MQTT.Username,
|
Username: conf.MQTT.Username,
|
||||||
Password: conf.MQTT.Password,
|
Password: conf.MQTT.Password,
|
||||||
}
|
}
|
||||||
if err := mqttclient.New(core.NewSubscription(), mqttConf); err != nil {
|
if writer == nil {
|
||||||
|
log.Warn("MK2 data source does not support write commands; MQTT command topic will be ignored")
|
||||||
|
}
|
||||||
|
if err := mqttclient.New(core.NewSubscription(), writer, mqttConf); err != nil {
|
||||||
log.Fatalf("Could not setup MQTT client: %v", err)
|
log.Fatalf("Could not setup MQTT client: %v", err)
|
||||||
}
|
}
|
||||||
|
log.Info("MQTT client initialized")
|
||||||
}
|
}
|
||||||
log.Infof("Invertergui web server starting on: %v", conf.Address)
|
log.Infof("Invertergui web server starting on: %v", conf.Address)
|
||||||
|
|
||||||
@@ -117,12 +233,14 @@ func getMk2Device(source, ip, dev string) (mk2driver.Mk2, error) {
|
|||||||
|
|
||||||
switch source {
|
switch source {
|
||||||
case "serial":
|
case "serial":
|
||||||
|
log.WithField("device", dev).Info("Opening serial MK2 source")
|
||||||
serialConfig := &serial.Config{Name: dev, Baud: 2400}
|
serialConfig := &serial.Config{Name: dev, Baud: 2400}
|
||||||
p, err = serial.OpenPort(serialConfig)
|
p, err = serial.OpenPort(serialConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case "tcp":
|
case "tcp":
|
||||||
|
log.WithField("host", ip).Info("Opening TCP MK2 source")
|
||||||
tcpAddr, err = net.ResolveTCPAddr("tcp", ip)
|
tcpAddr, err = net.ResolveTCPAddr("tcp", ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -132,6 +250,7 @@ func getMk2Device(source, ip, dev string) (mk2driver.Mk2, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case "mock":
|
case "mock":
|
||||||
|
log.Info("Using mock MK2 data source")
|
||||||
return mk2driver.NewMk2Mock(), nil
|
return mk2driver.NewMk2Mock(), nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Invalid source selection: %v\nUse \"serial\", \"tcp\" or \"mock\"", source)
|
return nil, fmt.Errorf("Invalid source selection: %v\nUse \"serial\", \"tcp\" or \"mock\"", source)
|
||||||
@@ -141,6 +260,58 @@ func getMk2Device(source, ip, dev string) (mk2driver.Mk2, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
log.WithField("source", source).Info("MK2 connection ready")
|
||||||
|
|
||||||
return mk2, nil
|
return mk2, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseAllowedPanelModes(raw string) (map[mk2driver.PanelSwitchState]struct{}, []string, error) {
|
||||||
|
if strings.TrimSpace(raw) == "" {
|
||||||
|
return nil, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
out := make(map[mk2driver.PanelSwitchState]struct{})
|
||||||
|
names := make([]string, 0, 4)
|
||||||
|
|
||||||
|
for _, token := range strings.Split(raw, ",") {
|
||||||
|
mode := strings.ToLower(strings.TrimSpace(token))
|
||||||
|
if mode == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var state mk2driver.PanelSwitchState
|
||||||
|
var canonical string
|
||||||
|
|
||||||
|
switch mode {
|
||||||
|
case "on":
|
||||||
|
state = mk2driver.PanelSwitchOn
|
||||||
|
canonical = "on"
|
||||||
|
case "off":
|
||||||
|
state = mk2driver.PanelSwitchOff
|
||||||
|
canonical = "off"
|
||||||
|
case "charger_only", "charger-only":
|
||||||
|
state = mk2driver.PanelSwitchChargerOnly
|
||||||
|
canonical = "charger_only"
|
||||||
|
case "inverter_only", "inverter-only":
|
||||||
|
state = mk2driver.PanelSwitchInverterOnly
|
||||||
|
canonical = "inverter_only"
|
||||||
|
default:
|
||||||
|
return nil, nil, fmt.Errorf(
|
||||||
|
"unsupported panel mode %q in control.allowed_panel_modes; supported values: on, off, charger_only, inverter_only",
|
||||||
|
token,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, exists := out[state]; !exists {
|
||||||
|
out[state] = struct{}{}
|
||||||
|
names = append(names, canonical)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(out) == 0 {
|
||||||
|
return nil, nil, fmt.Errorf("control.allowed_panel_modes is set but no valid modes were provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(names)
|
||||||
|
return out, names, nil
|
||||||
|
}
|
||||||
|
|||||||
46
cmd/invertergui/main_test.go
Normal file
46
cmd/invertergui/main_test.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseAllowedPanelModesEmpty(t *testing.T) {
|
||||||
|
states, names, err := parseAllowedPanelModes("")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if states != nil {
|
||||||
|
t.Fatalf("expected nil state map for empty input, got %#v", states)
|
||||||
|
}
|
||||||
|
if names != nil {
|
||||||
|
t.Fatalf("expected nil names for empty input, got %#v", names)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseAllowedPanelModesValid(t *testing.T) {
|
||||||
|
states, names, err := parseAllowedPanelModes("off, charger-only,off")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if len(states) != 2 {
|
||||||
|
t.Fatalf("expected 2 allowed states, got %d", len(states))
|
||||||
|
}
|
||||||
|
if _, ok := states[mk2driver.PanelSwitchOff]; !ok {
|
||||||
|
t.Fatalf("off should be allowed")
|
||||||
|
}
|
||||||
|
if _, ok := states[mk2driver.PanelSwitchChargerOnly]; !ok {
|
||||||
|
t.Fatalf("charger_only should be allowed")
|
||||||
|
}
|
||||||
|
if len(names) != 2 {
|
||||||
|
t.Fatalf("expected 2 names, got %d", len(names))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseAllowedPanelModesInvalid(t *testing.T) {
|
||||||
|
_, _, err := parseAllowedPanelModes("off,invalid_mode")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for invalid mode")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
coverage:
|
|
||||||
precision: 2
|
|
||||||
round: down
|
|
||||||
range: "65...100"
|
|
||||||
186
custom_components/victron_mk2_mqtt/__init__.py
Normal file
186
custom_components/victron_mk2_mqtt/__init__.py
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
"""Home Assistant integration for invertergui MQTT topics."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
from homeassistant.const import CONF_NAME
|
||||||
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
from homeassistant.helpers import config_validation as cv, discovery
|
||||||
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
|
from .const import (
|
||||||
|
ATTR_ESS_MAX_CHARGE_POWER,
|
||||||
|
ATTR_ESS_MAX_DISCHARGE_POWER,
|
||||||
|
ATTR_ESS_MODE,
|
||||||
|
ATTR_ESS_SETPOINT,
|
||||||
|
ATTR_CURRENT_LIMIT,
|
||||||
|
ATTR_MODE,
|
||||||
|
CONF_COMMAND_TOPIC,
|
||||||
|
CONF_STATE_TOPIC,
|
||||||
|
CONF_STATUS_TOPIC,
|
||||||
|
CONF_TOPIC_ROOT,
|
||||||
|
CONF_VENUS_GUIDE_COMPAT,
|
||||||
|
CONF_VENUS_PORTAL_ID,
|
||||||
|
CONF_VENUS_TOPIC_PREFIX,
|
||||||
|
DATA_BRIDGE,
|
||||||
|
DEFAULT_COMMAND_TOPIC,
|
||||||
|
DEFAULT_NAME,
|
||||||
|
DEFAULT_STATE_TOPIC,
|
||||||
|
DEFAULT_STATUS_TOPIC,
|
||||||
|
DEFAULT_TOPIC_ROOT,
|
||||||
|
DEFAULT_VENUS_GUIDE_COMPAT,
|
||||||
|
DEFAULT_VENUS_PORTAL_ID,
|
||||||
|
DEFAULT_VENUS_TOPIC_PREFIX,
|
||||||
|
DOMAIN,
|
||||||
|
PANEL_MODES,
|
||||||
|
PLATFORMS,
|
||||||
|
SERVICE_SET_ESS_CONTROL,
|
||||||
|
SERVICE_SET_REMOTE_PANEL_STATE,
|
||||||
|
)
|
||||||
|
from .coordinator import VictronMqttBridge
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
|
{
|
||||||
|
DOMAIN: vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
vol.Optional(CONF_STATE_TOPIC, default=DEFAULT_STATE_TOPIC): cv.string,
|
||||||
|
vol.Optional(
|
||||||
|
CONF_COMMAND_TOPIC, default=DEFAULT_COMMAND_TOPIC
|
||||||
|
): cv.string,
|
||||||
|
vol.Optional(CONF_STATUS_TOPIC, default=DEFAULT_STATUS_TOPIC): cv.string,
|
||||||
|
vol.Optional(CONF_TOPIC_ROOT): cv.string,
|
||||||
|
vol.Optional(CONF_VENUS_PORTAL_ID, default=DEFAULT_VENUS_PORTAL_ID): cv.string,
|
||||||
|
vol.Optional(CONF_VENUS_TOPIC_PREFIX, default=DEFAULT_VENUS_TOPIC_PREFIX): cv.string,
|
||||||
|
vol.Optional(CONF_VENUS_GUIDE_COMPAT, default=DEFAULT_VENUS_GUIDE_COMPAT): cv.boolean,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
extra=vol.ALLOW_EXTRA,
|
||||||
|
)
|
||||||
|
|
||||||
|
SERVICE_SET_REMOTE_PANEL_STATE_SCHEMA = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Optional(ATTR_MODE): vol.In(PANEL_MODES),
|
||||||
|
vol.Optional(ATTR_CURRENT_LIMIT): vol.Coerce(float),
|
||||||
|
},
|
||||||
|
extra=vol.PREVENT_EXTRA,
|
||||||
|
)
|
||||||
|
|
||||||
|
SERVICE_SET_ESS_CONTROL_SCHEMA = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Optional(ATTR_ESS_SETPOINT): vol.Coerce(float),
|
||||||
|
vol.Optional(ATTR_ESS_MAX_CHARGE_POWER): vol.Coerce(float),
|
||||||
|
vol.Optional(ATTR_ESS_MAX_DISCHARGE_POWER): vol.Coerce(float),
|
||||||
|
vol.Optional(ATTR_ESS_MODE): vol.Coerce(int),
|
||||||
|
},
|
||||||
|
extra=vol.PREVENT_EXTRA,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def mqtt_topic_root(topic: str) -> str:
|
||||||
|
"""Match invertergui MQTT root behavior."""
|
||||||
|
cleaned = topic.strip().strip("/")
|
||||||
|
if not cleaned:
|
||||||
|
return DEFAULT_TOPIC_ROOT
|
||||||
|
if cleaned.endswith("/updates"):
|
||||||
|
root = cleaned[: -len("/updates")]
|
||||||
|
if root:
|
||||||
|
return root
|
||||||
|
return cleaned
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
|
"""Set up Victron MK2 MQTT integration from YAML."""
|
||||||
|
conf = config.get(DOMAIN)
|
||||||
|
if conf is None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
setup_conf: dict[str, Any] = dict(conf)
|
||||||
|
if not setup_conf.get(CONF_TOPIC_ROOT):
|
||||||
|
setup_conf[CONF_TOPIC_ROOT] = mqtt_topic_root(setup_conf[CONF_STATE_TOPIC])
|
||||||
|
|
||||||
|
bridge = VictronMqttBridge(hass, setup_conf)
|
||||||
|
await bridge.async_setup()
|
||||||
|
|
||||||
|
hass.data.setdefault(DOMAIN, {})
|
||||||
|
hass.data[DOMAIN][DATA_BRIDGE] = bridge
|
||||||
|
|
||||||
|
await _register_services(hass, bridge)
|
||||||
|
|
||||||
|
for platform in PLATFORMS:
|
||||||
|
hass.async_create_task(
|
||||||
|
discovery.async_load_platform(hass, platform, DOMAIN, {}, config)
|
||||||
|
)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def _register_services(hass: HomeAssistant, bridge: VictronMqttBridge) -> None:
|
||||||
|
"""Register integration services."""
|
||||||
|
if hass.services.has_service(DOMAIN, SERVICE_SET_REMOTE_PANEL_STATE):
|
||||||
|
return
|
||||||
|
|
||||||
|
async def handle_set_remote_panel_state(call: ServiceCall) -> None:
|
||||||
|
mode = call.data.get(ATTR_MODE)
|
||||||
|
current_limit = call.data.get(ATTR_CURRENT_LIMIT)
|
||||||
|
|
||||||
|
if mode is None and current_limit is None:
|
||||||
|
raise HomeAssistantError("Provide at least one of mode or current_limit")
|
||||||
|
if current_limit is not None and current_limit < 0:
|
||||||
|
raise HomeAssistantError("current_limit must be >= 0")
|
||||||
|
|
||||||
|
payload: dict[str, Any] = {"kind": "panel_state"}
|
||||||
|
if mode is not None:
|
||||||
|
payload["switch"] = mode
|
||||||
|
if current_limit is not None:
|
||||||
|
payload["current_limit"] = float(current_limit)
|
||||||
|
|
||||||
|
await bridge.async_publish_command(payload)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_SET_REMOTE_PANEL_STATE,
|
||||||
|
handle_set_remote_panel_state,
|
||||||
|
schema=SERVICE_SET_REMOTE_PANEL_STATE_SCHEMA,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def handle_set_ess_control(call: ServiceCall) -> None:
|
||||||
|
setpoint = call.data.get(ATTR_ESS_SETPOINT)
|
||||||
|
max_charge = call.data.get(ATTR_ESS_MAX_CHARGE_POWER)
|
||||||
|
max_discharge = call.data.get(ATTR_ESS_MAX_DISCHARGE_POWER)
|
||||||
|
ess_mode = call.data.get(ATTR_ESS_MODE)
|
||||||
|
|
||||||
|
if all(value is None for value in (setpoint, max_charge, max_discharge, ess_mode)):
|
||||||
|
raise HomeAssistantError(
|
||||||
|
"Provide at least one of ess_setpoint, ess_max_charge_power, ess_max_discharge_power, or ess_mode"
|
||||||
|
)
|
||||||
|
if max_charge is not None and max_charge < 0:
|
||||||
|
raise HomeAssistantError("ess_max_charge_power must be >= 0")
|
||||||
|
if max_discharge is not None and max_discharge < 0:
|
||||||
|
raise HomeAssistantError("ess_max_discharge_power must be >= 0")
|
||||||
|
if ess_mode is not None and ess_mode not in (9, 10):
|
||||||
|
raise HomeAssistantError("ess_mode must be 9 or 10")
|
||||||
|
|
||||||
|
commands: list[dict[str, Any]] = []
|
||||||
|
if setpoint is not None:
|
||||||
|
commands.append({"kind": "ess_setpoint", "value": float(setpoint)})
|
||||||
|
if max_charge is not None:
|
||||||
|
commands.append({"kind": "ess_max_charge_power", "value": float(max_charge)})
|
||||||
|
if max_discharge is not None:
|
||||||
|
commands.append({"kind": "ess_max_discharge_power", "value": float(max_discharge)})
|
||||||
|
if ess_mode is not None:
|
||||||
|
commands.append({"kind": "ess_mode", "value": int(ess_mode)})
|
||||||
|
|
||||||
|
for payload in commands:
|
||||||
|
await bridge.async_publish_command(payload)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_SET_ESS_CONTROL,
|
||||||
|
handle_set_ess_control,
|
||||||
|
schema=SERVICE_SET_ESS_CONTROL_SCHEMA,
|
||||||
|
)
|
||||||
48
custom_components/victron_mk2_mqtt/binary_sensor.py
Normal file
48
custom_components/victron_mk2_mqtt/binary_sensor.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
"""Binary sensors for Victron MK2 MQTT integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||||
|
from homeassistant.const import EntityCategory
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import DATA_BRIDGE, DOMAIN
|
||||||
|
from .coordinator import VictronMqttBridge
|
||||||
|
from .entity import VictronMqttEntity
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: dict[str, Any],
|
||||||
|
async_add_entities,
|
||||||
|
discovery_info: dict[str, Any] | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Set up Victron binary sensors."""
|
||||||
|
bridge: VictronMqttBridge = hass.data[DOMAIN][DATA_BRIDGE]
|
||||||
|
async_add_entities([VictronDataValidBinarySensor(bridge)])
|
||||||
|
|
||||||
|
|
||||||
|
class VictronDataValidBinarySensor(VictronMqttEntity, BinarySensorEntity):
|
||||||
|
"""MQTT data validity sensor."""
|
||||||
|
|
||||||
|
_attr_name = "Data Valid"
|
||||||
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
||||||
|
_attr_icon = "mdi:check-network-outline"
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_data_valid"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
value = self.bridge.metric("Valid")
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return value
|
||||||
|
if isinstance(value, (int, float)):
|
||||||
|
return value != 0
|
||||||
|
if isinstance(value, str):
|
||||||
|
normalized = value.strip().lower()
|
||||||
|
return normalized in {"1", "true", "on", "yes"}
|
||||||
|
return False
|
||||||
48
custom_components/victron_mk2_mqtt/const.py
Normal file
48
custom_components/victron_mk2_mqtt/const.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
"""Constants for the Victron MK2 MQTT integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
DOMAIN = "victron_mk2_mqtt"
|
||||||
|
|
||||||
|
CONF_STATE_TOPIC = "state_topic"
|
||||||
|
CONF_COMMAND_TOPIC = "command_topic"
|
||||||
|
CONF_STATUS_TOPIC = "status_topic"
|
||||||
|
CONF_TOPIC_ROOT = "topic_root"
|
||||||
|
CONF_NAME = "name"
|
||||||
|
CONF_VENUS_PORTAL_ID = "venus_portal_id"
|
||||||
|
CONF_VENUS_TOPIC_PREFIX = "venus_topic_prefix"
|
||||||
|
CONF_VENUS_GUIDE_COMPAT = "venus_guide_compat"
|
||||||
|
|
||||||
|
DEFAULT_STATE_TOPIC = "invertergui/updates"
|
||||||
|
DEFAULT_COMMAND_TOPIC = "invertergui/settings/set"
|
||||||
|
DEFAULT_STATUS_TOPIC = "invertergui/settings/status"
|
||||||
|
DEFAULT_TOPIC_ROOT = "invertergui"
|
||||||
|
DEFAULT_NAME = "Victron Inverter"
|
||||||
|
DEFAULT_VENUS_PORTAL_ID = "invertergui"
|
||||||
|
DEFAULT_VENUS_TOPIC_PREFIX = ""
|
||||||
|
DEFAULT_VENUS_GUIDE_COMPAT = True
|
||||||
|
|
||||||
|
PLATFORMS = ("sensor", "binary_sensor", "select", "number", "switch")
|
||||||
|
|
||||||
|
DATA_BRIDGE = "bridge"
|
||||||
|
|
||||||
|
ATTR_MODE = "mode"
|
||||||
|
ATTR_CURRENT_LIMIT = "current_limit"
|
||||||
|
ATTR_ESS_SETPOINT = "ess_setpoint"
|
||||||
|
ATTR_ESS_MAX_CHARGE_POWER = "ess_max_charge_power"
|
||||||
|
ATTR_ESS_MAX_DISCHARGE_POWER = "ess_max_discharge_power"
|
||||||
|
ATTR_ESS_MODE = "ess_mode"
|
||||||
|
|
||||||
|
SERVICE_SET_REMOTE_PANEL_STATE = "set_remote_panel_state"
|
||||||
|
SERVICE_SET_ESS_CONTROL = "set_ess_control"
|
||||||
|
|
||||||
|
PANEL_MODE_CHARGER_ONLY = "charger_only"
|
||||||
|
PANEL_MODE_INVERTER_ONLY = "inverter_only"
|
||||||
|
PANEL_MODE_ON = "on"
|
||||||
|
PANEL_MODE_OFF = "off"
|
||||||
|
PANEL_MODES = (
|
||||||
|
PANEL_MODE_CHARGER_ONLY,
|
||||||
|
PANEL_MODE_INVERTER_ONLY,
|
||||||
|
PANEL_MODE_ON,
|
||||||
|
PANEL_MODE_OFF,
|
||||||
|
)
|
||||||
333
custom_components/victron_mk2_mqtt/coordinator.py
Normal file
333
custom_components/victron_mk2_mqtt/coordinator.py
Normal file
@@ -0,0 +1,333 @@
|
|||||||
|
"""MQTT bridge for Victron MK2 integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from collections.abc import Callable
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components import mqtt
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
|
|
||||||
|
from .const import (
|
||||||
|
CONF_COMMAND_TOPIC,
|
||||||
|
CONF_NAME,
|
||||||
|
CONF_STATE_TOPIC,
|
||||||
|
CONF_STATUS_TOPIC,
|
||||||
|
CONF_TOPIC_ROOT,
|
||||||
|
CONF_VENUS_GUIDE_COMPAT,
|
||||||
|
CONF_VENUS_PORTAL_ID,
|
||||||
|
CONF_VENUS_TOPIC_PREFIX,
|
||||||
|
DOMAIN,
|
||||||
|
PANEL_MODES,
|
||||||
|
)
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class VictronMqttBridge:
|
||||||
|
"""Maintain MQTT state and command publishing for Victron entities."""
|
||||||
|
|
||||||
|
def __init__(self, hass: HomeAssistant, config: dict[str, Any]) -> None:
|
||||||
|
self.hass = hass
|
||||||
|
|
||||||
|
self.name: str = config[CONF_NAME]
|
||||||
|
self.state_topic: str = config[CONF_STATE_TOPIC]
|
||||||
|
self.command_topic: str = config[CONF_COMMAND_TOPIC]
|
||||||
|
self.status_topic: str = config[CONF_STATUS_TOPIC]
|
||||||
|
self.topic_root: str = config[CONF_TOPIC_ROOT]
|
||||||
|
self.venus_portal_id: str = config[CONF_VENUS_PORTAL_ID]
|
||||||
|
self.venus_topic_prefix: str = config[CONF_VENUS_TOPIC_PREFIX]
|
||||||
|
self.venus_guide_compat: bool = bool(config[CONF_VENUS_GUIDE_COMPAT])
|
||||||
|
|
||||||
|
self.panel_mode_state_topic = f"{self.topic_root}/homeassistant/remote_panel_mode/state"
|
||||||
|
self.current_limit_state_topic = (
|
||||||
|
f"{self.topic_root}/homeassistant/remote_panel_current_limit/state"
|
||||||
|
)
|
||||||
|
self.standby_state_topic = f"{self.topic_root}/homeassistant/remote_panel_standby/state"
|
||||||
|
|
||||||
|
self.telemetry: dict[str, Any] = {}
|
||||||
|
self.panel_mode: str | None = None
|
||||||
|
self.current_limit: float | None = None
|
||||||
|
self.standby: bool | None = None
|
||||||
|
self.last_error: str | None = None
|
||||||
|
self.ess_setpoint: float | None = None
|
||||||
|
self.ess_max_charge_power: float | None = None
|
||||||
|
self.ess_max_discharge_power: float | None = None
|
||||||
|
self.ess_mode: int | None = None
|
||||||
|
|
||||||
|
self._listeners: set[Callable[[], None]] = set()
|
||||||
|
self._unsubscribers: list[Callable[[], None]] = []
|
||||||
|
|
||||||
|
venus_base = f"N/{self.venus_portal_id}/settings/0/Settings/CGwacs"
|
||||||
|
prefix = self.venus_topic_prefix.strip().strip("/")
|
||||||
|
if prefix:
|
||||||
|
venus_base = f"{prefix}/{venus_base}"
|
||||||
|
self.ess_setpoint_state_topic = f"{venus_base}/AcPowerSetPoint"
|
||||||
|
self.ess_max_charge_state_topic = f"{venus_base}/MaxChargePower"
|
||||||
|
self.ess_max_discharge_state_topic = f"{venus_base}/MaxDischargePower"
|
||||||
|
self.ess_mode_state_topic = f"{venus_base}/BatteryLife/State"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self) -> DeviceInfo:
|
||||||
|
"""Return shared Home Assistant device metadata."""
|
||||||
|
return DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, self.topic_root)},
|
||||||
|
name=self.name,
|
||||||
|
manufacturer="Victron Energy",
|
||||||
|
model="VE.Bus via invertergui MQTT",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_setup(self) -> None:
|
||||||
|
"""Subscribe to required MQTT topics."""
|
||||||
|
_LOGGER.info(
|
||||||
|
"Subscribing Victron MQTT bridge topics state=%s command=%s status=%s",
|
||||||
|
self.state_topic,
|
||||||
|
self.command_topic,
|
||||||
|
self.status_topic,
|
||||||
|
)
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass, self.state_topic, self._handle_state_message, qos=1
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass,
|
||||||
|
self.panel_mode_state_topic,
|
||||||
|
self._handle_panel_mode_message,
|
||||||
|
qos=1,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass,
|
||||||
|
self.current_limit_state_topic,
|
||||||
|
self._handle_current_limit_message,
|
||||||
|
qos=1,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass, self.standby_state_topic, self._handle_standby_message, qos=1
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if self.venus_guide_compat:
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass,
|
||||||
|
self.ess_setpoint_state_topic,
|
||||||
|
self._handle_ess_setpoint_message,
|
||||||
|
qos=1,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass,
|
||||||
|
self.ess_max_charge_state_topic,
|
||||||
|
self._handle_ess_max_charge_message,
|
||||||
|
qos=1,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass,
|
||||||
|
self.ess_max_discharge_state_topic,
|
||||||
|
self._handle_ess_max_discharge_message,
|
||||||
|
qos=1,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass,
|
||||||
|
self.ess_mode_state_topic,
|
||||||
|
self._handle_ess_mode_message,
|
||||||
|
qos=1,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if self.status_topic:
|
||||||
|
self._unsubscribers.append(
|
||||||
|
await mqtt.async_subscribe(
|
||||||
|
self.hass, self.status_topic, self._handle_status_message, qos=1
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_shutdown(self) -> None:
|
||||||
|
"""Unsubscribe all MQTT subscriptions."""
|
||||||
|
while self._unsubscribers:
|
||||||
|
unsub = self._unsubscribers.pop()
|
||||||
|
unsub()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_add_listener(self, listener: Callable[[], None]) -> Callable[[], None]:
|
||||||
|
"""Register a state listener."""
|
||||||
|
self._listeners.add(listener)
|
||||||
|
|
||||||
|
def remove() -> None:
|
||||||
|
self._listeners.discard(listener)
|
||||||
|
|
||||||
|
return remove
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _notify_listeners(self) -> None:
|
||||||
|
"""Notify all entities that state changed."""
|
||||||
|
for listener in tuple(self._listeners):
|
||||||
|
listener()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _payload_text(payload: Any) -> str:
|
||||||
|
if isinstance(payload, bytes):
|
||||||
|
return payload.decode("utf-8", errors="ignore")
|
||||||
|
if isinstance(payload, str):
|
||||||
|
return payload
|
||||||
|
return str(payload)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_state_message(self, msg: Any) -> None:
|
||||||
|
raw_payload = self._payload_text(msg.payload)
|
||||||
|
try:
|
||||||
|
payload = json.loads(raw_payload)
|
||||||
|
except json.JSONDecodeError as err:
|
||||||
|
_LOGGER.warning("Ignoring invalid state JSON from %s: %s", msg.topic, err)
|
||||||
|
return
|
||||||
|
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
_LOGGER.warning("Ignoring state payload from %s: expected object", msg.topic)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.telemetry = payload
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_panel_mode_message(self, msg: Any) -> None:
|
||||||
|
mode = self._payload_text(msg.payload).strip().lower()
|
||||||
|
if mode not in PANEL_MODES:
|
||||||
|
_LOGGER.debug("Ignoring unknown panel mode payload %r", msg.payload)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.panel_mode = mode
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_current_limit_message(self, msg: Any) -> None:
|
||||||
|
payload = self._payload_text(msg.payload).strip()
|
||||||
|
if not payload:
|
||||||
|
self.current_limit = None
|
||||||
|
self._notify_listeners()
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
self.current_limit = float(payload)
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.debug("Ignoring invalid current limit payload %r", msg.payload)
|
||||||
|
return
|
||||||
|
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_standby_message(self, msg: Any) -> None:
|
||||||
|
value = self._payload_text(msg.payload).strip().lower()
|
||||||
|
if value in {"on", "1", "true"}:
|
||||||
|
self.standby = True
|
||||||
|
elif value in {"off", "0", "false"}:
|
||||||
|
self.standby = False
|
||||||
|
else:
|
||||||
|
_LOGGER.debug("Ignoring invalid standby payload %r", msg.payload)
|
||||||
|
return
|
||||||
|
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_status_message(self, msg: Any) -> None:
|
||||||
|
raw_payload = self._payload_text(msg.payload)
|
||||||
|
try:
|
||||||
|
payload = json.loads(raw_payload)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
return
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
return
|
||||||
|
|
||||||
|
if payload.get("status") == "error":
|
||||||
|
err = payload.get("error")
|
||||||
|
self.last_error = str(err) if err is not None else "unknown error"
|
||||||
|
else:
|
||||||
|
self.last_error = None
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_ess_setpoint_message(self, msg: Any) -> None:
|
||||||
|
value = self._decode_venus_numeric(msg.payload)
|
||||||
|
if value is None:
|
||||||
|
return
|
||||||
|
self.ess_setpoint = value
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_ess_max_charge_message(self, msg: Any) -> None:
|
||||||
|
value = self._decode_venus_numeric(msg.payload)
|
||||||
|
if value is None:
|
||||||
|
return
|
||||||
|
self.ess_max_charge_power = value
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_ess_max_discharge_message(self, msg: Any) -> None:
|
||||||
|
value = self._decode_venus_numeric(msg.payload)
|
||||||
|
if value is None:
|
||||||
|
return
|
||||||
|
self.ess_max_discharge_power = value
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_ess_mode_message(self, msg: Any) -> None:
|
||||||
|
value = self._decode_venus_numeric(msg.payload)
|
||||||
|
if value is None:
|
||||||
|
return
|
||||||
|
self.ess_mode = int(value)
|
||||||
|
self._notify_listeners()
|
||||||
|
|
||||||
|
def _decode_venus_numeric(self, payload: Any) -> float | None:
|
||||||
|
raw_payload = self._payload_text(payload)
|
||||||
|
try:
|
||||||
|
data = json.loads(raw_payload)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
_LOGGER.debug("Ignoring invalid Venus payload %r", raw_payload)
|
||||||
|
return None
|
||||||
|
|
||||||
|
value = data.get("value") if isinstance(data, dict) else None
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return float(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
_LOGGER.debug("Ignoring non-numeric Venus payload value %r", value)
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def async_publish_command(self, payload: dict[str, Any]) -> None:
|
||||||
|
"""Publish a control command payload to invertergui command topic."""
|
||||||
|
if not self.command_topic:
|
||||||
|
raise HomeAssistantError("MQTT command topic is not configured")
|
||||||
|
|
||||||
|
mqtt.async_publish(
|
||||||
|
self.hass,
|
||||||
|
self.command_topic,
|
||||||
|
json.dumps(payload, separators=(",", ":")),
|
||||||
|
qos=1,
|
||||||
|
retain=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
def metric(self, key: str) -> Any:
|
||||||
|
"""Read a telemetry key."""
|
||||||
|
return self.telemetry.get(key)
|
||||||
|
|
||||||
|
def metric_float(self, key: str) -> float | None:
|
||||||
|
"""Read and coerce telemetry value to float."""
|
||||||
|
value = self.metric(key)
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return float(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
29
custom_components/victron_mk2_mqtt/entity.py
Normal file
29
custom_components/victron_mk2_mqtt/entity.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"""Shared entity base for Victron MK2 MQTT."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
|
from .coordinator import VictronMqttBridge
|
||||||
|
|
||||||
|
|
||||||
|
class VictronMqttEntity(Entity):
|
||||||
|
"""Base entity bound to shared MQTT bridge."""
|
||||||
|
|
||||||
|
_attr_should_poll = False
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
self.bridge = bridge
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self):
|
||||||
|
"""Return the shared device info."""
|
||||||
|
return self.bridge.device_info
|
||||||
|
|
||||||
|
async def async_added_to_hass(self) -> None:
|
||||||
|
"""Register for coordinator updates."""
|
||||||
|
self.async_on_remove(self.bridge.async_add_listener(self._handle_bridge_update))
|
||||||
|
|
||||||
|
def _handle_bridge_update(self) -> None:
|
||||||
|
self.async_write_ha_state()
|
||||||
14
custom_components/victron_mk2_mqtt/manifest.json
Normal file
14
custom_components/victron_mk2_mqtt/manifest.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"domain": "victron_mk2_mqtt",
|
||||||
|
"name": "Victron MK2 MQTT",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"documentation": "https://git.coadcorp.com/nathan/invertergui",
|
||||||
|
"issue_tracker": "https://git.coadcorp.com/nathan/invertergui/issues",
|
||||||
|
"dependencies": [
|
||||||
|
"mqtt"
|
||||||
|
],
|
||||||
|
"codeowners": [
|
||||||
|
"@nathan"
|
||||||
|
],
|
||||||
|
"iot_class": "local_push"
|
||||||
|
}
|
||||||
135
custom_components/victron_mk2_mqtt/number.py
Normal file
135
custom_components/victron_mk2_mqtt/number.py
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
"""Number entities for Victron MK2 MQTT integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode
|
||||||
|
from homeassistant.const import UnitOfElectricCurrent
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import DATA_BRIDGE, DOMAIN
|
||||||
|
from .coordinator import VictronMqttBridge
|
||||||
|
from .entity import VictronMqttEntity
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: dict[str, Any],
|
||||||
|
async_add_entities,
|
||||||
|
discovery_info: dict[str, Any] | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Set up Victron number entities."""
|
||||||
|
bridge: VictronMqttBridge = hass.data[DOMAIN][DATA_BRIDGE]
|
||||||
|
entities: list[NumberEntity] = [VictronRemotePanelCurrentLimitNumber(bridge)]
|
||||||
|
if bridge.venus_guide_compat:
|
||||||
|
entities.extend(
|
||||||
|
[
|
||||||
|
VictronESSGridSetpointNumber(bridge),
|
||||||
|
VictronESSMaxChargePowerNumber(bridge),
|
||||||
|
VictronESSMaxDischargePowerNumber(bridge),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
|
class VictronRemotePanelCurrentLimitNumber(VictronMqttEntity, NumberEntity):
|
||||||
|
"""Remote panel AC input current limit."""
|
||||||
|
|
||||||
|
_attr_name = "Remote Panel Current Limit"
|
||||||
|
_attr_icon = "mdi:current-ac"
|
||||||
|
_attr_native_min_value = 0.0
|
||||||
|
_attr_native_max_value = 100.0
|
||||||
|
_attr_native_step = 0.1
|
||||||
|
_attr_mode = NumberMode.BOX
|
||||||
|
_attr_device_class = NumberDeviceClass.CURRENT
|
||||||
|
_attr_native_unit_of_measurement = UnitOfElectricCurrent.AMPERE
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_remote_panel_current_limit"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> float | None:
|
||||||
|
return self.bridge.current_limit
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
return bool(self.bridge.command_topic)
|
||||||
|
|
||||||
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
|
await self.bridge.async_publish_command(
|
||||||
|
{"kind": "panel_state", "current_limit": float(value)}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class _VictronESSNumberBase(VictronMqttEntity, NumberEntity):
|
||||||
|
"""Base class for ESS compatibility numbers."""
|
||||||
|
|
||||||
|
_attr_mode = NumberMode.BOX
|
||||||
|
_attr_native_step = 1.0
|
||||||
|
_attr_native_min_value = -20000.0
|
||||||
|
_attr_native_max_value = 20000.0
|
||||||
|
_attr_native_unit_of_measurement = "W"
|
||||||
|
_attr_icon = "mdi:transmission-tower-export"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
return bool(self.bridge.command_topic and self.bridge.venus_guide_compat)
|
||||||
|
|
||||||
|
|
||||||
|
class VictronESSGridSetpointNumber(_VictronESSNumberBase):
|
||||||
|
"""Guide-compatible ESS AC power setpoint."""
|
||||||
|
|
||||||
|
_attr_name = "ESS Grid Setpoint"
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_ess_grid_setpoint"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> float | None:
|
||||||
|
return self.bridge.ess_setpoint
|
||||||
|
|
||||||
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
|
await self.bridge.async_publish_command({"kind": "ess_setpoint", "value": float(value)})
|
||||||
|
|
||||||
|
|
||||||
|
class VictronESSMaxChargePowerNumber(_VictronESSNumberBase):
|
||||||
|
"""Guide-compatible ESS max charge power."""
|
||||||
|
|
||||||
|
_attr_name = "ESS Max Charge Power"
|
||||||
|
_attr_native_min_value = 0.0
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_ess_max_charge_power"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> float | None:
|
||||||
|
return self.bridge.ess_max_charge_power
|
||||||
|
|
||||||
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
|
await self.bridge.async_publish_command(
|
||||||
|
{"kind": "ess_max_charge_power", "value": float(value)}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class VictronESSMaxDischargePowerNumber(_VictronESSNumberBase):
|
||||||
|
"""Guide-compatible ESS max discharge power."""
|
||||||
|
|
||||||
|
_attr_name = "ESS Max Discharge Power"
|
||||||
|
_attr_native_min_value = 0.0
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_ess_max_discharge_power"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> float | None:
|
||||||
|
return self.bridge.ess_max_discharge_power
|
||||||
|
|
||||||
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
|
await self.bridge.async_publish_command(
|
||||||
|
{"kind": "ess_max_discharge_power", "value": float(value)}
|
||||||
|
)
|
||||||
46
custom_components/victron_mk2_mqtt/select.py
Normal file
46
custom_components/victron_mk2_mqtt/select.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
"""Select entities for Victron MK2 MQTT integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.select import SelectEntity
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import DATA_BRIDGE, DOMAIN, PANEL_MODES
|
||||||
|
from .coordinator import VictronMqttBridge
|
||||||
|
from .entity import VictronMqttEntity
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: dict[str, Any],
|
||||||
|
async_add_entities,
|
||||||
|
discovery_info: dict[str, Any] | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Set up Victron select entities."""
|
||||||
|
bridge: VictronMqttBridge = hass.data[DOMAIN][DATA_BRIDGE]
|
||||||
|
async_add_entities([VictronRemotePanelModeSelect(bridge)])
|
||||||
|
|
||||||
|
|
||||||
|
class VictronRemotePanelModeSelect(VictronMqttEntity, SelectEntity):
|
||||||
|
"""Remote panel mode select."""
|
||||||
|
|
||||||
|
_attr_name = "Remote Panel Mode"
|
||||||
|
_attr_options = list(PANEL_MODES)
|
||||||
|
_attr_icon = "mdi:transmission-tower-export"
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_remote_panel_mode"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current_option(self) -> str | None:
|
||||||
|
return self.bridge.panel_mode
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
return bool(self.bridge.command_topic)
|
||||||
|
|
||||||
|
async def async_select_option(self, option: str) -> None:
|
||||||
|
await self.bridge.async_publish_command({"kind": "panel_state", "switch": option})
|
||||||
148
custom_components/victron_mk2_mqtt/sensor.py
Normal file
148
custom_components/victron_mk2_mqtt/sensor.py
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
"""Sensor entities for Victron MK2 MQTT integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
||||||
|
from homeassistant.const import (
|
||||||
|
EntityCategory,
|
||||||
|
PERCENTAGE,
|
||||||
|
UnitOfElectricCurrent,
|
||||||
|
UnitOfElectricPotential,
|
||||||
|
UnitOfFrequency,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import DATA_BRIDGE, DOMAIN
|
||||||
|
from .coordinator import VictronMqttBridge
|
||||||
|
from .entity import VictronMqttEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class MetricDescription:
|
||||||
|
"""Description for a telemetry-backed sensor."""
|
||||||
|
|
||||||
|
key: str
|
||||||
|
name: str
|
||||||
|
value_fn: Callable[[VictronMqttBridge], Any]
|
||||||
|
unit: str | None = None
|
||||||
|
state_class: SensorStateClass | None = SensorStateClass.MEASUREMENT
|
||||||
|
entity_category: EntityCategory | None = None
|
||||||
|
|
||||||
|
|
||||||
|
METRICS: tuple[MetricDescription, ...] = (
|
||||||
|
MetricDescription(
|
||||||
|
key="battery_voltage",
|
||||||
|
name="Battery Voltage",
|
||||||
|
value_fn=lambda bridge: bridge.metric_float("BatVoltage"),
|
||||||
|
unit=UnitOfElectricPotential.VOLT,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="battery_current",
|
||||||
|
name="Battery Current",
|
||||||
|
value_fn=lambda bridge: bridge.metric_float("BatCurrent"),
|
||||||
|
unit=UnitOfElectricCurrent.AMPERE,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="battery_charge",
|
||||||
|
name="Battery Charge",
|
||||||
|
value_fn=lambda bridge: (
|
||||||
|
bridge.metric_float("ChargeState") * 100.0
|
||||||
|
if bridge.metric_float("ChargeState") is not None
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
unit=PERCENTAGE,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="input_voltage",
|
||||||
|
name="Input Voltage",
|
||||||
|
value_fn=lambda bridge: bridge.metric_float("InVoltage"),
|
||||||
|
unit=UnitOfElectricPotential.VOLT,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="input_current",
|
||||||
|
name="Input Current",
|
||||||
|
value_fn=lambda bridge: bridge.metric_float("InCurrent"),
|
||||||
|
unit=UnitOfElectricCurrent.AMPERE,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="input_frequency",
|
||||||
|
name="Input Frequency",
|
||||||
|
value_fn=lambda bridge: bridge.metric_float("InFrequency"),
|
||||||
|
unit=UnitOfFrequency.HERTZ,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="output_voltage",
|
||||||
|
name="Output Voltage",
|
||||||
|
value_fn=lambda bridge: bridge.metric_float("OutVoltage"),
|
||||||
|
unit=UnitOfElectricPotential.VOLT,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="output_current",
|
||||||
|
name="Output Current",
|
||||||
|
value_fn=lambda bridge: bridge.metric_float("OutCurrent"),
|
||||||
|
unit=UnitOfElectricCurrent.AMPERE,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="output_frequency",
|
||||||
|
name="Output Frequency",
|
||||||
|
value_fn=lambda bridge: bridge.metric_float("OutFrequency"),
|
||||||
|
unit=UnitOfFrequency.HERTZ,
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="input_power",
|
||||||
|
name="Input Power",
|
||||||
|
value_fn=lambda bridge: _product(bridge.metric_float("InVoltage"), bridge.metric_float("InCurrent")),
|
||||||
|
unit="VA",
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="output_power",
|
||||||
|
name="Output Power",
|
||||||
|
value_fn=lambda bridge: _product(bridge.metric_float("OutVoltage"), bridge.metric_float("OutCurrent")),
|
||||||
|
unit="VA",
|
||||||
|
),
|
||||||
|
MetricDescription(
|
||||||
|
key="last_command_error",
|
||||||
|
name="Last Command Error",
|
||||||
|
value_fn=lambda bridge: bridge.last_error,
|
||||||
|
state_class=None,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _product(a: float | None, b: float | None) -> float | None:
|
||||||
|
if a is None or b is None:
|
||||||
|
return None
|
||||||
|
return a * b
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: dict[str, Any],
|
||||||
|
async_add_entities,
|
||||||
|
discovery_info: dict[str, Any] | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Set up Victron telemetry sensors."""
|
||||||
|
bridge: VictronMqttBridge = hass.data[DOMAIN][DATA_BRIDGE]
|
||||||
|
async_add_entities(VictronMetricSensor(bridge, metric) for metric in METRICS)
|
||||||
|
|
||||||
|
|
||||||
|
class VictronMetricSensor(VictronMqttEntity, SensorEntity):
|
||||||
|
"""Generic telemetry sensor."""
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge, description: MetricDescription) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self.entity_description = description
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_{description.key}"
|
||||||
|
self._attr_name = description.name
|
||||||
|
self._attr_native_unit_of_measurement = description.unit
|
||||||
|
self._attr_state_class = description.state_class
|
||||||
|
self._attr_entity_category = description.entity_category
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self):
|
||||||
|
return self.entity_description.value_fn(self.bridge)
|
||||||
75
custom_components/victron_mk2_mqtt/services.yaml
Normal file
75
custom_components/victron_mk2_mqtt/services.yaml
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
set_remote_panel_state:
|
||||||
|
name: Set Remote Panel State
|
||||||
|
description: Set the remote panel mode and/or AC input current limit over MQTT.
|
||||||
|
fields:
|
||||||
|
mode:
|
||||||
|
name: Mode
|
||||||
|
description: Remote panel mode.
|
||||||
|
required: false
|
||||||
|
selector:
|
||||||
|
select:
|
||||||
|
mode: dropdown
|
||||||
|
options:
|
||||||
|
- charger_only
|
||||||
|
- inverter_only
|
||||||
|
- on
|
||||||
|
- off
|
||||||
|
current_limit:
|
||||||
|
name: Current Limit
|
||||||
|
description: AC input current limit in amps.
|
||||||
|
required: false
|
||||||
|
selector:
|
||||||
|
number:
|
||||||
|
min: 0
|
||||||
|
max: 100
|
||||||
|
step: 0.1
|
||||||
|
unit_of_measurement: A
|
||||||
|
mode: box
|
||||||
|
|
||||||
|
set_ess_control:
|
||||||
|
name: Set ESS Control
|
||||||
|
description: Set ESS-style control values compatible with guide CGwacs paths.
|
||||||
|
fields:
|
||||||
|
ess_setpoint:
|
||||||
|
name: ESS Setpoint
|
||||||
|
description: AC power setpoint in watts. Positive charges/imports, negative discharges/exports.
|
||||||
|
required: false
|
||||||
|
selector:
|
||||||
|
number:
|
||||||
|
min: -20000
|
||||||
|
max: 20000
|
||||||
|
step: 1
|
||||||
|
unit_of_measurement: W
|
||||||
|
mode: box
|
||||||
|
ess_max_charge_power:
|
||||||
|
name: ESS Max Charge Power
|
||||||
|
description: Maximum allowed charge/import power in watts.
|
||||||
|
required: false
|
||||||
|
selector:
|
||||||
|
number:
|
||||||
|
min: 0
|
||||||
|
max: 20000
|
||||||
|
step: 1
|
||||||
|
unit_of_measurement: W
|
||||||
|
mode: box
|
||||||
|
ess_max_discharge_power:
|
||||||
|
name: ESS Max Discharge Power
|
||||||
|
description: Maximum allowed discharge/export power in watts.
|
||||||
|
required: false
|
||||||
|
selector:
|
||||||
|
number:
|
||||||
|
min: 0
|
||||||
|
max: 20000
|
||||||
|
step: 1
|
||||||
|
unit_of_measurement: W
|
||||||
|
mode: box
|
||||||
|
ess_mode:
|
||||||
|
name: ESS Mode
|
||||||
|
description: ESS battery life mode value (10 optimized, 9 keep charged).
|
||||||
|
required: false
|
||||||
|
selector:
|
||||||
|
number:
|
||||||
|
min: 9
|
||||||
|
max: 10
|
||||||
|
step: 1
|
||||||
|
mode: box
|
||||||
76
custom_components/victron_mk2_mqtt/switch.py
Normal file
76
custom_components/victron_mk2_mqtt/switch.py
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
"""Switch entities for Victron MK2 MQTT integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.switch import SwitchEntity
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import DATA_BRIDGE, DOMAIN
|
||||||
|
from .coordinator import VictronMqttBridge
|
||||||
|
from .entity import VictronMqttEntity
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: dict[str, Any],
|
||||||
|
async_add_entities,
|
||||||
|
discovery_info: dict[str, Any] | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Set up Victron switch entities."""
|
||||||
|
bridge: VictronMqttBridge = hass.data[DOMAIN][DATA_BRIDGE]
|
||||||
|
entities: list[SwitchEntity] = [VictronRemotePanelStandbySwitch(bridge)]
|
||||||
|
if bridge.venus_guide_compat:
|
||||||
|
entities.append(VictronESSOptimizedModeSwitch(bridge))
|
||||||
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
|
class VictronRemotePanelStandbySwitch(VictronMqttEntity, SwitchEntity):
|
||||||
|
"""Remote panel standby switch."""
|
||||||
|
|
||||||
|
_attr_name = "Remote Panel Standby"
|
||||||
|
_attr_icon = "mdi:power-sleep"
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_remote_panel_standby"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
return bool(self.bridge.standby)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
return bool(self.bridge.command_topic)
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
await self.bridge.async_publish_command({"kind": "standby", "standby": True})
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
|
await self.bridge.async_publish_command({"kind": "standby", "standby": False})
|
||||||
|
|
||||||
|
|
||||||
|
class VictronESSOptimizedModeSwitch(VictronMqttEntity, SwitchEntity):
|
||||||
|
"""Guide-compatible ESS optimized mode switch."""
|
||||||
|
|
||||||
|
_attr_name = "ESS Optimized Mode"
|
||||||
|
_attr_icon = "mdi:battery-sync"
|
||||||
|
|
||||||
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
||||||
|
super().__init__(bridge)
|
||||||
|
self._attr_unique_id = f"{bridge.topic_root}_ess_optimized_mode"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
return self.bridge.ess_mode == 10
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
return bool(self.bridge.command_topic and self.bridge.venus_guide_compat)
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
await self.bridge.async_publish_command({"kind": "ess_mode", "value": 10})
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
|
await self.bridge.async_publish_command({"kind": "ess_mode", "value": 9})
|
||||||
36
go.mod
36
go.mod
@@ -1,14 +1,32 @@
|
|||||||
module github.com/diebietse/invertergui
|
module git.coadcorp.com/nathan/invertergui
|
||||||
|
|
||||||
|
go 1.26
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/eclipse/paho.mqtt.golang v1.2.0
|
github.com/eclipse/paho.mqtt.golang v1.5.1
|
||||||
github.com/gorilla/websocket v1.4.0
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/jessevdk/go-flags v1.4.0
|
github.com/jessevdk/go-flags v1.6.1
|
||||||
github.com/prometheus/client_golang v0.9.2
|
github.com/prometheus/client_golang v1.23.2
|
||||||
github.com/rakyll/statik v0.1.5
|
github.com/sirupsen/logrus v1.9.4
|
||||||
github.com/sirupsen/logrus v1.6.0
|
github.com/stretchr/testify v1.11.1
|
||||||
github.com/stretchr/testify v1.3.0
|
|
||||||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
|
||||||
)
|
)
|
||||||
|
|
||||||
go 1.14
|
require (
|
||||||
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/kr/text v0.2.0 // indirect
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/prometheus/client_model v0.6.2 // indirect
|
||||||
|
github.com/prometheus/common v0.67.5 // indirect
|
||||||
|
github.com/prometheus/procfs v0.19.2 // indirect
|
||||||
|
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
||||||
|
go.yaml.in/yaml/v2 v2.4.3 // indirect
|
||||||
|
golang.org/x/net v0.50.0 // indirect
|
||||||
|
golang.org/x/sync v0.19.0 // indirect
|
||||||
|
golang.org/x/sys v0.41.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.36.11 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
|
|||||||
92
go.sum
92
go.sum
@@ -1,44 +1,60 @@
|
|||||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
|
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/eclipse/paho.mqtt.golang v1.2.0 h1:1F8mhG9+aO5/xpdtFkW4SxOJB67ukuDC3t2y2qayIX0=
|
github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE=
|
||||||
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
|
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
|
||||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
|
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
|
||||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/prometheus/client_golang v0.9.2 h1:awm861/B8OKDd2I/6o1dy3ra4BamzKhYOiGItCeZ740=
|
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
|
||||||
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
|
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
|
||||||
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8=
|
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||||
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
|
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||||
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275 h1:PnBWHBf+6L0jOqq0gIVUe6Yk0/QMZ640k6NvkxcBf+8=
|
github.com/prometheus/common v0.67.5 h1:pIgK94WWlQt1WLwAC5j2ynLaBRDiinoAb86HZHTUGI4=
|
||||||
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
|
github.com/prometheus/common v0.67.5/go.mod h1:SjE/0MzDEEAyrdr5Gqc6G+sXI67maCxzaT3A2+HqjUw=
|
||||||
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nLJdBg+pBmGgkJlSaKC2KaQmTCk1XDtE=
|
github.com/prometheus/procfs v0.19.2 h1:zUMhqEW66Ex7OXIiDkll3tl9a1ZdilUOd/F6ZXw4Vws=
|
||||||
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
github.com/prometheus/procfs v0.19.2/go.mod h1:M0aotyiemPhBCM0z5w87kL22CxfcH05ZpYlu+b4J7mw=
|
||||||
github.com/rakyll/statik v0.1.5 h1:Ly2UjURzxnsSYS0zI50fZ+srA+Fu7EbpV5hglvJvJG0=
|
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||||
github.com/rakyll/statik v0.1.5/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
|
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||||
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
|
github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w=
|
||||||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
|
||||||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
|
||||||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
|
||||||
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc h1:a3CU5tJYVj92DY2LaA1kUkrsqD5/3mLDhx2NcNqyW+0=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
|
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
|
||||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
|
golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60=
|
||||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM=
|
||||||
|
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||||
|
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
|
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||||
|
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
|
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||||
|
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
132
homeassistant/dashboards/invertergui_mqtt_dashboard.yaml
Normal file
132
homeassistant/dashboards/invertergui_mqtt_dashboard.yaml
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
title: Victron Inverter MQTT
|
||||||
|
views:
|
||||||
|
- title: Inverter
|
||||||
|
path: victron-inverter
|
||||||
|
icon: mdi:flash
|
||||||
|
badges:
|
||||||
|
- entity: binary_sensor.victron_online
|
||||||
|
- entity: binary_sensor.victron_data_valid
|
||||||
|
- entity: sensor.victron_last_command_status
|
||||||
|
cards:
|
||||||
|
- type: vertical-stack
|
||||||
|
cards:
|
||||||
|
- type: markdown
|
||||||
|
content: |
|
||||||
|
## Remote Panel Control
|
||||||
|
Mode and current limit are published together over MQTT, matching `set_remote_panel_state`.
|
||||||
|
- type: entities
|
||||||
|
title: Current Remote State
|
||||||
|
state_color: true
|
||||||
|
show_header_toggle: false
|
||||||
|
entities:
|
||||||
|
- entity: select.victron_remote_panel_mode
|
||||||
|
name: Mode
|
||||||
|
- entity: number.victron_remote_panel_current_limit
|
||||||
|
name: AC Input Current Limit
|
||||||
|
- entity: switch.victron_remote_panel_standby
|
||||||
|
name: Prevent Sleep While Off
|
||||||
|
- entity: sensor.victron_last_command_error
|
||||||
|
name: Last Command Error
|
||||||
|
- type: entities
|
||||||
|
title: Apply Mode + Current Limit
|
||||||
|
show_header_toggle: false
|
||||||
|
entities:
|
||||||
|
- entity: input_select.victron_remote_panel_mode_target
|
||||||
|
name: Target Mode
|
||||||
|
- entity: input_number.victron_remote_panel_current_limit_target
|
||||||
|
name: Target Current Limit
|
||||||
|
- entity: script.victron_mqtt_set_remote_panel_state
|
||||||
|
name: Apply Mode + Current Limit
|
||||||
|
- type: entities
|
||||||
|
title: Apply Standby
|
||||||
|
show_header_toggle: false
|
||||||
|
entities:
|
||||||
|
- entity: input_boolean.victron_remote_panel_standby_target
|
||||||
|
name: Target Standby
|
||||||
|
- entity: script.victron_mqtt_set_remote_panel_standby
|
||||||
|
name: Apply Standby
|
||||||
|
- type: entities
|
||||||
|
title: ESS Guide-Style Control (MQTT)
|
||||||
|
show_header_toggle: false
|
||||||
|
entities:
|
||||||
|
- entity: number.victron_ess_grid_setpoint
|
||||||
|
name: Grid Setpoint (W)
|
||||||
|
- entity: number.victron_ess_max_charge_power
|
||||||
|
name: Max Charge Power (W)
|
||||||
|
- entity: number.victron_ess_max_discharge_power
|
||||||
|
name: Max Discharge Power (W)
|
||||||
|
- entity: switch.victron_ess_optimized_mode
|
||||||
|
name: Optimized Mode (10 on / 9 off)
|
||||||
|
|
||||||
|
- type: grid
|
||||||
|
columns: 3
|
||||||
|
square: false
|
||||||
|
cards:
|
||||||
|
- type: entities
|
||||||
|
title: Output
|
||||||
|
show_header_toggle: false
|
||||||
|
entities:
|
||||||
|
- entity: sensor.victron_output_current
|
||||||
|
name: Output Current
|
||||||
|
- entity: sensor.victron_output_voltage
|
||||||
|
name: Output Voltage
|
||||||
|
- entity: sensor.victron_output_frequency
|
||||||
|
name: Output Frequency
|
||||||
|
- entity: sensor.victron_output_power
|
||||||
|
name: Output Power
|
||||||
|
- type: entities
|
||||||
|
title: Input
|
||||||
|
show_header_toggle: false
|
||||||
|
entities:
|
||||||
|
- entity: sensor.victron_input_current
|
||||||
|
name: Input Current
|
||||||
|
- entity: sensor.victron_input_voltage
|
||||||
|
name: Input Voltage
|
||||||
|
- entity: sensor.victron_input_frequency
|
||||||
|
name: Input Frequency
|
||||||
|
- entity: sensor.victron_input_power
|
||||||
|
name: Input Power
|
||||||
|
- entity: sensor.victron_input_minus_output_power
|
||||||
|
name: Input - Output Power
|
||||||
|
- type: entities
|
||||||
|
title: Battery
|
||||||
|
show_header_toggle: false
|
||||||
|
entities:
|
||||||
|
- entity: sensor.victron_battery_current
|
||||||
|
name: Battery Current
|
||||||
|
- entity: sensor.victron_battery_voltage
|
||||||
|
name: Battery Voltage
|
||||||
|
- entity: sensor.victron_battery_power
|
||||||
|
name: Battery Power
|
||||||
|
- entity: sensor.victron_battery_charge
|
||||||
|
name: Battery Charge
|
||||||
|
|
||||||
|
- type: entities
|
||||||
|
title: LED Status
|
||||||
|
show_header_toggle: false
|
||||||
|
entities:
|
||||||
|
- entity: sensor.victron_led_mains
|
||||||
|
name: Mains
|
||||||
|
- entity: sensor.victron_led_absorb
|
||||||
|
name: Absorb
|
||||||
|
- entity: sensor.victron_led_bulk
|
||||||
|
name: Bulk
|
||||||
|
- entity: sensor.victron_led_float
|
||||||
|
name: Float
|
||||||
|
- entity: sensor.victron_led_inverter
|
||||||
|
name: Inverter
|
||||||
|
- entity: sensor.victron_led_overload
|
||||||
|
name: Overload
|
||||||
|
- entity: sensor.victron_led_low_battery
|
||||||
|
name: Low Battery
|
||||||
|
- entity: sensor.victron_led_over_temp
|
||||||
|
name: Over Temperature
|
||||||
|
|
||||||
|
- type: history-graph
|
||||||
|
title: Power (Last 2 Hours)
|
||||||
|
hours_to_show: 2
|
||||||
|
refresh_interval: 60
|
||||||
|
entities:
|
||||||
|
- entity: sensor.victron_input_power
|
||||||
|
- entity: sensor.victron_output_power
|
||||||
|
- entity: sensor.victron_battery_power
|
||||||
401
homeassistant/packages/invertergui_mqtt.yaml
Normal file
401
homeassistant/packages/invertergui_mqtt.yaml
Normal file
@@ -0,0 +1,401 @@
|
|||||||
|
# MQTT-only Home Assistant package for invertergui.
|
||||||
|
# This avoids duplicate entities from MQTT auto-discovery/custom integration.
|
||||||
|
#
|
||||||
|
# Requirements:
|
||||||
|
# - invertergui started with MQTT publishing enabled.
|
||||||
|
# - invertergui MQTT discovery disabled (`--mqtt.ha.enabled=false`) when using this package.
|
||||||
|
# - for guide-style ESS controls below, enable:
|
||||||
|
# `MQTT_VENUS_ENABLED=true`
|
||||||
|
# `MQTT_VENUS_GUIDE_COMPAT=true`
|
||||||
|
# `MQTT_VENUS_TOPIC_PREFIX=victron`
|
||||||
|
# `MQTT_VENUS_PORTAL_ID=invertergui`
|
||||||
|
|
||||||
|
mqtt:
|
||||||
|
sensor:
|
||||||
|
- name: Victron Battery Voltage
|
||||||
|
unique_id: invertergui_mqtt_battery_voltage
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.BatVoltage }}"
|
||||||
|
unit_of_measurement: V
|
||||||
|
device_class: voltage
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Battery Current
|
||||||
|
unique_id: invertergui_mqtt_battery_current
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.BatCurrent }}"
|
||||||
|
unit_of_measurement: A
|
||||||
|
device_class: current
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Battery Charge
|
||||||
|
unique_id: invertergui_mqtt_battery_charge
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ ((value_json.ChargeState | float(0)) * 100) | round(1) }}"
|
||||||
|
unit_of_measurement: "%"
|
||||||
|
device_class: battery
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Input Voltage
|
||||||
|
unique_id: invertergui_mqtt_input_voltage
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.InVoltage }}"
|
||||||
|
unit_of_measurement: V
|
||||||
|
device_class: voltage
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Input Current
|
||||||
|
unique_id: invertergui_mqtt_input_current
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.InCurrent }}"
|
||||||
|
unit_of_measurement: A
|
||||||
|
device_class: current
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Input Frequency
|
||||||
|
unique_id: invertergui_mqtt_input_frequency
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.InFrequency }}"
|
||||||
|
unit_of_measurement: Hz
|
||||||
|
device_class: frequency
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Output Voltage
|
||||||
|
unique_id: invertergui_mqtt_output_voltage
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.OutVoltage }}"
|
||||||
|
unit_of_measurement: V
|
||||||
|
device_class: voltage
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Output Current
|
||||||
|
unique_id: invertergui_mqtt_output_current
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.OutCurrent }}"
|
||||||
|
unit_of_measurement: A
|
||||||
|
device_class: current
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Output Frequency
|
||||||
|
unique_id: invertergui_mqtt_output_frequency
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.OutFrequency }}"
|
||||||
|
unit_of_measurement: Hz
|
||||||
|
device_class: frequency
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Input Power
|
||||||
|
unique_id: invertergui_mqtt_input_power
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ ((value_json.InVoltage | float(0)) * (value_json.InCurrent | float(0))) | round(1) }}"
|
||||||
|
unit_of_measurement: VA
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Output Power
|
||||||
|
unique_id: invertergui_mqtt_output_power
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ ((value_json.OutVoltage | float(0)) * (value_json.OutCurrent | float(0))) | round(1) }}"
|
||||||
|
unit_of_measurement: VA
|
||||||
|
state_class: measurement
|
||||||
|
- name: Victron Last Command Status
|
||||||
|
unique_id: invertergui_mqtt_last_command_status
|
||||||
|
state_topic: invertergui/settings/status
|
||||||
|
value_template: "{{ value_json.status | default('unknown') }}"
|
||||||
|
icon: mdi:message-alert-outline
|
||||||
|
- name: Victron Last Command Error
|
||||||
|
unique_id: invertergui_mqtt_last_command_error
|
||||||
|
state_topic: invertergui/settings/status
|
||||||
|
value_template: >-
|
||||||
|
{% if value_json.status == 'error' %}
|
||||||
|
{{ value_json.error | default('unknown error') }}
|
||||||
|
{% else %}
|
||||||
|
none
|
||||||
|
{% endif %}
|
||||||
|
icon: mdi:alert-circle-outline
|
||||||
|
- name: Victron LED Mains
|
||||||
|
unique_id: invertergui_mqtt_led_mains
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: >-
|
||||||
|
{% set leds = value_json.LEDs | default({}) %}
|
||||||
|
{% set v = leds['0'] | default(0) | int(0) %}
|
||||||
|
{% if v == 1 %}on{% elif v == 2 %}blink{% else %}off{% endif %}
|
||||||
|
icon: mdi:transmission-tower
|
||||||
|
- name: Victron LED Absorb
|
||||||
|
unique_id: invertergui_mqtt_led_absorb
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: >-
|
||||||
|
{% set leds = value_json.LEDs | default({}) %}
|
||||||
|
{% set v = leds['1'] | default(0) | int(0) %}
|
||||||
|
{% if v == 1 %}on{% elif v == 2 %}blink{% else %}off{% endif %}
|
||||||
|
icon: mdi:water-outline
|
||||||
|
- name: Victron LED Bulk
|
||||||
|
unique_id: invertergui_mqtt_led_bulk
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: >-
|
||||||
|
{% set leds = value_json.LEDs | default({}) %}
|
||||||
|
{% set v = leds['2'] | default(0) | int(0) %}
|
||||||
|
{% if v == 1 %}on{% elif v == 2 %}blink{% else %}off{% endif %}
|
||||||
|
icon: mdi:battery-plus
|
||||||
|
- name: Victron LED Float
|
||||||
|
unique_id: invertergui_mqtt_led_float
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: >-
|
||||||
|
{% set leds = value_json.LEDs | default({}) %}
|
||||||
|
{% set v = leds['3'] | default(0) | int(0) %}
|
||||||
|
{% if v == 1 %}on{% elif v == 2 %}blink{% else %}off{% endif %}
|
||||||
|
icon: mdi:battery-heart-variant
|
||||||
|
- name: Victron LED Inverter
|
||||||
|
unique_id: invertergui_mqtt_led_inverter
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: >-
|
||||||
|
{% set leds = value_json.LEDs | default({}) %}
|
||||||
|
{% set v = leds['4'] | default(0) | int(0) %}
|
||||||
|
{% if v == 1 %}on{% elif v == 2 %}blink{% else %}off{% endif %}
|
||||||
|
icon: mdi:power-plug
|
||||||
|
- name: Victron LED Overload
|
||||||
|
unique_id: invertergui_mqtt_led_overload
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: >-
|
||||||
|
{% set leds = value_json.LEDs | default({}) %}
|
||||||
|
{% set v = leds['5'] | default(0) | int(0) %}
|
||||||
|
{% if v == 1 %}on{% elif v == 2 %}blink{% else %}off{% endif %}
|
||||||
|
icon: mdi:alert-octagon
|
||||||
|
- name: Victron LED Low Battery
|
||||||
|
unique_id: invertergui_mqtt_led_low_battery
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: >-
|
||||||
|
{% set leds = value_json.LEDs | default({}) %}
|
||||||
|
{% set v = leds['6'] | default(0) | int(0) %}
|
||||||
|
{% if v == 1 %}on{% elif v == 2 %}blink{% else %}off{% endif %}
|
||||||
|
icon: mdi:battery-alert-variant-outline
|
||||||
|
- name: Victron LED Over Temp
|
||||||
|
unique_id: invertergui_mqtt_led_over_temp
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: >-
|
||||||
|
{% set leds = value_json.LEDs | default({}) %}
|
||||||
|
{% set v = leds['7'] | default(0) | int(0) %}
|
||||||
|
{% if v == 1 %}on{% elif v == 2 %}blink{% else %}off{% endif %}
|
||||||
|
icon: mdi:thermometer-alert
|
||||||
|
|
||||||
|
binary_sensor:
|
||||||
|
- name: Victron Online
|
||||||
|
unique_id: invertergui_mqtt_online
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "ON"
|
||||||
|
payload_on: "ON"
|
||||||
|
payload_off: "OFF"
|
||||||
|
expire_after: 120
|
||||||
|
device_class: connectivity
|
||||||
|
- name: Victron Data Valid
|
||||||
|
unique_id: invertergui_mqtt_data_valid
|
||||||
|
state_topic: invertergui/updates
|
||||||
|
value_template: "{{ value_json.Valid }}"
|
||||||
|
payload_on: "true"
|
||||||
|
payload_off: "false"
|
||||||
|
entity_category: diagnostic
|
||||||
|
|
||||||
|
select:
|
||||||
|
- name: Victron Remote Panel Mode
|
||||||
|
unique_id: invertergui_mqtt_remote_panel_mode
|
||||||
|
state_topic: invertergui/homeassistant/remote_panel_mode/state
|
||||||
|
command_topic: invertergui/settings/set
|
||||||
|
command_template: '{"kind":"panel_state","switch":"{{ value }}"}'
|
||||||
|
options:
|
||||||
|
- charger_only
|
||||||
|
- inverter_only
|
||||||
|
- on
|
||||||
|
- off
|
||||||
|
icon: mdi:transmission-tower-export
|
||||||
|
|
||||||
|
number:
|
||||||
|
- name: Victron Remote Panel Current Limit
|
||||||
|
unique_id: invertergui_mqtt_remote_panel_current_limit
|
||||||
|
state_topic: invertergui/homeassistant/remote_panel_current_limit/state
|
||||||
|
command_topic: invertergui/settings/set
|
||||||
|
command_template: '{"kind":"panel_state","current_limit":{{ value | float(0) }}}'
|
||||||
|
unit_of_measurement: A
|
||||||
|
device_class: current
|
||||||
|
mode: box
|
||||||
|
min: 0
|
||||||
|
max: 100
|
||||||
|
step: 0.1
|
||||||
|
icon: mdi:current-ac
|
||||||
|
- name: Victron ESS Grid Setpoint
|
||||||
|
unique_id: invertergui_mqtt_ess_grid_setpoint
|
||||||
|
state_topic: victron/N/invertergui/settings/0/Settings/CGwacs/AcPowerSetPoint
|
||||||
|
value_template: "{{ value_json.value | float(0) }}"
|
||||||
|
command_topic: victron/W/invertergui/settings/0/Settings/CGwacs/AcPowerSetPoint
|
||||||
|
command_template: '{"value":{{ value | float(0) | round(0) }}}'
|
||||||
|
unit_of_measurement: W
|
||||||
|
mode: box
|
||||||
|
min: -20000
|
||||||
|
max: 20000
|
||||||
|
step: 1
|
||||||
|
icon: mdi:transmission-tower-export
|
||||||
|
- name: Victron ESS Max Charge Power
|
||||||
|
unique_id: invertergui_mqtt_ess_max_charge_power
|
||||||
|
state_topic: victron/N/invertergui/settings/0/Settings/CGwacs/MaxChargePower
|
||||||
|
value_template: "{{ value_json.value | float(0) }}"
|
||||||
|
command_topic: victron/W/invertergui/settings/0/Settings/CGwacs/MaxChargePower
|
||||||
|
command_template: '{"value":{{ value | float(0) | round(0) }}}'
|
||||||
|
unit_of_measurement: W
|
||||||
|
mode: box
|
||||||
|
min: 0
|
||||||
|
max: 20000
|
||||||
|
step: 1
|
||||||
|
icon: mdi:battery-plus
|
||||||
|
- name: Victron ESS Max Discharge Power
|
||||||
|
unique_id: invertergui_mqtt_ess_max_discharge_power
|
||||||
|
state_topic: victron/N/invertergui/settings/0/Settings/CGwacs/MaxDischargePower
|
||||||
|
value_template: "{{ value_json.value | float(0) }}"
|
||||||
|
command_topic: victron/W/invertergui/settings/0/Settings/CGwacs/MaxDischargePower
|
||||||
|
command_template: '{"value":{{ value | float(0) | round(0) }}}'
|
||||||
|
unit_of_measurement: W
|
||||||
|
mode: box
|
||||||
|
min: 0
|
||||||
|
max: 20000
|
||||||
|
step: 1
|
||||||
|
icon: mdi:battery-minus
|
||||||
|
|
||||||
|
switch:
|
||||||
|
- name: Victron Remote Panel Standby
|
||||||
|
unique_id: invertergui_mqtt_remote_panel_standby
|
||||||
|
state_topic: invertergui/homeassistant/remote_panel_standby/state
|
||||||
|
command_topic: invertergui/settings/set
|
||||||
|
payload_on: '{"kind":"standby","standby":true}'
|
||||||
|
payload_off: '{"kind":"standby","standby":false}'
|
||||||
|
state_on: "ON"
|
||||||
|
state_off: "OFF"
|
||||||
|
icon: mdi:power-sleep
|
||||||
|
- name: Victron ESS Optimized Mode
|
||||||
|
unique_id: invertergui_mqtt_ess_optimized_mode
|
||||||
|
state_topic: victron/N/invertergui/settings/0/Settings/CGwacs/BatteryLife/State
|
||||||
|
value_template: "{{ value_json.value | int(9) }}"
|
||||||
|
command_topic: victron/W/invertergui/settings/0/Settings/CGwacs/BatteryLife/State
|
||||||
|
payload_on: '{"value":10}'
|
||||||
|
payload_off: '{"value":9}'
|
||||||
|
state_on: "10"
|
||||||
|
state_off: "9"
|
||||||
|
icon: mdi:battery-sync
|
||||||
|
|
||||||
|
input_select:
|
||||||
|
victron_remote_panel_mode_target:
|
||||||
|
name: Victron Target Mode
|
||||||
|
options:
|
||||||
|
- charger_only
|
||||||
|
- inverter_only
|
||||||
|
- on
|
||||||
|
- off
|
||||||
|
icon: mdi:transmission-tower-export
|
||||||
|
|
||||||
|
input_number:
|
||||||
|
victron_remote_panel_current_limit_target:
|
||||||
|
name: Victron Target Current Limit
|
||||||
|
min: 0
|
||||||
|
max: 100
|
||||||
|
step: 0.1
|
||||||
|
unit_of_measurement: A
|
||||||
|
mode: box
|
||||||
|
icon: mdi:current-ac
|
||||||
|
|
||||||
|
input_boolean:
|
||||||
|
victron_remote_panel_standby_target:
|
||||||
|
name: Victron Target Standby
|
||||||
|
icon: mdi:power-sleep
|
||||||
|
|
||||||
|
script:
|
||||||
|
victron_mqtt_set_remote_panel_state:
|
||||||
|
alias: Victron MQTT Set Remote Panel State
|
||||||
|
description: Set panel mode and current limit in one MQTT command.
|
||||||
|
mode: single
|
||||||
|
icon: mdi:send
|
||||||
|
sequence:
|
||||||
|
- service: mqtt.publish
|
||||||
|
data:
|
||||||
|
topic: invertergui/settings/set
|
||||||
|
qos: 1
|
||||||
|
payload: >-
|
||||||
|
{"kind":"panel_state","switch":"{{ states('input_select.victron_remote_panel_mode_target') }}","current_limit":{{ states('input_number.victron_remote_panel_current_limit_target') | float(0) | round(1) }}}
|
||||||
|
|
||||||
|
victron_mqtt_set_remote_panel_standby:
|
||||||
|
alias: Victron MQTT Set Remote Panel Standby
|
||||||
|
description: Set standby state from helper input.
|
||||||
|
mode: single
|
||||||
|
icon: mdi:send-circle
|
||||||
|
sequence:
|
||||||
|
- service: mqtt.publish
|
||||||
|
data:
|
||||||
|
topic: invertergui/settings/set
|
||||||
|
qos: 1
|
||||||
|
payload: >-
|
||||||
|
{"kind":"standby","standby":{% if is_state('input_boolean.victron_remote_panel_standby_target', 'on') %}true{% else %}false{% endif %}}
|
||||||
|
|
||||||
|
template:
|
||||||
|
- sensor:
|
||||||
|
- name: Victron Battery Power
|
||||||
|
unique_id: invertergui_mqtt_battery_power
|
||||||
|
unit_of_measurement: W
|
||||||
|
state_class: measurement
|
||||||
|
icon: mdi:battery-charging
|
||||||
|
state: >-
|
||||||
|
{{ ((states('sensor.victron_battery_voltage') | float(0)) * (states('sensor.victron_battery_current') | float(0))) | round(1) }}
|
||||||
|
- name: Victron Input Minus Output Power
|
||||||
|
unique_id: invertergui_mqtt_input_minus_output_power
|
||||||
|
unit_of_measurement: VA
|
||||||
|
state_class: measurement
|
||||||
|
icon: mdi:flash-triangle
|
||||||
|
state: >-
|
||||||
|
{{ (states('sensor.victron_input_power') | float(0) - states('sensor.victron_output_power') | float(0)) | round(1) }}
|
||||||
|
|
||||||
|
automation:
|
||||||
|
- id: victron_mqtt_sync_target_mode
|
||||||
|
alias: Victron MQTT Sync Mode Target
|
||||||
|
trigger:
|
||||||
|
- platform: state
|
||||||
|
entity_id: select.victron_remote_panel_mode
|
||||||
|
- platform: homeassistant
|
||||||
|
event: start
|
||||||
|
condition:
|
||||||
|
- condition: template
|
||||||
|
value_template: >-
|
||||||
|
{{ states('select.victron_remote_panel_mode') in ['charger_only', 'inverter_only', 'on', 'off'] }}
|
||||||
|
action:
|
||||||
|
- service: input_select.select_option
|
||||||
|
target:
|
||||||
|
entity_id: input_select.victron_remote_panel_mode_target
|
||||||
|
data:
|
||||||
|
option: "{{ states('select.victron_remote_panel_mode') }}"
|
||||||
|
|
||||||
|
- id: victron_mqtt_sync_target_current_limit
|
||||||
|
alias: Victron MQTT Sync Current Limit Target
|
||||||
|
trigger:
|
||||||
|
- platform: state
|
||||||
|
entity_id: number.victron_remote_panel_current_limit
|
||||||
|
- platform: homeassistant
|
||||||
|
event: start
|
||||||
|
condition:
|
||||||
|
- condition: template
|
||||||
|
value_template: >-
|
||||||
|
{{ states('number.victron_remote_panel_current_limit') not in ['unknown', 'unavailable'] }}
|
||||||
|
action:
|
||||||
|
- service: input_number.set_value
|
||||||
|
target:
|
||||||
|
entity_id: input_number.victron_remote_panel_current_limit_target
|
||||||
|
data:
|
||||||
|
value: "{{ states('number.victron_remote_panel_current_limit') | float(0) }}"
|
||||||
|
|
||||||
|
- id: victron_mqtt_sync_target_standby
|
||||||
|
alias: Victron MQTT Sync Standby Target
|
||||||
|
trigger:
|
||||||
|
- platform: state
|
||||||
|
entity_id: switch.victron_remote_panel_standby
|
||||||
|
- platform: homeassistant
|
||||||
|
event: start
|
||||||
|
action:
|
||||||
|
- choose:
|
||||||
|
- conditions:
|
||||||
|
- condition: state
|
||||||
|
entity_id: switch.victron_remote_panel_standby
|
||||||
|
state: "on"
|
||||||
|
sequence:
|
||||||
|
- service: input_boolean.turn_on
|
||||||
|
target:
|
||||||
|
entity_id: input_boolean.victron_remote_panel_standby_target
|
||||||
|
- conditions:
|
||||||
|
- condition: state
|
||||||
|
entity_id: switch.victron_remote_panel_standby
|
||||||
|
state: "off"
|
||||||
|
sequence:
|
||||||
|
- service: input_boolean.turn_off
|
||||||
|
target:
|
||||||
|
entity_id: input_boolean.victron_remote_panel_standby_target
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
package mk2core
|
package mk2core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = logrus.WithField("ctx", "inverter-gui-core")
|
||||||
|
|
||||||
type Core struct {
|
type Core struct {
|
||||||
mk2driver.Mk2
|
mk2driver.Mk2
|
||||||
plugins map[*subscription]bool
|
plugins map[*subscription]bool
|
||||||
@@ -16,6 +19,7 @@ func NewCore(m mk2driver.Mk2) *Core {
|
|||||||
register: make(chan *subscription, 255),
|
register: make(chan *subscription, 255),
|
||||||
plugins: map[*subscription]bool{},
|
plugins: map[*subscription]bool{},
|
||||||
}
|
}
|
||||||
|
log.Info("Core initialized")
|
||||||
go core.run()
|
go core.run()
|
||||||
return core
|
return core
|
||||||
}
|
}
|
||||||
@@ -25,6 +29,7 @@ func (c *Core) NewSubscription() mk2driver.Mk2 {
|
|||||||
send: make(chan *mk2driver.Mk2Info),
|
send: make(chan *mk2driver.Mk2Info),
|
||||||
}
|
}
|
||||||
c.register <- sub
|
c.register <- sub
|
||||||
|
log.Debug("New plugin subscription registered")
|
||||||
return sub
|
return sub
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,11 +38,13 @@ func (c *Core) run() {
|
|||||||
select {
|
select {
|
||||||
case r := <-c.register:
|
case r := <-c.register:
|
||||||
c.plugins[r] = true
|
c.plugins[r] = true
|
||||||
|
log.WithField("subscribers", len(c.plugins)).Debug("Subscription added")
|
||||||
case e := <-c.C():
|
case e := <-c.C():
|
||||||
for plugin := range c.plugins {
|
for plugin := range c.plugins {
|
||||||
select {
|
select {
|
||||||
case plugin.send <- e:
|
case plugin.send <- e:
|
||||||
default:
|
default:
|
||||||
|
log.WithField("subscribers", len(c.plugins)).Warn("Dropping update for a slow subscriber")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
232
mk2driver/managed_writer.go
Normal file
232
mk2driver/managed_writer.go
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
package mk2driver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WriterProfile string
|
||||||
|
|
||||||
|
const (
|
||||||
|
WriterProfileNormal WriterProfile = "normal"
|
||||||
|
WriterProfileMaintenance WriterProfile = "maintenance"
|
||||||
|
WriterProfileReadOnly WriterProfile = "read_only"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WriterPolicy struct {
|
||||||
|
Profile WriterProfile
|
||||||
|
MaxCurrentLimitA *float64
|
||||||
|
ModeChangeMinInterval time.Duration
|
||||||
|
LockoutWindow time.Duration
|
||||||
|
AllowedPanelStates map[PanelSwitchState]struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommandEvent struct {
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
Source CommandSource `json:"source"`
|
||||||
|
Kind string `json:"kind"`
|
||||||
|
Allowed bool `json:"allowed"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ManagedWriter struct {
|
||||||
|
writer SettingsWriter
|
||||||
|
policy WriterPolicy
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
lastModeChange time.Time
|
||||||
|
lockoutUntil time.Time
|
||||||
|
events []CommandEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ SettingsWriter = (*ManagedWriter)(nil)
|
||||||
|
var _ SourceAwareSettingsWriter = (*ManagedWriter)(nil)
|
||||||
|
|
||||||
|
func NewManagedWriter(writer SettingsWriter, policy WriterPolicy) *ManagedWriter {
|
||||||
|
if policy.Profile == "" {
|
||||||
|
policy.Profile = WriterProfileNormal
|
||||||
|
}
|
||||||
|
return &ManagedWriter{
|
||||||
|
writer: writer,
|
||||||
|
policy: policy,
|
||||||
|
events: make([]CommandEvent, 0, 100),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) WriteRAMVar(id uint16, value int16) error {
|
||||||
|
return m.WriteRAMVarWithSource(CommandSourceUnknown, id, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) WriteSetting(id uint16, value int16) error {
|
||||||
|
return m.WriteSettingWithSource(CommandSourceUnknown, id, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) SetPanelState(switchState PanelSwitchState, currentLimitA *float64) error {
|
||||||
|
return m.SetPanelStateWithSource(CommandSourceUnknown, switchState, currentLimitA)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) SetStandby(enabled bool) error {
|
||||||
|
return m.SetStandbyWithSource(CommandSourceUnknown, enabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) WriteRAMVarWithSource(source CommandSource, id uint16, value int16) error {
|
||||||
|
return m.apply(source, "write_ram_var", func() error {
|
||||||
|
if err := m.ensureProfileAllows("write_ram_var"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.baseWriter().WriteRAMVar(id, value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) WriteSettingWithSource(source CommandSource, id uint16, value int16) error {
|
||||||
|
return m.apply(source, "write_setting", func() error {
|
||||||
|
if err := m.ensureProfileAllows("write_setting"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.baseWriter().WriteSetting(id, value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) SetPanelStateWithSource(source CommandSource, switchState PanelSwitchState, currentLimitA *float64) error {
|
||||||
|
return m.apply(source, "set_panel_state", func() error {
|
||||||
|
if err := m.ensureProfileAllows("set_panel_state"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(m.policy.AllowedPanelStates) > 0 {
|
||||||
|
if _, ok := m.policy.AllowedPanelStates[switchState]; !ok {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"panel switch mode %s denied by allowlist policy; allowed modes: %s",
|
||||||
|
panelStateLabel(switchState),
|
||||||
|
stringsForAllowedPanelStates(m.policy.AllowedPanelStates),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.policy.MaxCurrentLimitA != nil && currentLimitA != nil && *currentLimitA > *m.policy.MaxCurrentLimitA {
|
||||||
|
return fmt.Errorf("current limit %.2fA exceeds configured policy max %.2fA", *currentLimitA, *m.policy.MaxCurrentLimitA)
|
||||||
|
}
|
||||||
|
if m.policy.Profile == WriterProfileMaintenance && switchState != PanelSwitchOff {
|
||||||
|
return errors.New("maintenance profile only allows panel switch off")
|
||||||
|
}
|
||||||
|
if m.policy.ModeChangeMinInterval > 0 && !m.lastModeChange.IsZero() && time.Since(m.lastModeChange) < m.policy.ModeChangeMinInterval {
|
||||||
|
return fmt.Errorf("mode change denied due to rate limit; wait %s", m.policy.ModeChangeMinInterval-time.Since(m.lastModeChange))
|
||||||
|
}
|
||||||
|
if err := m.baseWriter().SetPanelState(switchState, currentLimitA); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m.lastModeChange = time.Now().UTC()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) SetStandbyWithSource(source CommandSource, enabled bool) error {
|
||||||
|
return m.apply(source, "set_standby", func() error {
|
||||||
|
if err := m.ensureProfileAllows("set_standby"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.baseWriter().SetStandby(enabled)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) History(limit int) []CommandEvent {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
if limit <= 0 || limit > len(m.events) {
|
||||||
|
limit = len(m.events)
|
||||||
|
}
|
||||||
|
out := make([]CommandEvent, limit)
|
||||||
|
if limit > 0 {
|
||||||
|
copy(out, m.events[len(m.events)-limit:])
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) apply(source CommandSource, kind string, fn func() error) error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
if m.writer == nil {
|
||||||
|
err := errors.New("settings writer is not available")
|
||||||
|
m.recordLocked(source, kind, false, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if m.policy.LockoutWindow > 0 && time.Now().UTC().Before(m.lockoutUntil) {
|
||||||
|
err := fmt.Errorf("command denied during lockout window until %s", m.lockoutUntil.Format(time.RFC3339))
|
||||||
|
m.recordLocked(source, kind, false, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := fn(); err != nil {
|
||||||
|
m.recordLocked(source, kind, false, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if m.policy.LockoutWindow > 0 {
|
||||||
|
m.lockoutUntil = time.Now().UTC().Add(m.policy.LockoutWindow)
|
||||||
|
}
|
||||||
|
m.recordLocked(source, kind, true, nil)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) ensureProfileAllows(kind string) error {
|
||||||
|
switch m.policy.Profile {
|
||||||
|
case WriterProfileReadOnly:
|
||||||
|
return errors.New("write denied by read-only profile")
|
||||||
|
case WriterProfileMaintenance:
|
||||||
|
if kind == "set_standby" || kind == "set_panel_state" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("maintenance profile blocks %s", kind)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) recordLocked(source CommandSource, kind string, allowed bool, err error) {
|
||||||
|
event := CommandEvent{
|
||||||
|
Timestamp: time.Now().UTC(),
|
||||||
|
Source: source,
|
||||||
|
Kind: kind,
|
||||||
|
Allowed: allowed,
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
event.Error = err.Error()
|
||||||
|
}
|
||||||
|
m.events = append(m.events, event)
|
||||||
|
if len(m.events) > 100 {
|
||||||
|
m.events = m.events[len(m.events)-100:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ManagedWriter) baseWriter() SettingsWriter {
|
||||||
|
return m.writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func panelStateLabel(state PanelSwitchState) string {
|
||||||
|
switch state {
|
||||||
|
case PanelSwitchOn:
|
||||||
|
return "on"
|
||||||
|
case PanelSwitchOff:
|
||||||
|
return "off"
|
||||||
|
case PanelSwitchChargerOnly:
|
||||||
|
return "charger_only"
|
||||||
|
case PanelSwitchInverterOnly:
|
||||||
|
return "inverter_only"
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("unknown(0x%02x)", byte(state))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringsForAllowedPanelStates(states map[PanelSwitchState]struct{}) string {
|
||||||
|
if len(states) == 0 {
|
||||||
|
return "<none>"
|
||||||
|
}
|
||||||
|
labels := make([]string, 0, len(states))
|
||||||
|
for state := range states {
|
||||||
|
labels = append(labels, panelStateLabel(state))
|
||||||
|
}
|
||||||
|
sort.Strings(labels)
|
||||||
|
return fmt.Sprintf("%v", labels)
|
||||||
|
}
|
||||||
97
mk2driver/managed_writer_test.go
Normal file
97
mk2driver/managed_writer_test.go
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
package mk2driver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type writerStub struct {
|
||||||
|
settingWrites int
|
||||||
|
ramWrites int
|
||||||
|
panelWrites int
|
||||||
|
standbyWrites int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *writerStub) WriteRAMVar(id uint16, value int16) error {
|
||||||
|
w.ramWrites++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *writerStub) WriteSetting(id uint16, value int16) error {
|
||||||
|
w.settingWrites++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *writerStub) SetPanelState(switchState PanelSwitchState, currentLimitA *float64) error {
|
||||||
|
w.panelWrites++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *writerStub) SetStandby(enabled bool) error {
|
||||||
|
w.standbyWrites++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManagedWriterReadOnlyProfile(t *testing.T) {
|
||||||
|
base := &writerStub{}
|
||||||
|
managed := NewManagedWriter(base, WriterPolicy{
|
||||||
|
Profile: WriterProfileReadOnly,
|
||||||
|
})
|
||||||
|
|
||||||
|
err := managed.WriteSettingWithSource(CommandSourceMQTT, 1, 1)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, 0, base.settingWrites)
|
||||||
|
history := managed.History(10)
|
||||||
|
if assert.Len(t, history, 1) {
|
||||||
|
assert.False(t, history[0].Allowed)
|
||||||
|
assert.Equal(t, CommandSourceMQTT, history[0].Source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManagedWriterCurrentLimitGuard(t *testing.T) {
|
||||||
|
base := &writerStub{}
|
||||||
|
max := 16.0
|
||||||
|
managed := NewManagedWriter(base, WriterPolicy{
|
||||||
|
Profile: WriterProfileNormal,
|
||||||
|
MaxCurrentLimitA: &max,
|
||||||
|
})
|
||||||
|
|
||||||
|
limit := 20.0
|
||||||
|
err := managed.SetPanelStateWithSource(CommandSourceUI, PanelSwitchOn, &limit)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, 0, base.panelWrites)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManagedWriterModeRateLimit(t *testing.T) {
|
||||||
|
base := &writerStub{}
|
||||||
|
managed := NewManagedWriter(base, WriterPolicy{
|
||||||
|
Profile: WriterProfileNormal,
|
||||||
|
ModeChangeMinInterval: 10 * time.Second,
|
||||||
|
})
|
||||||
|
|
||||||
|
err := managed.SetPanelStateWithSource(CommandSourceAutomation, PanelSwitchOn, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
err = managed.SetPanelStateWithSource(CommandSourceAutomation, PanelSwitchOff, nil)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, 1, base.panelWrites)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManagedWriterPanelModeAllowlist(t *testing.T) {
|
||||||
|
base := &writerStub{}
|
||||||
|
managed := NewManagedWriter(base, WriterPolicy{
|
||||||
|
Profile: WriterProfileNormal,
|
||||||
|
AllowedPanelStates: map[PanelSwitchState]struct{}{
|
||||||
|
PanelSwitchOff: {},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
err := managed.SetPanelStateWithSource(CommandSourceMQTT, PanelSwitchOn, nil)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, 0, base.panelWrites)
|
||||||
|
|
||||||
|
err = managed.SetPanelStateWithSource(CommandSourceMQTT, PanelSwitchOff, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, base.panelWrites)
|
||||||
|
}
|
||||||
332
mk2driver/metadata.go
Normal file
332
mk2driver/metadata.go
Normal file
@@ -0,0 +1,332 @@
|
|||||||
|
package mk2driver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultTransactionRetries = 2
|
||||||
|
defaultTransactionRetryDelay = 200 * time.Millisecond
|
||||||
|
defaultTransactionBackoff = 1.5
|
||||||
|
fastCommandTimeout = 1500 * time.Millisecond
|
||||||
|
standardCommandTimeout = 3 * time.Second
|
||||||
|
slowCommandTimeout = 6 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
type registerKey struct {
|
||||||
|
kind RegisterKind
|
||||||
|
id uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func int16Ptr(v int16) *int16 {
|
||||||
|
return &v
|
||||||
|
}
|
||||||
|
|
||||||
|
var knownRegisterMetadata = map[registerKey]RegisterMetadata{
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarVMains}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarVMains,
|
||||||
|
Name: "mains_voltage",
|
||||||
|
Description: "AC input mains voltage (scaled)",
|
||||||
|
Unit: "V",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarIMains}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarIMains,
|
||||||
|
Name: "mains_current",
|
||||||
|
Description: "AC input mains current (scaled)",
|
||||||
|
Unit: "A",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarVInverter}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarVInverter,
|
||||||
|
Name: "inverter_voltage",
|
||||||
|
Description: "AC output inverter voltage (scaled)",
|
||||||
|
Unit: "V",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarIInverter}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarIInverter,
|
||||||
|
Name: "inverter_current",
|
||||||
|
Description: "AC output inverter current (scaled)",
|
||||||
|
Unit: "A",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarVBat}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarVBat,
|
||||||
|
Name: "battery_voltage",
|
||||||
|
Description: "Battery voltage (scaled)",
|
||||||
|
Unit: "V",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarIBat}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarIBat,
|
||||||
|
Name: "battery_current",
|
||||||
|
Description: "Battery current (scaled)",
|
||||||
|
Unit: "A",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarVBatRipple}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarVBatRipple,
|
||||||
|
Name: "battery_voltage_ripple",
|
||||||
|
Description: "Battery ripple voltage (scaled)",
|
||||||
|
Unit: "V",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarInverterPeriod}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarInverterPeriod,
|
||||||
|
Name: "inverter_period",
|
||||||
|
Description: "Inverter period source value",
|
||||||
|
Unit: "ticks",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarMainPeriod}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarMainPeriod,
|
||||||
|
Name: "mains_period",
|
||||||
|
Description: "Mains period source value",
|
||||||
|
Unit: "ticks",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarIACLoad}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarIACLoad,
|
||||||
|
Name: "ac_load_current",
|
||||||
|
Description: "AC load current (scaled)",
|
||||||
|
Unit: "A",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarVirSwitchPos}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarVirSwitchPos,
|
||||||
|
Name: "virtual_switch_position",
|
||||||
|
Description: "Virtual switch position",
|
||||||
|
Unit: "state",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: true,
|
||||||
|
Signed: false,
|
||||||
|
MinValue: int16Ptr(0),
|
||||||
|
MaxValue: int16Ptr(255),
|
||||||
|
SafetyClass: RegisterSafetyGuarded,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarIgnACInState}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarIgnACInState,
|
||||||
|
Name: "ignored_ac_input_state",
|
||||||
|
Description: "AC input state as seen by firmware",
|
||||||
|
Unit: "state",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: false,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarMultiFuncRelay}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarMultiFuncRelay,
|
||||||
|
Name: "multifunction_relay",
|
||||||
|
Description: "Multifunction relay state",
|
||||||
|
Unit: "state",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: true,
|
||||||
|
Signed: false,
|
||||||
|
MinValue: int16Ptr(0),
|
||||||
|
MaxValue: int16Ptr(1),
|
||||||
|
SafetyClass: RegisterSafetyOperational,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarChargeState}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarChargeState,
|
||||||
|
Name: "battery_charge_state",
|
||||||
|
Description: "Battery charge state fraction",
|
||||||
|
Unit: "fraction",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarInverterPower1}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarInverterPower1,
|
||||||
|
Name: "inverter_power_1",
|
||||||
|
Description: "Inverter power source register 1",
|
||||||
|
Unit: "W",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarInverterPower2}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarInverterPower2,
|
||||||
|
Name: "inverter_power_2",
|
||||||
|
Description: "Inverter power source register 2",
|
||||||
|
Unit: "W",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
{kind: RegisterKindRAMVar, id: ramVarOutPower}: {
|
||||||
|
Kind: RegisterKindRAMVar,
|
||||||
|
ID: ramVarOutPower,
|
||||||
|
Name: "output_power",
|
||||||
|
Description: "Output power source register",
|
||||||
|
Unit: "W",
|
||||||
|
Scale: 1,
|
||||||
|
Writable: false,
|
||||||
|
Signed: true,
|
||||||
|
SafetyClass: RegisterSafetyReadOnly,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeTransactionOptions(opts TransactionOptions) TransactionOptions {
|
||||||
|
if opts.Retries < 0 {
|
||||||
|
opts.Retries = 0
|
||||||
|
}
|
||||||
|
if opts.RetryDelay < 0 {
|
||||||
|
opts.RetryDelay = 0
|
||||||
|
}
|
||||||
|
if opts.BackoffFactor <= 0 {
|
||||||
|
opts.BackoffFactor = defaultTransactionBackoff
|
||||||
|
}
|
||||||
|
if opts.Retries == 0 && opts.RetryDelay == 0 && !opts.ReadBeforeWrite && !opts.VerifyAfterWrite {
|
||||||
|
opts.Retries = defaultTransactionRetries
|
||||||
|
opts.RetryDelay = defaultTransactionRetryDelay
|
||||||
|
opts.BackoffFactor = defaultTransactionBackoff
|
||||||
|
opts.ReadBeforeWrite = true
|
||||||
|
opts.VerifyAfterWrite = true
|
||||||
|
opts.TimeoutClass = TimeoutClassStandard
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
if opts.RetryDelay == 0 {
|
||||||
|
opts.RetryDelay = defaultTransactionRetryDelay
|
||||||
|
}
|
||||||
|
if opts.TimeoutClass == "" {
|
||||||
|
opts.TimeoutClass = TimeoutClassStandard
|
||||||
|
}
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func lookupRegisterMetadata(kind RegisterKind, id uint16) (RegisterMetadata, bool) {
|
||||||
|
meta, ok := knownRegisterMetadata[registerKey{kind: kind, id: id}]
|
||||||
|
if ok {
|
||||||
|
meta = withMetadataDefaults(meta)
|
||||||
|
}
|
||||||
|
return meta, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func listRegisterMetadata() []RegisterMetadata {
|
||||||
|
out := make([]RegisterMetadata, 0, len(knownRegisterMetadata))
|
||||||
|
for _, meta := range knownRegisterMetadata {
|
||||||
|
out = append(out, withMetadataDefaults(meta))
|
||||||
|
}
|
||||||
|
sort.Slice(out, func(i, j int) bool {
|
||||||
|
if out[i].Kind != out[j].Kind {
|
||||||
|
return out[i].Kind < out[j].Kind
|
||||||
|
}
|
||||||
|
return out[i].ID < out[j].ID
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateValueAgainstMetadata(meta RegisterMetadata, value int16) error {
|
||||||
|
if meta.MinValue != nil && value < *meta.MinValue {
|
||||||
|
return fmt.Errorf("value %d is below minimum %d for %s:%d", value, *meta.MinValue, meta.Kind, meta.ID)
|
||||||
|
}
|
||||||
|
if meta.MaxValue != nil && value > *meta.MaxValue {
|
||||||
|
return fmt.Errorf("value %d is above maximum %d for %s:%d", value, *meta.MaxValue, meta.Kind, meta.ID)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func withMetadataDefaults(meta RegisterMetadata) RegisterMetadata {
|
||||||
|
if meta.Scale == 0 {
|
||||||
|
meta.Scale = 1
|
||||||
|
}
|
||||||
|
if meta.SafetyClass == "" {
|
||||||
|
if meta.Writable {
|
||||||
|
meta.SafetyClass = RegisterSafetyGuarded
|
||||||
|
} else {
|
||||||
|
meta.SafetyClass = RegisterSafetyReadOnly
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return meta
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveCommandTimeout(opts TransactionOptions) time.Duration {
|
||||||
|
if opts.CommandTimeout > 0 {
|
||||||
|
return opts.CommandTimeout
|
||||||
|
}
|
||||||
|
switch opts.TimeoutClass {
|
||||||
|
case TimeoutClassFast:
|
||||||
|
return fastCommandTimeout
|
||||||
|
case TimeoutClassSlow:
|
||||||
|
return slowCommandTimeout
|
||||||
|
default:
|
||||||
|
return standardCommandTimeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func retryDelayForAttempt(opts TransactionOptions, attempt int) time.Duration {
|
||||||
|
if opts.RetryDelay <= 0 || attempt <= 1 {
|
||||||
|
return opts.RetryDelay
|
||||||
|
}
|
||||||
|
factor := math.Pow(opts.BackoffFactor, float64(attempt-1))
|
||||||
|
delay := float64(opts.RetryDelay) * factor
|
||||||
|
return time.Duration(delay)
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultWritableRegisterAddresses() []RegisterAddress {
|
||||||
|
metas := listRegisterMetadata()
|
||||||
|
out := make([]RegisterAddress, 0, len(metas))
|
||||||
|
for _, meta := range metas {
|
||||||
|
if !meta.Writable {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, RegisterAddress{
|
||||||
|
Kind: meta.Kind,
|
||||||
|
ID: meta.ID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
1429
mk2driver/mk2.go
1429
mk2driver/mk2.go
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -76,3 +76,277 @@ type Mk2 interface {
|
|||||||
C() chan *Mk2Info
|
C() chan *Mk2Info
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PanelSwitchState byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
// PanelSwitchChargerOnly enables charging only.
|
||||||
|
PanelSwitchChargerOnly PanelSwitchState = 0x01
|
||||||
|
// PanelSwitchInverterOnly enables inverter output and disables charging.
|
||||||
|
PanelSwitchInverterOnly PanelSwitchState = 0x02
|
||||||
|
// PanelSwitchOn enables both inverter and charger.
|
||||||
|
PanelSwitchOn PanelSwitchState = 0x03
|
||||||
|
// PanelSwitchOff disables inverter and charger.
|
||||||
|
PanelSwitchOff PanelSwitchState = 0x04
|
||||||
|
)
|
||||||
|
|
||||||
|
type SettingsWriter interface {
|
||||||
|
// WriteRAMVar writes a signed 16-bit value to a RAM variable id.
|
||||||
|
WriteRAMVar(id uint16, value int16) error
|
||||||
|
// WriteSetting writes a signed 16-bit value to a setting id.
|
||||||
|
WriteSetting(id uint16, value int16) error
|
||||||
|
// SetPanelState sends the MK2 "S" command using a virtual panel switch state.
|
||||||
|
// If currentLimitA is nil, the command does not update the AC current limit.
|
||||||
|
SetPanelState(switchState PanelSwitchState, currentLimitA *float64) error
|
||||||
|
// SetStandby configures the remote panel standby line.
|
||||||
|
// When enabled, the inverter is prevented from sleeping while switched off.
|
||||||
|
SetStandby(enabled bool) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeviceState byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DeviceStateChargerOnly enables charging only.
|
||||||
|
DeviceStateChargerOnly DeviceState = 0x02
|
||||||
|
// DeviceStateInverterOnly enables inverter output and disables charging.
|
||||||
|
DeviceStateInverterOnly DeviceState = 0x03
|
||||||
|
// DeviceStateOn enables both inverter and charger.
|
||||||
|
DeviceStateOn DeviceState = 0x04
|
||||||
|
// DeviceStateOff disables inverter and charger.
|
||||||
|
DeviceStateOff DeviceState = 0x05
|
||||||
|
)
|
||||||
|
|
||||||
|
var DeviceStateNames = map[DeviceState]string{
|
||||||
|
DeviceStateChargerOnly: "charger_only",
|
||||||
|
DeviceStateInverterOnly: "inverter_only",
|
||||||
|
DeviceStateOn: "on",
|
||||||
|
DeviceStateOff: "off",
|
||||||
|
}
|
||||||
|
|
||||||
|
type RAMVarInfo struct {
|
||||||
|
ID uint16
|
||||||
|
Scale int16
|
||||||
|
Offset int16
|
||||||
|
Factor float64
|
||||||
|
Signed bool
|
||||||
|
Supported bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProtocolControl exposes protocol 3.14 command paths for direct MK2 control.
|
||||||
|
type ProtocolControl interface {
|
||||||
|
SettingsWriter
|
||||||
|
// GetDeviceState returns the current VE.Bus state using command 0x0E.
|
||||||
|
GetDeviceState() (DeviceState, error)
|
||||||
|
// SetDeviceState sets the VE.Bus state using command 0x0E.
|
||||||
|
SetDeviceState(state DeviceState) error
|
||||||
|
// ReadRAMVarByID reads a RAM variable via command 0x30.
|
||||||
|
ReadRAMVarByID(id uint16) (int16, error)
|
||||||
|
// ReadSettingByID reads a setting via command 0x31.
|
||||||
|
ReadSettingByID(id uint16) (int16, error)
|
||||||
|
// SelectRAMVar selects a RAM variable for follow-up read-selected/write-selected paths.
|
||||||
|
SelectRAMVar(id uint16) error
|
||||||
|
// SelectSetting selects a setting for follow-up read-selected/write-selected paths.
|
||||||
|
SelectSetting(id uint16) error
|
||||||
|
// ReadSelected reads the currently selected value via command 0x35.
|
||||||
|
ReadSelected() (int16, error)
|
||||||
|
// ReadRAMVarInfo reads RAM variable metadata via command 0x36.
|
||||||
|
ReadRAMVarInfo(id uint16) (RAMVarInfo, error)
|
||||||
|
// WriteSelectedData writes to the currently selected register via command 0x34.
|
||||||
|
WriteSelectedData(value int16) error
|
||||||
|
// WriteSettingBySelection performs 0x33 (select setting) followed by 0x34 (write data).
|
||||||
|
WriteSettingBySelection(id uint16, value int16) error
|
||||||
|
// WriteRAMVarBySelection performs 0x32 (select RAM var) followed by 0x34 (write data).
|
||||||
|
WriteRAMVarBySelection(id uint16, value int16) error
|
||||||
|
// WriteSettingByID writes a setting via command 0x37.
|
||||||
|
WriteSettingByID(id uint16, value int16) error
|
||||||
|
// WriteRAMVarByID writes a RAM variable via command 0x38.
|
||||||
|
WriteRAMVarByID(id uint16, value int16) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegisterKind string
|
||||||
|
|
||||||
|
const (
|
||||||
|
RegisterKindSetting RegisterKind = "setting"
|
||||||
|
RegisterKindRAMVar RegisterKind = "ram_var"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RegisterSafetyClass string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// RegisterSafetyReadOnly indicates no write path should be exposed.
|
||||||
|
RegisterSafetyReadOnly RegisterSafetyClass = "read_only"
|
||||||
|
// RegisterSafetyOperational indicates normal runtime write usage is expected.
|
||||||
|
RegisterSafetyOperational RegisterSafetyClass = "operational"
|
||||||
|
// RegisterSafetyGuarded indicates writes should be policy-guarded.
|
||||||
|
RegisterSafetyGuarded RegisterSafetyClass = "guarded"
|
||||||
|
// RegisterSafetyCritical indicates high-impact settings that need stricter controls.
|
||||||
|
RegisterSafetyCritical RegisterSafetyClass = "critical"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TimeoutClass string
|
||||||
|
|
||||||
|
const (
|
||||||
|
TimeoutClassFast TimeoutClass = "fast"
|
||||||
|
TimeoutClassStandard TimeoutClass = "standard"
|
||||||
|
TimeoutClassSlow TimeoutClass = "slow"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterMetadata documents known MK2 register IDs and expected value behavior.
|
||||||
|
type RegisterMetadata struct {
|
||||||
|
Kind RegisterKind
|
||||||
|
ID uint16
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
Unit string
|
||||||
|
Scale float64
|
||||||
|
Writable bool
|
||||||
|
Signed bool
|
||||||
|
MinValue *int16
|
||||||
|
MaxValue *int16
|
||||||
|
SafetyClass RegisterSafetyClass
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransactionOptions controls retry and verification semantics for safe writes.
|
||||||
|
type TransactionOptions struct {
|
||||||
|
// Retries is the number of additional write attempts after the first try.
|
||||||
|
Retries int
|
||||||
|
// RetryDelay is slept between retries. Zero uses a sensible default.
|
||||||
|
RetryDelay time.Duration
|
||||||
|
// BackoffFactor multiplies retry delay for each additional attempt (1 disables backoff).
|
||||||
|
BackoffFactor float64
|
||||||
|
// ReadBeforeWrite captures previous value before writing when possible.
|
||||||
|
ReadBeforeWrite bool
|
||||||
|
// VerifyAfterWrite reads the register back and compares with written value.
|
||||||
|
VerifyAfterWrite bool
|
||||||
|
// TimeoutClass applies standard timeout buckets when CommandTimeout is not set.
|
||||||
|
TimeoutClass TimeoutClass
|
||||||
|
// CommandTimeout overrides timeout class for each protocol command inside the transaction.
|
||||||
|
CommandTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterTransactionResult reports details about a transactional register write.
|
||||||
|
type RegisterTransactionResult struct {
|
||||||
|
Kind RegisterKind
|
||||||
|
ID uint16
|
||||||
|
TargetValue int16
|
||||||
|
PreviousValue *int16
|
||||||
|
VerifiedValue *int16
|
||||||
|
Attempts int
|
||||||
|
Timeout time.Duration
|
||||||
|
Duration time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// MetadataControl adds register metadata and transactional safety helpers.
|
||||||
|
type MetadataControl interface {
|
||||||
|
ProtocolControl
|
||||||
|
// RegisterMetadata returns metadata for a known register.
|
||||||
|
RegisterMetadata(kind RegisterKind, id uint16) (RegisterMetadata, bool)
|
||||||
|
// ListRegisterMetadata returns all known register metadata.
|
||||||
|
ListRegisterMetadata() []RegisterMetadata
|
||||||
|
// ReadRegister reads a setting or RAM var by kind and id.
|
||||||
|
ReadRegister(kind RegisterKind, id uint16) (int16, error)
|
||||||
|
// WriteRegister performs a safe transactional write with optional retry/verify.
|
||||||
|
WriteRegister(kind RegisterKind, id uint16, value int16, opts TransactionOptions) (RegisterTransactionResult, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegisterAddress struct {
|
||||||
|
Kind RegisterKind `json:"kind"`
|
||||||
|
ID uint16 `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegisterSnapshotEntry struct {
|
||||||
|
Kind RegisterKind `json:"kind"`
|
||||||
|
ID uint16 `json:"id"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Value int16 `json:"value"`
|
||||||
|
Writable bool `json:"writable"`
|
||||||
|
Safety RegisterSafetyClass `json:"safety_class,omitempty"`
|
||||||
|
CapturedAt time.Time `json:"captured_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegisterSnapshot struct {
|
||||||
|
CapturedAt time.Time `json:"captured_at"`
|
||||||
|
Entries []RegisterSnapshotEntry `json:"entries"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SnapshotDiff struct {
|
||||||
|
Kind RegisterKind `json:"kind"`
|
||||||
|
ID uint16 `json:"id"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Current int16 `json:"current"`
|
||||||
|
Target int16 `json:"target"`
|
||||||
|
Changed bool `json:"changed"`
|
||||||
|
Writable bool `json:"writable"`
|
||||||
|
Safety RegisterSafetyClass `json:"safety_class,omitempty"`
|
||||||
|
DiffValue int32 `json:"diff_value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SnapshotRestoreResult struct {
|
||||||
|
Applied []RegisterTransactionResult `json:"applied"`
|
||||||
|
RolledBack bool `json:"rolled_back"`
|
||||||
|
RollbackErrors []string `json:"rollback_errors,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SnapshotControl provides register snapshot, diff preview, and rollback-aware restore.
|
||||||
|
type SnapshotControl interface {
|
||||||
|
MetadataControl
|
||||||
|
// CaptureSnapshot reads the provided register list. Empty addresses captures known writable registers.
|
||||||
|
CaptureSnapshot(addresses []RegisterAddress) (RegisterSnapshot, error)
|
||||||
|
// DiffSnapshot compares current values against a snapshot.
|
||||||
|
DiffSnapshot(snapshot RegisterSnapshot) ([]SnapshotDiff, error)
|
||||||
|
// RestoreSnapshot applies snapshot target values; if restore fails mid-way it attempts rollback.
|
||||||
|
RestoreSnapshot(snapshot RegisterSnapshot, opts TransactionOptions) (SnapshotRestoreResult, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type TraceDirection string
|
||||||
|
|
||||||
|
const (
|
||||||
|
TraceDirectionTX TraceDirection = "tx"
|
||||||
|
TraceDirectionRX TraceDirection = "rx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProtocolTrace struct {
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
Direction TraceDirection `json:"direction"`
|
||||||
|
Frame string `json:"frame"`
|
||||||
|
Command string `json:"command,omitempty"`
|
||||||
|
BytesHex string `json:"bytes_hex"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DriverDiagnostics struct {
|
||||||
|
GeneratedAt time.Time `json:"generated_at"`
|
||||||
|
HealthScore int `json:"health_score"`
|
||||||
|
LastFrameAt *time.Time `json:"last_frame_at,omitempty"`
|
||||||
|
CommandTimeouts uint64 `json:"command_timeouts"`
|
||||||
|
CommandFailures uint64 `json:"command_failures"`
|
||||||
|
ChecksumFailures uint64 `json:"checksum_failures"`
|
||||||
|
RecentErrors []string `json:"recent_errors,omitempty"`
|
||||||
|
Traces []ProtocolTrace `json:"traces"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiagnosticsControl exposes recent protocol traces and health information for troubleshooting bundles.
|
||||||
|
type DiagnosticsControl interface {
|
||||||
|
DriverDiagnostics(limit int) DriverDiagnostics
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommandSource string
|
||||||
|
|
||||||
|
const (
|
||||||
|
CommandSourceUnknown CommandSource = "unknown"
|
||||||
|
CommandSourceUI CommandSource = "ui"
|
||||||
|
CommandSourceMQTT CommandSource = "mqtt"
|
||||||
|
CommandSourceAutomation CommandSource = "automation"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SourceAwareSettingsWriter accepts source tags for arbitration and diagnostics.
|
||||||
|
type SourceAwareSettingsWriter interface {
|
||||||
|
SettingsWriter
|
||||||
|
WriteRAMVarWithSource(source CommandSource, id uint16, value int16) error
|
||||||
|
WriteSettingWithSource(source CommandSource, id uint16, value int16) error
|
||||||
|
SetPanelStateWithSource(source CommandSource, switchState PanelSwitchState, currentLimitA *float64) error
|
||||||
|
SetStandbyWithSource(source CommandSource, enabled bool) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommandHistoryProvider interface {
|
||||||
|
History(limit int) []CommandEvent
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ type mock struct {
|
|||||||
c chan *Mk2Info
|
c chan *Mk2Info
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ ProtocolControl = (*mock)(nil)
|
||||||
|
|
||||||
func NewMk2Mock() Mk2 {
|
func NewMk2Mock() Mk2 {
|
||||||
tmp := &mock{
|
tmp := &mock{
|
||||||
c: make(chan *Mk2Info, 1),
|
c: make(chan *Mk2Info, 1),
|
||||||
@@ -37,6 +39,77 @@ func (m *mock) Close() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mock) WriteRAMVar(_ uint16, _ int16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) WriteSetting(_ uint16, _ int16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) SetPanelState(_ PanelSwitchState, _ *float64) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) SetStandby(_ bool) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) GetDeviceState() (DeviceState, error) {
|
||||||
|
return DeviceStateOn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) SetDeviceState(_ DeviceState) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) ReadRAMVarByID(_ uint16) (int16, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) ReadSettingByID(_ uint16) (int16, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) SelectRAMVar(_ uint16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) SelectSetting(_ uint16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) ReadSelected() (int16, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) ReadRAMVarInfo(id uint16) (RAMVarInfo, error) {
|
||||||
|
return RAMVarInfo{
|
||||||
|
ID: id,
|
||||||
|
Supported: false,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) WriteSettingByID(_ uint16, _ int16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) WriteRAMVarByID(_ uint16, _ int16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) WriteSelectedData(_ int16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) WriteSettingBySelection(_ uint16, _ int16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mock) WriteRAMVarBySelection(_ uint16, _ int16) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *mock) genMockValues() {
|
func (m *mock) genMockValues() {
|
||||||
mult := 1.0
|
mult := 1.0
|
||||||
ledState := LedOff
|
ledState := LedOff
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
503
plugins/mqttclient/mqtt_test.go
Normal file
503
plugins/mqttclient/mqtt_test.go
Normal file
@@ -0,0 +1,503 @@
|
|||||||
|
package mqttclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fakeWriter struct {
|
||||||
|
lastKind string
|
||||||
|
lastID uint16
|
||||||
|
lastValue int16
|
||||||
|
lastSwitchState mk2driver.PanelSwitchState
|
||||||
|
lastCurrentLimit *float64
|
||||||
|
lastStandby *bool
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeWriter) WriteRAMVar(id uint16, value int16) error {
|
||||||
|
f.lastKind = commandKindRAMVar
|
||||||
|
f.lastID = id
|
||||||
|
f.lastValue = value
|
||||||
|
return f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeWriter) WriteSetting(id uint16, value int16) error {
|
||||||
|
f.lastKind = commandKindSetting
|
||||||
|
f.lastID = id
|
||||||
|
f.lastValue = value
|
||||||
|
return f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeWriter) SetPanelState(switchState mk2driver.PanelSwitchState, currentLimitA *float64) error {
|
||||||
|
f.lastKind = commandKindPanel
|
||||||
|
f.lastSwitchState = switchState
|
||||||
|
f.lastCurrentLimit = currentLimitA
|
||||||
|
return f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeWriter) SetStandby(standby bool) error {
|
||||||
|
f.lastKind = commandKindStandby
|
||||||
|
f.lastStandby = &standby
|
||||||
|
return f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decodeWriteCommand(t *testing.T) {
|
||||||
|
currentLimit := 16.5
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload string
|
||||||
|
check func(*testing.T, writeCommand)
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "setting",
|
||||||
|
payload: `{"request_id":"abc","kind":"setting","id":15,"value":-5}`,
|
||||||
|
check: func(t *testing.T, got writeCommand) {
|
||||||
|
assert.Equal(t, writeCommand{
|
||||||
|
Source: mk2driver.CommandSourceMQTT,
|
||||||
|
RequestID: "abc",
|
||||||
|
Kind: commandKindSetting,
|
||||||
|
ID: 15,
|
||||||
|
Value: -5,
|
||||||
|
}, got)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ram_var alias from type",
|
||||||
|
payload: `{"type":"ramvar","id":2,"value":7}`,
|
||||||
|
check: func(t *testing.T, got writeCommand) {
|
||||||
|
assert.Equal(t, writeCommand{
|
||||||
|
Source: mk2driver.CommandSourceMQTT,
|
||||||
|
Kind: commandKindRAMVar,
|
||||||
|
ID: 2,
|
||||||
|
Value: 7,
|
||||||
|
}, got)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "panel state",
|
||||||
|
payload: `{"kind":"panel_state","switch":"on","current_limit":16.5}`,
|
||||||
|
check: func(t *testing.T, got writeCommand) {
|
||||||
|
assert.Equal(t, commandKindPanel, got.Kind)
|
||||||
|
assert.True(t, got.HasSwitch)
|
||||||
|
assert.Equal(t, mk2driver.PanelSwitchOn, got.SwitchState)
|
||||||
|
assert.Equal(t, "on", got.SwitchName)
|
||||||
|
if assert.NotNil(t, got.CurrentLimitA) {
|
||||||
|
assert.Equal(t, currentLimit, *got.CurrentLimitA)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "panel current limit only",
|
||||||
|
payload: `{"kind":"panel_state","current_limit":12.0}`,
|
||||||
|
check: func(t *testing.T, got writeCommand) {
|
||||||
|
assert.Equal(t, commandKindPanel, got.Kind)
|
||||||
|
assert.False(t, got.HasSwitch)
|
||||||
|
assert.Nil(t, got.Standby)
|
||||||
|
if assert.NotNil(t, got.CurrentLimitA) {
|
||||||
|
assert.Equal(t, 12.0, *got.CurrentLimitA)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "standby bool",
|
||||||
|
payload: `{"kind":"standby","standby":true}`,
|
||||||
|
check: func(t *testing.T, got writeCommand) {
|
||||||
|
assert.Equal(t, commandKindStandby, got.Kind)
|
||||||
|
if assert.NotNil(t, got.Standby) {
|
||||||
|
assert.True(t, *got.Standby)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "standby using value string",
|
||||||
|
payload: `{"kind":"standby","value":"OFF"}`,
|
||||||
|
check: func(t *testing.T, got writeCommand) {
|
||||||
|
assert.Equal(t, commandKindStandby, got.Kind)
|
||||||
|
if assert.NotNil(t, got.Standby) {
|
||||||
|
assert.False(t, *got.Standby)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "missing id",
|
||||||
|
payload: `{"kind":"setting","value":1}`,
|
||||||
|
wantErr: `missing required field "id"`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "missing panel switch and current limit",
|
||||||
|
payload: `{"kind":"panel_state"}`,
|
||||||
|
wantErr: `missing required field "switch"`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid standby",
|
||||||
|
payload: `{"kind":"standby","value":"banana"}`,
|
||||||
|
wantErr: `field "standby" must be true/false`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid kind",
|
||||||
|
payload: `{"kind":"unknown","id":1,"value":1}`,
|
||||||
|
wantErr: `unsupported write command kind "unknown"`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := decodeWriteCommand([]byte(tt.payload))
|
||||||
|
if tt.wantErr != "" {
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.ErrorContains(t, err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
tt.check(t, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_executeWriteCommand(t *testing.T) {
|
||||||
|
limit := 8.0
|
||||||
|
standby := true
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
cmd writeCommand
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "setting",
|
||||||
|
cmd: writeCommand{
|
||||||
|
Kind: commandKindSetting,
|
||||||
|
ID: 9,
|
||||||
|
Value: 2,
|
||||||
|
},
|
||||||
|
want: commandKindSetting,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ram_var",
|
||||||
|
cmd: writeCommand{
|
||||||
|
Kind: commandKindRAMVar,
|
||||||
|
ID: 3,
|
||||||
|
Value: -1,
|
||||||
|
},
|
||||||
|
want: commandKindRAMVar,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "panel_state",
|
||||||
|
cmd: writeCommand{
|
||||||
|
Kind: commandKindPanel,
|
||||||
|
HasSwitch: true,
|
||||||
|
SwitchState: mk2driver.PanelSwitchInverterOnly,
|
||||||
|
CurrentLimitA: &limit,
|
||||||
|
},
|
||||||
|
want: commandKindPanel,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "standby",
|
||||||
|
cmd: writeCommand{
|
||||||
|
Kind: commandKindStandby,
|
||||||
|
Standby: &standby,
|
||||||
|
},
|
||||||
|
want: commandKindStandby,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
writer := &fakeWriter{}
|
||||||
|
err := executeWriteCommand(writer, tt.cmd)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.want, writer.lastKind)
|
||||||
|
switch tt.want {
|
||||||
|
case commandKindPanel:
|
||||||
|
assert.Equal(t, tt.cmd.SwitchState, writer.lastSwitchState)
|
||||||
|
if assert.NotNil(t, writer.lastCurrentLimit) {
|
||||||
|
assert.Equal(t, *tt.cmd.CurrentLimitA, *writer.lastCurrentLimit)
|
||||||
|
}
|
||||||
|
case commandKindStandby:
|
||||||
|
if assert.NotNil(t, writer.lastStandby) {
|
||||||
|
assert.Equal(t, *tt.cmd.Standby, *writer.lastStandby)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
assert.Equal(t, tt.cmd.ID, writer.lastID)
|
||||||
|
assert.Equal(t, tt.cmd.Value, writer.lastValue)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_buildHADiscoveryDefinitions(t *testing.T) {
|
||||||
|
cfg := Config{
|
||||||
|
Topic: "invertergui/updates",
|
||||||
|
CommandTopic: "invertergui/settings/set",
|
||||||
|
HomeAssistant: HomeAssistantConfig{
|
||||||
|
Enabled: true,
|
||||||
|
DiscoveryPrefix: "homeassistant",
|
||||||
|
NodeID: "victron_main",
|
||||||
|
DeviceName: "Shed Victron",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
definitions := buildHADiscoveryDefinitions(cfg)
|
||||||
|
assert.NotEmpty(t, definitions)
|
||||||
|
|
||||||
|
var panelMode *haDiscoveryDefinition
|
||||||
|
var panelCurrentLimit *haDiscoveryDefinition
|
||||||
|
var panelStandby *haDiscoveryDefinition
|
||||||
|
var batteryVoltage *haDiscoveryDefinition
|
||||||
|
for i := range definitions {
|
||||||
|
def := &definitions[i]
|
||||||
|
if def.Component == "select" && def.ObjectID == "remote_panel_mode" {
|
||||||
|
panelMode = def
|
||||||
|
}
|
||||||
|
if def.Component == "number" && def.ObjectID == "remote_panel_current_limit" {
|
||||||
|
panelCurrentLimit = def
|
||||||
|
}
|
||||||
|
if def.Component == "switch" && def.ObjectID == "remote_panel_standby" {
|
||||||
|
panelStandby = def
|
||||||
|
}
|
||||||
|
if def.Component == "sensor" && def.ObjectID == "battery_voltage" {
|
||||||
|
batteryVoltage = def
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if assert.NotNil(t, panelMode) {
|
||||||
|
assert.Equal(t, cfg.CommandTopic, panelMode.Config["command_topic"])
|
||||||
|
assert.Equal(t, haPanelSwitchStateTopic(cfg), panelMode.Config["state_topic"])
|
||||||
|
}
|
||||||
|
if assert.NotNil(t, panelCurrentLimit) {
|
||||||
|
assert.Equal(t, cfg.CommandTopic, panelCurrentLimit.Config["command_topic"])
|
||||||
|
assert.Equal(t, haCurrentLimitStateTopic(cfg), panelCurrentLimit.Config["state_topic"])
|
||||||
|
}
|
||||||
|
if assert.NotNil(t, panelStandby) {
|
||||||
|
assert.Equal(t, cfg.CommandTopic, panelStandby.Config["command_topic"])
|
||||||
|
assert.Equal(t, haStandbyStateTopic(cfg), panelStandby.Config["state_topic"])
|
||||||
|
}
|
||||||
|
if assert.NotNil(t, batteryVoltage) {
|
||||||
|
assert.Equal(t, cfg.Topic, batteryVoltage.Config["state_topic"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_panelStateCacheResolvePanelCommand(t *testing.T) {
|
||||||
|
cache := &panelStateCache{}
|
||||||
|
|
||||||
|
_, err := cache.resolvePanelCommand(writeCommand{
|
||||||
|
Kind: commandKindPanel,
|
||||||
|
CurrentLimitA: float64Ptr(12),
|
||||||
|
})
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
cache.remember(writeCommand{
|
||||||
|
Kind: commandKindPanel,
|
||||||
|
HasSwitch: true,
|
||||||
|
SwitchState: mk2driver.PanelSwitchOn,
|
||||||
|
SwitchName: "on",
|
||||||
|
})
|
||||||
|
|
||||||
|
resolved, err := cache.resolvePanelCommand(writeCommand{
|
||||||
|
Kind: commandKindPanel,
|
||||||
|
CurrentLimitA: float64Ptr(10),
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, resolved.HasSwitch)
|
||||||
|
assert.Equal(t, mk2driver.PanelSwitchOn, resolved.SwitchState)
|
||||||
|
assert.Equal(t, "on", resolved.SwitchName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_normalizeID(t *testing.T) {
|
||||||
|
assert.Equal(t, "victron_main_01", normalizeID("Victron Main #01"))
|
||||||
|
assert.Equal(t, "inverter-gui", normalizeID(" inverter-gui "))
|
||||||
|
assert.Equal(t, "", normalizeID(" "))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decodeVenusWriteCommand(t *testing.T) {
|
||||||
|
cfg := Config{
|
||||||
|
ClientID: "inverter-gui",
|
||||||
|
Venus: VenusConfig{
|
||||||
|
Enabled: true,
|
||||||
|
PortalID: "site01",
|
||||||
|
Service: "vebus/257",
|
||||||
|
GuideCompat: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
topic string
|
||||||
|
payload string
|
||||||
|
assertion func(*testing.T, writeCommand)
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "mode numeric",
|
||||||
|
topic: "W/site01/vebus/257/Mode",
|
||||||
|
payload: `{"value":3}`,
|
||||||
|
assertion: func(t *testing.T, cmd writeCommand) {
|
||||||
|
assert.Equal(t, commandKindPanel, cmd.Kind)
|
||||||
|
assert.True(t, cmd.HasSwitch)
|
||||||
|
assert.Equal(t, mk2driver.PanelSwitchOn, cmd.SwitchState)
|
||||||
|
assert.Equal(t, "on", cmd.SwitchName)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "current limit",
|
||||||
|
topic: "W/site01/vebus/257/Ac/ActiveIn/CurrentLimit",
|
||||||
|
payload: `{"value":16.5}`,
|
||||||
|
assertion: func(t *testing.T, cmd writeCommand) {
|
||||||
|
assert.Equal(t, commandKindPanel, cmd.Kind)
|
||||||
|
if assert.NotNil(t, cmd.CurrentLimitA) {
|
||||||
|
assert.Equal(t, 16.5, *cmd.CurrentLimitA)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "standby",
|
||||||
|
topic: "W/site01/vebus/257/Settings/Standby",
|
||||||
|
payload: `{"value":true}`,
|
||||||
|
assertion: func(t *testing.T, cmd writeCommand) {
|
||||||
|
assert.Equal(t, commandKindStandby, cmd.Kind)
|
||||||
|
if assert.NotNil(t, cmd.Standby) {
|
||||||
|
assert.True(t, *cmd.Standby)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid topic",
|
||||||
|
topic: "W/site01/vebus/257/Unknown",
|
||||||
|
payload: `{"value":1}`,
|
||||||
|
wantErr: "unsupported Venus write path",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "guide ess setpoint",
|
||||||
|
topic: "W/site01/settings/0/Settings/CGwacs/AcPowerSetPoint",
|
||||||
|
payload: `{"value":-1200}`,
|
||||||
|
assertion: func(t *testing.T, cmd writeCommand) {
|
||||||
|
assert.Equal(t, commandKindESSSet, cmd.Kind)
|
||||||
|
if assert.NotNil(t, cmd.FloatValue) {
|
||||||
|
assert.InDelta(t, -1200, *cmd.FloatValue, 0.01)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "guide ess mode with prefix",
|
||||||
|
topic: "victron/W/site01/settings/0/Settings/CGwacs/BatteryLife/State",
|
||||||
|
payload: `{"value":10}`,
|
||||||
|
assertion: func(t *testing.T, cmd writeCommand) {
|
||||||
|
assert.Equal(t, commandKindESSMode, cmd.Kind)
|
||||||
|
assert.Equal(t, int16(10), cmd.Value)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
testCfg := cfg
|
||||||
|
if tt.name == "guide ess mode with prefix" {
|
||||||
|
testCfg.Venus.TopicPrefix = "victron"
|
||||||
|
}
|
||||||
|
cmd, err := decodeVenusWriteCommand(testCfg, tt.topic, []byte(tt.payload))
|
||||||
|
if tt.wantErr != "" {
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.ErrorContains(t, err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.NoError(t, err)
|
||||||
|
tt.assertion(t, cmd)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_panelStateCacheRememberTracksFields(t *testing.T) {
|
||||||
|
cache := &panelStateCache{}
|
||||||
|
limit := 12.0
|
||||||
|
standby := true
|
||||||
|
|
||||||
|
cache.remember(writeCommand{
|
||||||
|
Kind: commandKindPanel,
|
||||||
|
HasSwitch: true,
|
||||||
|
SwitchName: "on",
|
||||||
|
SwitchState: mk2driver.PanelSwitchOn,
|
||||||
|
CurrentLimitA: &limit,
|
||||||
|
})
|
||||||
|
cache.remember(writeCommand{
|
||||||
|
Kind: commandKindStandby,
|
||||||
|
Standby: &standby,
|
||||||
|
})
|
||||||
|
|
||||||
|
s := cache.snapshot()
|
||||||
|
assert.True(t, s.HasSwitch)
|
||||||
|
assert.Equal(t, "on", s.Switch)
|
||||||
|
assert.True(t, s.HasCurrent)
|
||||||
|
assert.InDelta(t, 12.0, s.CurrentLimit, 0.001)
|
||||||
|
assert.True(t, s.HasStandby)
|
||||||
|
assert.True(t, s.Standby)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_historyTrackerSummary(t *testing.T) {
|
||||||
|
h := newHistoryTracker(2)
|
||||||
|
now := time.Now().UTC()
|
||||||
|
|
||||||
|
h.Add(telemetrySnapshot{
|
||||||
|
Timestamp: now,
|
||||||
|
InputPower: 100,
|
||||||
|
OutputPower: 90,
|
||||||
|
BatteryPower: -10,
|
||||||
|
BatteryVoltage: 25.0,
|
||||||
|
}, operatingStatePassthru, 0, nil)
|
||||||
|
summary := h.Add(telemetrySnapshot{
|
||||||
|
Timestamp: now.Add(1 * time.Second),
|
||||||
|
InputPower: 200,
|
||||||
|
OutputPower: 180,
|
||||||
|
BatteryPower: -20,
|
||||||
|
BatteryVoltage: 24.5,
|
||||||
|
}, operatingStateInverter, 2, &now)
|
||||||
|
|
||||||
|
assert.Equal(t, 2, summary.Samples)
|
||||||
|
assert.InDelta(t, 150, summary.AverageInputPower, 0.01)
|
||||||
|
assert.InDelta(t, 135, summary.AverageOutputPower, 0.01)
|
||||||
|
assert.InDelta(t, 180, summary.MaxOutputPower, 0.01)
|
||||||
|
assert.InDelta(t, 24.5, summary.MinBatteryVoltage, 0.01)
|
||||||
|
assert.Equal(t, uint64(2), summary.FaultCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_resolveESSWriteCommand(t *testing.T) {
|
||||||
|
ess := newESSControlCache()
|
||||||
|
telemetry := &telemetryCache{}
|
||||||
|
telemetry.set(telemetrySnapshot{InputVoltage: 230})
|
||||||
|
|
||||||
|
setpoint := 920.0
|
||||||
|
mapped, err := resolveESSWriteCommand(writeCommand{
|
||||||
|
Kind: commandKindESSSet,
|
||||||
|
FloatValue: &setpoint,
|
||||||
|
}, ess, telemetry)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
if assert.NotNil(t, mapped) {
|
||||||
|
assert.Equal(t, commandKindPanel, mapped.Kind)
|
||||||
|
assert.Equal(t, "charger_only", mapped.SwitchName)
|
||||||
|
if assert.NotNil(t, mapped.CurrentLimitA) {
|
||||||
|
assert.InDelta(t, 4.0, *mapped.CurrentLimitA, 0.01)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
maxDischarge := 1000.0
|
||||||
|
_, err = resolveESSWriteCommand(writeCommand{
|
||||||
|
Kind: commandKindESSMaxD,
|
||||||
|
FloatValue: &maxDischarge,
|
||||||
|
}, ess, telemetry)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
dischargeSetpoint := -2000.0
|
||||||
|
mapped, err = resolveESSWriteCommand(writeCommand{
|
||||||
|
Kind: commandKindESSSet,
|
||||||
|
FloatValue: &dischargeSetpoint,
|
||||||
|
}, ess, telemetry)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
if assert.NotNil(t, mapped) {
|
||||||
|
assert.Equal(t, commandKindPanel, mapped.Kind)
|
||||||
|
assert.Equal(t, "inverter_only", mapped.SwitchName)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,7 +36,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ func NewMunin(mk2 mk2driver.Mk2) *Munin {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Munin) ServeMuninHTTP(rw http.ResponseWriter, r *http.Request) {
|
func (m *Munin) ServeMuninHTTP(rw http.ResponseWriter, _ *http.Request) {
|
||||||
muninDat := <-m.muninResponse
|
muninDat := <-m.muninResponse
|
||||||
if muninDat.timesUpdated == 0 {
|
if muninDat.timesUpdated == 0 {
|
||||||
log.Error("No data returned")
|
log.Error("No data returned")
|
||||||
@@ -103,7 +103,7 @@ func (m *Munin) ServeMuninHTTP(rw http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Munin) ServeMuninConfigHTTP(rw http.ResponseWriter, r *http.Request) {
|
func (m *Munin) ServeMuninConfigHTTP(rw http.ResponseWriter, _ *http.Request) {
|
||||||
output := muninConfig
|
output := muninConfig
|
||||||
_, err := rw.Write([]byte(output))
|
_, err := rw.Write([]byte(output))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -127,7 +127,7 @@ func (m *Munin) run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Munin only samples once every 5 minutes so averages have to be calculated for some values.
|
// Munin only samples once every 5 minutes so averages have to be calculated for some values.
|
||||||
func calcMuninValues(m *muninData, newStatus *mk2driver.Mk2Info) {
|
func calcMuninValues(m *muninData, newStatus *mk2driver.Mk2Info) {
|
||||||
m.timesUpdated++
|
m.timesUpdated++
|
||||||
m.status.OutCurrent += newStatus.OutCurrent
|
m.status.OutCurrent += newStatus.OutCurrent
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
package munin
|
package munin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestServer(t *testing.T) {
|
func TestServer(_ *testing.T) {
|
||||||
|
|
||||||
mockMk2 := mk2driver.NewMk2Mock()
|
mockMk2 := mk2driver.NewMk2Mock()
|
||||||
muninServer := NewMunin(mockMk2)
|
muninServer := NewMunin(mockMk2)
|
||||||
@@ -21,7 +21,7 @@ func TestServer(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
_, err = ioutil.ReadAll(res.Body)
|
_, err = io.ReadAll(res.Body)
|
||||||
res.Body.Close()
|
res.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
package prometheus
|
package prometheus
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
package static
|
package static
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rakyll/statik/fs"
|
"embed"
|
||||||
|
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// New exports the static part of the webgui that is served via statik
|
//go:embed css js index.html favicon.ico
|
||||||
|
var content embed.FS
|
||||||
|
|
||||||
|
// New exports the static part of the webgui that is served via embed
|
||||||
func New() http.Handler {
|
func New() http.Handler {
|
||||||
statikFs, err := fs.New()
|
return http.FileServer(http.FS(content))
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return http.FileServer(statikFs)
|
|
||||||
}
|
}
|
||||||
|
|||||||
12
plugins/webui/static/css/bootstrap.min.css
vendored
Normal file
12
plugins/webui/static/css/bootstrap.min.css
vendored
Normal file
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
@@ -98,6 +98,117 @@
|
|||||||
<div class="alert alert-danger" role="alert" v-if="error.has_error">
|
<div class="alert alert-danger" role="alert" v-if="error.has_error">
|
||||||
{{ error.error_message }}
|
{{ error.error_message }}
|
||||||
</div>
|
</div>
|
||||||
|
<div
|
||||||
|
class="alert"
|
||||||
|
v-if="control.message !== ''"
|
||||||
|
v-bind:class="[control.has_error ? 'alert-danger' : 'alert-success']"
|
||||||
|
>
|
||||||
|
{{ control.message }}
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<div class="card mb-3">
|
||||||
|
<div class="card-body">
|
||||||
|
<h4 class="card-title">Remote Panel Control</h4>
|
||||||
|
<p class="text-muted mb-2">
|
||||||
|
Mode and current limit are applied together, equivalent to
|
||||||
|
<code>set_remote_panel_state</code>.
|
||||||
|
</p>
|
||||||
|
<p class="mb-1">
|
||||||
|
<strong>Current Mode:</strong>
|
||||||
|
{{ remoteModeLabel(state.remote_panel) }}
|
||||||
|
</p>
|
||||||
|
<p class="mb-1">
|
||||||
|
<strong>Current Limit:</strong>
|
||||||
|
{{ state.remote_panel.current_limit === null || state.remote_panel.current_limit === undefined ? 'Unknown' : state.remote_panel.current_limit + ' A' }}
|
||||||
|
</p>
|
||||||
|
<p class="mb-3">
|
||||||
|
<strong>Standby:</strong>
|
||||||
|
{{ remoteStandbyLabel(state.remote_panel) }}
|
||||||
|
</p>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-8">
|
||||||
|
<form v-on:submit.prevent="applyRemotePanelState">
|
||||||
|
<div class="form-row">
|
||||||
|
<div class="form-group col-md-6">
|
||||||
|
<label for="modeSelect">Remote Panel Mode</label>
|
||||||
|
<select
|
||||||
|
class="form-control"
|
||||||
|
id="modeSelect"
|
||||||
|
v-model="remote_form.mode"
|
||||||
|
v-bind:disabled="!state.remote_panel.writable || control.busy"
|
||||||
|
>
|
||||||
|
<option value="on">on</option>
|
||||||
|
<option value="off">off</option>
|
||||||
|
<option value="charger_only">charger_only</option>
|
||||||
|
<option value="inverter_only">inverter_only</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-group col-md-6">
|
||||||
|
<label for="currentLimitInput">AC Input Current Limit (A)</label>
|
||||||
|
<input
|
||||||
|
id="currentLimitInput"
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
step="0.1"
|
||||||
|
class="form-control"
|
||||||
|
v-model="remote_form.current_limit"
|
||||||
|
placeholder="leave blank to keep current"
|
||||||
|
v-bind:disabled="!state.remote_panel.writable || control.busy"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="btn btn-primary"
|
||||||
|
v-bind:disabled="!state.remote_panel.writable || control.busy"
|
||||||
|
>
|
||||||
|
Apply Mode + Current Limit
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-4">
|
||||||
|
<form v-on:submit.prevent="applyStandby">
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="form-check mt-4">
|
||||||
|
<input
|
||||||
|
id="standbySwitch"
|
||||||
|
type="checkbox"
|
||||||
|
class="form-check-input"
|
||||||
|
v-model="remote_form.standby"
|
||||||
|
v-bind:disabled="!state.remote_panel.writable || control.busy"
|
||||||
|
/>
|
||||||
|
<label class="form-check-label" for="standbySwitch">
|
||||||
|
Prevent sleep while off
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="btn btn-secondary"
|
||||||
|
v-bind:disabled="!state.remote_panel.writable || control.busy"
|
||||||
|
>
|
||||||
|
Apply Standby
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="mt-3 text-muted" v-if="state.remote_panel.last_updated">
|
||||||
|
Last update {{ state.remote_panel.last_updated }}
|
||||||
|
<span v-if="state.remote_panel.last_command">
|
||||||
|
({{ state.remote_panel.last_command }})
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="mt-2 text-danger" v-if="state.remote_panel.last_error">
|
||||||
|
{{ state.remote_panel.last_error }}
|
||||||
|
</div>
|
||||||
|
<div class="mt-2 text-warning" v-if="!state.remote_panel.writable">
|
||||||
|
Remote control is unavailable for this data source.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<hr />
|
<hr />
|
||||||
280
plugins/webui/static/js/controller.js
Normal file
280
plugins/webui/static/js/controller.js
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
var app;
|
||||||
|
const timeoutMax = 30000;
|
||||||
|
const timeoutMin = 1000;
|
||||||
|
var timeout = timeoutMin;
|
||||||
|
|
||||||
|
function defaultRemotePanelState() {
|
||||||
|
return {
|
||||||
|
writable: false,
|
||||||
|
mode: "unknown",
|
||||||
|
current_limit: null,
|
||||||
|
standby: null,
|
||||||
|
last_command: "",
|
||||||
|
last_error: "",
|
||||||
|
last_updated: ""
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function defaultState() {
|
||||||
|
return {
|
||||||
|
output_current: null,
|
||||||
|
output_voltage: 0,
|
||||||
|
output_frequency: 0,
|
||||||
|
output_power: 0,
|
||||||
|
input_current: 0,
|
||||||
|
input_voltage: 0,
|
||||||
|
input_frequency: 0,
|
||||||
|
input_power: 0,
|
||||||
|
battery_current: 0,
|
||||||
|
battery_voltage: 0,
|
||||||
|
battery_charge: 0,
|
||||||
|
battery_power: 0,
|
||||||
|
led_map: {
|
||||||
|
led_mains: "dot-off",
|
||||||
|
led_absorb: "dot-off",
|
||||||
|
led_bulk: "dot-off",
|
||||||
|
led_float: "dot-off",
|
||||||
|
led_inverter: "dot-off",
|
||||||
|
led_overload: "dot-off",
|
||||||
|
led_bat_low: "dot-off",
|
||||||
|
led_over_temp: "dot-off"
|
||||||
|
},
|
||||||
|
remote_panel: defaultRemotePanelState()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadContent() {
|
||||||
|
app = new Vue({
|
||||||
|
el: "#app",
|
||||||
|
data: {
|
||||||
|
error: {
|
||||||
|
has_error: false,
|
||||||
|
error_message: ""
|
||||||
|
},
|
||||||
|
control: {
|
||||||
|
busy: false,
|
||||||
|
has_error: false,
|
||||||
|
message: ""
|
||||||
|
},
|
||||||
|
remote_form: {
|
||||||
|
mode: "on",
|
||||||
|
current_limit: "",
|
||||||
|
standby: false
|
||||||
|
},
|
||||||
|
state: defaultState()
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
syncRemoteFormFromState: function(remoteState) {
|
||||||
|
if (!remoteState) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (remoteState.mode && remoteState.mode !== "unknown") {
|
||||||
|
this.remote_form.mode = remoteState.mode;
|
||||||
|
}
|
||||||
|
if (remoteState.current_limit === null || remoteState.current_limit === undefined) {
|
||||||
|
this.remote_form.current_limit = "";
|
||||||
|
} else {
|
||||||
|
this.remote_form.current_limit = String(remoteState.current_limit);
|
||||||
|
}
|
||||||
|
if (remoteState.standby === null || remoteState.standby === undefined) {
|
||||||
|
this.remote_form.standby = false;
|
||||||
|
} else {
|
||||||
|
this.remote_form.standby = !!remoteState.standby;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
remoteModeLabel: function(remoteState) {
|
||||||
|
var mode = (remoteState && remoteState.mode) || "unknown";
|
||||||
|
if (mode === "charger_only") {
|
||||||
|
return "Charger Only";
|
||||||
|
}
|
||||||
|
if (mode === "inverter_only") {
|
||||||
|
return "Inverter Only";
|
||||||
|
}
|
||||||
|
if (mode === "on") {
|
||||||
|
return "On";
|
||||||
|
}
|
||||||
|
if (mode === "off") {
|
||||||
|
return "Off";
|
||||||
|
}
|
||||||
|
return "Unknown";
|
||||||
|
},
|
||||||
|
remoteStandbyLabel: function(remoteState) {
|
||||||
|
if (!remoteState || remoteState.standby === null || remoteState.standby === undefined) {
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
|
return remoteState.standby ? "Enabled" : "Disabled";
|
||||||
|
},
|
||||||
|
refreshRemoteState: function() {
|
||||||
|
var self = this;
|
||||||
|
fetch(getAPIURI("api/remote-panel/state"))
|
||||||
|
.then(function(resp) {
|
||||||
|
if (!resp.ok) {
|
||||||
|
throw new Error("Could not load remote panel state.");
|
||||||
|
}
|
||||||
|
return resp.json();
|
||||||
|
})
|
||||||
|
.then(function(payload) {
|
||||||
|
self.state.remote_panel = payload;
|
||||||
|
self.syncRemoteFormFromState(payload);
|
||||||
|
})
|
||||||
|
.catch(function(err) {
|
||||||
|
self.control.has_error = true;
|
||||||
|
self.control.message = err.message;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
applyRemotePanelState: function() {
|
||||||
|
var self = this;
|
||||||
|
if (!self.state.remote_panel.writable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var body = {
|
||||||
|
mode: self.remote_form.mode
|
||||||
|
};
|
||||||
|
if (self.remote_form.current_limit !== "") {
|
||||||
|
var parsed = parseFloat(self.remote_form.current_limit);
|
||||||
|
if (isNaN(parsed)) {
|
||||||
|
self.control.has_error = true;
|
||||||
|
self.control.message = "Current limit must be numeric.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
body.current_limit = parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.control.busy = true;
|
||||||
|
self.control.has_error = false;
|
||||||
|
self.control.message = "";
|
||||||
|
fetch(getAPIURI("api/remote-panel/state"), {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body)
|
||||||
|
})
|
||||||
|
.then(function(resp) {
|
||||||
|
if (!resp.ok) {
|
||||||
|
return resp.text().then(function(text) {
|
||||||
|
throw new Error(text || "Failed to set remote panel mode/current limit.");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return resp.json();
|
||||||
|
})
|
||||||
|
.then(function(payload) {
|
||||||
|
self.state.remote_panel = payload;
|
||||||
|
self.syncRemoteFormFromState(payload);
|
||||||
|
self.control.has_error = false;
|
||||||
|
self.control.message = "Remote panel state updated.";
|
||||||
|
})
|
||||||
|
.catch(function(err) {
|
||||||
|
self.control.has_error = true;
|
||||||
|
self.control.message = err.message;
|
||||||
|
})
|
||||||
|
.finally(function() {
|
||||||
|
self.control.busy = false;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
applyStandby: function() {
|
||||||
|
var self = this;
|
||||||
|
if (!self.state.remote_panel.writable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.control.busy = true;
|
||||||
|
self.control.has_error = false;
|
||||||
|
self.control.message = "";
|
||||||
|
fetch(getAPIURI("api/remote-panel/standby"), {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
standby: !!self.remote_form.standby
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(function(resp) {
|
||||||
|
if (!resp.ok) {
|
||||||
|
return resp.text().then(function(text) {
|
||||||
|
throw new Error(text || "Failed to set standby mode.");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return resp.json();
|
||||||
|
})
|
||||||
|
.then(function(payload) {
|
||||||
|
self.state.remote_panel = payload;
|
||||||
|
self.syncRemoteFormFromState(payload);
|
||||||
|
self.control.has_error = false;
|
||||||
|
self.control.message = "Standby mode updated.";
|
||||||
|
})
|
||||||
|
.catch(function(err) {
|
||||||
|
self.control.has_error = true;
|
||||||
|
self.control.message = err.message;
|
||||||
|
})
|
||||||
|
.finally(function() {
|
||||||
|
self.control.busy = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.refreshRemoteState();
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
function connect() {
|
||||||
|
if (window["WebSocket"]) {
|
||||||
|
var conn = new WebSocket(getURI());
|
||||||
|
conn.onclose = function(evt) {
|
||||||
|
app.error.has_error = true;
|
||||||
|
app.error.error_message =
|
||||||
|
"Server not reachable. Trying to reconnect in " +
|
||||||
|
timeout / 1000 +
|
||||||
|
" second(s).";
|
||||||
|
|
||||||
|
console.log(app.error.error_message, evt.reason);
|
||||||
|
setTimeout(function() {
|
||||||
|
connect();
|
||||||
|
}, timeout);
|
||||||
|
timeout = timeout * 2;
|
||||||
|
if (timeout > timeoutMax) {
|
||||||
|
timeout = timeoutMax;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
conn.onopen = function() {
|
||||||
|
timeout = timeoutMin;
|
||||||
|
app.error.has_error = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
conn.onmessage = function(evt) {
|
||||||
|
var update = JSON.parse(evt.data);
|
||||||
|
app.state = update;
|
||||||
|
if (!app.control.busy) {
|
||||||
|
app.syncRemoteFormFromState(update.remote_panel);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
app.error.has_error = true;
|
||||||
|
app.error.error_message = "Our browser does not support WebSockets.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getURI() {
|
||||||
|
var loc = window.location,
|
||||||
|
new_uri;
|
||||||
|
if (loc.protocol === "https:") {
|
||||||
|
new_uri = "wss:";
|
||||||
|
} else {
|
||||||
|
new_uri = "ws:";
|
||||||
|
}
|
||||||
|
new_uri += "//" + loc.host;
|
||||||
|
new_uri += loc.pathname + "ws";
|
||||||
|
return new_uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAPIURI(path) {
|
||||||
|
var base = window.location.pathname;
|
||||||
|
if (base.slice(-1) !== "/") {
|
||||||
|
base += "/";
|
||||||
|
}
|
||||||
|
return base + path.replace(/^\/+/, "");
|
||||||
|
}
|
||||||
6
plugins/webui/static/js/vue.js
Normal file
6
plugins/webui/static/js/vue.js
Normal file
File diff suppressed because one or more lines are too long
12
plugins/webui/static/root/css/bootstrap.min.css
vendored
12
plugins/webui/static/root/css/bootstrap.min.css
vendored
File diff suppressed because one or more lines are too long
@@ -1,88 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
|
||||||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; img-src data:; connect-src 'self'">
|
|
||||||
<title>Page not found · GitHub Pages</title>
|
|
||||||
<style type="text/css" media="screen">
|
|
||||||
body {
|
|
||||||
background-color: #f1f1f1;
|
|
||||||
margin: 0;
|
|
||||||
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
.container { margin: 50px auto 40px auto; width: 600px; text-align: center; }
|
|
||||||
|
|
||||||
a { color: #4183c4; text-decoration: none; }
|
|
||||||
a:hover { text-decoration: underline; }
|
|
||||||
|
|
||||||
h1 { width: 800px; position:relative; left: -100px; letter-spacing: -1px; line-height: 60px; font-size: 60px; font-weight: 100; margin: 0px 0 50px 0; text-shadow: 0 1px 0 #fff; }
|
|
||||||
p { color: rgba(0, 0, 0, 0.5); margin: 20px 0; line-height: 1.6; }
|
|
||||||
|
|
||||||
ul { list-style: none; margin: 25px 0; padding: 0; }
|
|
||||||
li { display: table-cell; font-weight: bold; width: 1%; }
|
|
||||||
|
|
||||||
.logo { display: inline-block; margin-top: 35px; }
|
|
||||||
.logo-img-2x { display: none; }
|
|
||||||
@media
|
|
||||||
only screen and (-webkit-min-device-pixel-ratio: 2),
|
|
||||||
only screen and ( min--moz-device-pixel-ratio: 2),
|
|
||||||
only screen and ( -o-min-device-pixel-ratio: 2/1),
|
|
||||||
only screen and ( min-device-pixel-ratio: 2),
|
|
||||||
only screen and ( min-resolution: 192dpi),
|
|
||||||
only screen and ( min-resolution: 2dppx) {
|
|
||||||
.logo-img-1x { display: none; }
|
|
||||||
.logo-img-2x { display: inline-block; }
|
|
||||||
}
|
|
||||||
|
|
||||||
#suggestions {
|
|
||||||
margin-top: 35px;
|
|
||||||
color: #ccc;
|
|
||||||
}
|
|
||||||
#suggestions a {
|
|
||||||
color: #666666;
|
|
||||||
font-weight: 200;
|
|
||||||
font-size: 14px;
|
|
||||||
margin: 0 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<div class="container">
|
|
||||||
|
|
||||||
<h1>404</h1>
|
|
||||||
<p><strong>File not found</strong></p>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
The site configured at this address does not
|
|
||||||
contain the requested file.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
If this is your site, make sure that the filename case matches the URL.<br>
|
|
||||||
For root URLs (like <code>http://example.com/</code>) you must provide an
|
|
||||||
<code>index.html</code> file.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<a href="https://help.github.com/pages/">Read the full documentation</a>
|
|
||||||
for more information about using <strong>GitHub Pages</strong>.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<div id="suggestions">
|
|
||||||
<a href="https://githubstatus.com">GitHub Status</a> —
|
|
||||||
<a href="https://twitter.com/githubstatus">@githubstatus</a>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<a href="/" class="logo logo-img-1x">
|
|
||||||
<img width="32" height="32" title="" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoTWFjaW50b3NoKSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpFMTZCRDY3REIzRjAxMUUyQUQzREIxQzRENUFFNUM5NiIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpFMTZCRDY3RUIzRjAxMUUyQUQzREIxQzRENUFFNUM5NiI+IDx4bXBNTTpEZXJpdmVkRnJvbSBzdFJlZjppbnN0YW5jZUlEPSJ4bXAuaWlkOkUxNkJENjdCQjNGMDExRTJBRDNEQjFDNEQ1QUU1Qzk2IiBzdFJlZjpkb2N1bWVudElEPSJ4bXAuZGlkOkUxNkJENjdDQjNGMDExRTJBRDNEQjFDNEQ1QUU1Qzk2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+SM9MCAAAA+5JREFUeNrEV11Ik1EY3s4+ddOp29Q5b0opCgKFsoKoi5Kg6CIhuwi6zLJLoYLopq4qsKKgi4i6CYIoU/q5iDAKs6syoS76IRWtyJ+p7cdt7sf1PGOD+e0c3dygAx/67ZzzPM95/877GYdHRg3ZjMXFxepQKNS6sLCwJxqNNuFpiMfjVs4ZjUa/pmmjeD6VlJS8NpvNT4QQ7mxwjSsJiEQim/1+/9lgMHgIr5ohuxG1WCw9Vqv1clFR0dCqBODElV6v90ogEDjGdYbVjXhpaendioqK07CIR7ZAqE49PT09BPL2PMgTByQGsYiZlQD4uMXtdr+JxWINhgINYhGT2MsKgMrm2dnZXgRXhaHAg5jEJodUAHxux4LudHJE9RdEdA+i3Juz7bGHe4mhE9FNrgwBCLirMFV9Okh5eflFh8PR5nK5nDabrR2BNJlKO0T35+Li4n4+/J+/JQCxhmu5h3uJoXNHPbmWZAHMshWB8l5/ipqammaAf0zPDDx1ONV3vurdidqwAQL+pEc8sLcAe1CCvQ3YHxIW8Pl85xSWNC1hADDIv0rIE/o4J0k3kww4xSlwIhcq3EFFOm7KN/hUGOQkt0CFa5WpNJlMvxBEz/IVQAxg/ZRZl9wiHA63yDYieM7DnLP5CiAGsC7I5sgtYKJGWe2A8seFqgFJrJjEPY1Cn3pJ8/9W1e5VWsFDTEmFrBcoDhZJEQkXuhICMyKpjhahqN21hRYATKfUOlDmkygrR4o4C0VOLGJKrOITKB4jijzdXygBKixyC5TDQdnk/Pz8qRw6oOWGlsTKGOQW6OH6FBWsyePxdOXLTgxiyebILZCjz+GLgMIKnXNzc49YMlcRdHXcSwxFVgTInQhC9G33UhNoJLuqq6t345p9y3eUy8OTk5PjAHuI9uo4b07FBaOhsu0A4Unc+T1TU1Nj3KsSSE5yJ65jqF2DDd8QqWYmAZrIM2VlZTdnZmb6AbpdV9V6ec9znf5Q7HjYumdRE0JOp3MjitO4SFa+cZz8Umqe3TCbSLvdfkR/kWDdNQl5InuTcysOcpFT35ZrbBxx4p3JAHlZVVW1D/634VRt+FvLBgK/v5LV9WS+10xMTEwtRw7XvqOL+e2Q8V3AYIOIAXQ26/heWVnZCVfcyKHg2CBgTpmPmjYM8l24GyaUHyaIh7XwfR9ErE8qHoDfn2LTNAVC0HX6MFcBIP8Bi+6F6cdW/DICkANRfx99fEYFQ7Nph5i/uQiA214gno7K+guhaiKg9gC62+M8eR7XsBsYJ4ilam60Fb7r7uAj8wFyuwM1oIOWgfmDy6RXEEQzJMPe23DXrVS7rtyD3Df8z/FPgAEAzWU5Ku59ZAUAAAAASUVORK5CYII=">
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<a href="/" class="logo logo-img-2x">
|
|
||||||
<img width="32" height="32" title="" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoTWFjaW50b3NoKSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpEQUM1QkUxRUI0MUMxMUUyQUQzREIxQzRENUFFNUM5NiIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpEQUM1QkUxRkI0MUMxMUUyQUQzREIxQzRENUFFNUM5NiI+IDx4bXBNTTpEZXJpdmVkRnJvbSBzdFJlZjppbnN0YW5jZUlEPSJ4bXAuaWlkOkUxNkJENjdGQjNGMDExRTJBRDNEQjFDNEQ1QUU1Qzk2IiBzdFJlZjpkb2N1bWVudElEPSJ4bXAuZGlkOkUxNkJENjgwQjNGMDExRTJBRDNEQjFDNEQ1QUU1Qzk2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+hfPRaQAAB6lJREFUeNrsW2mME2UYbodtt+2222u35QheoCCYGBQligIJgkZJNPzgigoaTEj8AdFEMfADfyABkgWiiWcieK4S+QOiHAYUj2hMNKgYlEujpNttu9vttbvdw+chU1K6M535pt3ubHCSyezR+b73eb73+t7vrfXsufOW4bz6+vom9/b23ovnNNw34b5xYGAgODg46Mbt4mesVmsWd1qSpHhdXd2fuP/Afcput5/A88xwymcdBgLqenp6FuRyuWV4zu/v759QyWBjxoz5t76+/gun09mK5xFyakoCAPSaTCazNpvNPoYVbh6O1YKGRF0u13sNDQ27QMzfpiAAKj0lnU6/gBVfAZW2WWpwwVzy0IgP3G73FpjI6REhAGA9qVRqA1b9mVoBVyIC2tDi8Xg24+dUzQiAbS/s7Ox8G2o/3mKCC+Zw0efzPQEfcVjYrARX3dbV1bUtHo8fMgt42f+Mp0yUTVQbdWsAHVsikdiHkHaPxcQXQufXgUBgMRxme9U0AAxfH4vFvjM7eF6UkbJS5qoQwEQGA57Ac5JllFyUVZZ5ckUEgMVxsK2jlSYzI+QXJsiyjzNEAJyJAzb/KQa41jJKL8pODMQiTEAymXw5n8/P0IjD3bh7Rgog59aanxiIRTVvV/oj0tnHca/WMrVwODwB3raTGxzkBg/gnZVapFV62Wy2n5AO70HM/5wbJ0QnXyQSaVPDIuNZzY0V3ntHMwxiwHA0Gj2Np7ecIBDgaDAYXKCQJM1DhrgJ3nhulcPbl8j4NmHe46X/g60fwbz3aewjkqFQaAqebWU1AOqyQwt8Id6qEHMc97zu7u7FGGsn7HAiVuosVw7P35C1nccdgSCxop1dHeZswmfHMnxBo6ZTk+jN8dl/vF7vWofDsa+MLN9oEUBMxOb3+1eoEsBVw6Zmua49r8YmhAKDiEPcMwBsxMiqQ+ixzPFxZyqRpXARG/YOr1ObFJ0gUskXBbamcR1OKmMUvDxHRAu8/LmY3jFLMUpFqz9HxG65smYJdyKyECOxDiEAe/p1gjF2oonivZAsxVgl2daa4EQWCW6J55qFAFFZiJWYLxNQy2qOSUzGRsyXCUDIeliwAHEO4WSlWQBRFoZakXcKmCXmyXAKs0Ve9vl8q42WoIYpJU4hV3hKcNs8m9gl7p/xQ73eF5kB4j5mNrWmTJRNwAzqiV1CxjVTZCIkEq+Z1bZFZSN2CenmVAFVy4Plz8xKAGWjjAKFk6lCBMDR/MJjLLMSQNm43xAiQKTaA+9/wewhDjL+JVI1kkTSSOTcKbMTwPqESAot6dn6Fr1gHwVJju6IRuyiByPuUUBAg5DGkAgBmxlvdgIEK9gDkohdY/BJo4CAG0R8miRSsGABkgVQs4KXu098IgUXSSRsFAoKZiVAVDY2WUiiPTjYRi41KwGisrGsLtlsth8Fiwnz2fBkQvWfRtlE3iF2yW63/yCacXZ1dW02GwGyTFaRd4idJnCKHRaCxYRHoG5LTKT6SyiToP1fJHbmAYPYRR0UnZQtMnA6s0zg+GZBlt0Gdo7EPHgpE3Q6nZ8YyLhc8Xj8MJh/aKTAY+5FPAKHLE7RdwuYJZmNwzyCMkBCYyKROJBMJl9B/PXXCjjmCmDOVzH3fiPpObEWGqoKe4EBl8v1hlqsdLvd23mkxHM9pc9kMpmno9HoeTii7ewbHEZPPx1ztLS1tV3AnGuMjiNjvbQFuHw6zDo5By7dTPAQNBgMLrRarTkSls1mnwT7uwp9virx9QzbW/HuV/j5d/b+6jniKlllP8lkeONJDk+dq9GsQTnC4fB1heO0K47Hwe7WdDr9nAKgXwOBwHI+C45Htj1d6sd429TUNEcmUdc+PRaLHcvn87dXW4ugzdsaGxufL94NFv9zi1J7GVbhlvb2dnaJ3SVrxfc+n2+NTsZ7/H7/Mr3g5XdSIHyJSH1PZ+7fToyl2+ErqilgZ4NaLYB9goVGaHjR93Hv1ZrU4XDsFT20kH3PObzbWk0CgG1jacVIUnAQb9F+VexyLMzkpcLv0IJV7AHQIOCAUYHx7v5qgScmYHtTqSAyZLEJTK22Bie4iq3xsqpm4SAf9Hq9a2DnJ4uLK3SEULcdRvp3i3zHySqpficxEdsQc1NrlYXXvR+O7qASSezXB+h1SuUomgg9LL8BUoV4749EIolKh+EiqWmqVEZlDgHks2pxHw7xTqUQw9J5NcAXOK10AGIoZ6Zli6JY6Z1Q461KoZ4NiKLHarW+KDsxlDUPHZ5zPQZqUVDPJsTqb5n9malbpAh8C2XXDLl62+WZIDFRUlNVOiwencnNU3aQEkL+cDMSoLvZo2fQB7AJssNAuFuvorlDVVkkg2I87+jo2K2QAVphDrfyViK5VqtO34OkaxXCp+7drdDBCAdubm6eidX+2WwqT5komwh4YQLk+H4aE93h8Xg2gvHekQZOGSgLZTLyDTLJ4Lx9/KZWKBSainT4Iy3FqQBfnUZR42PKQFksBr9QKVXCPusD3OiA/RkQ5kP8qV/Jl1WywAp/6+dcmPM2zL1UrUahe4JqfnWWKXIul3uUbfP8njAFLW1OFr3gdFtZ72cNH+PtQT7/brW+NXqJAHh0y9V8/U/A1U7AfwIMAD7mS3pCbuWJAAAAAElFTkSuQmCC">
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
var app;
|
|
||||||
const timeoutMax = 30000;
|
|
||||||
const timeoutMin = 1000;
|
|
||||||
var timeout = timeoutMin;
|
|
||||||
|
|
||||||
function loadContent() {
|
|
||||||
app = new Vue({
|
|
||||||
el: "#app",
|
|
||||||
data: {
|
|
||||||
error: {
|
|
||||||
has_error: false,
|
|
||||||
error_message: ""
|
|
||||||
},
|
|
||||||
state: {
|
|
||||||
output_current: null,
|
|
||||||
output_voltage: 0,
|
|
||||||
output_frequency: 0,
|
|
||||||
output_power: 0,
|
|
||||||
input_current: 0,
|
|
||||||
input_voltage: 0,
|
|
||||||
input_frequency: 0,
|
|
||||||
input_power: 0,
|
|
||||||
battery_current: 0,
|
|
||||||
battery_voltage: 0,
|
|
||||||
battery_charge: 0,
|
|
||||||
battery_power: 0,
|
|
||||||
led_map: [
|
|
||||||
{ led_mains: "dot-off" },
|
|
||||||
{ led_absorb: "dot-off" },
|
|
||||||
{ led_bulk: "dot-off" },
|
|
||||||
{ led_float: "dot-off" },
|
|
||||||
{ led_inverter: "dot-off" },
|
|
||||||
{ led_overload: "dot-off" },
|
|
||||||
{ led_bat_low: "dot-off" },
|
|
||||||
{ led_over_temp: "dot-off" }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
function connect() {
|
|
||||||
if (window["WebSocket"]) {
|
|
||||||
var conn = new WebSocket(getURI());
|
|
||||||
conn.onclose = function(evt) {
|
|
||||||
app.error.has_error = true;
|
|
||||||
app.error.error_message =
|
|
||||||
"Server not reachable. Trying to reconnect in " +
|
|
||||||
timeout / 1000 +
|
|
||||||
" second(s).";
|
|
||||||
|
|
||||||
console.log(app.error.error_message, evt.reason);
|
|
||||||
setTimeout(function() {
|
|
||||||
connect();
|
|
||||||
}, timeout);
|
|
||||||
timeout = timeout * 2;
|
|
||||||
if (timeout > timeoutMax) {
|
|
||||||
timeout = timeoutMax;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
conn.onopen = function(evt) {
|
|
||||||
timeout = timeoutMin;
|
|
||||||
app.error.has_error = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
conn.onmessage = function(evt) {
|
|
||||||
var update = JSON.parse(evt.data);
|
|
||||||
app.state = update;
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
app.error.has_error = true;
|
|
||||||
app.error.error_message = "Our browser does not support WebSockets.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getURI() {
|
|
||||||
var loc = window.location,
|
|
||||||
new_uri;
|
|
||||||
if (loc.protocol === "https:") {
|
|
||||||
new_uri = "wss:";
|
|
||||||
} else {
|
|
||||||
new_uri = "ws:";
|
|
||||||
}
|
|
||||||
new_uri += "//" + loc.host;
|
|
||||||
new_uri += loc.pathname + "ws";
|
|
||||||
return new_uri;
|
|
||||||
}
|
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -31,13 +31,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
package webui
|
package webui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
"github.com/diebietse/invertergui/websocket"
|
"git.coadcorp.com/nathan/invertergui/websocket"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -51,25 +53,54 @@ const (
|
|||||||
BlinkGreen = "blink-green"
|
BlinkGreen = "blink-green"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
modeChargerOnly = "charger_only"
|
||||||
|
modeInverterOnly = "inverter_only"
|
||||||
|
modeOn = "on"
|
||||||
|
modeOff = "off"
|
||||||
|
modeUnknown = "unknown"
|
||||||
|
)
|
||||||
|
|
||||||
type WebGui struct {
|
type WebGui struct {
|
||||||
mk2driver.Mk2
|
mk2driver.Mk2
|
||||||
|
writer mk2driver.SettingsWriter
|
||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
|
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
hub *websocket.Hub
|
hub *websocket.Hub
|
||||||
|
|
||||||
|
stateMu sync.RWMutex
|
||||||
|
latest *templateInput
|
||||||
|
remote remotePanelState
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebGui(source mk2driver.Mk2) *WebGui {
|
func NewWebGui(source mk2driver.Mk2, writer mk2driver.SettingsWriter) *WebGui {
|
||||||
w := &WebGui{
|
w := &WebGui{
|
||||||
stopChan: make(chan struct{}),
|
stopChan: make(chan struct{}),
|
||||||
Mk2: source,
|
Mk2: source,
|
||||||
|
writer: writer,
|
||||||
hub: websocket.NewHub(),
|
hub: websocket.NewHub(),
|
||||||
|
remote: remotePanelState{
|
||||||
|
Writable: writer != nil,
|
||||||
|
Mode: modeUnknown,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
log.WithField("remote_writable", writer != nil).Info("Web UI initialized")
|
||||||
w.wg.Add(1)
|
w.wg.Add(1)
|
||||||
go w.dataPoll()
|
go w.dataPoll()
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type remotePanelState struct {
|
||||||
|
Writable bool `json:"writable"`
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
CurrentLimit *float64 `json:"current_limit,omitempty"`
|
||||||
|
Standby *bool `json:"standby,omitempty"`
|
||||||
|
LastCommand string `json:"last_command,omitempty"`
|
||||||
|
LastError string `json:"last_error,omitempty"`
|
||||||
|
LastUpdated string `json:"last_updated,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
type templateInput struct {
|
type templateInput struct {
|
||||||
Error []error `json:"errors"`
|
Error []error `json:"errors"`
|
||||||
|
|
||||||
@@ -94,12 +125,170 @@ type templateInput struct {
|
|||||||
OutFreq string `json:"output_frequency"`
|
OutFreq string `json:"output_frequency"`
|
||||||
|
|
||||||
LedMap map[string]string `json:"led_map"`
|
LedMap map[string]string `json:"led_map"`
|
||||||
|
|
||||||
|
RemotePanel remotePanelState `json:"remote_panel"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type setRemotePanelStateRequest struct {
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
CurrentLimit *float64 `json:"current_limit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type setRemotePanelStandbyRequest struct {
|
||||||
|
Standby bool `json:"standby"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WebGui) ServeHub(rw http.ResponseWriter, r *http.Request) {
|
func (w *WebGui) ServeHub(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"remote": r.RemoteAddr,
|
||||||
|
"path": r.URL.Path,
|
||||||
|
}).Debug("WebSocket hub request")
|
||||||
w.hub.ServeHTTP(rw, r)
|
w.hub.ServeHTTP(rw, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WebGui) ServeRemotePanelState(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"method": r.Method,
|
||||||
|
"path": r.URL.Path,
|
||||||
|
"remote": r.RemoteAddr,
|
||||||
|
}).Debug("Remote panel state API request")
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
writeJSON(rw, http.StatusOK, w.getRemotePanelState())
|
||||||
|
case http.MethodPost:
|
||||||
|
w.handleSetRemotePanelState(rw, r)
|
||||||
|
default:
|
||||||
|
log.WithField("method", r.Method).Warn("Remote panel state API received unsupported method")
|
||||||
|
http.Error(rw, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebGui) ServeRemotePanelStandby(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"method": r.Method,
|
||||||
|
"path": r.URL.Path,
|
||||||
|
"remote": r.RemoteAddr,
|
||||||
|
}).Debug("Remote panel standby API request")
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
writeJSON(rw, http.StatusOK, w.getRemotePanelState())
|
||||||
|
case http.MethodPost:
|
||||||
|
w.handleSetRemotePanelStandby(rw, r)
|
||||||
|
default:
|
||||||
|
log.WithField("method", r.Method).Warn("Remote panel standby API received unsupported method")
|
||||||
|
http.Error(rw, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebGui) handleSetRemotePanelState(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
if w.writer == nil {
|
||||||
|
log.Warn("Remote panel state write requested, but writer is unavailable")
|
||||||
|
http.Error(rw, "remote control is not supported by this data source", http.StatusNotImplemented)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
req := setRemotePanelStateRequest{}
|
||||||
|
if err := decodeJSONBody(r, &req); err != nil {
|
||||||
|
log.WithError(err).Warn("Invalid remote panel state request body")
|
||||||
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logEntry := log.WithField("mode", req.Mode)
|
||||||
|
if req.CurrentLimit != nil {
|
||||||
|
logEntry = logEntry.WithField("current_limit_a", *req.CurrentLimit)
|
||||||
|
}
|
||||||
|
logEntry.Info("Applying remote panel state from API")
|
||||||
|
|
||||||
|
switchState, normalizedMode, err := parsePanelMode(req.Mode)
|
||||||
|
if err != nil {
|
||||||
|
logEntry.WithError(err).Warn("Unsupported remote panel mode")
|
||||||
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var setErr error
|
||||||
|
if sourceAware, ok := w.writer.(mk2driver.SourceAwareSettingsWriter); ok {
|
||||||
|
setErr = sourceAware.SetPanelStateWithSource(mk2driver.CommandSourceUI, switchState, req.CurrentLimit)
|
||||||
|
} else {
|
||||||
|
setErr = w.writer.SetPanelState(switchState, req.CurrentLimit)
|
||||||
|
}
|
||||||
|
if setErr != nil {
|
||||||
|
logEntry.WithError(setErr).Error("Failed to apply remote panel state")
|
||||||
|
w.updateRemotePanelState(func(state *remotePanelState) {
|
||||||
|
state.LastCommand = "set_remote_panel_state"
|
||||||
|
state.LastError = setErr.Error()
|
||||||
|
})
|
||||||
|
http.Error(rw, setErr.Error(), http.StatusBadGateway)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.updateRemotePanelState(func(state *remotePanelState) {
|
||||||
|
state.Mode = normalizedMode
|
||||||
|
state.CurrentLimit = copyFloat64Ptr(req.CurrentLimit)
|
||||||
|
state.LastCommand = "set_remote_panel_state"
|
||||||
|
state.LastError = ""
|
||||||
|
})
|
||||||
|
logEntry.WithField("normalized_mode", normalizedMode).Info("Remote panel state applied")
|
||||||
|
writeJSON(rw, http.StatusOK, w.getRemotePanelState())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebGui) handleSetRemotePanelStandby(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
if w.writer == nil {
|
||||||
|
log.Warn("Remote panel standby write requested, but writer is unavailable")
|
||||||
|
http.Error(rw, "remote control is not supported by this data source", http.StatusNotImplemented)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
req := setRemotePanelStandbyRequest{}
|
||||||
|
if err := decodeJSONBody(r, &req); err != nil {
|
||||||
|
log.WithError(err).Warn("Invalid remote panel standby request body")
|
||||||
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.WithField("standby", req.Standby).Info("Applying standby state from API")
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if sourceAware, ok := w.writer.(mk2driver.SourceAwareSettingsWriter); ok {
|
||||||
|
err = sourceAware.SetStandbyWithSource(mk2driver.CommandSourceUI, req.Standby)
|
||||||
|
} else {
|
||||||
|
err = w.writer.SetStandby(req.Standby)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).WithField("standby", req.Standby).Error("Failed to apply standby state")
|
||||||
|
w.updateRemotePanelState(func(state *remotePanelState) {
|
||||||
|
state.LastCommand = "set_remote_panel_standby"
|
||||||
|
state.LastError = err.Error()
|
||||||
|
})
|
||||||
|
http.Error(rw, err.Error(), http.StatusBadGateway)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.updateRemotePanelState(func(state *remotePanelState) {
|
||||||
|
state.Standby = copyBoolPtr(&req.Standby)
|
||||||
|
state.LastCommand = "set_remote_panel_standby"
|
||||||
|
state.LastError = ""
|
||||||
|
})
|
||||||
|
log.WithField("standby", req.Standby).Info("Standby state applied")
|
||||||
|
writeJSON(rw, http.StatusOK, w.getRemotePanelState())
|
||||||
|
}
|
||||||
|
|
||||||
|
func parsePanelMode(raw string) (mk2driver.PanelSwitchState, string, error) {
|
||||||
|
switch strings.TrimSpace(strings.ToLower(raw)) {
|
||||||
|
case modeChargerOnly:
|
||||||
|
return mk2driver.PanelSwitchChargerOnly, modeChargerOnly, nil
|
||||||
|
case modeInverterOnly:
|
||||||
|
return mk2driver.PanelSwitchInverterOnly, modeInverterOnly, nil
|
||||||
|
case modeOn:
|
||||||
|
return mk2driver.PanelSwitchOn, modeOn, nil
|
||||||
|
case modeOff:
|
||||||
|
return mk2driver.PanelSwitchOff, modeOff, nil
|
||||||
|
default:
|
||||||
|
return 0, "", fmt.Errorf("unsupported panel mode %q", raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ledName(led mk2driver.Led) string {
|
func ledName(led mk2driver.Led) string {
|
||||||
name, ok := mk2driver.LedNames[led]
|
name, ok := mk2driver.LedNames[led]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -158,21 +347,32 @@ func buildTemplateInput(status *mk2driver.Mk2Info) *templateInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *WebGui) Stop() {
|
func (w *WebGui) Stop() {
|
||||||
|
log.Info("Stopping Web UI polling")
|
||||||
close(w.stopChan)
|
close(w.stopChan)
|
||||||
w.wg.Wait()
|
w.wg.Wait()
|
||||||
|
log.Info("Web UI polling stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
// dataPoll waits for data from the w.poller channel. It will send its currently stored status
|
|
||||||
// to respChan if anything reads from it.
|
|
||||||
func (w *WebGui) dataPoll() {
|
func (w *WebGui) dataPoll() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case s := <-w.C():
|
case s := <-w.C():
|
||||||
if s.Valid {
|
if s == nil {
|
||||||
err := w.hub.Broadcast(buildTemplateInput(s))
|
log.Debug("Skipping nil MK2 update in Web UI poller")
|
||||||
if err != nil {
|
continue
|
||||||
log.Errorf("Could not send update to clients: %v", err)
|
}
|
||||||
}
|
if !s.Valid {
|
||||||
|
log.WithField("errors", len(s.Errors)).Debug("Skipping invalid MK2 update in Web UI poller")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := buildTemplateInput(s)
|
||||||
|
w.stateMu.Lock()
|
||||||
|
payload.RemotePanel = w.remote
|
||||||
|
w.latest = payload
|
||||||
|
w.stateMu.Unlock()
|
||||||
|
if err := w.hub.Broadcast(payload); err != nil {
|
||||||
|
log.Errorf("Could not send update to clients: %v", err)
|
||||||
}
|
}
|
||||||
case <-w.stopChan:
|
case <-w.stopChan:
|
||||||
w.wg.Done()
|
w.wg.Done()
|
||||||
@@ -180,3 +380,100 @@ func (w *WebGui) dataPoll() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WebGui) getRemotePanelState() remotePanelState {
|
||||||
|
w.stateMu.RLock()
|
||||||
|
defer w.stateMu.RUnlock()
|
||||||
|
return copyRemotePanelState(w.remote)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebGui) updateRemotePanelState(update func(state *remotePanelState)) {
|
||||||
|
w.stateMu.Lock()
|
||||||
|
update(&w.remote)
|
||||||
|
w.remote.LastUpdated = time.Now().UTC().Format(time.RFC3339)
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"mode": w.remote.Mode,
|
||||||
|
"has_limit": w.remote.CurrentLimit != nil,
|
||||||
|
"has_standby": w.remote.Standby != nil,
|
||||||
|
"last_command": w.remote.LastCommand,
|
||||||
|
"last_error": w.remote.LastError,
|
||||||
|
}).Debug("Updated remote panel state cache")
|
||||||
|
snapshot := w.snapshotLocked()
|
||||||
|
w.stateMu.Unlock()
|
||||||
|
|
||||||
|
if snapshot != nil {
|
||||||
|
if err := w.hub.Broadcast(snapshot); err != nil {
|
||||||
|
log.Errorf("Could not send control update to clients: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WebGui) snapshotLocked() *templateInput {
|
||||||
|
if w.latest == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot := cloneTemplateInput(w.latest)
|
||||||
|
snapshot.RemotePanel = copyRemotePanelState(w.remote)
|
||||||
|
return snapshot
|
||||||
|
}
|
||||||
|
|
||||||
|
func cloneTemplateInput(in *templateInput) *templateInput {
|
||||||
|
if in == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := *in
|
||||||
|
|
||||||
|
if in.Error != nil {
|
||||||
|
out.Error = append([]error(nil), in.Error...)
|
||||||
|
}
|
||||||
|
if in.LedMap != nil {
|
||||||
|
out.LedMap = make(map[string]string, len(in.LedMap))
|
||||||
|
for k, v := range in.LedMap {
|
||||||
|
out.LedMap[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out.RemotePanel = copyRemotePanelState(in.RemotePanel)
|
||||||
|
return &out
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyRemotePanelState(in remotePanelState) remotePanelState {
|
||||||
|
in.CurrentLimit = copyFloat64Ptr(in.CurrentLimit)
|
||||||
|
in.Standby = copyBoolPtr(in.Standby)
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyFloat64Ptr(in *float64) *float64 {
|
||||||
|
if in == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
value := *in
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyBoolPtr(in *bool) *bool {
|
||||||
|
if in == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
value := *in
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeJSONBody(r *http.Request, destination any) error {
|
||||||
|
defer r.Body.Close()
|
||||||
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
decoder.DisallowUnknownFields()
|
||||||
|
if err := decoder.Decode(destination); err != nil {
|
||||||
|
return fmt.Errorf("invalid request body: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeJSON(rw http.ResponseWriter, statusCode int, payload any) {
|
||||||
|
rw.Header().Set("Content-Type", "application/json")
|
||||||
|
rw.WriteHeader(statusCode)
|
||||||
|
if err := json.NewEncoder(rw).Encode(payload); err != nil {
|
||||||
|
log.Errorf("Could not encode webui API response: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"git.coadcorp.com/nathan/invertergui/mk2driver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type templateTest struct {
|
type templateTest struct {
|
||||||
@@ -91,3 +91,53 @@ func TestTemplateInput(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParsePanelMode(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want mk2driver.PanelSwitchState
|
||||||
|
wantRaw string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "on",
|
||||||
|
input: "on",
|
||||||
|
want: mk2driver.PanelSwitchOn,
|
||||||
|
wantRaw: "on",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "charger_only",
|
||||||
|
input: "charger_only",
|
||||||
|
want: mk2driver.PanelSwitchChargerOnly,
|
||||||
|
wantRaw: "charger_only",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid",
|
||||||
|
input: "banana",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, gotRaw, err := parsePanelMode(tt.input)
|
||||||
|
if tt.wantErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error, got nil")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Fatalf("got switch %d, want %d", got, tt.want)
|
||||||
|
}
|
||||||
|
if gotRaw != tt.wantRaw {
|
||||||
|
t.Fatalf("got mode %q, want %q", gotRaw, tt.wantRaw)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
20
vendor/github.com/beorn7/perks/LICENSE
generated
vendored
20
vendor/github.com/beorn7/perks/LICENSE
generated
vendored
@@ -1,20 +0,0 @@
|
|||||||
Copyright (C) 2013 Blake Mizerany
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
a copy of this software and associated documentation files (the
|
|
||||||
"Software"), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be
|
|
||||||
included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
||||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
||||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
||||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
2388
vendor/github.com/beorn7/perks/quantile/exampledata.txt
generated
vendored
2388
vendor/github.com/beorn7/perks/quantile/exampledata.txt
generated
vendored
File diff suppressed because it is too large
Load Diff
316
vendor/github.com/beorn7/perks/quantile/stream.go
generated
vendored
316
vendor/github.com/beorn7/perks/quantile/stream.go
generated
vendored
@@ -1,316 +0,0 @@
|
|||||||
// Package quantile computes approximate quantiles over an unbounded data
|
|
||||||
// stream within low memory and CPU bounds.
|
|
||||||
//
|
|
||||||
// A small amount of accuracy is traded to achieve the above properties.
|
|
||||||
//
|
|
||||||
// Multiple streams can be merged before calling Query to generate a single set
|
|
||||||
// of results. This is meaningful when the streams represent the same type of
|
|
||||||
// data. See Merge and Samples.
|
|
||||||
//
|
|
||||||
// For more detailed information about the algorithm used, see:
|
|
||||||
//
|
|
||||||
// Effective Computation of Biased Quantiles over Data Streams
|
|
||||||
//
|
|
||||||
// http://www.cs.rutgers.edu/~muthu/bquant.pdf
|
|
||||||
package quantile
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math"
|
|
||||||
"sort"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Sample holds an observed value and meta information for compression. JSON
|
|
||||||
// tags have been added for convenience.
|
|
||||||
type Sample struct {
|
|
||||||
Value float64 `json:",string"`
|
|
||||||
Width float64 `json:",string"`
|
|
||||||
Delta float64 `json:",string"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Samples represents a slice of samples. It implements sort.Interface.
|
|
||||||
type Samples []Sample
|
|
||||||
|
|
||||||
func (a Samples) Len() int { return len(a) }
|
|
||||||
func (a Samples) Less(i, j int) bool { return a[i].Value < a[j].Value }
|
|
||||||
func (a Samples) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
|
|
||||||
type invariant func(s *stream, r float64) float64
|
|
||||||
|
|
||||||
// NewLowBiased returns an initialized Stream for low-biased quantiles
|
|
||||||
// (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
|
|
||||||
// error guarantees can still be given even for the lower ranks of the data
|
|
||||||
// distribution.
|
|
||||||
//
|
|
||||||
// The provided epsilon is a relative error, i.e. the true quantile of a value
|
|
||||||
// returned by a query is guaranteed to be within (1±Epsilon)*Quantile.
|
|
||||||
//
|
|
||||||
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error
|
|
||||||
// properties.
|
|
||||||
func NewLowBiased(epsilon float64) *Stream {
|
|
||||||
ƒ := func(s *stream, r float64) float64 {
|
|
||||||
return 2 * epsilon * r
|
|
||||||
}
|
|
||||||
return newStream(ƒ)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewHighBiased returns an initialized Stream for high-biased quantiles
|
|
||||||
// (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
|
|
||||||
// error guarantees can still be given even for the higher ranks of the data
|
|
||||||
// distribution.
|
|
||||||
//
|
|
||||||
// The provided epsilon is a relative error, i.e. the true quantile of a value
|
|
||||||
// returned by a query is guaranteed to be within 1-(1±Epsilon)*(1-Quantile).
|
|
||||||
//
|
|
||||||
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error
|
|
||||||
// properties.
|
|
||||||
func NewHighBiased(epsilon float64) *Stream {
|
|
||||||
ƒ := func(s *stream, r float64) float64 {
|
|
||||||
return 2 * epsilon * (s.n - r)
|
|
||||||
}
|
|
||||||
return newStream(ƒ)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTargeted returns an initialized Stream concerned with a particular set of
|
|
||||||
// quantile values that are supplied a priori. Knowing these a priori reduces
|
|
||||||
// space and computation time. The targets map maps the desired quantiles to
|
|
||||||
// their absolute errors, i.e. the true quantile of a value returned by a query
|
|
||||||
// is guaranteed to be within (Quantile±Epsilon).
|
|
||||||
//
|
|
||||||
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error properties.
|
|
||||||
func NewTargeted(targetMap map[float64]float64) *Stream {
|
|
||||||
// Convert map to slice to avoid slow iterations on a map.
|
|
||||||
// ƒ is called on the hot path, so converting the map to a slice
|
|
||||||
// beforehand results in significant CPU savings.
|
|
||||||
targets := targetMapToSlice(targetMap)
|
|
||||||
|
|
||||||
ƒ := func(s *stream, r float64) float64 {
|
|
||||||
var m = math.MaxFloat64
|
|
||||||
var f float64
|
|
||||||
for _, t := range targets {
|
|
||||||
if t.quantile*s.n <= r {
|
|
||||||
f = (2 * t.epsilon * r) / t.quantile
|
|
||||||
} else {
|
|
||||||
f = (2 * t.epsilon * (s.n - r)) / (1 - t.quantile)
|
|
||||||
}
|
|
||||||
if f < m {
|
|
||||||
m = f
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
return newStream(ƒ)
|
|
||||||
}
|
|
||||||
|
|
||||||
type target struct {
|
|
||||||
quantile float64
|
|
||||||
epsilon float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func targetMapToSlice(targetMap map[float64]float64) []target {
|
|
||||||
targets := make([]target, 0, len(targetMap))
|
|
||||||
|
|
||||||
for quantile, epsilon := range targetMap {
|
|
||||||
t := target{
|
|
||||||
quantile: quantile,
|
|
||||||
epsilon: epsilon,
|
|
||||||
}
|
|
||||||
targets = append(targets, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
return targets
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stream computes quantiles for a stream of float64s. It is not thread-safe by
|
|
||||||
// design. Take care when using across multiple goroutines.
|
|
||||||
type Stream struct {
|
|
||||||
*stream
|
|
||||||
b Samples
|
|
||||||
sorted bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func newStream(ƒ invariant) *Stream {
|
|
||||||
x := &stream{ƒ: ƒ}
|
|
||||||
return &Stream{x, make(Samples, 0, 500), true}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert inserts v into the stream.
|
|
||||||
func (s *Stream) Insert(v float64) {
|
|
||||||
s.insert(Sample{Value: v, Width: 1})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) insert(sample Sample) {
|
|
||||||
s.b = append(s.b, sample)
|
|
||||||
s.sorted = false
|
|
||||||
if len(s.b) == cap(s.b) {
|
|
||||||
s.flush()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Query returns the computed qth percentiles value. If s was created with
|
|
||||||
// NewTargeted, and q is not in the set of quantiles provided a priori, Query
|
|
||||||
// will return an unspecified result.
|
|
||||||
func (s *Stream) Query(q float64) float64 {
|
|
||||||
if !s.flushed() {
|
|
||||||
// Fast path when there hasn't been enough data for a flush;
|
|
||||||
// this also yields better accuracy for small sets of data.
|
|
||||||
l := len(s.b)
|
|
||||||
if l == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
i := int(math.Ceil(float64(l) * q))
|
|
||||||
if i > 0 {
|
|
||||||
i -= 1
|
|
||||||
}
|
|
||||||
s.maybeSort()
|
|
||||||
return s.b[i].Value
|
|
||||||
}
|
|
||||||
s.flush()
|
|
||||||
return s.stream.query(q)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge merges samples into the underlying streams samples. This is handy when
|
|
||||||
// merging multiple streams from separate threads, database shards, etc.
|
|
||||||
//
|
|
||||||
// ATTENTION: This method is broken and does not yield correct results. The
|
|
||||||
// underlying algorithm is not capable of merging streams correctly.
|
|
||||||
func (s *Stream) Merge(samples Samples) {
|
|
||||||
sort.Sort(samples)
|
|
||||||
s.stream.merge(samples)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset reinitializes and clears the list reusing the samples buffer memory.
|
|
||||||
func (s *Stream) Reset() {
|
|
||||||
s.stream.reset()
|
|
||||||
s.b = s.b[:0]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Samples returns stream samples held by s.
|
|
||||||
func (s *Stream) Samples() Samples {
|
|
||||||
if !s.flushed() {
|
|
||||||
return s.b
|
|
||||||
}
|
|
||||||
s.flush()
|
|
||||||
return s.stream.samples()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Count returns the total number of samples observed in the stream
|
|
||||||
// since initialization.
|
|
||||||
func (s *Stream) Count() int {
|
|
||||||
return len(s.b) + s.stream.count()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) flush() {
|
|
||||||
s.maybeSort()
|
|
||||||
s.stream.merge(s.b)
|
|
||||||
s.b = s.b[:0]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) maybeSort() {
|
|
||||||
if !s.sorted {
|
|
||||||
s.sorted = true
|
|
||||||
sort.Sort(s.b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) flushed() bool {
|
|
||||||
return len(s.stream.l) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
type stream struct {
|
|
||||||
n float64
|
|
||||||
l []Sample
|
|
||||||
ƒ invariant
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) reset() {
|
|
||||||
s.l = s.l[:0]
|
|
||||||
s.n = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) insert(v float64) {
|
|
||||||
s.merge(Samples{{v, 1, 0}})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) merge(samples Samples) {
|
|
||||||
// TODO(beorn7): This tries to merge not only individual samples, but
|
|
||||||
// whole summaries. The paper doesn't mention merging summaries at
|
|
||||||
// all. Unittests show that the merging is inaccurate. Find out how to
|
|
||||||
// do merges properly.
|
|
||||||
var r float64
|
|
||||||
i := 0
|
|
||||||
for _, sample := range samples {
|
|
||||||
for ; i < len(s.l); i++ {
|
|
||||||
c := s.l[i]
|
|
||||||
if c.Value > sample.Value {
|
|
||||||
// Insert at position i.
|
|
||||||
s.l = append(s.l, Sample{})
|
|
||||||
copy(s.l[i+1:], s.l[i:])
|
|
||||||
s.l[i] = Sample{
|
|
||||||
sample.Value,
|
|
||||||
sample.Width,
|
|
||||||
math.Max(sample.Delta, math.Floor(s.ƒ(s, r))-1),
|
|
||||||
// TODO(beorn7): How to calculate delta correctly?
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
goto inserted
|
|
||||||
}
|
|
||||||
r += c.Width
|
|
||||||
}
|
|
||||||
s.l = append(s.l, Sample{sample.Value, sample.Width, 0})
|
|
||||||
i++
|
|
||||||
inserted:
|
|
||||||
s.n += sample.Width
|
|
||||||
r += sample.Width
|
|
||||||
}
|
|
||||||
s.compress()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) count() int {
|
|
||||||
return int(s.n)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) query(q float64) float64 {
|
|
||||||
t := math.Ceil(q * s.n)
|
|
||||||
t += math.Ceil(s.ƒ(s, t) / 2)
|
|
||||||
p := s.l[0]
|
|
||||||
var r float64
|
|
||||||
for _, c := range s.l[1:] {
|
|
||||||
r += p.Width
|
|
||||||
if r+c.Width+c.Delta > t {
|
|
||||||
return p.Value
|
|
||||||
}
|
|
||||||
p = c
|
|
||||||
}
|
|
||||||
return p.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) compress() {
|
|
||||||
if len(s.l) < 2 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
x := s.l[len(s.l)-1]
|
|
||||||
xi := len(s.l) - 1
|
|
||||||
r := s.n - 1 - x.Width
|
|
||||||
|
|
||||||
for i := len(s.l) - 2; i >= 0; i-- {
|
|
||||||
c := s.l[i]
|
|
||||||
if c.Width+x.Width+x.Delta <= s.ƒ(s, r) {
|
|
||||||
x.Width += c.Width
|
|
||||||
s.l[xi] = x
|
|
||||||
// Remove element at i.
|
|
||||||
copy(s.l[i:], s.l[i+1:])
|
|
||||||
s.l = s.l[:len(s.l)-1]
|
|
||||||
xi -= 1
|
|
||||||
} else {
|
|
||||||
x = c
|
|
||||||
xi = i
|
|
||||||
}
|
|
||||||
r -= c.Width
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) samples() Samples {
|
|
||||||
samples := make(Samples, len(s.l))
|
|
||||||
copy(samples, s.l)
|
|
||||||
return samples
|
|
||||||
}
|
|
||||||
15
vendor/github.com/davecgh/go-spew/LICENSE
generated
vendored
15
vendor/github.com/davecgh/go-spew/LICENSE
generated
vendored
@@ -1,15 +0,0 @@
|
|||||||
ISC License
|
|
||||||
|
|
||||||
Copyright (c) 2012-2016 Dave Collins <dave@davec.name>
|
|
||||||
|
|
||||||
Permission to use, copy, modify, and/or distribute this software for any
|
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
|
||||||
copyright notice and this permission notice appear in all copies.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
145
vendor/github.com/davecgh/go-spew/spew/bypass.go
generated
vendored
145
vendor/github.com/davecgh/go-spew/spew/bypass.go
generated
vendored
@@ -1,145 +0,0 @@
|
|||||||
// Copyright (c) 2015-2016 Dave Collins <dave@davec.name>
|
|
||||||
//
|
|
||||||
// Permission to use, copy, modify, and distribute this software for any
|
|
||||||
// purpose with or without fee is hereby granted, provided that the above
|
|
||||||
// copyright notice and this permission notice appear in all copies.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
|
|
||||||
// NOTE: Due to the following build constraints, this file will only be compiled
|
|
||||||
// when the code is not running on Google App Engine, compiled by GopherJS, and
|
|
||||||
// "-tags safe" is not added to the go build command line. The "disableunsafe"
|
|
||||||
// tag is deprecated and thus should not be used.
|
|
||||||
// Go versions prior to 1.4 are disabled because they use a different layout
|
|
||||||
// for interfaces which make the implementation of unsafeReflectValue more complex.
|
|
||||||
// +build !js,!appengine,!safe,!disableunsafe,go1.4
|
|
||||||
|
|
||||||
package spew
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// UnsafeDisabled is a build-time constant which specifies whether or
|
|
||||||
// not access to the unsafe package is available.
|
|
||||||
UnsafeDisabled = false
|
|
||||||
|
|
||||||
// ptrSize is the size of a pointer on the current arch.
|
|
||||||
ptrSize = unsafe.Sizeof((*byte)(nil))
|
|
||||||
)
|
|
||||||
|
|
||||||
type flag uintptr
|
|
||||||
|
|
||||||
var (
|
|
||||||
// flagRO indicates whether the value field of a reflect.Value
|
|
||||||
// is read-only.
|
|
||||||
flagRO flag
|
|
||||||
|
|
||||||
// flagAddr indicates whether the address of the reflect.Value's
|
|
||||||
// value may be taken.
|
|
||||||
flagAddr flag
|
|
||||||
)
|
|
||||||
|
|
||||||
// flagKindMask holds the bits that make up the kind
|
|
||||||
// part of the flags field. In all the supported versions,
|
|
||||||
// it is in the lower 5 bits.
|
|
||||||
const flagKindMask = flag(0x1f)
|
|
||||||
|
|
||||||
// Different versions of Go have used different
|
|
||||||
// bit layouts for the flags type. This table
|
|
||||||
// records the known combinations.
|
|
||||||
var okFlags = []struct {
|
|
||||||
ro, addr flag
|
|
||||||
}{{
|
|
||||||
// From Go 1.4 to 1.5
|
|
||||||
ro: 1 << 5,
|
|
||||||
addr: 1 << 7,
|
|
||||||
}, {
|
|
||||||
// Up to Go tip.
|
|
||||||
ro: 1<<5 | 1<<6,
|
|
||||||
addr: 1 << 8,
|
|
||||||
}}
|
|
||||||
|
|
||||||
var flagValOffset = func() uintptr {
|
|
||||||
field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag")
|
|
||||||
if !ok {
|
|
||||||
panic("reflect.Value has no flag field")
|
|
||||||
}
|
|
||||||
return field.Offset
|
|
||||||
}()
|
|
||||||
|
|
||||||
// flagField returns a pointer to the flag field of a reflect.Value.
|
|
||||||
func flagField(v *reflect.Value) *flag {
|
|
||||||
return (*flag)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + flagValOffset))
|
|
||||||
}
|
|
||||||
|
|
||||||
// unsafeReflectValue converts the passed reflect.Value into a one that bypasses
|
|
||||||
// the typical safety restrictions preventing access to unaddressable and
|
|
||||||
// unexported data. It works by digging the raw pointer to the underlying
|
|
||||||
// value out of the protected value and generating a new unprotected (unsafe)
|
|
||||||
// reflect.Value to it.
|
|
||||||
//
|
|
||||||
// This allows us to check for implementations of the Stringer and error
|
|
||||||
// interfaces to be used for pretty printing ordinarily unaddressable and
|
|
||||||
// inaccessible values such as unexported struct fields.
|
|
||||||
func unsafeReflectValue(v reflect.Value) reflect.Value {
|
|
||||||
if !v.IsValid() || (v.CanInterface() && v.CanAddr()) {
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
flagFieldPtr := flagField(&v)
|
|
||||||
*flagFieldPtr &^= flagRO
|
|
||||||
*flagFieldPtr |= flagAddr
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sanity checks against future reflect package changes
|
|
||||||
// to the type or semantics of the Value.flag field.
|
|
||||||
func init() {
|
|
||||||
field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag")
|
|
||||||
if !ok {
|
|
||||||
panic("reflect.Value has no flag field")
|
|
||||||
}
|
|
||||||
if field.Type.Kind() != reflect.TypeOf(flag(0)).Kind() {
|
|
||||||
panic("reflect.Value flag field has changed kind")
|
|
||||||
}
|
|
||||||
type t0 int
|
|
||||||
var t struct {
|
|
||||||
A t0
|
|
||||||
// t0 will have flagEmbedRO set.
|
|
||||||
t0
|
|
||||||
// a will have flagStickyRO set
|
|
||||||
a t0
|
|
||||||
}
|
|
||||||
vA := reflect.ValueOf(t).FieldByName("A")
|
|
||||||
va := reflect.ValueOf(t).FieldByName("a")
|
|
||||||
vt0 := reflect.ValueOf(t).FieldByName("t0")
|
|
||||||
|
|
||||||
// Infer flagRO from the difference between the flags
|
|
||||||
// for the (otherwise identical) fields in t.
|
|
||||||
flagPublic := *flagField(&vA)
|
|
||||||
flagWithRO := *flagField(&va) | *flagField(&vt0)
|
|
||||||
flagRO = flagPublic ^ flagWithRO
|
|
||||||
|
|
||||||
// Infer flagAddr from the difference between a value
|
|
||||||
// taken from a pointer and not.
|
|
||||||
vPtrA := reflect.ValueOf(&t).Elem().FieldByName("A")
|
|
||||||
flagNoPtr := *flagField(&vA)
|
|
||||||
flagPtr := *flagField(&vPtrA)
|
|
||||||
flagAddr = flagNoPtr ^ flagPtr
|
|
||||||
|
|
||||||
// Check that the inferred flags tally with one of the known versions.
|
|
||||||
for _, f := range okFlags {
|
|
||||||
if flagRO == f.ro && flagAddr == f.addr {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
panic("reflect.Value read-only flag has changed semantics")
|
|
||||||
}
|
|
||||||
38
vendor/github.com/davecgh/go-spew/spew/bypasssafe.go
generated
vendored
38
vendor/github.com/davecgh/go-spew/spew/bypasssafe.go
generated
vendored
@@ -1,38 +0,0 @@
|
|||||||
// Copyright (c) 2015-2016 Dave Collins <dave@davec.name>
|
|
||||||
//
|
|
||||||
// Permission to use, copy, modify, and distribute this software for any
|
|
||||||
// purpose with or without fee is hereby granted, provided that the above
|
|
||||||
// copyright notice and this permission notice appear in all copies.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
|
|
||||||
// NOTE: Due to the following build constraints, this file will only be compiled
|
|
||||||
// when the code is running on Google App Engine, compiled by GopherJS, or
|
|
||||||
// "-tags safe" is added to the go build command line. The "disableunsafe"
|
|
||||||
// tag is deprecated and thus should not be used.
|
|
||||||
// +build js appengine safe disableunsafe !go1.4
|
|
||||||
|
|
||||||
package spew
|
|
||||||
|
|
||||||
import "reflect"
|
|
||||||
|
|
||||||
const (
|
|
||||||
// UnsafeDisabled is a build-time constant which specifies whether or
|
|
||||||
// not access to the unsafe package is available.
|
|
||||||
UnsafeDisabled = true
|
|
||||||
)
|
|
||||||
|
|
||||||
// unsafeReflectValue typically converts the passed reflect.Value into a one
|
|
||||||
// that bypasses the typical safety restrictions preventing access to
|
|
||||||
// unaddressable and unexported data. However, doing this relies on access to
|
|
||||||
// the unsafe package. This is a stub version which simply returns the passed
|
|
||||||
// reflect.Value when the unsafe package is not available.
|
|
||||||
func unsafeReflectValue(v reflect.Value) reflect.Value {
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
341
vendor/github.com/davecgh/go-spew/spew/common.go
generated
vendored
341
vendor/github.com/davecgh/go-spew/spew/common.go
generated
vendored
@@ -1,341 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package spew
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Some constants in the form of bytes to avoid string overhead. This mirrors
|
|
||||||
// the technique used in the fmt package.
|
|
||||||
var (
|
|
||||||
panicBytes = []byte("(PANIC=")
|
|
||||||
plusBytes = []byte("+")
|
|
||||||
iBytes = []byte("i")
|
|
||||||
trueBytes = []byte("true")
|
|
||||||
falseBytes = []byte("false")
|
|
||||||
interfaceBytes = []byte("(interface {})")
|
|
||||||
commaNewlineBytes = []byte(",\n")
|
|
||||||
newlineBytes = []byte("\n")
|
|
||||||
openBraceBytes = []byte("{")
|
|
||||||
openBraceNewlineBytes = []byte("{\n")
|
|
||||||
closeBraceBytes = []byte("}")
|
|
||||||
asteriskBytes = []byte("*")
|
|
||||||
colonBytes = []byte(":")
|
|
||||||
colonSpaceBytes = []byte(": ")
|
|
||||||
openParenBytes = []byte("(")
|
|
||||||
closeParenBytes = []byte(")")
|
|
||||||
spaceBytes = []byte(" ")
|
|
||||||
pointerChainBytes = []byte("->")
|
|
||||||
nilAngleBytes = []byte("<nil>")
|
|
||||||
maxNewlineBytes = []byte("<max depth reached>\n")
|
|
||||||
maxShortBytes = []byte("<max>")
|
|
||||||
circularBytes = []byte("<already shown>")
|
|
||||||
circularShortBytes = []byte("<shown>")
|
|
||||||
invalidAngleBytes = []byte("<invalid>")
|
|
||||||
openBracketBytes = []byte("[")
|
|
||||||
closeBracketBytes = []byte("]")
|
|
||||||
percentBytes = []byte("%")
|
|
||||||
precisionBytes = []byte(".")
|
|
||||||
openAngleBytes = []byte("<")
|
|
||||||
closeAngleBytes = []byte(">")
|
|
||||||
openMapBytes = []byte("map[")
|
|
||||||
closeMapBytes = []byte("]")
|
|
||||||
lenEqualsBytes = []byte("len=")
|
|
||||||
capEqualsBytes = []byte("cap=")
|
|
||||||
)
|
|
||||||
|
|
||||||
// hexDigits is used to map a decimal value to a hex digit.
|
|
||||||
var hexDigits = "0123456789abcdef"
|
|
||||||
|
|
||||||
// catchPanic handles any panics that might occur during the handleMethods
|
|
||||||
// calls.
|
|
||||||
func catchPanic(w io.Writer, v reflect.Value) {
|
|
||||||
if err := recover(); err != nil {
|
|
||||||
w.Write(panicBytes)
|
|
||||||
fmt.Fprintf(w, "%v", err)
|
|
||||||
w.Write(closeParenBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleMethods attempts to call the Error and String methods on the underlying
|
|
||||||
// type the passed reflect.Value represents and outputes the result to Writer w.
|
|
||||||
//
|
|
||||||
// It handles panics in any called methods by catching and displaying the error
|
|
||||||
// as the formatted value.
|
|
||||||
func handleMethods(cs *ConfigState, w io.Writer, v reflect.Value) (handled bool) {
|
|
||||||
// We need an interface to check if the type implements the error or
|
|
||||||
// Stringer interface. However, the reflect package won't give us an
|
|
||||||
// interface on certain things like unexported struct fields in order
|
|
||||||
// to enforce visibility rules. We use unsafe, when it's available,
|
|
||||||
// to bypass these restrictions since this package does not mutate the
|
|
||||||
// values.
|
|
||||||
if !v.CanInterface() {
|
|
||||||
if UnsafeDisabled {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
v = unsafeReflectValue(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Choose whether or not to do error and Stringer interface lookups against
|
|
||||||
// the base type or a pointer to the base type depending on settings.
|
|
||||||
// Technically calling one of these methods with a pointer receiver can
|
|
||||||
// mutate the value, however, types which choose to satisify an error or
|
|
||||||
// Stringer interface with a pointer receiver should not be mutating their
|
|
||||||
// state inside these interface methods.
|
|
||||||
if !cs.DisablePointerMethods && !UnsafeDisabled && !v.CanAddr() {
|
|
||||||
v = unsafeReflectValue(v)
|
|
||||||
}
|
|
||||||
if v.CanAddr() {
|
|
||||||
v = v.Addr()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is it an error or Stringer?
|
|
||||||
switch iface := v.Interface().(type) {
|
|
||||||
case error:
|
|
||||||
defer catchPanic(w, v)
|
|
||||||
if cs.ContinueOnMethod {
|
|
||||||
w.Write(openParenBytes)
|
|
||||||
w.Write([]byte(iface.Error()))
|
|
||||||
w.Write(closeParenBytes)
|
|
||||||
w.Write(spaceBytes)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write([]byte(iface.Error()))
|
|
||||||
return true
|
|
||||||
|
|
||||||
case fmt.Stringer:
|
|
||||||
defer catchPanic(w, v)
|
|
||||||
if cs.ContinueOnMethod {
|
|
||||||
w.Write(openParenBytes)
|
|
||||||
w.Write([]byte(iface.String()))
|
|
||||||
w.Write(closeParenBytes)
|
|
||||||
w.Write(spaceBytes)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
w.Write([]byte(iface.String()))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// printBool outputs a boolean value as true or false to Writer w.
|
|
||||||
func printBool(w io.Writer, val bool) {
|
|
||||||
if val {
|
|
||||||
w.Write(trueBytes)
|
|
||||||
} else {
|
|
||||||
w.Write(falseBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// printInt outputs a signed integer value to Writer w.
|
|
||||||
func printInt(w io.Writer, val int64, base int) {
|
|
||||||
w.Write([]byte(strconv.FormatInt(val, base)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// printUint outputs an unsigned integer value to Writer w.
|
|
||||||
func printUint(w io.Writer, val uint64, base int) {
|
|
||||||
w.Write([]byte(strconv.FormatUint(val, base)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// printFloat outputs a floating point value using the specified precision,
|
|
||||||
// which is expected to be 32 or 64bit, to Writer w.
|
|
||||||
func printFloat(w io.Writer, val float64, precision int) {
|
|
||||||
w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// printComplex outputs a complex value using the specified float precision
|
|
||||||
// for the real and imaginary parts to Writer w.
|
|
||||||
func printComplex(w io.Writer, c complex128, floatPrecision int) {
|
|
||||||
r := real(c)
|
|
||||||
w.Write(openParenBytes)
|
|
||||||
w.Write([]byte(strconv.FormatFloat(r, 'g', -1, floatPrecision)))
|
|
||||||
i := imag(c)
|
|
||||||
if i >= 0 {
|
|
||||||
w.Write(plusBytes)
|
|
||||||
}
|
|
||||||
w.Write([]byte(strconv.FormatFloat(i, 'g', -1, floatPrecision)))
|
|
||||||
w.Write(iBytes)
|
|
||||||
w.Write(closeParenBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// printHexPtr outputs a uintptr formatted as hexadecimal with a leading '0x'
|
|
||||||
// prefix to Writer w.
|
|
||||||
func printHexPtr(w io.Writer, p uintptr) {
|
|
||||||
// Null pointer.
|
|
||||||
num := uint64(p)
|
|
||||||
if num == 0 {
|
|
||||||
w.Write(nilAngleBytes)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Max uint64 is 16 bytes in hex + 2 bytes for '0x' prefix
|
|
||||||
buf := make([]byte, 18)
|
|
||||||
|
|
||||||
// It's simpler to construct the hex string right to left.
|
|
||||||
base := uint64(16)
|
|
||||||
i := len(buf) - 1
|
|
||||||
for num >= base {
|
|
||||||
buf[i] = hexDigits[num%base]
|
|
||||||
num /= base
|
|
||||||
i--
|
|
||||||
}
|
|
||||||
buf[i] = hexDigits[num]
|
|
||||||
|
|
||||||
// Add '0x' prefix.
|
|
||||||
i--
|
|
||||||
buf[i] = 'x'
|
|
||||||
i--
|
|
||||||
buf[i] = '0'
|
|
||||||
|
|
||||||
// Strip unused leading bytes.
|
|
||||||
buf = buf[i:]
|
|
||||||
w.Write(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
// valuesSorter implements sort.Interface to allow a slice of reflect.Value
|
|
||||||
// elements to be sorted.
|
|
||||||
type valuesSorter struct {
|
|
||||||
values []reflect.Value
|
|
||||||
strings []string // either nil or same len and values
|
|
||||||
cs *ConfigState
|
|
||||||
}
|
|
||||||
|
|
||||||
// newValuesSorter initializes a valuesSorter instance, which holds a set of
|
|
||||||
// surrogate keys on which the data should be sorted. It uses flags in
|
|
||||||
// ConfigState to decide if and how to populate those surrogate keys.
|
|
||||||
func newValuesSorter(values []reflect.Value, cs *ConfigState) sort.Interface {
|
|
||||||
vs := &valuesSorter{values: values, cs: cs}
|
|
||||||
if canSortSimply(vs.values[0].Kind()) {
|
|
||||||
return vs
|
|
||||||
}
|
|
||||||
if !cs.DisableMethods {
|
|
||||||
vs.strings = make([]string, len(values))
|
|
||||||
for i := range vs.values {
|
|
||||||
b := bytes.Buffer{}
|
|
||||||
if !handleMethods(cs, &b, vs.values[i]) {
|
|
||||||
vs.strings = nil
|
|
||||||
break
|
|
||||||
}
|
|
||||||
vs.strings[i] = b.String()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if vs.strings == nil && cs.SpewKeys {
|
|
||||||
vs.strings = make([]string, len(values))
|
|
||||||
for i := range vs.values {
|
|
||||||
vs.strings[i] = Sprintf("%#v", vs.values[i].Interface())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return vs
|
|
||||||
}
|
|
||||||
|
|
||||||
// canSortSimply tests whether a reflect.Kind is a primitive that can be sorted
|
|
||||||
// directly, or whether it should be considered for sorting by surrogate keys
|
|
||||||
// (if the ConfigState allows it).
|
|
||||||
func canSortSimply(kind reflect.Kind) bool {
|
|
||||||
// This switch parallels valueSortLess, except for the default case.
|
|
||||||
switch kind {
|
|
||||||
case reflect.Bool:
|
|
||||||
return true
|
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
|
||||||
return true
|
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
|
||||||
return true
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
return true
|
|
||||||
case reflect.String:
|
|
||||||
return true
|
|
||||||
case reflect.Uintptr:
|
|
||||||
return true
|
|
||||||
case reflect.Array:
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Len returns the number of values in the slice. It is part of the
|
|
||||||
// sort.Interface implementation.
|
|
||||||
func (s *valuesSorter) Len() int {
|
|
||||||
return len(s.values)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swap swaps the values at the passed indices. It is part of the
|
|
||||||
// sort.Interface implementation.
|
|
||||||
func (s *valuesSorter) Swap(i, j int) {
|
|
||||||
s.values[i], s.values[j] = s.values[j], s.values[i]
|
|
||||||
if s.strings != nil {
|
|
||||||
s.strings[i], s.strings[j] = s.strings[j], s.strings[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// valueSortLess returns whether the first value should sort before the second
|
|
||||||
// value. It is used by valueSorter.Less as part of the sort.Interface
|
|
||||||
// implementation.
|
|
||||||
func valueSortLess(a, b reflect.Value) bool {
|
|
||||||
switch a.Kind() {
|
|
||||||
case reflect.Bool:
|
|
||||||
return !a.Bool() && b.Bool()
|
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
|
||||||
return a.Int() < b.Int()
|
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
|
||||||
return a.Uint() < b.Uint()
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
return a.Float() < b.Float()
|
|
||||||
case reflect.String:
|
|
||||||
return a.String() < b.String()
|
|
||||||
case reflect.Uintptr:
|
|
||||||
return a.Uint() < b.Uint()
|
|
||||||
case reflect.Array:
|
|
||||||
// Compare the contents of both arrays.
|
|
||||||
l := a.Len()
|
|
||||||
for i := 0; i < l; i++ {
|
|
||||||
av := a.Index(i)
|
|
||||||
bv := b.Index(i)
|
|
||||||
if av.Interface() == bv.Interface() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return valueSortLess(av, bv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return a.String() < b.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Less returns whether the value at index i should sort before the
|
|
||||||
// value at index j. It is part of the sort.Interface implementation.
|
|
||||||
func (s *valuesSorter) Less(i, j int) bool {
|
|
||||||
if s.strings == nil {
|
|
||||||
return valueSortLess(s.values[i], s.values[j])
|
|
||||||
}
|
|
||||||
return s.strings[i] < s.strings[j]
|
|
||||||
}
|
|
||||||
|
|
||||||
// sortValues is a sort function that handles both native types and any type that
|
|
||||||
// can be converted to error or Stringer. Other inputs are sorted according to
|
|
||||||
// their Value.String() value to ensure display stability.
|
|
||||||
func sortValues(values []reflect.Value, cs *ConfigState) {
|
|
||||||
if len(values) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sort.Sort(newValuesSorter(values, cs))
|
|
||||||
}
|
|
||||||
306
vendor/github.com/davecgh/go-spew/spew/config.go
generated
vendored
306
vendor/github.com/davecgh/go-spew/spew/config.go
generated
vendored
@@ -1,306 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package spew
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ConfigState houses the configuration options used by spew to format and
|
|
||||||
// display values. There is a global instance, Config, that is used to control
|
|
||||||
// all top-level Formatter and Dump functionality. Each ConfigState instance
|
|
||||||
// provides methods equivalent to the top-level functions.
|
|
||||||
//
|
|
||||||
// The zero value for ConfigState provides no indentation. You would typically
|
|
||||||
// want to set it to a space or a tab.
|
|
||||||
//
|
|
||||||
// Alternatively, you can use NewDefaultConfig to get a ConfigState instance
|
|
||||||
// with default settings. See the documentation of NewDefaultConfig for default
|
|
||||||
// values.
|
|
||||||
type ConfigState struct {
|
|
||||||
// Indent specifies the string to use for each indentation level. The
|
|
||||||
// global config instance that all top-level functions use set this to a
|
|
||||||
// single space by default. If you would like more indentation, you might
|
|
||||||
// set this to a tab with "\t" or perhaps two spaces with " ".
|
|
||||||
Indent string
|
|
||||||
|
|
||||||
// MaxDepth controls the maximum number of levels to descend into nested
|
|
||||||
// data structures. The default, 0, means there is no limit.
|
|
||||||
//
|
|
||||||
// NOTE: Circular data structures are properly detected, so it is not
|
|
||||||
// necessary to set this value unless you specifically want to limit deeply
|
|
||||||
// nested data structures.
|
|
||||||
MaxDepth int
|
|
||||||
|
|
||||||
// DisableMethods specifies whether or not error and Stringer interfaces are
|
|
||||||
// invoked for types that implement them.
|
|
||||||
DisableMethods bool
|
|
||||||
|
|
||||||
// DisablePointerMethods specifies whether or not to check for and invoke
|
|
||||||
// error and Stringer interfaces on types which only accept a pointer
|
|
||||||
// receiver when the current type is not a pointer.
|
|
||||||
//
|
|
||||||
// NOTE: This might be an unsafe action since calling one of these methods
|
|
||||||
// with a pointer receiver could technically mutate the value, however,
|
|
||||||
// in practice, types which choose to satisify an error or Stringer
|
|
||||||
// interface with a pointer receiver should not be mutating their state
|
|
||||||
// inside these interface methods. As a result, this option relies on
|
|
||||||
// access to the unsafe package, so it will not have any effect when
|
|
||||||
// running in environments without access to the unsafe package such as
|
|
||||||
// Google App Engine or with the "safe" build tag specified.
|
|
||||||
DisablePointerMethods bool
|
|
||||||
|
|
||||||
// DisablePointerAddresses specifies whether to disable the printing of
|
|
||||||
// pointer addresses. This is useful when diffing data structures in tests.
|
|
||||||
DisablePointerAddresses bool
|
|
||||||
|
|
||||||
// DisableCapacities specifies whether to disable the printing of capacities
|
|
||||||
// for arrays, slices, maps and channels. This is useful when diffing
|
|
||||||
// data structures in tests.
|
|
||||||
DisableCapacities bool
|
|
||||||
|
|
||||||
// ContinueOnMethod specifies whether or not recursion should continue once
|
|
||||||
// a custom error or Stringer interface is invoked. The default, false,
|
|
||||||
// means it will print the results of invoking the custom error or Stringer
|
|
||||||
// interface and return immediately instead of continuing to recurse into
|
|
||||||
// the internals of the data type.
|
|
||||||
//
|
|
||||||
// NOTE: This flag does not have any effect if method invocation is disabled
|
|
||||||
// via the DisableMethods or DisablePointerMethods options.
|
|
||||||
ContinueOnMethod bool
|
|
||||||
|
|
||||||
// SortKeys specifies map keys should be sorted before being printed. Use
|
|
||||||
// this to have a more deterministic, diffable output. Note that only
|
|
||||||
// native types (bool, int, uint, floats, uintptr and string) and types
|
|
||||||
// that support the error or Stringer interfaces (if methods are
|
|
||||||
// enabled) are supported, with other types sorted according to the
|
|
||||||
// reflect.Value.String() output which guarantees display stability.
|
|
||||||
SortKeys bool
|
|
||||||
|
|
||||||
// SpewKeys specifies that, as a last resort attempt, map keys should
|
|
||||||
// be spewed to strings and sorted by those strings. This is only
|
|
||||||
// considered if SortKeys is true.
|
|
||||||
SpewKeys bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Config is the active configuration of the top-level functions.
|
|
||||||
// The configuration can be changed by modifying the contents of spew.Config.
|
|
||||||
var Config = ConfigState{Indent: " "}
|
|
||||||
|
|
||||||
// Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. It returns
|
|
||||||
// the formatted string as a value that satisfies error. See NewFormatter
|
|
||||||
// for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Errorf(format, c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Errorf(format string, a ...interface{}) (err error) {
|
|
||||||
return fmt.Errorf(format, c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fprint is a wrapper for fmt.Fprint that treats each argument as if it were
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. It returns
|
|
||||||
// the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Fprint(w, c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Fprint(w, c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. It returns
|
|
||||||
// the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Fprintf(w, format, c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Fprintf(w, format, c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Fprintln(w, c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Fprintln(w, c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print is a wrapper for fmt.Print that treats each argument as if it were
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. It returns
|
|
||||||
// the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Print(c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Print(a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Print(c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Printf is a wrapper for fmt.Printf that treats each argument as if it were
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. It returns
|
|
||||||
// the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Printf(format, c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Printf(format string, a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Printf(format, c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Println is a wrapper for fmt.Println that treats each argument as if it were
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. It returns
|
|
||||||
// the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Println(c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Println(a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Println(c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sprint is a wrapper for fmt.Sprint that treats each argument as if it were
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. It returns
|
|
||||||
// the resulting string. See NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Sprint(c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Sprint(a ...interface{}) string {
|
|
||||||
return fmt.Sprint(c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were
|
|
||||||
// passed with a Formatter interface returned by c.NewFormatter. It returns
|
|
||||||
// the resulting string. See NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Sprintf(format, c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Sprintf(format string, a ...interface{}) string {
|
|
||||||
return fmt.Sprintf(format, c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it
|
|
||||||
// were passed with a Formatter interface returned by c.NewFormatter. It
|
|
||||||
// returns the resulting string. See NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Sprintln(c.NewFormatter(a), c.NewFormatter(b))
|
|
||||||
func (c *ConfigState) Sprintln(a ...interface{}) string {
|
|
||||||
return fmt.Sprintln(c.convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
NewFormatter returns a custom formatter that satisfies the fmt.Formatter
|
|
||||||
interface. As a result, it integrates cleanly with standard fmt package
|
|
||||||
printing functions. The formatter is useful for inline printing of smaller data
|
|
||||||
types similar to the standard %v format specifier.
|
|
||||||
|
|
||||||
The custom formatter only responds to the %v (most compact), %+v (adds pointer
|
|
||||||
addresses), %#v (adds types), and %#+v (adds types and pointer addresses) verb
|
|
||||||
combinations. Any other verbs such as %x and %q will be sent to the the
|
|
||||||
standard fmt package for formatting. In addition, the custom formatter ignores
|
|
||||||
the width and precision arguments (however they will still work on the format
|
|
||||||
specifiers not handled by the custom formatter).
|
|
||||||
|
|
||||||
Typically this function shouldn't be called directly. It is much easier to make
|
|
||||||
use of the custom formatter by calling one of the convenience functions such as
|
|
||||||
c.Printf, c.Println, or c.Printf.
|
|
||||||
*/
|
|
||||||
func (c *ConfigState) NewFormatter(v interface{}) fmt.Formatter {
|
|
||||||
return newFormatter(c, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fdump formats and displays the passed arguments to io.Writer w. It formats
|
|
||||||
// exactly the same as Dump.
|
|
||||||
func (c *ConfigState) Fdump(w io.Writer, a ...interface{}) {
|
|
||||||
fdump(c, w, a...)
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Dump displays the passed parameters to standard out with newlines, customizable
|
|
||||||
indentation, and additional debug information such as complete types and all
|
|
||||||
pointer addresses used to indirect to the final value. It provides the
|
|
||||||
following features over the built-in printing facilities provided by the fmt
|
|
||||||
package:
|
|
||||||
|
|
||||||
* Pointers are dereferenced and followed
|
|
||||||
* Circular data structures are detected and handled properly
|
|
||||||
* Custom Stringer/error interfaces are optionally invoked, including
|
|
||||||
on unexported types
|
|
||||||
* Custom types which only implement the Stringer/error interfaces via
|
|
||||||
a pointer receiver are optionally invoked when passing non-pointer
|
|
||||||
variables
|
|
||||||
* Byte arrays and slices are dumped like the hexdump -C command which
|
|
||||||
includes offsets, byte values in hex, and ASCII output
|
|
||||||
|
|
||||||
The configuration options are controlled by modifying the public members
|
|
||||||
of c. See ConfigState for options documentation.
|
|
||||||
|
|
||||||
See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to
|
|
||||||
get the formatted result as a string.
|
|
||||||
*/
|
|
||||||
func (c *ConfigState) Dump(a ...interface{}) {
|
|
||||||
fdump(c, os.Stdout, a...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sdump returns a string with the passed arguments formatted exactly the same
|
|
||||||
// as Dump.
|
|
||||||
func (c *ConfigState) Sdump(a ...interface{}) string {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
fdump(c, &buf, a...)
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// convertArgs accepts a slice of arguments and returns a slice of the same
|
|
||||||
// length with each argument converted to a spew Formatter interface using
|
|
||||||
// the ConfigState associated with s.
|
|
||||||
func (c *ConfigState) convertArgs(args []interface{}) (formatters []interface{}) {
|
|
||||||
formatters = make([]interface{}, len(args))
|
|
||||||
for index, arg := range args {
|
|
||||||
formatters[index] = newFormatter(c, arg)
|
|
||||||
}
|
|
||||||
return formatters
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewDefaultConfig returns a ConfigState with the following default settings.
|
|
||||||
//
|
|
||||||
// Indent: " "
|
|
||||||
// MaxDepth: 0
|
|
||||||
// DisableMethods: false
|
|
||||||
// DisablePointerMethods: false
|
|
||||||
// ContinueOnMethod: false
|
|
||||||
// SortKeys: false
|
|
||||||
func NewDefaultConfig() *ConfigState {
|
|
||||||
return &ConfigState{Indent: " "}
|
|
||||||
}
|
|
||||||
211
vendor/github.com/davecgh/go-spew/spew/doc.go
generated
vendored
211
vendor/github.com/davecgh/go-spew/spew/doc.go
generated
vendored
@@ -1,211 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package spew implements a deep pretty printer for Go data structures to aid in
|
|
||||||
debugging.
|
|
||||||
|
|
||||||
A quick overview of the additional features spew provides over the built-in
|
|
||||||
printing facilities for Go data types are as follows:
|
|
||||||
|
|
||||||
* Pointers are dereferenced and followed
|
|
||||||
* Circular data structures are detected and handled properly
|
|
||||||
* Custom Stringer/error interfaces are optionally invoked, including
|
|
||||||
on unexported types
|
|
||||||
* Custom types which only implement the Stringer/error interfaces via
|
|
||||||
a pointer receiver are optionally invoked when passing non-pointer
|
|
||||||
variables
|
|
||||||
* Byte arrays and slices are dumped like the hexdump -C command which
|
|
||||||
includes offsets, byte values in hex, and ASCII output (only when using
|
|
||||||
Dump style)
|
|
||||||
|
|
||||||
There are two different approaches spew allows for dumping Go data structures:
|
|
||||||
|
|
||||||
* Dump style which prints with newlines, customizable indentation,
|
|
||||||
and additional debug information such as types and all pointer addresses
|
|
||||||
used to indirect to the final value
|
|
||||||
* A custom Formatter interface that integrates cleanly with the standard fmt
|
|
||||||
package and replaces %v, %+v, %#v, and %#+v to provide inline printing
|
|
||||||
similar to the default %v while providing the additional functionality
|
|
||||||
outlined above and passing unsupported format verbs such as %x and %q
|
|
||||||
along to fmt
|
|
||||||
|
|
||||||
Quick Start
|
|
||||||
|
|
||||||
This section demonstrates how to quickly get started with spew. See the
|
|
||||||
sections below for further details on formatting and configuration options.
|
|
||||||
|
|
||||||
To dump a variable with full newlines, indentation, type, and pointer
|
|
||||||
information use Dump, Fdump, or Sdump:
|
|
||||||
spew.Dump(myVar1, myVar2, ...)
|
|
||||||
spew.Fdump(someWriter, myVar1, myVar2, ...)
|
|
||||||
str := spew.Sdump(myVar1, myVar2, ...)
|
|
||||||
|
|
||||||
Alternatively, if you would prefer to use format strings with a compacted inline
|
|
||||||
printing style, use the convenience wrappers Printf, Fprintf, etc with
|
|
||||||
%v (most compact), %+v (adds pointer addresses), %#v (adds types), or
|
|
||||||
%#+v (adds types and pointer addresses):
|
|
||||||
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
|
||||||
spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
|
|
||||||
spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
|
||||||
spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
|
|
||||||
|
|
||||||
Configuration Options
|
|
||||||
|
|
||||||
Configuration of spew is handled by fields in the ConfigState type. For
|
|
||||||
convenience, all of the top-level functions use a global state available
|
|
||||||
via the spew.Config global.
|
|
||||||
|
|
||||||
It is also possible to create a ConfigState instance that provides methods
|
|
||||||
equivalent to the top-level functions. This allows concurrent configuration
|
|
||||||
options. See the ConfigState documentation for more details.
|
|
||||||
|
|
||||||
The following configuration options are available:
|
|
||||||
* Indent
|
|
||||||
String to use for each indentation level for Dump functions.
|
|
||||||
It is a single space by default. A popular alternative is "\t".
|
|
||||||
|
|
||||||
* MaxDepth
|
|
||||||
Maximum number of levels to descend into nested data structures.
|
|
||||||
There is no limit by default.
|
|
||||||
|
|
||||||
* DisableMethods
|
|
||||||
Disables invocation of error and Stringer interface methods.
|
|
||||||
Method invocation is enabled by default.
|
|
||||||
|
|
||||||
* DisablePointerMethods
|
|
||||||
Disables invocation of error and Stringer interface methods on types
|
|
||||||
which only accept pointer receivers from non-pointer variables.
|
|
||||||
Pointer method invocation is enabled by default.
|
|
||||||
|
|
||||||
* DisablePointerAddresses
|
|
||||||
DisablePointerAddresses specifies whether to disable the printing of
|
|
||||||
pointer addresses. This is useful when diffing data structures in tests.
|
|
||||||
|
|
||||||
* DisableCapacities
|
|
||||||
DisableCapacities specifies whether to disable the printing of
|
|
||||||
capacities for arrays, slices, maps and channels. This is useful when
|
|
||||||
diffing data structures in tests.
|
|
||||||
|
|
||||||
* ContinueOnMethod
|
|
||||||
Enables recursion into types after invoking error and Stringer interface
|
|
||||||
methods. Recursion after method invocation is disabled by default.
|
|
||||||
|
|
||||||
* SortKeys
|
|
||||||
Specifies map keys should be sorted before being printed. Use
|
|
||||||
this to have a more deterministic, diffable output. Note that
|
|
||||||
only native types (bool, int, uint, floats, uintptr and string)
|
|
||||||
and types which implement error or Stringer interfaces are
|
|
||||||
supported with other types sorted according to the
|
|
||||||
reflect.Value.String() output which guarantees display
|
|
||||||
stability. Natural map order is used by default.
|
|
||||||
|
|
||||||
* SpewKeys
|
|
||||||
Specifies that, as a last resort attempt, map keys should be
|
|
||||||
spewed to strings and sorted by those strings. This is only
|
|
||||||
considered if SortKeys is true.
|
|
||||||
|
|
||||||
Dump Usage
|
|
||||||
|
|
||||||
Simply call spew.Dump with a list of variables you want to dump:
|
|
||||||
|
|
||||||
spew.Dump(myVar1, myVar2, ...)
|
|
||||||
|
|
||||||
You may also call spew.Fdump if you would prefer to output to an arbitrary
|
|
||||||
io.Writer. For example, to dump to standard error:
|
|
||||||
|
|
||||||
spew.Fdump(os.Stderr, myVar1, myVar2, ...)
|
|
||||||
|
|
||||||
A third option is to call spew.Sdump to get the formatted output as a string:
|
|
||||||
|
|
||||||
str := spew.Sdump(myVar1, myVar2, ...)
|
|
||||||
|
|
||||||
Sample Dump Output
|
|
||||||
|
|
||||||
See the Dump example for details on the setup of the types and variables being
|
|
||||||
shown here.
|
|
||||||
|
|
||||||
(main.Foo) {
|
|
||||||
unexportedField: (*main.Bar)(0xf84002e210)({
|
|
||||||
flag: (main.Flag) flagTwo,
|
|
||||||
data: (uintptr) <nil>
|
|
||||||
}),
|
|
||||||
ExportedField: (map[interface {}]interface {}) (len=1) {
|
|
||||||
(string) (len=3) "one": (bool) true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Byte (and uint8) arrays and slices are displayed uniquely like the hexdump -C
|
|
||||||
command as shown.
|
|
||||||
([]uint8) (len=32 cap=32) {
|
|
||||||
00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
|
|
||||||
00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0|
|
|
||||||
00000020 31 32 |12|
|
|
||||||
}
|
|
||||||
|
|
||||||
Custom Formatter
|
|
||||||
|
|
||||||
Spew provides a custom formatter that implements the fmt.Formatter interface
|
|
||||||
so that it integrates cleanly with standard fmt package printing functions. The
|
|
||||||
formatter is useful for inline printing of smaller data types similar to the
|
|
||||||
standard %v format specifier.
|
|
||||||
|
|
||||||
The custom formatter only responds to the %v (most compact), %+v (adds pointer
|
|
||||||
addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb
|
|
||||||
combinations. Any other verbs such as %x and %q will be sent to the the
|
|
||||||
standard fmt package for formatting. In addition, the custom formatter ignores
|
|
||||||
the width and precision arguments (however they will still work on the format
|
|
||||||
specifiers not handled by the custom formatter).
|
|
||||||
|
|
||||||
Custom Formatter Usage
|
|
||||||
|
|
||||||
The simplest way to make use of the spew custom formatter is to call one of the
|
|
||||||
convenience functions such as spew.Printf, spew.Println, or spew.Printf. The
|
|
||||||
functions have syntax you are most likely already familiar with:
|
|
||||||
|
|
||||||
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
|
||||||
spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
|
|
||||||
spew.Println(myVar, myVar2)
|
|
||||||
spew.Fprintf(os.Stderr, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
|
||||||
spew.Fprintf(os.Stderr, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
|
|
||||||
|
|
||||||
See the Index for the full list convenience functions.
|
|
||||||
|
|
||||||
Sample Formatter Output
|
|
||||||
|
|
||||||
Double pointer to a uint8:
|
|
||||||
%v: <**>5
|
|
||||||
%+v: <**>(0xf8400420d0->0xf8400420c8)5
|
|
||||||
%#v: (**uint8)5
|
|
||||||
%#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5
|
|
||||||
|
|
||||||
Pointer to circular struct with a uint8 field and a pointer to itself:
|
|
||||||
%v: <*>{1 <*><shown>}
|
|
||||||
%+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)<shown>}
|
|
||||||
%#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)<shown>}
|
|
||||||
%#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)<shown>}
|
|
||||||
|
|
||||||
See the Printf example for details on the setup of variables being shown
|
|
||||||
here.
|
|
||||||
|
|
||||||
Errors
|
|
||||||
|
|
||||||
Since it is possible for custom Stringer/error interfaces to panic, spew
|
|
||||||
detects them and handles them internally by printing the panic information
|
|
||||||
inline with the output. Since spew is intended to provide deep pretty printing
|
|
||||||
capabilities on structures, it intentionally does not return any errors.
|
|
||||||
*/
|
|
||||||
package spew
|
|
||||||
509
vendor/github.com/davecgh/go-spew/spew/dump.go
generated
vendored
509
vendor/github.com/davecgh/go-spew/spew/dump.go
generated
vendored
@@ -1,509 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package spew
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"reflect"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// uint8Type is a reflect.Type representing a uint8. It is used to
|
|
||||||
// convert cgo types to uint8 slices for hexdumping.
|
|
||||||
uint8Type = reflect.TypeOf(uint8(0))
|
|
||||||
|
|
||||||
// cCharRE is a regular expression that matches a cgo char.
|
|
||||||
// It is used to detect character arrays to hexdump them.
|
|
||||||
cCharRE = regexp.MustCompile(`^.*\._Ctype_char$`)
|
|
||||||
|
|
||||||
// cUnsignedCharRE is a regular expression that matches a cgo unsigned
|
|
||||||
// char. It is used to detect unsigned character arrays to hexdump
|
|
||||||
// them.
|
|
||||||
cUnsignedCharRE = regexp.MustCompile(`^.*\._Ctype_unsignedchar$`)
|
|
||||||
|
|
||||||
// cUint8tCharRE is a regular expression that matches a cgo uint8_t.
|
|
||||||
// It is used to detect uint8_t arrays to hexdump them.
|
|
||||||
cUint8tCharRE = regexp.MustCompile(`^.*\._Ctype_uint8_t$`)
|
|
||||||
)
|
|
||||||
|
|
||||||
// dumpState contains information about the state of a dump operation.
|
|
||||||
type dumpState struct {
|
|
||||||
w io.Writer
|
|
||||||
depth int
|
|
||||||
pointers map[uintptr]int
|
|
||||||
ignoreNextType bool
|
|
||||||
ignoreNextIndent bool
|
|
||||||
cs *ConfigState
|
|
||||||
}
|
|
||||||
|
|
||||||
// indent performs indentation according to the depth level and cs.Indent
|
|
||||||
// option.
|
|
||||||
func (d *dumpState) indent() {
|
|
||||||
if d.ignoreNextIndent {
|
|
||||||
d.ignoreNextIndent = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
d.w.Write(bytes.Repeat([]byte(d.cs.Indent), d.depth))
|
|
||||||
}
|
|
||||||
|
|
||||||
// unpackValue returns values inside of non-nil interfaces when possible.
|
|
||||||
// This is useful for data types like structs, arrays, slices, and maps which
|
|
||||||
// can contain varying types packed inside an interface.
|
|
||||||
func (d *dumpState) unpackValue(v reflect.Value) reflect.Value {
|
|
||||||
if v.Kind() == reflect.Interface && !v.IsNil() {
|
|
||||||
v = v.Elem()
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
// dumpPtr handles formatting of pointers by indirecting them as necessary.
|
|
||||||
func (d *dumpState) dumpPtr(v reflect.Value) {
|
|
||||||
// Remove pointers at or below the current depth from map used to detect
|
|
||||||
// circular refs.
|
|
||||||
for k, depth := range d.pointers {
|
|
||||||
if depth >= d.depth {
|
|
||||||
delete(d.pointers, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep list of all dereferenced pointers to show later.
|
|
||||||
pointerChain := make([]uintptr, 0)
|
|
||||||
|
|
||||||
// Figure out how many levels of indirection there are by dereferencing
|
|
||||||
// pointers and unpacking interfaces down the chain while detecting circular
|
|
||||||
// references.
|
|
||||||
nilFound := false
|
|
||||||
cycleFound := false
|
|
||||||
indirects := 0
|
|
||||||
ve := v
|
|
||||||
for ve.Kind() == reflect.Ptr {
|
|
||||||
if ve.IsNil() {
|
|
||||||
nilFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
indirects++
|
|
||||||
addr := ve.Pointer()
|
|
||||||
pointerChain = append(pointerChain, addr)
|
|
||||||
if pd, ok := d.pointers[addr]; ok && pd < d.depth {
|
|
||||||
cycleFound = true
|
|
||||||
indirects--
|
|
||||||
break
|
|
||||||
}
|
|
||||||
d.pointers[addr] = d.depth
|
|
||||||
|
|
||||||
ve = ve.Elem()
|
|
||||||
if ve.Kind() == reflect.Interface {
|
|
||||||
if ve.IsNil() {
|
|
||||||
nilFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
ve = ve.Elem()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display type information.
|
|
||||||
d.w.Write(openParenBytes)
|
|
||||||
d.w.Write(bytes.Repeat(asteriskBytes, indirects))
|
|
||||||
d.w.Write([]byte(ve.Type().String()))
|
|
||||||
d.w.Write(closeParenBytes)
|
|
||||||
|
|
||||||
// Display pointer information.
|
|
||||||
if !d.cs.DisablePointerAddresses && len(pointerChain) > 0 {
|
|
||||||
d.w.Write(openParenBytes)
|
|
||||||
for i, addr := range pointerChain {
|
|
||||||
if i > 0 {
|
|
||||||
d.w.Write(pointerChainBytes)
|
|
||||||
}
|
|
||||||
printHexPtr(d.w, addr)
|
|
||||||
}
|
|
||||||
d.w.Write(closeParenBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display dereferenced value.
|
|
||||||
d.w.Write(openParenBytes)
|
|
||||||
switch {
|
|
||||||
case nilFound:
|
|
||||||
d.w.Write(nilAngleBytes)
|
|
||||||
|
|
||||||
case cycleFound:
|
|
||||||
d.w.Write(circularBytes)
|
|
||||||
|
|
||||||
default:
|
|
||||||
d.ignoreNextType = true
|
|
||||||
d.dump(ve)
|
|
||||||
}
|
|
||||||
d.w.Write(closeParenBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// dumpSlice handles formatting of arrays and slices. Byte (uint8 under
|
|
||||||
// reflection) arrays and slices are dumped in hexdump -C fashion.
|
|
||||||
func (d *dumpState) dumpSlice(v reflect.Value) {
|
|
||||||
// Determine whether this type should be hex dumped or not. Also,
|
|
||||||
// for types which should be hexdumped, try to use the underlying data
|
|
||||||
// first, then fall back to trying to convert them to a uint8 slice.
|
|
||||||
var buf []uint8
|
|
||||||
doConvert := false
|
|
||||||
doHexDump := false
|
|
||||||
numEntries := v.Len()
|
|
||||||
if numEntries > 0 {
|
|
||||||
vt := v.Index(0).Type()
|
|
||||||
vts := vt.String()
|
|
||||||
switch {
|
|
||||||
// C types that need to be converted.
|
|
||||||
case cCharRE.MatchString(vts):
|
|
||||||
fallthrough
|
|
||||||
case cUnsignedCharRE.MatchString(vts):
|
|
||||||
fallthrough
|
|
||||||
case cUint8tCharRE.MatchString(vts):
|
|
||||||
doConvert = true
|
|
||||||
|
|
||||||
// Try to use existing uint8 slices and fall back to converting
|
|
||||||
// and copying if that fails.
|
|
||||||
case vt.Kind() == reflect.Uint8:
|
|
||||||
// We need an addressable interface to convert the type
|
|
||||||
// to a byte slice. However, the reflect package won't
|
|
||||||
// give us an interface on certain things like
|
|
||||||
// unexported struct fields in order to enforce
|
|
||||||
// visibility rules. We use unsafe, when available, to
|
|
||||||
// bypass these restrictions since this package does not
|
|
||||||
// mutate the values.
|
|
||||||
vs := v
|
|
||||||
if !vs.CanInterface() || !vs.CanAddr() {
|
|
||||||
vs = unsafeReflectValue(vs)
|
|
||||||
}
|
|
||||||
if !UnsafeDisabled {
|
|
||||||
vs = vs.Slice(0, numEntries)
|
|
||||||
|
|
||||||
// Use the existing uint8 slice if it can be
|
|
||||||
// type asserted.
|
|
||||||
iface := vs.Interface()
|
|
||||||
if slice, ok := iface.([]uint8); ok {
|
|
||||||
buf = slice
|
|
||||||
doHexDump = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The underlying data needs to be converted if it can't
|
|
||||||
// be type asserted to a uint8 slice.
|
|
||||||
doConvert = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy and convert the underlying type if needed.
|
|
||||||
if doConvert && vt.ConvertibleTo(uint8Type) {
|
|
||||||
// Convert and copy each element into a uint8 byte
|
|
||||||
// slice.
|
|
||||||
buf = make([]uint8, numEntries)
|
|
||||||
for i := 0; i < numEntries; i++ {
|
|
||||||
vv := v.Index(i)
|
|
||||||
buf[i] = uint8(vv.Convert(uint8Type).Uint())
|
|
||||||
}
|
|
||||||
doHexDump = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hexdump the entire slice as needed.
|
|
||||||
if doHexDump {
|
|
||||||
indent := strings.Repeat(d.cs.Indent, d.depth)
|
|
||||||
str := indent + hex.Dump(buf)
|
|
||||||
str = strings.Replace(str, "\n", "\n"+indent, -1)
|
|
||||||
str = strings.TrimRight(str, d.cs.Indent)
|
|
||||||
d.w.Write([]byte(str))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recursively call dump for each item.
|
|
||||||
for i := 0; i < numEntries; i++ {
|
|
||||||
d.dump(d.unpackValue(v.Index(i)))
|
|
||||||
if i < (numEntries - 1) {
|
|
||||||
d.w.Write(commaNewlineBytes)
|
|
||||||
} else {
|
|
||||||
d.w.Write(newlineBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// dump is the main workhorse for dumping a value. It uses the passed reflect
|
|
||||||
// value to figure out what kind of object we are dealing with and formats it
|
|
||||||
// appropriately. It is a recursive function, however circular data structures
|
|
||||||
// are detected and handled properly.
|
|
||||||
func (d *dumpState) dump(v reflect.Value) {
|
|
||||||
// Handle invalid reflect values immediately.
|
|
||||||
kind := v.Kind()
|
|
||||||
if kind == reflect.Invalid {
|
|
||||||
d.w.Write(invalidAngleBytes)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle pointers specially.
|
|
||||||
if kind == reflect.Ptr {
|
|
||||||
d.indent()
|
|
||||||
d.dumpPtr(v)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print type information unless already handled elsewhere.
|
|
||||||
if !d.ignoreNextType {
|
|
||||||
d.indent()
|
|
||||||
d.w.Write(openParenBytes)
|
|
||||||
d.w.Write([]byte(v.Type().String()))
|
|
||||||
d.w.Write(closeParenBytes)
|
|
||||||
d.w.Write(spaceBytes)
|
|
||||||
}
|
|
||||||
d.ignoreNextType = false
|
|
||||||
|
|
||||||
// Display length and capacity if the built-in len and cap functions
|
|
||||||
// work with the value's kind and the len/cap itself is non-zero.
|
|
||||||
valueLen, valueCap := 0, 0
|
|
||||||
switch v.Kind() {
|
|
||||||
case reflect.Array, reflect.Slice, reflect.Chan:
|
|
||||||
valueLen, valueCap = v.Len(), v.Cap()
|
|
||||||
case reflect.Map, reflect.String:
|
|
||||||
valueLen = v.Len()
|
|
||||||
}
|
|
||||||
if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 {
|
|
||||||
d.w.Write(openParenBytes)
|
|
||||||
if valueLen != 0 {
|
|
||||||
d.w.Write(lenEqualsBytes)
|
|
||||||
printInt(d.w, int64(valueLen), 10)
|
|
||||||
}
|
|
||||||
if !d.cs.DisableCapacities && valueCap != 0 {
|
|
||||||
if valueLen != 0 {
|
|
||||||
d.w.Write(spaceBytes)
|
|
||||||
}
|
|
||||||
d.w.Write(capEqualsBytes)
|
|
||||||
printInt(d.w, int64(valueCap), 10)
|
|
||||||
}
|
|
||||||
d.w.Write(closeParenBytes)
|
|
||||||
d.w.Write(spaceBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call Stringer/error interfaces if they exist and the handle methods flag
|
|
||||||
// is enabled
|
|
||||||
if !d.cs.DisableMethods {
|
|
||||||
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
|
|
||||||
if handled := handleMethods(d.cs, d.w, v); handled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch kind {
|
|
||||||
case reflect.Invalid:
|
|
||||||
// Do nothing. We should never get here since invalid has already
|
|
||||||
// been handled above.
|
|
||||||
|
|
||||||
case reflect.Bool:
|
|
||||||
printBool(d.w, v.Bool())
|
|
||||||
|
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
|
||||||
printInt(d.w, v.Int(), 10)
|
|
||||||
|
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
|
||||||
printUint(d.w, v.Uint(), 10)
|
|
||||||
|
|
||||||
case reflect.Float32:
|
|
||||||
printFloat(d.w, v.Float(), 32)
|
|
||||||
|
|
||||||
case reflect.Float64:
|
|
||||||
printFloat(d.w, v.Float(), 64)
|
|
||||||
|
|
||||||
case reflect.Complex64:
|
|
||||||
printComplex(d.w, v.Complex(), 32)
|
|
||||||
|
|
||||||
case reflect.Complex128:
|
|
||||||
printComplex(d.w, v.Complex(), 64)
|
|
||||||
|
|
||||||
case reflect.Slice:
|
|
||||||
if v.IsNil() {
|
|
||||||
d.w.Write(nilAngleBytes)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
fallthrough
|
|
||||||
|
|
||||||
case reflect.Array:
|
|
||||||
d.w.Write(openBraceNewlineBytes)
|
|
||||||
d.depth++
|
|
||||||
if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
|
|
||||||
d.indent()
|
|
||||||
d.w.Write(maxNewlineBytes)
|
|
||||||
} else {
|
|
||||||
d.dumpSlice(v)
|
|
||||||
}
|
|
||||||
d.depth--
|
|
||||||
d.indent()
|
|
||||||
d.w.Write(closeBraceBytes)
|
|
||||||
|
|
||||||
case reflect.String:
|
|
||||||
d.w.Write([]byte(strconv.Quote(v.String())))
|
|
||||||
|
|
||||||
case reflect.Interface:
|
|
||||||
// The only time we should get here is for nil interfaces due to
|
|
||||||
// unpackValue calls.
|
|
||||||
if v.IsNil() {
|
|
||||||
d.w.Write(nilAngleBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
case reflect.Ptr:
|
|
||||||
// Do nothing. We should never get here since pointers have already
|
|
||||||
// been handled above.
|
|
||||||
|
|
||||||
case reflect.Map:
|
|
||||||
// nil maps should be indicated as different than empty maps
|
|
||||||
if v.IsNil() {
|
|
||||||
d.w.Write(nilAngleBytes)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
d.w.Write(openBraceNewlineBytes)
|
|
||||||
d.depth++
|
|
||||||
if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
|
|
||||||
d.indent()
|
|
||||||
d.w.Write(maxNewlineBytes)
|
|
||||||
} else {
|
|
||||||
numEntries := v.Len()
|
|
||||||
keys := v.MapKeys()
|
|
||||||
if d.cs.SortKeys {
|
|
||||||
sortValues(keys, d.cs)
|
|
||||||
}
|
|
||||||
for i, key := range keys {
|
|
||||||
d.dump(d.unpackValue(key))
|
|
||||||
d.w.Write(colonSpaceBytes)
|
|
||||||
d.ignoreNextIndent = true
|
|
||||||
d.dump(d.unpackValue(v.MapIndex(key)))
|
|
||||||
if i < (numEntries - 1) {
|
|
||||||
d.w.Write(commaNewlineBytes)
|
|
||||||
} else {
|
|
||||||
d.w.Write(newlineBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
d.depth--
|
|
||||||
d.indent()
|
|
||||||
d.w.Write(closeBraceBytes)
|
|
||||||
|
|
||||||
case reflect.Struct:
|
|
||||||
d.w.Write(openBraceNewlineBytes)
|
|
||||||
d.depth++
|
|
||||||
if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
|
|
||||||
d.indent()
|
|
||||||
d.w.Write(maxNewlineBytes)
|
|
||||||
} else {
|
|
||||||
vt := v.Type()
|
|
||||||
numFields := v.NumField()
|
|
||||||
for i := 0; i < numFields; i++ {
|
|
||||||
d.indent()
|
|
||||||
vtf := vt.Field(i)
|
|
||||||
d.w.Write([]byte(vtf.Name))
|
|
||||||
d.w.Write(colonSpaceBytes)
|
|
||||||
d.ignoreNextIndent = true
|
|
||||||
d.dump(d.unpackValue(v.Field(i)))
|
|
||||||
if i < (numFields - 1) {
|
|
||||||
d.w.Write(commaNewlineBytes)
|
|
||||||
} else {
|
|
||||||
d.w.Write(newlineBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
d.depth--
|
|
||||||
d.indent()
|
|
||||||
d.w.Write(closeBraceBytes)
|
|
||||||
|
|
||||||
case reflect.Uintptr:
|
|
||||||
printHexPtr(d.w, uintptr(v.Uint()))
|
|
||||||
|
|
||||||
case reflect.UnsafePointer, reflect.Chan, reflect.Func:
|
|
||||||
printHexPtr(d.w, v.Pointer())
|
|
||||||
|
|
||||||
// There were not any other types at the time this code was written, but
|
|
||||||
// fall back to letting the default fmt package handle it in case any new
|
|
||||||
// types are added.
|
|
||||||
default:
|
|
||||||
if v.CanInterface() {
|
|
||||||
fmt.Fprintf(d.w, "%v", v.Interface())
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(d.w, "%v", v.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fdump is a helper function to consolidate the logic from the various public
|
|
||||||
// methods which take varying writers and config states.
|
|
||||||
func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
|
|
||||||
for _, arg := range a {
|
|
||||||
if arg == nil {
|
|
||||||
w.Write(interfaceBytes)
|
|
||||||
w.Write(spaceBytes)
|
|
||||||
w.Write(nilAngleBytes)
|
|
||||||
w.Write(newlineBytes)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
d := dumpState{w: w, cs: cs}
|
|
||||||
d.pointers = make(map[uintptr]int)
|
|
||||||
d.dump(reflect.ValueOf(arg))
|
|
||||||
d.w.Write(newlineBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fdump formats and displays the passed arguments to io.Writer w. It formats
|
|
||||||
// exactly the same as Dump.
|
|
||||||
func Fdump(w io.Writer, a ...interface{}) {
|
|
||||||
fdump(&Config, w, a...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sdump returns a string with the passed arguments formatted exactly the same
|
|
||||||
// as Dump.
|
|
||||||
func Sdump(a ...interface{}) string {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
fdump(&Config, &buf, a...)
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Dump displays the passed parameters to standard out with newlines, customizable
|
|
||||||
indentation, and additional debug information such as complete types and all
|
|
||||||
pointer addresses used to indirect to the final value. It provides the
|
|
||||||
following features over the built-in printing facilities provided by the fmt
|
|
||||||
package:
|
|
||||||
|
|
||||||
* Pointers are dereferenced and followed
|
|
||||||
* Circular data structures are detected and handled properly
|
|
||||||
* Custom Stringer/error interfaces are optionally invoked, including
|
|
||||||
on unexported types
|
|
||||||
* Custom types which only implement the Stringer/error interfaces via
|
|
||||||
a pointer receiver are optionally invoked when passing non-pointer
|
|
||||||
variables
|
|
||||||
* Byte arrays and slices are dumped like the hexdump -C command which
|
|
||||||
includes offsets, byte values in hex, and ASCII output
|
|
||||||
|
|
||||||
The configuration options are controlled by an exported package global,
|
|
||||||
spew.Config. See ConfigState for options documentation.
|
|
||||||
|
|
||||||
See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to
|
|
||||||
get the formatted result as a string.
|
|
||||||
*/
|
|
||||||
func Dump(a ...interface{}) {
|
|
||||||
fdump(&Config, os.Stdout, a...)
|
|
||||||
}
|
|
||||||
419
vendor/github.com/davecgh/go-spew/spew/format.go
generated
vendored
419
vendor/github.com/davecgh/go-spew/spew/format.go
generated
vendored
@@ -1,419 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package spew
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// supportedFlags is a list of all the character flags supported by fmt package.
|
|
||||||
const supportedFlags = "0-+# "
|
|
||||||
|
|
||||||
// formatState implements the fmt.Formatter interface and contains information
|
|
||||||
// about the state of a formatting operation. The NewFormatter function can
|
|
||||||
// be used to get a new Formatter which can be used directly as arguments
|
|
||||||
// in standard fmt package printing calls.
|
|
||||||
type formatState struct {
|
|
||||||
value interface{}
|
|
||||||
fs fmt.State
|
|
||||||
depth int
|
|
||||||
pointers map[uintptr]int
|
|
||||||
ignoreNextType bool
|
|
||||||
cs *ConfigState
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildDefaultFormat recreates the original format string without precision
|
|
||||||
// and width information to pass in to fmt.Sprintf in the case of an
|
|
||||||
// unrecognized type. Unless new types are added to the language, this
|
|
||||||
// function won't ever be called.
|
|
||||||
func (f *formatState) buildDefaultFormat() (format string) {
|
|
||||||
buf := bytes.NewBuffer(percentBytes)
|
|
||||||
|
|
||||||
for _, flag := range supportedFlags {
|
|
||||||
if f.fs.Flag(int(flag)) {
|
|
||||||
buf.WriteRune(flag)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.WriteRune('v')
|
|
||||||
|
|
||||||
format = buf.String()
|
|
||||||
return format
|
|
||||||
}
|
|
||||||
|
|
||||||
// constructOrigFormat recreates the original format string including precision
|
|
||||||
// and width information to pass along to the standard fmt package. This allows
|
|
||||||
// automatic deferral of all format strings this package doesn't support.
|
|
||||||
func (f *formatState) constructOrigFormat(verb rune) (format string) {
|
|
||||||
buf := bytes.NewBuffer(percentBytes)
|
|
||||||
|
|
||||||
for _, flag := range supportedFlags {
|
|
||||||
if f.fs.Flag(int(flag)) {
|
|
||||||
buf.WriteRune(flag)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if width, ok := f.fs.Width(); ok {
|
|
||||||
buf.WriteString(strconv.Itoa(width))
|
|
||||||
}
|
|
||||||
|
|
||||||
if precision, ok := f.fs.Precision(); ok {
|
|
||||||
buf.Write(precisionBytes)
|
|
||||||
buf.WriteString(strconv.Itoa(precision))
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.WriteRune(verb)
|
|
||||||
|
|
||||||
format = buf.String()
|
|
||||||
return format
|
|
||||||
}
|
|
||||||
|
|
||||||
// unpackValue returns values inside of non-nil interfaces when possible and
|
|
||||||
// ensures that types for values which have been unpacked from an interface
|
|
||||||
// are displayed when the show types flag is also set.
|
|
||||||
// This is useful for data types like structs, arrays, slices, and maps which
|
|
||||||
// can contain varying types packed inside an interface.
|
|
||||||
func (f *formatState) unpackValue(v reflect.Value) reflect.Value {
|
|
||||||
if v.Kind() == reflect.Interface {
|
|
||||||
f.ignoreNextType = false
|
|
||||||
if !v.IsNil() {
|
|
||||||
v = v.Elem()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
// formatPtr handles formatting of pointers by indirecting them as necessary.
|
|
||||||
func (f *formatState) formatPtr(v reflect.Value) {
|
|
||||||
// Display nil if top level pointer is nil.
|
|
||||||
showTypes := f.fs.Flag('#')
|
|
||||||
if v.IsNil() && (!showTypes || f.ignoreNextType) {
|
|
||||||
f.fs.Write(nilAngleBytes)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove pointers at or below the current depth from map used to detect
|
|
||||||
// circular refs.
|
|
||||||
for k, depth := range f.pointers {
|
|
||||||
if depth >= f.depth {
|
|
||||||
delete(f.pointers, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep list of all dereferenced pointers to possibly show later.
|
|
||||||
pointerChain := make([]uintptr, 0)
|
|
||||||
|
|
||||||
// Figure out how many levels of indirection there are by derferencing
|
|
||||||
// pointers and unpacking interfaces down the chain while detecting circular
|
|
||||||
// references.
|
|
||||||
nilFound := false
|
|
||||||
cycleFound := false
|
|
||||||
indirects := 0
|
|
||||||
ve := v
|
|
||||||
for ve.Kind() == reflect.Ptr {
|
|
||||||
if ve.IsNil() {
|
|
||||||
nilFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
indirects++
|
|
||||||
addr := ve.Pointer()
|
|
||||||
pointerChain = append(pointerChain, addr)
|
|
||||||
if pd, ok := f.pointers[addr]; ok && pd < f.depth {
|
|
||||||
cycleFound = true
|
|
||||||
indirects--
|
|
||||||
break
|
|
||||||
}
|
|
||||||
f.pointers[addr] = f.depth
|
|
||||||
|
|
||||||
ve = ve.Elem()
|
|
||||||
if ve.Kind() == reflect.Interface {
|
|
||||||
if ve.IsNil() {
|
|
||||||
nilFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
ve = ve.Elem()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display type or indirection level depending on flags.
|
|
||||||
if showTypes && !f.ignoreNextType {
|
|
||||||
f.fs.Write(openParenBytes)
|
|
||||||
f.fs.Write(bytes.Repeat(asteriskBytes, indirects))
|
|
||||||
f.fs.Write([]byte(ve.Type().String()))
|
|
||||||
f.fs.Write(closeParenBytes)
|
|
||||||
} else {
|
|
||||||
if nilFound || cycleFound {
|
|
||||||
indirects += strings.Count(ve.Type().String(), "*")
|
|
||||||
}
|
|
||||||
f.fs.Write(openAngleBytes)
|
|
||||||
f.fs.Write([]byte(strings.Repeat("*", indirects)))
|
|
||||||
f.fs.Write(closeAngleBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display pointer information depending on flags.
|
|
||||||
if f.fs.Flag('+') && (len(pointerChain) > 0) {
|
|
||||||
f.fs.Write(openParenBytes)
|
|
||||||
for i, addr := range pointerChain {
|
|
||||||
if i > 0 {
|
|
||||||
f.fs.Write(pointerChainBytes)
|
|
||||||
}
|
|
||||||
printHexPtr(f.fs, addr)
|
|
||||||
}
|
|
||||||
f.fs.Write(closeParenBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display dereferenced value.
|
|
||||||
switch {
|
|
||||||
case nilFound:
|
|
||||||
f.fs.Write(nilAngleBytes)
|
|
||||||
|
|
||||||
case cycleFound:
|
|
||||||
f.fs.Write(circularShortBytes)
|
|
||||||
|
|
||||||
default:
|
|
||||||
f.ignoreNextType = true
|
|
||||||
f.format(ve)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// format is the main workhorse for providing the Formatter interface. It
|
|
||||||
// uses the passed reflect value to figure out what kind of object we are
|
|
||||||
// dealing with and formats it appropriately. It is a recursive function,
|
|
||||||
// however circular data structures are detected and handled properly.
|
|
||||||
func (f *formatState) format(v reflect.Value) {
|
|
||||||
// Handle invalid reflect values immediately.
|
|
||||||
kind := v.Kind()
|
|
||||||
if kind == reflect.Invalid {
|
|
||||||
f.fs.Write(invalidAngleBytes)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle pointers specially.
|
|
||||||
if kind == reflect.Ptr {
|
|
||||||
f.formatPtr(v)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print type information unless already handled elsewhere.
|
|
||||||
if !f.ignoreNextType && f.fs.Flag('#') {
|
|
||||||
f.fs.Write(openParenBytes)
|
|
||||||
f.fs.Write([]byte(v.Type().String()))
|
|
||||||
f.fs.Write(closeParenBytes)
|
|
||||||
}
|
|
||||||
f.ignoreNextType = false
|
|
||||||
|
|
||||||
// Call Stringer/error interfaces if they exist and the handle methods
|
|
||||||
// flag is enabled.
|
|
||||||
if !f.cs.DisableMethods {
|
|
||||||
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
|
|
||||||
if handled := handleMethods(f.cs, f.fs, v); handled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch kind {
|
|
||||||
case reflect.Invalid:
|
|
||||||
// Do nothing. We should never get here since invalid has already
|
|
||||||
// been handled above.
|
|
||||||
|
|
||||||
case reflect.Bool:
|
|
||||||
printBool(f.fs, v.Bool())
|
|
||||||
|
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
|
||||||
printInt(f.fs, v.Int(), 10)
|
|
||||||
|
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
|
||||||
printUint(f.fs, v.Uint(), 10)
|
|
||||||
|
|
||||||
case reflect.Float32:
|
|
||||||
printFloat(f.fs, v.Float(), 32)
|
|
||||||
|
|
||||||
case reflect.Float64:
|
|
||||||
printFloat(f.fs, v.Float(), 64)
|
|
||||||
|
|
||||||
case reflect.Complex64:
|
|
||||||
printComplex(f.fs, v.Complex(), 32)
|
|
||||||
|
|
||||||
case reflect.Complex128:
|
|
||||||
printComplex(f.fs, v.Complex(), 64)
|
|
||||||
|
|
||||||
case reflect.Slice:
|
|
||||||
if v.IsNil() {
|
|
||||||
f.fs.Write(nilAngleBytes)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
fallthrough
|
|
||||||
|
|
||||||
case reflect.Array:
|
|
||||||
f.fs.Write(openBracketBytes)
|
|
||||||
f.depth++
|
|
||||||
if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
|
|
||||||
f.fs.Write(maxShortBytes)
|
|
||||||
} else {
|
|
||||||
numEntries := v.Len()
|
|
||||||
for i := 0; i < numEntries; i++ {
|
|
||||||
if i > 0 {
|
|
||||||
f.fs.Write(spaceBytes)
|
|
||||||
}
|
|
||||||
f.ignoreNextType = true
|
|
||||||
f.format(f.unpackValue(v.Index(i)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.depth--
|
|
||||||
f.fs.Write(closeBracketBytes)
|
|
||||||
|
|
||||||
case reflect.String:
|
|
||||||
f.fs.Write([]byte(v.String()))
|
|
||||||
|
|
||||||
case reflect.Interface:
|
|
||||||
// The only time we should get here is for nil interfaces due to
|
|
||||||
// unpackValue calls.
|
|
||||||
if v.IsNil() {
|
|
||||||
f.fs.Write(nilAngleBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
case reflect.Ptr:
|
|
||||||
// Do nothing. We should never get here since pointers have already
|
|
||||||
// been handled above.
|
|
||||||
|
|
||||||
case reflect.Map:
|
|
||||||
// nil maps should be indicated as different than empty maps
|
|
||||||
if v.IsNil() {
|
|
||||||
f.fs.Write(nilAngleBytes)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
f.fs.Write(openMapBytes)
|
|
||||||
f.depth++
|
|
||||||
if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
|
|
||||||
f.fs.Write(maxShortBytes)
|
|
||||||
} else {
|
|
||||||
keys := v.MapKeys()
|
|
||||||
if f.cs.SortKeys {
|
|
||||||
sortValues(keys, f.cs)
|
|
||||||
}
|
|
||||||
for i, key := range keys {
|
|
||||||
if i > 0 {
|
|
||||||
f.fs.Write(spaceBytes)
|
|
||||||
}
|
|
||||||
f.ignoreNextType = true
|
|
||||||
f.format(f.unpackValue(key))
|
|
||||||
f.fs.Write(colonBytes)
|
|
||||||
f.ignoreNextType = true
|
|
||||||
f.format(f.unpackValue(v.MapIndex(key)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.depth--
|
|
||||||
f.fs.Write(closeMapBytes)
|
|
||||||
|
|
||||||
case reflect.Struct:
|
|
||||||
numFields := v.NumField()
|
|
||||||
f.fs.Write(openBraceBytes)
|
|
||||||
f.depth++
|
|
||||||
if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
|
|
||||||
f.fs.Write(maxShortBytes)
|
|
||||||
} else {
|
|
||||||
vt := v.Type()
|
|
||||||
for i := 0; i < numFields; i++ {
|
|
||||||
if i > 0 {
|
|
||||||
f.fs.Write(spaceBytes)
|
|
||||||
}
|
|
||||||
vtf := vt.Field(i)
|
|
||||||
if f.fs.Flag('+') || f.fs.Flag('#') {
|
|
||||||
f.fs.Write([]byte(vtf.Name))
|
|
||||||
f.fs.Write(colonBytes)
|
|
||||||
}
|
|
||||||
f.format(f.unpackValue(v.Field(i)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.depth--
|
|
||||||
f.fs.Write(closeBraceBytes)
|
|
||||||
|
|
||||||
case reflect.Uintptr:
|
|
||||||
printHexPtr(f.fs, uintptr(v.Uint()))
|
|
||||||
|
|
||||||
case reflect.UnsafePointer, reflect.Chan, reflect.Func:
|
|
||||||
printHexPtr(f.fs, v.Pointer())
|
|
||||||
|
|
||||||
// There were not any other types at the time this code was written, but
|
|
||||||
// fall back to letting the default fmt package handle it if any get added.
|
|
||||||
default:
|
|
||||||
format := f.buildDefaultFormat()
|
|
||||||
if v.CanInterface() {
|
|
||||||
fmt.Fprintf(f.fs, format, v.Interface())
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(f.fs, format, v.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format satisfies the fmt.Formatter interface. See NewFormatter for usage
|
|
||||||
// details.
|
|
||||||
func (f *formatState) Format(fs fmt.State, verb rune) {
|
|
||||||
f.fs = fs
|
|
||||||
|
|
||||||
// Use standard formatting for verbs that are not v.
|
|
||||||
if verb != 'v' {
|
|
||||||
format := f.constructOrigFormat(verb)
|
|
||||||
fmt.Fprintf(fs, format, f.value)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.value == nil {
|
|
||||||
if fs.Flag('#') {
|
|
||||||
fs.Write(interfaceBytes)
|
|
||||||
}
|
|
||||||
fs.Write(nilAngleBytes)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
f.format(reflect.ValueOf(f.value))
|
|
||||||
}
|
|
||||||
|
|
||||||
// newFormatter is a helper function to consolidate the logic from the various
|
|
||||||
// public methods which take varying config states.
|
|
||||||
func newFormatter(cs *ConfigState, v interface{}) fmt.Formatter {
|
|
||||||
fs := &formatState{value: v, cs: cs}
|
|
||||||
fs.pointers = make(map[uintptr]int)
|
|
||||||
return fs
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
NewFormatter returns a custom formatter that satisfies the fmt.Formatter
|
|
||||||
interface. As a result, it integrates cleanly with standard fmt package
|
|
||||||
printing functions. The formatter is useful for inline printing of smaller data
|
|
||||||
types similar to the standard %v format specifier.
|
|
||||||
|
|
||||||
The custom formatter only responds to the %v (most compact), %+v (adds pointer
|
|
||||||
addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb
|
|
||||||
combinations. Any other verbs such as %x and %q will be sent to the the
|
|
||||||
standard fmt package for formatting. In addition, the custom formatter ignores
|
|
||||||
the width and precision arguments (however they will still work on the format
|
|
||||||
specifiers not handled by the custom formatter).
|
|
||||||
|
|
||||||
Typically this function shouldn't be called directly. It is much easier to make
|
|
||||||
use of the custom formatter by calling one of the convenience functions such as
|
|
||||||
Printf, Println, or Fprintf.
|
|
||||||
*/
|
|
||||||
func NewFormatter(v interface{}) fmt.Formatter {
|
|
||||||
return newFormatter(&Config, v)
|
|
||||||
}
|
|
||||||
148
vendor/github.com/davecgh/go-spew/spew/spew.go
generated
vendored
148
vendor/github.com/davecgh/go-spew/spew/spew.go
generated
vendored
@@ -1,148 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
|
|
||||||
*
|
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
|
||||||
* copyright notice and this permission notice appear in all copies.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package spew
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the formatted string as a value that satisfies error. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Errorf(format, spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Errorf(format string, a ...interface{}) (err error) {
|
|
||||||
return fmt.Errorf(format, convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fprint is a wrapper for fmt.Fprint that treats each argument as if it were
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Fprint(w, spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Fprint(w, convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Fprintf(w, format, spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Fprintf(w, format, convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Fprintln(w, spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Fprintln(w, convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print is a wrapper for fmt.Print that treats each argument as if it were
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Print(spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Print(a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Print(convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Printf is a wrapper for fmt.Printf that treats each argument as if it were
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Printf(format, spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Printf(format string, a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Printf(format, convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Println is a wrapper for fmt.Println that treats each argument as if it were
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the number of bytes written and any write error encountered. See
|
|
||||||
// NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Println(spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Println(a ...interface{}) (n int, err error) {
|
|
||||||
return fmt.Println(convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sprint is a wrapper for fmt.Sprint that treats each argument as if it were
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the resulting string. See NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Sprint(spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Sprint(a ...interface{}) string {
|
|
||||||
return fmt.Sprint(convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were
|
|
||||||
// passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the resulting string. See NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Sprintf(format, spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Sprintf(format string, a ...interface{}) string {
|
|
||||||
return fmt.Sprintf(format, convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it
|
|
||||||
// were passed with a default Formatter interface returned by NewFormatter. It
|
|
||||||
// returns the resulting string. See NewFormatter for formatting details.
|
|
||||||
//
|
|
||||||
// This function is shorthand for the following syntax:
|
|
||||||
//
|
|
||||||
// fmt.Sprintln(spew.NewFormatter(a), spew.NewFormatter(b))
|
|
||||||
func Sprintln(a ...interface{}) string {
|
|
||||||
return fmt.Sprintln(convertArgs(a)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// convertArgs accepts a slice of arguments and returns a slice of the same
|
|
||||||
// length with each argument converted to a default spew Formatter interface.
|
|
||||||
func convertArgs(args []interface{}) (formatters []interface{}) {
|
|
||||||
formatters = make([]interface{}, len(args))
|
|
||||||
for index, arg := range args {
|
|
||||||
formatters[index] = NewFormatter(arg)
|
|
||||||
}
|
|
||||||
return formatters
|
|
||||||
}
|
|
||||||
36
vendor/github.com/eclipse/paho.mqtt.golang/.gitignore
generated
vendored
36
vendor/github.com/eclipse/paho.mqtt.golang/.gitignore
generated
vendored
@@ -1,36 +0,0 @@
|
|||||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
|
||||||
*.o
|
|
||||||
*.a
|
|
||||||
*.so
|
|
||||||
|
|
||||||
# Folders
|
|
||||||
_obj
|
|
||||||
_test
|
|
||||||
|
|
||||||
# Architecture specific extensions/prefixes
|
|
||||||
*.[568vq]
|
|
||||||
[568vq].out
|
|
||||||
|
|
||||||
*.cgo1.go
|
|
||||||
*.cgo2.c
|
|
||||||
_cgo_defun.c
|
|
||||||
_cgo_gotypes.go
|
|
||||||
_cgo_export.*
|
|
||||||
|
|
||||||
_testmain.go
|
|
||||||
|
|
||||||
*.exe
|
|
||||||
|
|
||||||
*.msg
|
|
||||||
*.lok
|
|
||||||
|
|
||||||
samples/trivial
|
|
||||||
samples/trivial2
|
|
||||||
samples/sample
|
|
||||||
samples/reconnect
|
|
||||||
samples/ssl
|
|
||||||
samples/custom_store
|
|
||||||
samples/simple
|
|
||||||
samples/stdinpub
|
|
||||||
samples/stdoutsub
|
|
||||||
samples/routing
|
|
||||||
56
vendor/github.com/eclipse/paho.mqtt.golang/CONTRIBUTING.md
generated
vendored
56
vendor/github.com/eclipse/paho.mqtt.golang/CONTRIBUTING.md
generated
vendored
@@ -1,56 +0,0 @@
|
|||||||
Contributing to Paho
|
|
||||||
====================
|
|
||||||
|
|
||||||
Thanks for your interest in this project.
|
|
||||||
|
|
||||||
Project description:
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
The Paho project has been created to provide scalable open-source implementations of open and standard messaging protocols aimed at new, existing, and emerging applications for Machine-to-Machine (M2M) and Internet of Things (IoT).
|
|
||||||
Paho reflects the inherent physical and cost constraints of device connectivity. Its objectives include effective levels of decoupling between devices and applications, designed to keep markets open and encourage the rapid growth of scalable Web and Enterprise middleware and applications. Paho is being kicked off with MQTT publish/subscribe client implementations for use on embedded platforms, along with corresponding server support as determined by the community.
|
|
||||||
|
|
||||||
- https://projects.eclipse.org/projects/technology.paho
|
|
||||||
|
|
||||||
Developer resources:
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
Information regarding source code management, builds, coding standards, and more.
|
|
||||||
|
|
||||||
- https://projects.eclipse.org/projects/technology.paho/developer
|
|
||||||
|
|
||||||
Contributor License Agreement:
|
|
||||||
------------------------------
|
|
||||||
|
|
||||||
Before your contribution can be accepted by the project, you need to create and electronically sign the Eclipse Foundation Contributor License Agreement (CLA).
|
|
||||||
|
|
||||||
- http://www.eclipse.org/legal/CLA.php
|
|
||||||
|
|
||||||
Contributing Code:
|
|
||||||
------------------
|
|
||||||
|
|
||||||
The Go client is developed in Github, see their documentation on the process of forking and pull requests; https://help.github.com/categories/collaborating-on-projects-using-pull-requests/
|
|
||||||
|
|
||||||
Git commit messages should follow the style described here;
|
|
||||||
|
|
||||||
http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
|
|
||||||
|
|
||||||
Contact:
|
|
||||||
--------
|
|
||||||
|
|
||||||
Contact the project developers via the project's "dev" list.
|
|
||||||
|
|
||||||
- https://dev.eclipse.org/mailman/listinfo/paho-dev
|
|
||||||
|
|
||||||
Search for bugs:
|
|
||||||
----------------
|
|
||||||
|
|
||||||
This project uses Github issues to track ongoing development and issues.
|
|
||||||
|
|
||||||
- https://github.com/eclipse/paho.mqtt.golang/issues
|
|
||||||
|
|
||||||
Create a new bug:
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
Be sure to search for existing bugs before you create another one. Remember that contributions are always welcome!
|
|
||||||
|
|
||||||
- https://github.com/eclipse/paho.mqtt.golang/issues
|
|
||||||
15
vendor/github.com/eclipse/paho.mqtt.golang/DISTRIBUTION
generated
vendored
15
vendor/github.com/eclipse/paho.mqtt.golang/DISTRIBUTION
generated
vendored
@@ -1,15 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
Eclipse Distribution License - v 1.0
|
|
||||||
|
|
||||||
Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.
|
|
||||||
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
||||||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
||||||
Neither the name of the Eclipse Foundation, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
87
vendor/github.com/eclipse/paho.mqtt.golang/LICENSE
generated
vendored
87
vendor/github.com/eclipse/paho.mqtt.golang/LICENSE
generated
vendored
@@ -1,87 +0,0 @@
|
|||||||
Eclipse Public License - v 1.0
|
|
||||||
|
|
||||||
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
|
|
||||||
|
|
||||||
1. DEFINITIONS
|
|
||||||
|
|
||||||
"Contribution" means:
|
|
||||||
|
|
||||||
a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and
|
|
||||||
|
|
||||||
b) in the case of each subsequent Contributor:
|
|
||||||
|
|
||||||
i) changes to the Program, and
|
|
||||||
|
|
||||||
ii) additions to the Program;
|
|
||||||
|
|
||||||
where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program.
|
|
||||||
|
|
||||||
"Contributor" means any person or entity that distributes the Program.
|
|
||||||
|
|
||||||
"Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program.
|
|
||||||
|
|
||||||
"Program" means the Contributions distributed in accordance with this Agreement.
|
|
||||||
|
|
||||||
"Recipient" means anyone who receives the Program under this Agreement, including all Contributors.
|
|
||||||
|
|
||||||
2. GRANT OF RIGHTS
|
|
||||||
|
|
||||||
a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form.
|
|
||||||
|
|
||||||
b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder.
|
|
||||||
|
|
||||||
c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program.
|
|
||||||
|
|
||||||
d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement.
|
|
||||||
|
|
||||||
3. REQUIREMENTS
|
|
||||||
|
|
||||||
A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that:
|
|
||||||
|
|
||||||
a) it complies with the terms and conditions of this Agreement; and
|
|
||||||
|
|
||||||
b) its license agreement:
|
|
||||||
|
|
||||||
i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose;
|
|
||||||
|
|
||||||
ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits;
|
|
||||||
|
|
||||||
iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and
|
|
||||||
|
|
||||||
iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange.
|
|
||||||
|
|
||||||
When the Program is made available in source code form:
|
|
||||||
|
|
||||||
a) it must be made available under this Agreement; and
|
|
||||||
|
|
||||||
b) a copy of this Agreement must be included with each copy of the Program.
|
|
||||||
|
|
||||||
Contributors may not remove or alter any copyright notices contained within the Program.
|
|
||||||
|
|
||||||
Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution.
|
|
||||||
|
|
||||||
4. COMMERCIAL DISTRIBUTION
|
|
||||||
|
|
||||||
Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense.
|
|
||||||
|
|
||||||
For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages.
|
|
||||||
|
|
||||||
5. NO WARRANTY
|
|
||||||
|
|
||||||
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations.
|
|
||||||
|
|
||||||
6. DISCLAIMER OF LIABILITY
|
|
||||||
|
|
||||||
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|
||||||
|
|
||||||
7. GENERAL
|
|
||||||
|
|
||||||
If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
|
|
||||||
|
|
||||||
If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed.
|
|
||||||
|
|
||||||
All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive.
|
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved.
|
|
||||||
|
|
||||||
This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation.
|
|
||||||
67
vendor/github.com/eclipse/paho.mqtt.golang/README.md
generated
vendored
67
vendor/github.com/eclipse/paho.mqtt.golang/README.md
generated
vendored
@@ -1,67 +0,0 @@
|
|||||||
|
|
||||||
[](https://godoc.org/github.com/eclipse/paho.mqtt.golang)
|
|
||||||
[](https://goreportcard.com/report/github.com/eclipse/paho.mqtt.golang)
|
|
||||||
|
|
||||||
Eclipse Paho MQTT Go client
|
|
||||||
===========================
|
|
||||||
|
|
||||||
|
|
||||||
This repository contains the source code for the [Eclipse Paho](http://eclipse.org/paho) MQTT Go client library.
|
|
||||||
|
|
||||||
This code builds a library which enable applications to connect to an [MQTT](http://mqtt.org) broker to publish messages, and to subscribe to topics and receive published messages.
|
|
||||||
|
|
||||||
This library supports a fully asynchronous mode of operation.
|
|
||||||
|
|
||||||
|
|
||||||
Installation and Build
|
|
||||||
----------------------
|
|
||||||
|
|
||||||
This client is designed to work with the standard Go tools, so installation is as easy as:
|
|
||||||
|
|
||||||
```
|
|
||||||
go get github.com/eclipse/paho.mqtt.golang
|
|
||||||
```
|
|
||||||
|
|
||||||
The client depends on Google's [websockets](https://godoc.org/golang.org/x/net/websocket) and [proxy](https://godoc.org/golang.org/x/net/proxy) package,
|
|
||||||
also easily installed with the commands:
|
|
||||||
|
|
||||||
```
|
|
||||||
go get golang.org/x/net/websocket
|
|
||||||
go get golang.org/x/net/proxy
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
Usage and API
|
|
||||||
-------------
|
|
||||||
|
|
||||||
Detailed API documentation is available by using to godoc tool, or can be browsed online
|
|
||||||
using the [godoc.org](http://godoc.org/github.com/eclipse/paho.mqtt.golang) service.
|
|
||||||
|
|
||||||
Make use of the library by importing it in your Go client source code. For example,
|
|
||||||
```
|
|
||||||
import "github.com/eclipse/paho.mqtt.golang"
|
|
||||||
```
|
|
||||||
|
|
||||||
Samples are available in the `cmd` directory for reference.
|
|
||||||
|
|
||||||
|
|
||||||
Runtime tracing
|
|
||||||
---------------
|
|
||||||
|
|
||||||
Tracing is enabled by assigning logs (from the Go log package) to the logging endpoints, ERROR, CRITICAL, WARN and DEBUG
|
|
||||||
|
|
||||||
|
|
||||||
Reporting bugs
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Please report bugs by raising issues for this project in github https://github.com/eclipse/paho.mqtt.golang/issues
|
|
||||||
|
|
||||||
|
|
||||||
More information
|
|
||||||
----------------
|
|
||||||
|
|
||||||
Discussion of the Paho clients takes place on the [Eclipse paho-dev mailing list](https://dev.eclipse.org/mailman/listinfo/paho-dev).
|
|
||||||
|
|
||||||
General questions about the MQTT protocol are discussed in the [MQTT Google Group](https://groups.google.com/forum/?hl=en-US&fromgroups#!forum/mqtt).
|
|
||||||
|
|
||||||
There is much more information available via the [MQTT community site](http://mqtt.org).
|
|
||||||
41
vendor/github.com/eclipse/paho.mqtt.golang/about.html
generated
vendored
41
vendor/github.com/eclipse/paho.mqtt.golang/about.html
generated
vendored
@@ -1,41 +0,0 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml"><head>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
||||||
<title>About</title>
|
|
||||||
</head>
|
|
||||||
<body lang="EN-US">
|
|
||||||
<h2>About This Content</h2>
|
|
||||||
|
|
||||||
<p><em>December 9, 2013</em></p>
|
|
||||||
<h3>License</h3>
|
|
||||||
|
|
||||||
<p>The Eclipse Foundation makes available all content in this plug-in ("Content"). Unless otherwise
|
|
||||||
indicated below, the Content is provided to you under the terms and conditions of the
|
|
||||||
Eclipse Public License Version 1.0 ("EPL") and Eclipse Distribution License Version 1.0 ("EDL").
|
|
||||||
A copy of the EPL is available at
|
|
||||||
<a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>
|
|
||||||
and a copy of the EDL is available at
|
|
||||||
<a href="http://www.eclipse.org/org/documents/edl-v10.php">http://www.eclipse.org/org/documents/edl-v10.php</a>.
|
|
||||||
For purposes of the EPL, "Program" will mean the Content.</p>
|
|
||||||
|
|
||||||
<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is
|
|
||||||
being redistributed by another party ("Redistributor") and different terms and conditions may
|
|
||||||
apply to your use of any object code in the Content. Check the Redistributor's license that was
|
|
||||||
provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise
|
|
||||||
indicated below, the terms and conditions of the EPL still apply to any source code in the Content
|
|
||||||
and such source code may be obtained at <a href="http://www.eclipse.org/">http://www.eclipse.org</a>.</p>
|
|
||||||
|
|
||||||
|
|
||||||
<h3>Third Party Content</h3>
|
|
||||||
<p>The Content includes items that have been sourced from third parties as set out below. If you
|
|
||||||
did not receive this Content directly from the Eclipse Foundation, the following is provided
|
|
||||||
for informational purposes only, and you should look to the Redistributor's license for
|
|
||||||
terms and conditions of use.</p>
|
|
||||||
<p><em>
|
|
||||||
<strong>None</strong> <br><br>
|
|
||||||
<br><br>
|
|
||||||
</em></p>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body></html>
|
|
||||||
759
vendor/github.com/eclipse/paho.mqtt.golang/client.go
generated
vendored
759
vendor/github.com/eclipse/paho.mqtt.golang/client.go
generated
vendored
@@ -1,759 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Portions copyright © 2018 TIBCO Software Inc.
|
|
||||||
|
|
||||||
// Package mqtt provides an MQTT v3.1.1 client library.
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/eclipse/paho.mqtt.golang/packets"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
disconnected uint32 = iota
|
|
||||||
connecting
|
|
||||||
reconnecting
|
|
||||||
connected
|
|
||||||
)
|
|
||||||
|
|
||||||
// Client is the interface definition for a Client as used by this
|
|
||||||
// library, the interface is primarily to allow mocking tests.
|
|
||||||
//
|
|
||||||
// It is an MQTT v3.1.1 client for communicating
|
|
||||||
// with an MQTT server using non-blocking methods that allow work
|
|
||||||
// to be done in the background.
|
|
||||||
// An application may connect to an MQTT server using:
|
|
||||||
// A plain TCP socket
|
|
||||||
// A secure SSL/TLS socket
|
|
||||||
// A websocket
|
|
||||||
// To enable ensured message delivery at Quality of Service (QoS) levels
|
|
||||||
// described in the MQTT spec, a message persistence mechanism must be
|
|
||||||
// used. This is done by providing a type which implements the Store
|
|
||||||
// interface. For convenience, FileStore and MemoryStore are provided
|
|
||||||
// implementations that should be sufficient for most use cases. More
|
|
||||||
// information can be found in their respective documentation.
|
|
||||||
// Numerous connection options may be specified by configuring a
|
|
||||||
// and then supplying a ClientOptions type.
|
|
||||||
type Client interface {
|
|
||||||
// IsConnected returns a bool signifying whether
|
|
||||||
// the client is connected or not.
|
|
||||||
IsConnected() bool
|
|
||||||
// IsConnectionOpen return a bool signifying wether the client has an active
|
|
||||||
// connection to mqtt broker, i.e not in disconnected or reconnect mode
|
|
||||||
IsConnectionOpen() bool
|
|
||||||
// Connect will create a connection to the message broker, by default
|
|
||||||
// it will attempt to connect at v3.1.1 and auto retry at v3.1 if that
|
|
||||||
// fails
|
|
||||||
Connect() Token
|
|
||||||
// Disconnect will end the connection with the server, but not before waiting
|
|
||||||
// the specified number of milliseconds to wait for existing work to be
|
|
||||||
// completed.
|
|
||||||
Disconnect(quiesce uint)
|
|
||||||
// Publish will publish a message with the specified QoS and content
|
|
||||||
// to the specified topic.
|
|
||||||
// Returns a token to track delivery of the message to the broker
|
|
||||||
Publish(topic string, qos byte, retained bool, payload interface{}) Token
|
|
||||||
// Subscribe starts a new subscription. Provide a MessageHandler to be executed when
|
|
||||||
// a message is published on the topic provided, or nil for the default handler
|
|
||||||
Subscribe(topic string, qos byte, callback MessageHandler) Token
|
|
||||||
// SubscribeMultiple starts a new subscription for multiple topics. Provide a MessageHandler to
|
|
||||||
// be executed when a message is published on one of the topics provided, or nil for the
|
|
||||||
// default handler
|
|
||||||
SubscribeMultiple(filters map[string]byte, callback MessageHandler) Token
|
|
||||||
// Unsubscribe will end the subscription from each of the topics provided.
|
|
||||||
// Messages published to those topics from other clients will no longer be
|
|
||||||
// received.
|
|
||||||
Unsubscribe(topics ...string) Token
|
|
||||||
// AddRoute allows you to add a handler for messages on a specific topic
|
|
||||||
// without making a subscription. For example having a different handler
|
|
||||||
// for parts of a wildcard subscription
|
|
||||||
AddRoute(topic string, callback MessageHandler)
|
|
||||||
// OptionsReader returns a ClientOptionsReader which is a copy of the clientoptions
|
|
||||||
// in use by the client.
|
|
||||||
OptionsReader() ClientOptionsReader
|
|
||||||
}
|
|
||||||
|
|
||||||
// client implements the Client interface
|
|
||||||
type client struct {
|
|
||||||
lastSent atomic.Value
|
|
||||||
lastReceived atomic.Value
|
|
||||||
pingOutstanding int32
|
|
||||||
status uint32
|
|
||||||
sync.RWMutex
|
|
||||||
messageIds
|
|
||||||
conn net.Conn
|
|
||||||
ibound chan packets.ControlPacket
|
|
||||||
obound chan *PacketAndToken
|
|
||||||
oboundP chan *PacketAndToken
|
|
||||||
msgRouter *router
|
|
||||||
stopRouter chan bool
|
|
||||||
incomingPubChan chan *packets.PublishPacket
|
|
||||||
errors chan error
|
|
||||||
stop chan struct{}
|
|
||||||
persist Store
|
|
||||||
options ClientOptions
|
|
||||||
workers sync.WaitGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewClient will create an MQTT v3.1.1 client with all of the options specified
|
|
||||||
// in the provided ClientOptions. The client must have the Connect method called
|
|
||||||
// on it before it may be used. This is to make sure resources (such as a net
|
|
||||||
// connection) are created before the application is actually ready.
|
|
||||||
func NewClient(o *ClientOptions) Client {
|
|
||||||
c := &client{}
|
|
||||||
c.options = *o
|
|
||||||
|
|
||||||
if c.options.Store == nil {
|
|
||||||
c.options.Store = NewMemoryStore()
|
|
||||||
}
|
|
||||||
switch c.options.ProtocolVersion {
|
|
||||||
case 3, 4:
|
|
||||||
c.options.protocolVersionExplicit = true
|
|
||||||
case 0x83, 0x84:
|
|
||||||
c.options.protocolVersionExplicit = true
|
|
||||||
default:
|
|
||||||
c.options.ProtocolVersion = 4
|
|
||||||
c.options.protocolVersionExplicit = false
|
|
||||||
}
|
|
||||||
c.persist = c.options.Store
|
|
||||||
c.status = disconnected
|
|
||||||
c.messageIds = messageIds{index: make(map[uint16]tokenCompletor)}
|
|
||||||
c.msgRouter, c.stopRouter = newRouter()
|
|
||||||
c.msgRouter.setDefaultHandler(c.options.DefaultPublishHandler)
|
|
||||||
if !c.options.AutoReconnect {
|
|
||||||
c.options.MessageChannelDepth = 0
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddRoute allows you to add a handler for messages on a specific topic
|
|
||||||
// without making a subscription. For example having a different handler
|
|
||||||
// for parts of a wildcard subscription
|
|
||||||
func (c *client) AddRoute(topic string, callback MessageHandler) {
|
|
||||||
if callback != nil {
|
|
||||||
c.msgRouter.addRoute(topic, callback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsConnected returns a bool signifying whether
|
|
||||||
// the client is connected or not.
|
|
||||||
func (c *client) IsConnected() bool {
|
|
||||||
c.RLock()
|
|
||||||
defer c.RUnlock()
|
|
||||||
status := atomic.LoadUint32(&c.status)
|
|
||||||
switch {
|
|
||||||
case status == connected:
|
|
||||||
return true
|
|
||||||
case c.options.AutoReconnect && status > connecting:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsConnectionOpen return a bool signifying whether the client has an active
|
|
||||||
// connection to mqtt broker, i.e not in disconnected or reconnect mode
|
|
||||||
func (c *client) IsConnectionOpen() bool {
|
|
||||||
c.RLock()
|
|
||||||
defer c.RUnlock()
|
|
||||||
status := atomic.LoadUint32(&c.status)
|
|
||||||
switch {
|
|
||||||
case status == connected:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *client) connectionStatus() uint32 {
|
|
||||||
c.RLock()
|
|
||||||
defer c.RUnlock()
|
|
||||||
status := atomic.LoadUint32(&c.status)
|
|
||||||
return status
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *client) setConnected(status uint32) {
|
|
||||||
c.Lock()
|
|
||||||
defer c.Unlock()
|
|
||||||
atomic.StoreUint32(&c.status, uint32(status))
|
|
||||||
}
|
|
||||||
|
|
||||||
//ErrNotConnected is the error returned from function calls that are
|
|
||||||
//made when the client is not connected to a broker
|
|
||||||
var ErrNotConnected = errors.New("Not Connected")
|
|
||||||
|
|
||||||
// Connect will create a connection to the message broker, by default
|
|
||||||
// it will attempt to connect at v3.1.1 and auto retry at v3.1 if that
|
|
||||||
// fails
|
|
||||||
func (c *client) Connect() Token {
|
|
||||||
var err error
|
|
||||||
t := newToken(packets.Connect).(*ConnectToken)
|
|
||||||
DEBUG.Println(CLI, "Connect()")
|
|
||||||
|
|
||||||
c.obound = make(chan *PacketAndToken, c.options.MessageChannelDepth)
|
|
||||||
c.oboundP = make(chan *PacketAndToken, c.options.MessageChannelDepth)
|
|
||||||
c.ibound = make(chan packets.ControlPacket)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
c.persist.Open()
|
|
||||||
|
|
||||||
c.setConnected(connecting)
|
|
||||||
c.errors = make(chan error, 1)
|
|
||||||
c.stop = make(chan struct{})
|
|
||||||
|
|
||||||
var rc byte
|
|
||||||
protocolVersion := c.options.ProtocolVersion
|
|
||||||
|
|
||||||
if len(c.options.Servers) == 0 {
|
|
||||||
t.setError(fmt.Errorf("No servers defined to connect to"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, broker := range c.options.Servers {
|
|
||||||
cm := newConnectMsgFromOptions(&c.options, broker)
|
|
||||||
c.options.ProtocolVersion = protocolVersion
|
|
||||||
CONN:
|
|
||||||
DEBUG.Println(CLI, "about to write new connect msg")
|
|
||||||
c.conn, err = openConnection(broker, c.options.TLSConfig, c.options.ConnectTimeout, c.options.HTTPHeaders)
|
|
||||||
if err == nil {
|
|
||||||
DEBUG.Println(CLI, "socket connected to broker")
|
|
||||||
switch c.options.ProtocolVersion {
|
|
||||||
case 3:
|
|
||||||
DEBUG.Println(CLI, "Using MQTT 3.1 protocol")
|
|
||||||
cm.ProtocolName = "MQIsdp"
|
|
||||||
cm.ProtocolVersion = 3
|
|
||||||
case 0x83:
|
|
||||||
DEBUG.Println(CLI, "Using MQTT 3.1b protocol")
|
|
||||||
cm.ProtocolName = "MQIsdp"
|
|
||||||
cm.ProtocolVersion = 0x83
|
|
||||||
case 0x84:
|
|
||||||
DEBUG.Println(CLI, "Using MQTT 3.1.1b protocol")
|
|
||||||
cm.ProtocolName = "MQTT"
|
|
||||||
cm.ProtocolVersion = 0x84
|
|
||||||
default:
|
|
||||||
DEBUG.Println(CLI, "Using MQTT 3.1.1 protocol")
|
|
||||||
c.options.ProtocolVersion = 4
|
|
||||||
cm.ProtocolName = "MQTT"
|
|
||||||
cm.ProtocolVersion = 4
|
|
||||||
}
|
|
||||||
cm.Write(c.conn)
|
|
||||||
|
|
||||||
rc, t.sessionPresent = c.connect()
|
|
||||||
if rc != packets.Accepted {
|
|
||||||
if c.conn != nil {
|
|
||||||
c.conn.Close()
|
|
||||||
c.conn = nil
|
|
||||||
}
|
|
||||||
//if the protocol version was explicitly set don't do any fallback
|
|
||||||
if c.options.protocolVersionExplicit {
|
|
||||||
ERROR.Println(CLI, "Connecting to", broker, "CONNACK was not CONN_ACCEPTED, but rather", packets.ConnackReturnCodes[rc])
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if c.options.ProtocolVersion == 4 {
|
|
||||||
DEBUG.Println(CLI, "Trying reconnect using MQTT 3.1 protocol")
|
|
||||||
c.options.ProtocolVersion = 3
|
|
||||||
goto CONN
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
ERROR.Println(CLI, err.Error())
|
|
||||||
WARN.Println(CLI, "failed to connect to broker, trying next")
|
|
||||||
rc = packets.ErrNetworkError
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.conn == nil {
|
|
||||||
ERROR.Println(CLI, "Failed to connect to a broker")
|
|
||||||
c.setConnected(disconnected)
|
|
||||||
c.persist.Close()
|
|
||||||
t.returnCode = rc
|
|
||||||
if rc != packets.ErrNetworkError {
|
|
||||||
t.setError(packets.ConnErrors[rc])
|
|
||||||
} else {
|
|
||||||
t.setError(fmt.Errorf("%s : %s", packets.ConnErrors[rc], err))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.options.protocolVersionExplicit = true
|
|
||||||
|
|
||||||
if c.options.KeepAlive != 0 {
|
|
||||||
atomic.StoreInt32(&c.pingOutstanding, 0)
|
|
||||||
c.lastReceived.Store(time.Now())
|
|
||||||
c.lastSent.Store(time.Now())
|
|
||||||
c.workers.Add(1)
|
|
||||||
go keepalive(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.incomingPubChan = make(chan *packets.PublishPacket, c.options.MessageChannelDepth)
|
|
||||||
c.msgRouter.matchAndDispatch(c.incomingPubChan, c.options.Order, c)
|
|
||||||
|
|
||||||
c.setConnected(connected)
|
|
||||||
DEBUG.Println(CLI, "client is connected")
|
|
||||||
if c.options.OnConnect != nil {
|
|
||||||
go c.options.OnConnect(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.workers.Add(4)
|
|
||||||
go errorWatch(c)
|
|
||||||
go alllogic(c)
|
|
||||||
go outgoing(c)
|
|
||||||
go incoming(c)
|
|
||||||
|
|
||||||
// Take care of any messages in the store
|
|
||||||
if c.options.CleanSession == false {
|
|
||||||
c.resume(c.options.ResumeSubs)
|
|
||||||
} else {
|
|
||||||
c.persist.Reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG.Println(CLI, "exit startClient")
|
|
||||||
t.flowComplete()
|
|
||||||
}()
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
// internal function used to reconnect the client when it loses its connection
|
|
||||||
func (c *client) reconnect() {
|
|
||||||
DEBUG.Println(CLI, "enter reconnect")
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
|
|
||||||
rc = byte(1)
|
|
||||||
sleep = time.Duration(1 * time.Second)
|
|
||||||
)
|
|
||||||
|
|
||||||
for rc != 0 && atomic.LoadUint32(&c.status) != disconnected {
|
|
||||||
for _, broker := range c.options.Servers {
|
|
||||||
cm := newConnectMsgFromOptions(&c.options, broker)
|
|
||||||
DEBUG.Println(CLI, "about to write new connect msg")
|
|
||||||
c.Lock()
|
|
||||||
c.conn, err = openConnection(broker, c.options.TLSConfig, c.options.ConnectTimeout, c.options.HTTPHeaders)
|
|
||||||
c.Unlock()
|
|
||||||
if err == nil {
|
|
||||||
DEBUG.Println(CLI, "socket connected to broker")
|
|
||||||
switch c.options.ProtocolVersion {
|
|
||||||
case 0x83:
|
|
||||||
DEBUG.Println(CLI, "Using MQTT 3.1b protocol")
|
|
||||||
cm.ProtocolName = "MQIsdp"
|
|
||||||
cm.ProtocolVersion = 0x83
|
|
||||||
case 0x84:
|
|
||||||
DEBUG.Println(CLI, "Using MQTT 3.1.1b protocol")
|
|
||||||
cm.ProtocolName = "MQTT"
|
|
||||||
cm.ProtocolVersion = 0x84
|
|
||||||
case 3:
|
|
||||||
DEBUG.Println(CLI, "Using MQTT 3.1 protocol")
|
|
||||||
cm.ProtocolName = "MQIsdp"
|
|
||||||
cm.ProtocolVersion = 3
|
|
||||||
default:
|
|
||||||
DEBUG.Println(CLI, "Using MQTT 3.1.1 protocol")
|
|
||||||
cm.ProtocolName = "MQTT"
|
|
||||||
cm.ProtocolVersion = 4
|
|
||||||
}
|
|
||||||
cm.Write(c.conn)
|
|
||||||
|
|
||||||
rc, _ = c.connect()
|
|
||||||
if rc != packets.Accepted {
|
|
||||||
c.conn.Close()
|
|
||||||
c.conn = nil
|
|
||||||
//if the protocol version was explicitly set don't do any fallback
|
|
||||||
if c.options.protocolVersionExplicit {
|
|
||||||
ERROR.Println(CLI, "Connecting to", broker, "CONNACK was not Accepted, but rather", packets.ConnackReturnCodes[rc])
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
ERROR.Println(CLI, err.Error())
|
|
||||||
WARN.Println(CLI, "failed to connect to broker, trying next")
|
|
||||||
rc = packets.ErrNetworkError
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if rc != 0 {
|
|
||||||
DEBUG.Println(CLI, "Reconnect failed, sleeping for", int(sleep.Seconds()), "seconds")
|
|
||||||
time.Sleep(sleep)
|
|
||||||
if sleep < c.options.MaxReconnectInterval {
|
|
||||||
sleep *= 2
|
|
||||||
}
|
|
||||||
|
|
||||||
if sleep > c.options.MaxReconnectInterval {
|
|
||||||
sleep = c.options.MaxReconnectInterval
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Disconnect() must have been called while we were trying to reconnect.
|
|
||||||
if c.connectionStatus() == disconnected {
|
|
||||||
DEBUG.Println(CLI, "Client moved to disconnected state while reconnecting, abandoning reconnect")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.stop = make(chan struct{})
|
|
||||||
|
|
||||||
if c.options.KeepAlive != 0 {
|
|
||||||
atomic.StoreInt32(&c.pingOutstanding, 0)
|
|
||||||
c.lastReceived.Store(time.Now())
|
|
||||||
c.lastSent.Store(time.Now())
|
|
||||||
c.workers.Add(1)
|
|
||||||
go keepalive(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.setConnected(connected)
|
|
||||||
DEBUG.Println(CLI, "client is reconnected")
|
|
||||||
if c.options.OnConnect != nil {
|
|
||||||
go c.options.OnConnect(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.workers.Add(4)
|
|
||||||
go errorWatch(c)
|
|
||||||
go alllogic(c)
|
|
||||||
go outgoing(c)
|
|
||||||
go incoming(c)
|
|
||||||
|
|
||||||
c.resume(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is only used for receiving a connack
|
|
||||||
// when the connection is first started.
|
|
||||||
// This prevents receiving incoming data while resume
|
|
||||||
// is in progress if clean session is false.
|
|
||||||
func (c *client) connect() (byte, bool) {
|
|
||||||
DEBUG.Println(NET, "connect started")
|
|
||||||
|
|
||||||
ca, err := packets.ReadPacket(c.conn)
|
|
||||||
if err != nil {
|
|
||||||
ERROR.Println(NET, "connect got error", err)
|
|
||||||
return packets.ErrNetworkError, false
|
|
||||||
}
|
|
||||||
if ca == nil {
|
|
||||||
ERROR.Println(NET, "received nil packet")
|
|
||||||
return packets.ErrNetworkError, false
|
|
||||||
}
|
|
||||||
|
|
||||||
msg, ok := ca.(*packets.ConnackPacket)
|
|
||||||
if !ok {
|
|
||||||
ERROR.Println(NET, "received msg that was not CONNACK")
|
|
||||||
return packets.ErrNetworkError, false
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG.Println(NET, "received connack")
|
|
||||||
return msg.ReturnCode, msg.SessionPresent
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disconnect will end the connection with the server, but not before waiting
|
|
||||||
// the specified number of milliseconds to wait for existing work to be
|
|
||||||
// completed.
|
|
||||||
func (c *client) Disconnect(quiesce uint) {
|
|
||||||
status := atomic.LoadUint32(&c.status)
|
|
||||||
if status == connected {
|
|
||||||
DEBUG.Println(CLI, "disconnecting")
|
|
||||||
c.setConnected(disconnected)
|
|
||||||
|
|
||||||
dm := packets.NewControlPacket(packets.Disconnect).(*packets.DisconnectPacket)
|
|
||||||
dt := newToken(packets.Disconnect)
|
|
||||||
c.oboundP <- &PacketAndToken{p: dm, t: dt}
|
|
||||||
|
|
||||||
// wait for work to finish, or quiesce time consumed
|
|
||||||
dt.WaitTimeout(time.Duration(quiesce) * time.Millisecond)
|
|
||||||
} else {
|
|
||||||
WARN.Println(CLI, "Disconnect() called but not connected (disconnected/reconnecting)")
|
|
||||||
c.setConnected(disconnected)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.disconnect()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ForceDisconnect will end the connection with the mqtt broker immediately.
|
|
||||||
func (c *client) forceDisconnect() {
|
|
||||||
if !c.IsConnected() {
|
|
||||||
WARN.Println(CLI, "already disconnected")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.setConnected(disconnected)
|
|
||||||
c.conn.Close()
|
|
||||||
DEBUG.Println(CLI, "forcefully disconnecting")
|
|
||||||
c.disconnect()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *client) internalConnLost(err error) {
|
|
||||||
// Only do anything if this was called and we are still "connected"
|
|
||||||
// forceDisconnect can cause incoming/outgoing/alllogic to end with
|
|
||||||
// error from closing the socket but state will be "disconnected"
|
|
||||||
if c.IsConnected() {
|
|
||||||
c.closeStop()
|
|
||||||
c.conn.Close()
|
|
||||||
c.workers.Wait()
|
|
||||||
if c.options.CleanSession && !c.options.AutoReconnect {
|
|
||||||
c.messageIds.cleanUp()
|
|
||||||
}
|
|
||||||
if c.options.AutoReconnect {
|
|
||||||
c.setConnected(reconnecting)
|
|
||||||
go c.reconnect()
|
|
||||||
} else {
|
|
||||||
c.setConnected(disconnected)
|
|
||||||
}
|
|
||||||
if c.options.OnConnectionLost != nil {
|
|
||||||
go c.options.OnConnectionLost(c, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *client) closeStop() {
|
|
||||||
c.Lock()
|
|
||||||
defer c.Unlock()
|
|
||||||
select {
|
|
||||||
case <-c.stop:
|
|
||||||
DEBUG.Println("In disconnect and stop channel is already closed")
|
|
||||||
default:
|
|
||||||
if c.stop != nil {
|
|
||||||
close(c.stop)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *client) closeStopRouter() {
|
|
||||||
c.Lock()
|
|
||||||
defer c.Unlock()
|
|
||||||
select {
|
|
||||||
case <-c.stopRouter:
|
|
||||||
DEBUG.Println("In disconnect and stop channel is already closed")
|
|
||||||
default:
|
|
||||||
if c.stopRouter != nil {
|
|
||||||
close(c.stopRouter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *client) closeConn() {
|
|
||||||
c.Lock()
|
|
||||||
defer c.Unlock()
|
|
||||||
if c.conn != nil {
|
|
||||||
c.conn.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *client) disconnect() {
|
|
||||||
c.closeStop()
|
|
||||||
c.closeConn()
|
|
||||||
c.workers.Wait()
|
|
||||||
c.messageIds.cleanUp()
|
|
||||||
c.closeStopRouter()
|
|
||||||
DEBUG.Println(CLI, "disconnected")
|
|
||||||
c.persist.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Publish will publish a message with the specified QoS and content
|
|
||||||
// to the specified topic.
|
|
||||||
// Returns a token to track delivery of the message to the broker
|
|
||||||
func (c *client) Publish(topic string, qos byte, retained bool, payload interface{}) Token {
|
|
||||||
token := newToken(packets.Publish).(*PublishToken)
|
|
||||||
DEBUG.Println(CLI, "enter Publish")
|
|
||||||
switch {
|
|
||||||
case !c.IsConnected():
|
|
||||||
token.setError(ErrNotConnected)
|
|
||||||
return token
|
|
||||||
case c.connectionStatus() == reconnecting && qos == 0:
|
|
||||||
token.flowComplete()
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
pub := packets.NewControlPacket(packets.Publish).(*packets.PublishPacket)
|
|
||||||
pub.Qos = qos
|
|
||||||
pub.TopicName = topic
|
|
||||||
pub.Retain = retained
|
|
||||||
switch payload.(type) {
|
|
||||||
case string:
|
|
||||||
pub.Payload = []byte(payload.(string))
|
|
||||||
case []byte:
|
|
||||||
pub.Payload = payload.([]byte)
|
|
||||||
default:
|
|
||||||
token.setError(fmt.Errorf("Unknown payload type"))
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
if pub.Qos != 0 && pub.MessageID == 0 {
|
|
||||||
pub.MessageID = c.getID(token)
|
|
||||||
token.messageID = pub.MessageID
|
|
||||||
}
|
|
||||||
persistOutbound(c.persist, pub)
|
|
||||||
if c.connectionStatus() == reconnecting {
|
|
||||||
DEBUG.Println(CLI, "storing publish message (reconnecting), topic:", topic)
|
|
||||||
} else {
|
|
||||||
DEBUG.Println(CLI, "sending publish message, topic:", topic)
|
|
||||||
c.obound <- &PacketAndToken{p: pub, t: token}
|
|
||||||
}
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subscribe starts a new subscription. Provide a MessageHandler to be executed when
|
|
||||||
// a message is published on the topic provided.
|
|
||||||
func (c *client) Subscribe(topic string, qos byte, callback MessageHandler) Token {
|
|
||||||
token := newToken(packets.Subscribe).(*SubscribeToken)
|
|
||||||
DEBUG.Println(CLI, "enter Subscribe")
|
|
||||||
if !c.IsConnected() {
|
|
||||||
token.setError(ErrNotConnected)
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
sub := packets.NewControlPacket(packets.Subscribe).(*packets.SubscribePacket)
|
|
||||||
if err := validateTopicAndQos(topic, qos); err != nil {
|
|
||||||
token.setError(err)
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
sub.Topics = append(sub.Topics, topic)
|
|
||||||
sub.Qoss = append(sub.Qoss, qos)
|
|
||||||
DEBUG.Println(CLI, sub.String())
|
|
||||||
|
|
||||||
if strings.HasPrefix(topic, "$share") {
|
|
||||||
topic = strings.Join(strings.Split(topic, "/")[2:], "/")
|
|
||||||
}
|
|
||||||
|
|
||||||
if callback != nil {
|
|
||||||
c.msgRouter.addRoute(topic, callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
token.subs = append(token.subs, topic)
|
|
||||||
c.oboundP <- &PacketAndToken{p: sub, t: token}
|
|
||||||
DEBUG.Println(CLI, "exit Subscribe")
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
// SubscribeMultiple starts a new subscription for multiple topics. Provide a MessageHandler to
|
|
||||||
// be executed when a message is published on one of the topics provided.
|
|
||||||
func (c *client) SubscribeMultiple(filters map[string]byte, callback MessageHandler) Token {
|
|
||||||
var err error
|
|
||||||
token := newToken(packets.Subscribe).(*SubscribeToken)
|
|
||||||
DEBUG.Println(CLI, "enter SubscribeMultiple")
|
|
||||||
if !c.IsConnected() {
|
|
||||||
token.setError(ErrNotConnected)
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
sub := packets.NewControlPacket(packets.Subscribe).(*packets.SubscribePacket)
|
|
||||||
if sub.Topics, sub.Qoss, err = validateSubscribeMap(filters); err != nil {
|
|
||||||
token.setError(err)
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
if callback != nil {
|
|
||||||
for topic := range filters {
|
|
||||||
c.msgRouter.addRoute(topic, callback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
token.subs = make([]string, len(sub.Topics))
|
|
||||||
copy(token.subs, sub.Topics)
|
|
||||||
c.oboundP <- &PacketAndToken{p: sub, t: token}
|
|
||||||
DEBUG.Println(CLI, "exit SubscribeMultiple")
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load all stored messages and resend them
|
|
||||||
// Call this to ensure QOS > 1,2 even after an application crash
|
|
||||||
func (c *client) resume(subscription bool) {
|
|
||||||
|
|
||||||
storedKeys := c.persist.All()
|
|
||||||
for _, key := range storedKeys {
|
|
||||||
packet := c.persist.Get(key)
|
|
||||||
if packet == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
details := packet.Details()
|
|
||||||
if isKeyOutbound(key) {
|
|
||||||
switch packet.(type) {
|
|
||||||
case *packets.SubscribePacket:
|
|
||||||
if subscription {
|
|
||||||
DEBUG.Println(STR, fmt.Sprintf("loaded pending subscribe (%d)", details.MessageID))
|
|
||||||
token := newToken(packets.Subscribe).(*SubscribeToken)
|
|
||||||
c.oboundP <- &PacketAndToken{p: packet, t: token}
|
|
||||||
}
|
|
||||||
case *packets.UnsubscribePacket:
|
|
||||||
if subscription {
|
|
||||||
DEBUG.Println(STR, fmt.Sprintf("loaded pending unsubscribe (%d)", details.MessageID))
|
|
||||||
token := newToken(packets.Unsubscribe).(*UnsubscribeToken)
|
|
||||||
c.oboundP <- &PacketAndToken{p: packet, t: token}
|
|
||||||
}
|
|
||||||
case *packets.PubrelPacket:
|
|
||||||
DEBUG.Println(STR, fmt.Sprintf("loaded pending pubrel (%d)", details.MessageID))
|
|
||||||
select {
|
|
||||||
case c.oboundP <- &PacketAndToken{p: packet, t: nil}:
|
|
||||||
case <-c.stop:
|
|
||||||
}
|
|
||||||
case *packets.PublishPacket:
|
|
||||||
token := newToken(packets.Publish).(*PublishToken)
|
|
||||||
token.messageID = details.MessageID
|
|
||||||
c.claimID(token, details.MessageID)
|
|
||||||
DEBUG.Println(STR, fmt.Sprintf("loaded pending publish (%d)", details.MessageID))
|
|
||||||
DEBUG.Println(STR, details)
|
|
||||||
c.obound <- &PacketAndToken{p: packet, t: token}
|
|
||||||
default:
|
|
||||||
ERROR.Println(STR, "invalid message type in store (discarded)")
|
|
||||||
c.persist.Del(key)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
switch packet.(type) {
|
|
||||||
case *packets.PubrelPacket, *packets.PublishPacket:
|
|
||||||
DEBUG.Println(STR, fmt.Sprintf("loaded pending incomming (%d)", details.MessageID))
|
|
||||||
select {
|
|
||||||
case c.ibound <- packet:
|
|
||||||
case <-c.stop:
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
ERROR.Println(STR, "invalid message type in store (discarded)")
|
|
||||||
c.persist.Del(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unsubscribe will end the subscription from each of the topics provided.
|
|
||||||
// Messages published to those topics from other clients will no longer be
|
|
||||||
// received.
|
|
||||||
func (c *client) Unsubscribe(topics ...string) Token {
|
|
||||||
token := newToken(packets.Unsubscribe).(*UnsubscribeToken)
|
|
||||||
DEBUG.Println(CLI, "enter Unsubscribe")
|
|
||||||
if !c.IsConnected() {
|
|
||||||
token.setError(ErrNotConnected)
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
unsub := packets.NewControlPacket(packets.Unsubscribe).(*packets.UnsubscribePacket)
|
|
||||||
unsub.Topics = make([]string, len(topics))
|
|
||||||
copy(unsub.Topics, topics)
|
|
||||||
|
|
||||||
c.oboundP <- &PacketAndToken{p: unsub, t: token}
|
|
||||||
for _, topic := range topics {
|
|
||||||
c.msgRouter.deleteRoute(topic)
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG.Println(CLI, "exit Unsubscribe")
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
// OptionsReader returns a ClientOptionsReader which is a copy of the clientoptions
|
|
||||||
// in use by the client.
|
|
||||||
func (c *client) OptionsReader() ClientOptionsReader {
|
|
||||||
r := ClientOptionsReader{options: &c.options}
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
//DefaultConnectionLostHandler is a definition of a function that simply
|
|
||||||
//reports to the DEBUG log the reason for the client losing a connection.
|
|
||||||
func DefaultConnectionLostHandler(client Client, reason error) {
|
|
||||||
DEBUG.Println("Connection lost:", reason.Error())
|
|
||||||
}
|
|
||||||
31
vendor/github.com/eclipse/paho.mqtt.golang/components.go
generated
vendored
31
vendor/github.com/eclipse/paho.mqtt.golang/components.go
generated
vendored
@@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
type component string
|
|
||||||
|
|
||||||
// Component names for debug output
|
|
||||||
const (
|
|
||||||
NET component = "[net] "
|
|
||||||
PNG component = "[pinger] "
|
|
||||||
CLI component = "[client] "
|
|
||||||
DEC component = "[decode] "
|
|
||||||
MES component = "[message] "
|
|
||||||
STR component = "[store] "
|
|
||||||
MID component = "[msgids] "
|
|
||||||
TST component = "[test] "
|
|
||||||
STA component = "[state] "
|
|
||||||
ERR component = "[error] "
|
|
||||||
)
|
|
||||||
15
vendor/github.com/eclipse/paho.mqtt.golang/edl-v10
generated
vendored
15
vendor/github.com/eclipse/paho.mqtt.golang/edl-v10
generated
vendored
@@ -1,15 +0,0 @@
|
|||||||
|
|
||||||
Eclipse Distribution License - v 1.0
|
|
||||||
|
|
||||||
Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.
|
|
||||||
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
||||||
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
||||||
Neither the name of the Eclipse Foundation, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
70
vendor/github.com/eclipse/paho.mqtt.golang/epl-v10
generated
vendored
70
vendor/github.com/eclipse/paho.mqtt.golang/epl-v10
generated
vendored
@@ -1,70 +0,0 @@
|
|||||||
Eclipse Public License - v 1.0
|
|
||||||
|
|
||||||
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
|
|
||||||
|
|
||||||
1. DEFINITIONS
|
|
||||||
|
|
||||||
"Contribution" means:
|
|
||||||
|
|
||||||
a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and
|
|
||||||
b) in the case of each subsequent Contributor:
|
|
||||||
i) changes to the Program, and
|
|
||||||
ii) additions to the Program;
|
|
||||||
where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program.
|
|
||||||
"Contributor" means any person or entity that distributes the Program.
|
|
||||||
|
|
||||||
"Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program.
|
|
||||||
|
|
||||||
"Program" means the Contributions distributed in accordance with this Agreement.
|
|
||||||
|
|
||||||
"Recipient" means anyone who receives the Program under this Agreement, including all Contributors.
|
|
||||||
|
|
||||||
2. GRANT OF RIGHTS
|
|
||||||
|
|
||||||
a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form.
|
|
||||||
b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder.
|
|
||||||
c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program.
|
|
||||||
d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement.
|
|
||||||
3. REQUIREMENTS
|
|
||||||
|
|
||||||
A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that:
|
|
||||||
|
|
||||||
a) it complies with the terms and conditions of this Agreement; and
|
|
||||||
b) its license agreement:
|
|
||||||
i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose;
|
|
||||||
ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits;
|
|
||||||
iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and
|
|
||||||
iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange.
|
|
||||||
When the Program is made available in source code form:
|
|
||||||
|
|
||||||
a) it must be made available under this Agreement; and
|
|
||||||
b) a copy of this Agreement must be included with each copy of the Program.
|
|
||||||
Contributors may not remove or alter any copyright notices contained within the Program.
|
|
||||||
|
|
||||||
Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution.
|
|
||||||
|
|
||||||
4. COMMERCIAL DISTRIBUTION
|
|
||||||
|
|
||||||
Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense.
|
|
||||||
|
|
||||||
For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages.
|
|
||||||
|
|
||||||
5. NO WARRANTY
|
|
||||||
|
|
||||||
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations.
|
|
||||||
|
|
||||||
6. DISCLAIMER OF LIABILITY
|
|
||||||
|
|
||||||
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|
||||||
|
|
||||||
7. GENERAL
|
|
||||||
|
|
||||||
If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
|
|
||||||
|
|
||||||
If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed.
|
|
||||||
|
|
||||||
All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive.
|
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved.
|
|
||||||
|
|
||||||
This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation.
|
|
||||||
255
vendor/github.com/eclipse/paho.mqtt.golang/filestore.go
generated
vendored
255
vendor/github.com/eclipse/paho.mqtt.golang/filestore.go
generated
vendored
@@ -1,255 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"sort"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/eclipse/paho.mqtt.golang/packets"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
msgExt = ".msg"
|
|
||||||
tmpExt = ".tmp"
|
|
||||||
corruptExt = ".CORRUPT"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FileStore implements the store interface using the filesystem to provide
|
|
||||||
// true persistence, even across client failure. This is designed to use a
|
|
||||||
// single directory per running client. If you are running multiple clients
|
|
||||||
// on the same filesystem, you will need to be careful to specify unique
|
|
||||||
// store directories for each.
|
|
||||||
type FileStore struct {
|
|
||||||
sync.RWMutex
|
|
||||||
directory string
|
|
||||||
opened bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFileStore will create a new FileStore which stores its messages in the
|
|
||||||
// directory provided.
|
|
||||||
func NewFileStore(directory string) *FileStore {
|
|
||||||
store := &FileStore{
|
|
||||||
directory: directory,
|
|
||||||
opened: false,
|
|
||||||
}
|
|
||||||
return store
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open will allow the FileStore to be used.
|
|
||||||
func (store *FileStore) Open() {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
// if no store directory was specified in ClientOpts, by default use the
|
|
||||||
// current working directory
|
|
||||||
if store.directory == "" {
|
|
||||||
store.directory, _ = os.Getwd()
|
|
||||||
}
|
|
||||||
|
|
||||||
// if store dir exists, great, otherwise, create it
|
|
||||||
if !exists(store.directory) {
|
|
||||||
perms := os.FileMode(0770)
|
|
||||||
merr := os.MkdirAll(store.directory, perms)
|
|
||||||
chkerr(merr)
|
|
||||||
}
|
|
||||||
store.opened = true
|
|
||||||
DEBUG.Println(STR, "store is opened at", store.directory)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close will disallow the FileStore from being used.
|
|
||||||
func (store *FileStore) Close() {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
store.opened = false
|
|
||||||
DEBUG.Println(STR, "store is closed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put will put a message into the store, associated with the provided
|
|
||||||
// key value.
|
|
||||||
func (store *FileStore) Put(key string, m packets.ControlPacket) {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to use file store, but not open")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
full := fullpath(store.directory, key)
|
|
||||||
write(store.directory, key, m)
|
|
||||||
if !exists(full) {
|
|
||||||
ERROR.Println(STR, "file not created:", full)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get will retrieve a message from the store, the one associated with
|
|
||||||
// the provided key value.
|
|
||||||
func (store *FileStore) Get(key string) packets.ControlPacket {
|
|
||||||
store.RLock()
|
|
||||||
defer store.RUnlock()
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to use file store, but not open")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
filepath := fullpath(store.directory, key)
|
|
||||||
if !exists(filepath) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
mfile, oerr := os.Open(filepath)
|
|
||||||
chkerr(oerr)
|
|
||||||
msg, rerr := packets.ReadPacket(mfile)
|
|
||||||
chkerr(mfile.Close())
|
|
||||||
|
|
||||||
// Message was unreadable, return nil
|
|
||||||
if rerr != nil {
|
|
||||||
newpath := corruptpath(store.directory, key)
|
|
||||||
WARN.Println(STR, "corrupted file detected:", rerr.Error(), "archived at:", newpath)
|
|
||||||
os.Rename(filepath, newpath)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// All will provide a list of all of the keys associated with messages
|
|
||||||
// currenly residing in the FileStore.
|
|
||||||
func (store *FileStore) All() []string {
|
|
||||||
store.RLock()
|
|
||||||
defer store.RUnlock()
|
|
||||||
return store.all()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Del will remove the persisted message associated with the provided
|
|
||||||
// key from the FileStore.
|
|
||||||
func (store *FileStore) Del(key string) {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
store.del(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset will remove all persisted messages from the FileStore.
|
|
||||||
func (store *FileStore) Reset() {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
WARN.Println(STR, "FileStore Reset")
|
|
||||||
for _, key := range store.all() {
|
|
||||||
store.del(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// lockless
|
|
||||||
func (store *FileStore) all() []string {
|
|
||||||
var err error
|
|
||||||
var keys []string
|
|
||||||
var files fileInfos
|
|
||||||
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to use file store, but not open")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
files, err = ioutil.ReadDir(store.directory)
|
|
||||||
chkerr(err)
|
|
||||||
sort.Sort(files)
|
|
||||||
for _, f := range files {
|
|
||||||
DEBUG.Println(STR, "file in All():", f.Name())
|
|
||||||
name := f.Name()
|
|
||||||
if name[len(name)-4:len(name)] != msgExt {
|
|
||||||
DEBUG.Println(STR, "skipping file, doesn't have right extension: ", name)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
key := name[0 : len(name)-4] // remove file extension
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
// lockless
|
|
||||||
func (store *FileStore) del(key string) {
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to use file store, but not open")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
DEBUG.Println(STR, "store del filepath:", store.directory)
|
|
||||||
DEBUG.Println(STR, "store delete key:", key)
|
|
||||||
filepath := fullpath(store.directory, key)
|
|
||||||
DEBUG.Println(STR, "path of deletion:", filepath)
|
|
||||||
if !exists(filepath) {
|
|
||||||
WARN.Println(STR, "store could not delete key:", key)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rerr := os.Remove(filepath)
|
|
||||||
chkerr(rerr)
|
|
||||||
DEBUG.Println(STR, "del msg:", key)
|
|
||||||
if exists(filepath) {
|
|
||||||
ERROR.Println(STR, "file not deleted:", filepath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fullpath(store string, key string) string {
|
|
||||||
p := path.Join(store, key+msgExt)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func tmppath(store string, key string) string {
|
|
||||||
p := path.Join(store, key+tmpExt)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func corruptpath(store string, key string) string {
|
|
||||||
p := path.Join(store, key+corruptExt)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// create file called "X.[messageid].tmp" located in the store
|
|
||||||
// the contents of the file is the bytes of the message, then
|
|
||||||
// rename it to "X.[messageid].msg", overwriting any existing
|
|
||||||
// message with the same id
|
|
||||||
// X will be 'i' for inbound messages, and O for outbound messages
|
|
||||||
func write(store, key string, m packets.ControlPacket) {
|
|
||||||
temppath := tmppath(store, key)
|
|
||||||
f, err := os.Create(temppath)
|
|
||||||
chkerr(err)
|
|
||||||
werr := m.Write(f)
|
|
||||||
chkerr(werr)
|
|
||||||
cerr := f.Close()
|
|
||||||
chkerr(cerr)
|
|
||||||
rerr := os.Rename(temppath, fullpath(store, key))
|
|
||||||
chkerr(rerr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func exists(file string) bool {
|
|
||||||
if _, err := os.Stat(file); err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
chkerr(err)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
type fileInfos []os.FileInfo
|
|
||||||
|
|
||||||
func (f fileInfos) Len() int {
|
|
||||||
return len(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f fileInfos) Swap(i, j int) {
|
|
||||||
f[i], f[j] = f[j], f[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f fileInfos) Less(i, j int) bool {
|
|
||||||
return f[i].ModTime().Before(f[j].ModTime())
|
|
||||||
}
|
|
||||||
138
vendor/github.com/eclipse/paho.mqtt.golang/memstore.go
generated
vendored
138
vendor/github.com/eclipse/paho.mqtt.golang/memstore.go
generated
vendored
@@ -1,138 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/eclipse/paho.mqtt.golang/packets"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MemoryStore implements the store interface to provide a "persistence"
|
|
||||||
// mechanism wholly stored in memory. This is only useful for
|
|
||||||
// as long as the client instance exists.
|
|
||||||
type MemoryStore struct {
|
|
||||||
sync.RWMutex
|
|
||||||
messages map[string]packets.ControlPacket
|
|
||||||
opened bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMemoryStore returns a pointer to a new instance of
|
|
||||||
// MemoryStore, the instance is not initialized and ready to
|
|
||||||
// use until Open() has been called on it.
|
|
||||||
func NewMemoryStore() *MemoryStore {
|
|
||||||
store := &MemoryStore{
|
|
||||||
messages: make(map[string]packets.ControlPacket),
|
|
||||||
opened: false,
|
|
||||||
}
|
|
||||||
return store
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open initializes a MemoryStore instance.
|
|
||||||
func (store *MemoryStore) Open() {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
store.opened = true
|
|
||||||
DEBUG.Println(STR, "memorystore initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put takes a key and a pointer to a Message and stores the
|
|
||||||
// message.
|
|
||||||
func (store *MemoryStore) Put(key string, message packets.ControlPacket) {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to use memory store, but not open")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
store.messages[key] = message
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get takes a key and looks in the store for a matching Message
|
|
||||||
// returning either the Message pointer or nil.
|
|
||||||
func (store *MemoryStore) Get(key string) packets.ControlPacket {
|
|
||||||
store.RLock()
|
|
||||||
defer store.RUnlock()
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to use memory store, but not open")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
mid := mIDFromKey(key)
|
|
||||||
m := store.messages[key]
|
|
||||||
if m == nil {
|
|
||||||
CRITICAL.Println(STR, "memorystore get: message", mid, "not found")
|
|
||||||
} else {
|
|
||||||
DEBUG.Println(STR, "memorystore get: message", mid, "found")
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
// All returns a slice of strings containing all the keys currently
|
|
||||||
// in the MemoryStore.
|
|
||||||
func (store *MemoryStore) All() []string {
|
|
||||||
store.RLock()
|
|
||||||
defer store.RUnlock()
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to use memory store, but not open")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
keys := []string{}
|
|
||||||
for k := range store.messages {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
// Del takes a key, searches the MemoryStore and if the key is found
|
|
||||||
// deletes the Message pointer associated with it.
|
|
||||||
func (store *MemoryStore) Del(key string) {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to use memory store, but not open")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mid := mIDFromKey(key)
|
|
||||||
m := store.messages[key]
|
|
||||||
if m == nil {
|
|
||||||
WARN.Println(STR, "memorystore del: message", mid, "not found")
|
|
||||||
} else {
|
|
||||||
delete(store.messages, key)
|
|
||||||
DEBUG.Println(STR, "memorystore del: message", mid, "was deleted")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close will disallow modifications to the state of the store.
|
|
||||||
func (store *MemoryStore) Close() {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to close memory store, but not open")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
store.opened = false
|
|
||||||
DEBUG.Println(STR, "memorystore closed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset eliminates all persisted message data in the store.
|
|
||||||
func (store *MemoryStore) Reset() {
|
|
||||||
store.Lock()
|
|
||||||
defer store.Unlock()
|
|
||||||
if !store.opened {
|
|
||||||
ERROR.Println(STR, "Trying to reset memory store, but not open")
|
|
||||||
}
|
|
||||||
store.messages = make(map[string]packets.ControlPacket)
|
|
||||||
WARN.Println(STR, "memorystore wiped")
|
|
||||||
}
|
|
||||||
127
vendor/github.com/eclipse/paho.mqtt.golang/message.go
generated
vendored
127
vendor/github.com/eclipse/paho.mqtt.golang/message.go
generated
vendored
@@ -1,127 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/eclipse/paho.mqtt.golang/packets"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Message defines the externals that a message implementation must support
|
|
||||||
// these are received messages that are passed to the callbacks, not internal
|
|
||||||
// messages
|
|
||||||
type Message interface {
|
|
||||||
Duplicate() bool
|
|
||||||
Qos() byte
|
|
||||||
Retained() bool
|
|
||||||
Topic() string
|
|
||||||
MessageID() uint16
|
|
||||||
Payload() []byte
|
|
||||||
Ack()
|
|
||||||
}
|
|
||||||
|
|
||||||
type message struct {
|
|
||||||
duplicate bool
|
|
||||||
qos byte
|
|
||||||
retained bool
|
|
||||||
topic string
|
|
||||||
messageID uint16
|
|
||||||
payload []byte
|
|
||||||
once sync.Once
|
|
||||||
ack func()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *message) Duplicate() bool {
|
|
||||||
return m.duplicate
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *message) Qos() byte {
|
|
||||||
return m.qos
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *message) Retained() bool {
|
|
||||||
return m.retained
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *message) Topic() string {
|
|
||||||
return m.topic
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *message) MessageID() uint16 {
|
|
||||||
return m.messageID
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *message) Payload() []byte {
|
|
||||||
return m.payload
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *message) Ack() {
|
|
||||||
m.once.Do(m.ack)
|
|
||||||
}
|
|
||||||
|
|
||||||
func messageFromPublish(p *packets.PublishPacket, ack func()) Message {
|
|
||||||
return &message{
|
|
||||||
duplicate: p.Dup,
|
|
||||||
qos: p.Qos,
|
|
||||||
retained: p.Retain,
|
|
||||||
topic: p.TopicName,
|
|
||||||
messageID: p.MessageID,
|
|
||||||
payload: p.Payload,
|
|
||||||
ack: ack,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newConnectMsgFromOptions(options *ClientOptions, broker *url.URL) *packets.ConnectPacket {
|
|
||||||
m := packets.NewControlPacket(packets.Connect).(*packets.ConnectPacket)
|
|
||||||
|
|
||||||
m.CleanSession = options.CleanSession
|
|
||||||
m.WillFlag = options.WillEnabled
|
|
||||||
m.WillRetain = options.WillRetained
|
|
||||||
m.ClientIdentifier = options.ClientID
|
|
||||||
|
|
||||||
if options.WillEnabled {
|
|
||||||
m.WillQos = options.WillQos
|
|
||||||
m.WillTopic = options.WillTopic
|
|
||||||
m.WillMessage = options.WillPayload
|
|
||||||
}
|
|
||||||
|
|
||||||
username := options.Username
|
|
||||||
password := options.Password
|
|
||||||
if broker.User != nil {
|
|
||||||
username = broker.User.Username()
|
|
||||||
if pwd, ok := broker.User.Password(); ok {
|
|
||||||
password = pwd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if options.CredentialsProvider != nil {
|
|
||||||
username, password = options.CredentialsProvider()
|
|
||||||
}
|
|
||||||
|
|
||||||
if username != "" {
|
|
||||||
m.UsernameFlag = true
|
|
||||||
m.Username = username
|
|
||||||
//mustn't have password without user as well
|
|
||||||
if password != "" {
|
|
||||||
m.PasswordFlag = true
|
|
||||||
m.Password = []byte(password)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Keepalive = uint16(options.KeepAlive)
|
|
||||||
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
117
vendor/github.com/eclipse/paho.mqtt.golang/messageids.go
generated
vendored
117
vendor/github.com/eclipse/paho.mqtt.golang/messageids.go
generated
vendored
@@ -1,117 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MId is 16 bit message id as specified by the MQTT spec.
|
|
||||||
// In general, these values should not be depended upon by
|
|
||||||
// the client application.
|
|
||||||
type MId uint16
|
|
||||||
|
|
||||||
type messageIds struct {
|
|
||||||
sync.RWMutex
|
|
||||||
index map[uint16]tokenCompletor
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
midMin uint16 = 1
|
|
||||||
midMax uint16 = 65535
|
|
||||||
)
|
|
||||||
|
|
||||||
func (mids *messageIds) cleanUp() {
|
|
||||||
mids.Lock()
|
|
||||||
for _, token := range mids.index {
|
|
||||||
switch token.(type) {
|
|
||||||
case *PublishToken:
|
|
||||||
token.setError(fmt.Errorf("Connection lost before Publish completed"))
|
|
||||||
case *SubscribeToken:
|
|
||||||
token.setError(fmt.Errorf("Connection lost before Subscribe completed"))
|
|
||||||
case *UnsubscribeToken:
|
|
||||||
token.setError(fmt.Errorf("Connection lost before Unsubscribe completed"))
|
|
||||||
case nil:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
token.flowComplete()
|
|
||||||
}
|
|
||||||
mids.index = make(map[uint16]tokenCompletor)
|
|
||||||
mids.Unlock()
|
|
||||||
DEBUG.Println(MID, "cleaned up")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mids *messageIds) freeID(id uint16) {
|
|
||||||
mids.Lock()
|
|
||||||
delete(mids.index, id)
|
|
||||||
mids.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mids *messageIds) claimID(token tokenCompletor, id uint16) {
|
|
||||||
mids.Lock()
|
|
||||||
defer mids.Unlock()
|
|
||||||
if _, ok := mids.index[id]; !ok {
|
|
||||||
mids.index[id] = token
|
|
||||||
} else {
|
|
||||||
old := mids.index[id]
|
|
||||||
old.flowComplete()
|
|
||||||
mids.index[id] = token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mids *messageIds) getID(t tokenCompletor) uint16 {
|
|
||||||
mids.Lock()
|
|
||||||
defer mids.Unlock()
|
|
||||||
for i := midMin; i < midMax; i++ {
|
|
||||||
if _, ok := mids.index[i]; !ok {
|
|
||||||
mids.index[i] = t
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mids *messageIds) getToken(id uint16) tokenCompletor {
|
|
||||||
mids.RLock()
|
|
||||||
defer mids.RUnlock()
|
|
||||||
if token, ok := mids.index[id]; ok {
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
return &DummyToken{id: id}
|
|
||||||
}
|
|
||||||
|
|
||||||
type DummyToken struct {
|
|
||||||
id uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DummyToken) Wait() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DummyToken) WaitTimeout(t time.Duration) bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DummyToken) flowComplete() {
|
|
||||||
ERROR.Printf("A lookup for token %d returned nil\n", d.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DummyToken) Error() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DummyToken) setError(e error) {}
|
|
||||||
355
vendor/github.com/eclipse/paho.mqtt.golang/net.go
generated
vendored
355
vendor/github.com/eclipse/paho.mqtt.golang/net.go
generated
vendored
@@ -1,355 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"reflect"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/eclipse/paho.mqtt.golang/packets"
|
|
||||||
"golang.org/x/net/proxy"
|
|
||||||
"golang.org/x/net/websocket"
|
|
||||||
)
|
|
||||||
|
|
||||||
func signalError(c chan<- error, err error) {
|
|
||||||
select {
|
|
||||||
case c <- err:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func openConnection(uri *url.URL, tlsc *tls.Config, timeout time.Duration, headers http.Header) (net.Conn, error) {
|
|
||||||
switch uri.Scheme {
|
|
||||||
case "ws":
|
|
||||||
config, _ := websocket.NewConfig(uri.String(), fmt.Sprintf("http://%s", uri.Host))
|
|
||||||
config.Protocol = []string{"mqtt"}
|
|
||||||
config.Header = headers
|
|
||||||
config.Dialer = &net.Dialer{Timeout: timeout}
|
|
||||||
conn, err := websocket.DialConfig(config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
conn.PayloadType = websocket.BinaryFrame
|
|
||||||
return conn, err
|
|
||||||
case "wss":
|
|
||||||
config, _ := websocket.NewConfig(uri.String(), fmt.Sprintf("https://%s", uri.Host))
|
|
||||||
config.Protocol = []string{"mqtt"}
|
|
||||||
config.TlsConfig = tlsc
|
|
||||||
config.Header = headers
|
|
||||||
config.Dialer = &net.Dialer{Timeout: timeout}
|
|
||||||
conn, err := websocket.DialConfig(config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
conn.PayloadType = websocket.BinaryFrame
|
|
||||||
return conn, err
|
|
||||||
case "tcp":
|
|
||||||
allProxy := os.Getenv("all_proxy")
|
|
||||||
if len(allProxy) == 0 {
|
|
||||||
conn, err := net.DialTimeout("tcp", uri.Host, timeout)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
proxyDialer := proxy.FromEnvironment()
|
|
||||||
|
|
||||||
conn, err := proxyDialer.Dial("tcp", uri.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
case "unix":
|
|
||||||
conn, err := net.DialTimeout("unix", uri.Host, timeout)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
case "ssl":
|
|
||||||
fallthrough
|
|
||||||
case "tls":
|
|
||||||
fallthrough
|
|
||||||
case "tcps":
|
|
||||||
allProxy := os.Getenv("all_proxy")
|
|
||||||
if len(allProxy) == 0 {
|
|
||||||
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", uri.Host, tlsc)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
proxyDialer := proxy.FromEnvironment()
|
|
||||||
|
|
||||||
conn, err := proxyDialer.Dial("tcp", uri.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tlsConn := tls.Client(conn, tlsc)
|
|
||||||
|
|
||||||
err = tlsConn.Handshake()
|
|
||||||
if err != nil {
|
|
||||||
conn.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return tlsConn, nil
|
|
||||||
}
|
|
||||||
return nil, errors.New("Unknown protocol")
|
|
||||||
}
|
|
||||||
|
|
||||||
// actually read incoming messages off the wire
|
|
||||||
// send Message object into ibound channel
|
|
||||||
func incoming(c *client) {
|
|
||||||
var err error
|
|
||||||
var cp packets.ControlPacket
|
|
||||||
|
|
||||||
defer c.workers.Done()
|
|
||||||
|
|
||||||
DEBUG.Println(NET, "incoming started")
|
|
||||||
|
|
||||||
for {
|
|
||||||
if cp, err = packets.ReadPacket(c.conn); err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
DEBUG.Println(NET, "Received Message")
|
|
||||||
select {
|
|
||||||
case c.ibound <- cp:
|
|
||||||
// Notify keepalive logic that we recently received a packet
|
|
||||||
if c.options.KeepAlive != 0 {
|
|
||||||
c.lastReceived.Store(time.Now())
|
|
||||||
}
|
|
||||||
case <-c.stop:
|
|
||||||
// This avoids a deadlock should a message arrive while shutting down.
|
|
||||||
// In that case the "reader" of c.ibound might already be gone
|
|
||||||
WARN.Println(NET, "incoming dropped a received message during shutdown")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// We received an error on read.
|
|
||||||
// If disconnect is in progress, swallow error and return
|
|
||||||
select {
|
|
||||||
case <-c.stop:
|
|
||||||
DEBUG.Println(NET, "incoming stopped")
|
|
||||||
return
|
|
||||||
// Not trying to disconnect, send the error to the errors channel
|
|
||||||
default:
|
|
||||||
ERROR.Println(NET, "incoming stopped with error", err)
|
|
||||||
signalError(c.errors, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// receive a Message object on obound, and then
|
|
||||||
// actually send outgoing message to the wire
|
|
||||||
func outgoing(c *client) {
|
|
||||||
defer c.workers.Done()
|
|
||||||
DEBUG.Println(NET, "outgoing started")
|
|
||||||
|
|
||||||
for {
|
|
||||||
DEBUG.Println(NET, "outgoing waiting for an outbound message")
|
|
||||||
select {
|
|
||||||
case <-c.stop:
|
|
||||||
DEBUG.Println(NET, "outgoing stopped")
|
|
||||||
return
|
|
||||||
case pub := <-c.obound:
|
|
||||||
msg := pub.p.(*packets.PublishPacket)
|
|
||||||
|
|
||||||
if c.options.WriteTimeout > 0 {
|
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(c.options.WriteTimeout))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := msg.Write(c.conn); err != nil {
|
|
||||||
ERROR.Println(NET, "outgoing stopped with error", err)
|
|
||||||
pub.t.setError(err)
|
|
||||||
signalError(c.errors, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.options.WriteTimeout > 0 {
|
|
||||||
// If we successfully wrote, we don't want the timeout to happen during an idle period
|
|
||||||
// so we reset it to infinite.
|
|
||||||
c.conn.SetWriteDeadline(time.Time{})
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.Qos == 0 {
|
|
||||||
pub.t.flowComplete()
|
|
||||||
}
|
|
||||||
DEBUG.Println(NET, "obound wrote msg, id:", msg.MessageID)
|
|
||||||
case msg := <-c.oboundP:
|
|
||||||
switch msg.p.(type) {
|
|
||||||
case *packets.SubscribePacket:
|
|
||||||
msg.p.(*packets.SubscribePacket).MessageID = c.getID(msg.t)
|
|
||||||
case *packets.UnsubscribePacket:
|
|
||||||
msg.p.(*packets.UnsubscribePacket).MessageID = c.getID(msg.t)
|
|
||||||
}
|
|
||||||
DEBUG.Println(NET, "obound priority msg to write, type", reflect.TypeOf(msg.p))
|
|
||||||
if err := msg.p.Write(c.conn); err != nil {
|
|
||||||
ERROR.Println(NET, "outgoing stopped with error", err)
|
|
||||||
if msg.t != nil {
|
|
||||||
msg.t.setError(err)
|
|
||||||
}
|
|
||||||
signalError(c.errors, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch msg.p.(type) {
|
|
||||||
case *packets.DisconnectPacket:
|
|
||||||
msg.t.(*DisconnectToken).flowComplete()
|
|
||||||
DEBUG.Println(NET, "outbound wrote disconnect, stopping")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Reset ping timer after sending control packet.
|
|
||||||
if c.options.KeepAlive != 0 {
|
|
||||||
c.lastSent.Store(time.Now())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// receive Message objects on ibound
|
|
||||||
// store messages if necessary
|
|
||||||
// send replies on obound
|
|
||||||
// delete messages from store if necessary
|
|
||||||
func alllogic(c *client) {
|
|
||||||
defer c.workers.Done()
|
|
||||||
DEBUG.Println(NET, "logic started")
|
|
||||||
|
|
||||||
for {
|
|
||||||
DEBUG.Println(NET, "logic waiting for msg on ibound")
|
|
||||||
|
|
||||||
select {
|
|
||||||
case msg := <-c.ibound:
|
|
||||||
DEBUG.Println(NET, "logic got msg on ibound")
|
|
||||||
persistInbound(c.persist, msg)
|
|
||||||
switch m := msg.(type) {
|
|
||||||
case *packets.PingrespPacket:
|
|
||||||
DEBUG.Println(NET, "received pingresp")
|
|
||||||
atomic.StoreInt32(&c.pingOutstanding, 0)
|
|
||||||
case *packets.SubackPacket:
|
|
||||||
DEBUG.Println(NET, "received suback, id:", m.MessageID)
|
|
||||||
token := c.getToken(m.MessageID)
|
|
||||||
switch t := token.(type) {
|
|
||||||
case *SubscribeToken:
|
|
||||||
DEBUG.Println(NET, "granted qoss", m.ReturnCodes)
|
|
||||||
for i, qos := range m.ReturnCodes {
|
|
||||||
t.subResult[t.subs[i]] = qos
|
|
||||||
}
|
|
||||||
}
|
|
||||||
token.flowComplete()
|
|
||||||
c.freeID(m.MessageID)
|
|
||||||
case *packets.UnsubackPacket:
|
|
||||||
DEBUG.Println(NET, "received unsuback, id:", m.MessageID)
|
|
||||||
c.getToken(m.MessageID).flowComplete()
|
|
||||||
c.freeID(m.MessageID)
|
|
||||||
case *packets.PublishPacket:
|
|
||||||
DEBUG.Println(NET, "received publish, msgId:", m.MessageID)
|
|
||||||
DEBUG.Println(NET, "putting msg on onPubChan")
|
|
||||||
switch m.Qos {
|
|
||||||
case 2:
|
|
||||||
c.incomingPubChan <- m
|
|
||||||
DEBUG.Println(NET, "done putting msg on incomingPubChan")
|
|
||||||
case 1:
|
|
||||||
c.incomingPubChan <- m
|
|
||||||
DEBUG.Println(NET, "done putting msg on incomingPubChan")
|
|
||||||
case 0:
|
|
||||||
select {
|
|
||||||
case c.incomingPubChan <- m:
|
|
||||||
case <-c.stop:
|
|
||||||
}
|
|
||||||
DEBUG.Println(NET, "done putting msg on incomingPubChan")
|
|
||||||
}
|
|
||||||
case *packets.PubackPacket:
|
|
||||||
DEBUG.Println(NET, "received puback, id:", m.MessageID)
|
|
||||||
// c.receipts.get(msg.MsgId()) <- Receipt{}
|
|
||||||
// c.receipts.end(msg.MsgId())
|
|
||||||
c.getToken(m.MessageID).flowComplete()
|
|
||||||
c.freeID(m.MessageID)
|
|
||||||
case *packets.PubrecPacket:
|
|
||||||
DEBUG.Println(NET, "received pubrec, id:", m.MessageID)
|
|
||||||
prel := packets.NewControlPacket(packets.Pubrel).(*packets.PubrelPacket)
|
|
||||||
prel.MessageID = m.MessageID
|
|
||||||
select {
|
|
||||||
case c.oboundP <- &PacketAndToken{p: prel, t: nil}:
|
|
||||||
case <-c.stop:
|
|
||||||
}
|
|
||||||
case *packets.PubrelPacket:
|
|
||||||
DEBUG.Println(NET, "received pubrel, id:", m.MessageID)
|
|
||||||
pc := packets.NewControlPacket(packets.Pubcomp).(*packets.PubcompPacket)
|
|
||||||
pc.MessageID = m.MessageID
|
|
||||||
persistOutbound(c.persist, pc)
|
|
||||||
select {
|
|
||||||
case c.oboundP <- &PacketAndToken{p: pc, t: nil}:
|
|
||||||
case <-c.stop:
|
|
||||||
}
|
|
||||||
case *packets.PubcompPacket:
|
|
||||||
DEBUG.Println(NET, "received pubcomp, id:", m.MessageID)
|
|
||||||
c.getToken(m.MessageID).flowComplete()
|
|
||||||
c.freeID(m.MessageID)
|
|
||||||
}
|
|
||||||
case <-c.stop:
|
|
||||||
WARN.Println(NET, "logic stopped")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *client) ackFunc(packet *packets.PublishPacket) func() {
|
|
||||||
return func() {
|
|
||||||
switch packet.Qos {
|
|
||||||
case 2:
|
|
||||||
pr := packets.NewControlPacket(packets.Pubrec).(*packets.PubrecPacket)
|
|
||||||
pr.MessageID = packet.MessageID
|
|
||||||
DEBUG.Println(NET, "putting pubrec msg on obound")
|
|
||||||
select {
|
|
||||||
case c.oboundP <- &PacketAndToken{p: pr, t: nil}:
|
|
||||||
case <-c.stop:
|
|
||||||
}
|
|
||||||
DEBUG.Println(NET, "done putting pubrec msg on obound")
|
|
||||||
case 1:
|
|
||||||
pa := packets.NewControlPacket(packets.Puback).(*packets.PubackPacket)
|
|
||||||
pa.MessageID = packet.MessageID
|
|
||||||
DEBUG.Println(NET, "putting puback msg on obound")
|
|
||||||
persistOutbound(c.persist, pa)
|
|
||||||
select {
|
|
||||||
case c.oboundP <- &PacketAndToken{p: pa, t: nil}:
|
|
||||||
case <-c.stop:
|
|
||||||
}
|
|
||||||
DEBUG.Println(NET, "done putting puback msg on obound")
|
|
||||||
case 0:
|
|
||||||
// do nothing, since there is no need to send an ack packet back
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func errorWatch(c *client) {
|
|
||||||
defer c.workers.Done()
|
|
||||||
select {
|
|
||||||
case <-c.stop:
|
|
||||||
WARN.Println(NET, "errorWatch stopped")
|
|
||||||
return
|
|
||||||
case err := <-c.errors:
|
|
||||||
ERROR.Println(NET, "error triggered, stopping")
|
|
||||||
go c.internalConnLost(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
108
vendor/github.com/eclipse/paho.mqtt.golang/notice.html
generated
vendored
108
vendor/github.com/eclipse/paho.mqtt.golang/notice.html
generated
vendored
@@ -1,108 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
|
||||||
<title>Eclipse Foundation Software User Agreement</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body lang="EN-US">
|
|
||||||
<h2>Eclipse Foundation Software User Agreement</h2>
|
|
||||||
<p>February 1, 2011</p>
|
|
||||||
|
|
||||||
<h3>Usage Of Content</h3>
|
|
||||||
|
|
||||||
<p>THE ECLIPSE FOUNDATION MAKES AVAILABLE SOFTWARE, DOCUMENTATION, INFORMATION AND/OR OTHER MATERIALS FOR OPEN SOURCE PROJECTS
|
|
||||||
(COLLECTIVELY "CONTENT"). USE OF THE CONTENT IS GOVERNED BY THE TERMS AND CONDITIONS OF THIS AGREEMENT AND/OR THE TERMS AND
|
|
||||||
CONDITIONS OF LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW. BY USING THE CONTENT, YOU AGREE THAT YOUR USE
|
|
||||||
OF THE CONTENT IS GOVERNED BY THIS AGREEMENT AND/OR THE TERMS AND CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR
|
|
||||||
NOTICES INDICATED OR REFERENCED BELOW. IF YOU DO NOT AGREE TO THE TERMS AND CONDITIONS OF THIS AGREEMENT AND THE TERMS AND
|
|
||||||
CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW, THEN YOU MAY NOT USE THE CONTENT.</p>
|
|
||||||
|
|
||||||
<h3>Applicable Licenses</h3>
|
|
||||||
|
|
||||||
<p>Unless otherwise indicated, all Content made available by the Eclipse Foundation is provided to you under the terms and conditions of the Eclipse Public License Version 1.0
|
|
||||||
("EPL"). A copy of the EPL is provided with this Content and is also available at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
|
|
||||||
For purposes of the EPL, "Program" will mean the Content.</p>
|
|
||||||
|
|
||||||
<p>Content includes, but is not limited to, source code, object code, documentation and other files maintained in the Eclipse Foundation source code
|
|
||||||
repository ("Repository") in software modules ("Modules") and made available as downloadable archives ("Downloads").</p>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li>Content may be structured and packaged into modules to facilitate delivering, extending, and upgrading the Content. Typical modules may include plug-ins ("Plug-ins"), plug-in fragments ("Fragments"), and features ("Features").</li>
|
|
||||||
<li>Each Plug-in or Fragment may be packaged as a sub-directory or JAR (Java™ ARchive) in a directory named "plugins".</li>
|
|
||||||
<li>A Feature is a bundle of one or more Plug-ins and/or Fragments and associated material. Each Feature may be packaged as a sub-directory in a directory named "features". Within a Feature, files named "feature.xml" may contain a list of the names and version numbers of the Plug-ins
|
|
||||||
and/or Fragments associated with that Feature.</li>
|
|
||||||
<li>Features may also include other Features ("Included Features"). Within a Feature, files named "feature.xml" may contain a list of the names and version numbers of Included Features.</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<p>The terms and conditions governing Plug-ins and Fragments should be contained in files named "about.html" ("Abouts"). The terms and conditions governing Features and
|
|
||||||
Included Features should be contained in files named "license.html" ("Feature Licenses"). Abouts and Feature Licenses may be located in any directory of a Download or Module
|
|
||||||
including, but not limited to the following locations:</p>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li>The top-level (root) directory</li>
|
|
||||||
<li>Plug-in and Fragment directories</li>
|
|
||||||
<li>Inside Plug-ins and Fragments packaged as JARs</li>
|
|
||||||
<li>Sub-directories of the directory named "src" of certain Plug-ins</li>
|
|
||||||
<li>Feature directories</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<p>Note: if a Feature made available by the Eclipse Foundation is installed using the Provisioning Technology (as defined below), you must agree to a license ("Feature Update License") during the
|
|
||||||
installation process. If the Feature contains Included Features, the Feature Update License should either provide you with the terms and conditions governing the Included Features or
|
|
||||||
inform you where you can locate them. Feature Update Licenses may be found in the "license" property of files named "feature.properties" found within a Feature.
|
|
||||||
Such Abouts, Feature Licenses, and Feature Update Licenses contain the terms and conditions (or references to such terms and conditions) that govern your use of the associated Content in
|
|
||||||
that directory.</p>
|
|
||||||
|
|
||||||
<p>THE ABOUTS, FEATURE LICENSES, AND FEATURE UPDATE LICENSES MAY REFER TO THE EPL OR OTHER LICENSE AGREEMENTS, NOTICES OR TERMS AND CONDITIONS. SOME OF THESE
|
|
||||||
OTHER LICENSE AGREEMENTS MAY INCLUDE (BUT ARE NOT LIMITED TO):</p>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li>Eclipse Distribution License Version 1.0 (available at <a href="http://www.eclipse.org/licenses/edl-v10.html">http://www.eclipse.org/licenses/edl-v1.0.html</a>)</li>
|
|
||||||
<li>Common Public License Version 1.0 (available at <a href="http://www.eclipse.org/legal/cpl-v10.html">http://www.eclipse.org/legal/cpl-v10.html</a>)</li>
|
|
||||||
<li>Apache Software License 1.1 (available at <a href="http://www.apache.org/licenses/LICENSE">http://www.apache.org/licenses/LICENSE</a>)</li>
|
|
||||||
<li>Apache Software License 2.0 (available at <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>)</li>
|
|
||||||
<li>Metro Link Public License 1.00 (available at <a href="http://www.opengroup.org/openmotif/supporters/metrolink/license.html">http://www.opengroup.org/openmotif/supporters/metrolink/license.html</a>)</li>
|
|
||||||
<li>Mozilla Public License Version 1.1 (available at <a href="http://www.mozilla.org/MPL/MPL-1.1.html">http://www.mozilla.org/MPL/MPL-1.1.html</a>)</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<p>IT IS YOUR OBLIGATION TO READ AND ACCEPT ALL SUCH TERMS AND CONDITIONS PRIOR TO USE OF THE CONTENT. If no About, Feature License, or Feature Update License is provided, please
|
|
||||||
contact the Eclipse Foundation to determine what terms and conditions govern that particular Content.</p>
|
|
||||||
|
|
||||||
|
|
||||||
<h3>Use of Provisioning Technology</h3>
|
|
||||||
|
|
||||||
<p>The Eclipse Foundation makes available provisioning software, examples of which include, but are not limited to, p2 and the Eclipse
|
|
||||||
Update Manager ("Provisioning Technology") for the purpose of allowing users to install software, documentation, information and/or
|
|
||||||
other materials (collectively "Installable Software"). This capability is provided with the intent of allowing such users to
|
|
||||||
install, extend and update Eclipse-based products. Information about packaging Installable Software is available at <a
|
|
||||||
href="http://eclipse.org/equinox/p2/repository_packaging.html">http://eclipse.org/equinox/p2/repository_packaging.html</a>
|
|
||||||
("Specification").</p>
|
|
||||||
|
|
||||||
<p>You may use Provisioning Technology to allow other parties to install Installable Software. You shall be responsible for enabling the
|
|
||||||
applicable license agreements relating to the Installable Software to be presented to, and accepted by, the users of the Provisioning Technology
|
|
||||||
in accordance with the Specification. By using Provisioning Technology in such a manner and making it available in accordance with the
|
|
||||||
Specification, you further acknowledge your agreement to, and the acquisition of all necessary rights to permit the following:</p>
|
|
||||||
|
|
||||||
<ol>
|
|
||||||
<li>A series of actions may occur ("Provisioning Process") in which a user may execute the Provisioning Technology
|
|
||||||
on a machine ("Target Machine") with the intent of installing, extending or updating the functionality of an Eclipse-based
|
|
||||||
product.</li>
|
|
||||||
<li>During the Provisioning Process, the Provisioning Technology may cause third party Installable Software or a portion thereof to be
|
|
||||||
accessed and copied to the Target Machine.</li>
|
|
||||||
<li>Pursuant to the Specification, you will provide to the user the terms and conditions that govern the use of the Installable
|
|
||||||
Software ("Installable Software Agreement") and such Installable Software Agreement shall be accessed from the Target
|
|
||||||
Machine in accordance with the Specification. Such Installable Software Agreement must inform the user of the terms and conditions that govern
|
|
||||||
the Installable Software and must solicit acceptance by the end user in the manner prescribed in such Installable Software Agreement. Upon such
|
|
||||||
indication of agreement by the user, the provisioning Technology will complete installation of the Installable Software.</li>
|
|
||||||
</ol>
|
|
||||||
|
|
||||||
<h3>Cryptography</h3>
|
|
||||||
|
|
||||||
<p>Content may contain encryption software. The country in which you are currently may have restrictions on the import, possession, and use, and/or re-export to
|
|
||||||
another country, of encryption software. BEFORE using any encryption software, please check the country's laws, regulations and policies concerning the import,
|
|
||||||
possession, or use, and re-export of encryption software, to see if this is permitted.</p>
|
|
||||||
|
|
||||||
<p><small>Java and all Java-based trademarks are trademarks of Oracle Corporation in the United States, other countries, or both.</small></p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
21
vendor/github.com/eclipse/paho.mqtt.golang/oops.go
generated
vendored
21
vendor/github.com/eclipse/paho.mqtt.golang/oops.go
generated
vendored
@@ -1,21 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
func chkerr(e error) {
|
|
||||||
if e != nil {
|
|
||||||
panic(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
340
vendor/github.com/eclipse/paho.mqtt.golang/options.go
generated
vendored
340
vendor/github.com/eclipse/paho.mqtt.golang/options.go
generated
vendored
@@ -1,340 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Portions copyright © 2018 TIBCO Software Inc.
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CredentialsProvider allows the username and password to be updated
|
|
||||||
// before reconnecting. It should return the current username and password.
|
|
||||||
type CredentialsProvider func() (username string, password string)
|
|
||||||
|
|
||||||
// MessageHandler is a callback type which can be set to be
|
|
||||||
// executed upon the arrival of messages published to topics
|
|
||||||
// to which the client is subscribed.
|
|
||||||
type MessageHandler func(Client, Message)
|
|
||||||
|
|
||||||
// ConnectionLostHandler is a callback type which can be set to be
|
|
||||||
// executed upon an unintended disconnection from the MQTT broker.
|
|
||||||
// Disconnects caused by calling Disconnect or ForceDisconnect will
|
|
||||||
// not cause an OnConnectionLost callback to execute.
|
|
||||||
type ConnectionLostHandler func(Client, error)
|
|
||||||
|
|
||||||
// OnConnectHandler is a callback that is called when the client
|
|
||||||
// state changes from unconnected/disconnected to connected. Both
|
|
||||||
// at initial connection and on reconnection
|
|
||||||
type OnConnectHandler func(Client)
|
|
||||||
|
|
||||||
// ClientOptions contains configurable options for an Client.
|
|
||||||
type ClientOptions struct {
|
|
||||||
Servers []*url.URL
|
|
||||||
ClientID string
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
CredentialsProvider CredentialsProvider
|
|
||||||
CleanSession bool
|
|
||||||
Order bool
|
|
||||||
WillEnabled bool
|
|
||||||
WillTopic string
|
|
||||||
WillPayload []byte
|
|
||||||
WillQos byte
|
|
||||||
WillRetained bool
|
|
||||||
ProtocolVersion uint
|
|
||||||
protocolVersionExplicit bool
|
|
||||||
TLSConfig *tls.Config
|
|
||||||
KeepAlive int64
|
|
||||||
PingTimeout time.Duration
|
|
||||||
ConnectTimeout time.Duration
|
|
||||||
MaxReconnectInterval time.Duration
|
|
||||||
AutoReconnect bool
|
|
||||||
Store Store
|
|
||||||
DefaultPublishHandler MessageHandler
|
|
||||||
OnConnect OnConnectHandler
|
|
||||||
OnConnectionLost ConnectionLostHandler
|
|
||||||
WriteTimeout time.Duration
|
|
||||||
MessageChannelDepth uint
|
|
||||||
ResumeSubs bool
|
|
||||||
HTTPHeaders http.Header
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewClientOptions will create a new ClientClientOptions type with some
|
|
||||||
// default values.
|
|
||||||
// Port: 1883
|
|
||||||
// CleanSession: True
|
|
||||||
// Order: True
|
|
||||||
// KeepAlive: 30 (seconds)
|
|
||||||
// ConnectTimeout: 30 (seconds)
|
|
||||||
// MaxReconnectInterval 10 (minutes)
|
|
||||||
// AutoReconnect: True
|
|
||||||
func NewClientOptions() *ClientOptions {
|
|
||||||
o := &ClientOptions{
|
|
||||||
Servers: nil,
|
|
||||||
ClientID: "",
|
|
||||||
Username: "",
|
|
||||||
Password: "",
|
|
||||||
CleanSession: true,
|
|
||||||
Order: true,
|
|
||||||
WillEnabled: false,
|
|
||||||
WillTopic: "",
|
|
||||||
WillPayload: nil,
|
|
||||||
WillQos: 0,
|
|
||||||
WillRetained: false,
|
|
||||||
ProtocolVersion: 0,
|
|
||||||
protocolVersionExplicit: false,
|
|
||||||
KeepAlive: 30,
|
|
||||||
PingTimeout: 10 * time.Second,
|
|
||||||
ConnectTimeout: 30 * time.Second,
|
|
||||||
MaxReconnectInterval: 10 * time.Minute,
|
|
||||||
AutoReconnect: true,
|
|
||||||
Store: nil,
|
|
||||||
OnConnect: nil,
|
|
||||||
OnConnectionLost: DefaultConnectionLostHandler,
|
|
||||||
WriteTimeout: 0, // 0 represents timeout disabled
|
|
||||||
MessageChannelDepth: 100,
|
|
||||||
ResumeSubs: false,
|
|
||||||
HTTPHeaders: make(map[string][]string),
|
|
||||||
}
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddBroker adds a broker URI to the list of brokers to be used. The format should be
|
|
||||||
// scheme://host:port
|
|
||||||
// Where "scheme" is one of "tcp", "ssl", or "ws", "host" is the ip-address (or hostname)
|
|
||||||
// and "port" is the port on which the broker is accepting connections.
|
|
||||||
//
|
|
||||||
// Default values for hostname is "127.0.0.1", for schema is "tcp://".
|
|
||||||
//
|
|
||||||
// An example broker URI would look like: tcp://foobar.com:1883
|
|
||||||
func (o *ClientOptions) AddBroker(server string) *ClientOptions {
|
|
||||||
if len(server) > 0 && server[0] == ':' {
|
|
||||||
server = "127.0.0.1" + server
|
|
||||||
}
|
|
||||||
if !strings.Contains(server, "://") {
|
|
||||||
server = "tcp://" + server
|
|
||||||
}
|
|
||||||
brokerURI, err := url.Parse(server)
|
|
||||||
if err != nil {
|
|
||||||
ERROR.Println(CLI, "Failed to parse %q broker address: %s", server, err)
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
o.Servers = append(o.Servers, brokerURI)
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetResumeSubs will enable resuming of stored (un)subscribe messages when connecting
|
|
||||||
// but not reconnecting if CleanSession is false. Otherwise these messages are discarded.
|
|
||||||
func (o *ClientOptions) SetResumeSubs(resume bool) *ClientOptions {
|
|
||||||
o.ResumeSubs = resume
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetClientID will set the client id to be used by this client when
|
|
||||||
// connecting to the MQTT broker. According to the MQTT v3.1 specification,
|
|
||||||
// a client id mus be no longer than 23 characters.
|
|
||||||
func (o *ClientOptions) SetClientID(id string) *ClientOptions {
|
|
||||||
o.ClientID = id
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetUsername will set the username to be used by this client when connecting
|
|
||||||
// to the MQTT broker. Note: without the use of SSL/TLS, this information will
|
|
||||||
// be sent in plaintext accross the wire.
|
|
||||||
func (o *ClientOptions) SetUsername(u string) *ClientOptions {
|
|
||||||
o.Username = u
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetPassword will set the password to be used by this client when connecting
|
|
||||||
// to the MQTT broker. Note: without the use of SSL/TLS, this information will
|
|
||||||
// be sent in plaintext accross the wire.
|
|
||||||
func (o *ClientOptions) SetPassword(p string) *ClientOptions {
|
|
||||||
o.Password = p
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCredentialsProvider will set a method to be called by this client when
|
|
||||||
// connecting to the MQTT broker that provide the current username and password.
|
|
||||||
// Note: without the use of SSL/TLS, this information will be sent
|
|
||||||
// in plaintext accross the wire.
|
|
||||||
func (o *ClientOptions) SetCredentialsProvider(p CredentialsProvider) *ClientOptions {
|
|
||||||
o.CredentialsProvider = p
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCleanSession will set the "clean session" flag in the connect message
|
|
||||||
// when this client connects to an MQTT broker. By setting this flag, you are
|
|
||||||
// indicating that no messages saved by the broker for this client should be
|
|
||||||
// delivered. Any messages that were going to be sent by this client before
|
|
||||||
// diconnecting previously but didn't will not be sent upon connecting to the
|
|
||||||
// broker.
|
|
||||||
func (o *ClientOptions) SetCleanSession(clean bool) *ClientOptions {
|
|
||||||
o.CleanSession = clean
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetOrderMatters will set the message routing to guarantee order within
|
|
||||||
// each QoS level. By default, this value is true. If set to false,
|
|
||||||
// this flag indicates that messages can be delivered asynchronously
|
|
||||||
// from the client to the application and possibly arrive out of order.
|
|
||||||
func (o *ClientOptions) SetOrderMatters(order bool) *ClientOptions {
|
|
||||||
o.Order = order
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetTLSConfig will set an SSL/TLS configuration to be used when connecting
|
|
||||||
// to an MQTT broker. Please read the official Go documentation for more
|
|
||||||
// information.
|
|
||||||
func (o *ClientOptions) SetTLSConfig(t *tls.Config) *ClientOptions {
|
|
||||||
o.TLSConfig = t
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetStore will set the implementation of the Store interface
|
|
||||||
// used to provide message persistence in cases where QoS levels
|
|
||||||
// QoS_ONE or QoS_TWO are used. If no store is provided, then the
|
|
||||||
// client will use MemoryStore by default.
|
|
||||||
func (o *ClientOptions) SetStore(s Store) *ClientOptions {
|
|
||||||
o.Store = s
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetKeepAlive will set the amount of time (in seconds) that the client
|
|
||||||
// should wait before sending a PING request to the broker. This will
|
|
||||||
// allow the client to know that a connection has not been lost with the
|
|
||||||
// server.
|
|
||||||
func (o *ClientOptions) SetKeepAlive(k time.Duration) *ClientOptions {
|
|
||||||
o.KeepAlive = int64(k / time.Second)
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetPingTimeout will set the amount of time (in seconds) that the client
|
|
||||||
// will wait after sending a PING request to the broker, before deciding
|
|
||||||
// that the connection has been lost. Default is 10 seconds.
|
|
||||||
func (o *ClientOptions) SetPingTimeout(k time.Duration) *ClientOptions {
|
|
||||||
o.PingTimeout = k
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetProtocolVersion sets the MQTT version to be used to connect to the
|
|
||||||
// broker. Legitimate values are currently 3 - MQTT 3.1 or 4 - MQTT 3.1.1
|
|
||||||
func (o *ClientOptions) SetProtocolVersion(pv uint) *ClientOptions {
|
|
||||||
if (pv >= 3 && pv <= 4) || (pv > 0x80) {
|
|
||||||
o.ProtocolVersion = pv
|
|
||||||
o.protocolVersionExplicit = true
|
|
||||||
}
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnsetWill will cause any set will message to be disregarded.
|
|
||||||
func (o *ClientOptions) UnsetWill() *ClientOptions {
|
|
||||||
o.WillEnabled = false
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetWill accepts a string will message to be set. When the client connects,
|
|
||||||
// it will give this will message to the broker, which will then publish the
|
|
||||||
// provided payload (the will) to any clients that are subscribed to the provided
|
|
||||||
// topic.
|
|
||||||
func (o *ClientOptions) SetWill(topic string, payload string, qos byte, retained bool) *ClientOptions {
|
|
||||||
o.SetBinaryWill(topic, []byte(payload), qos, retained)
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetBinaryWill accepts a []byte will message to be set. When the client connects,
|
|
||||||
// it will give this will message to the broker, which will then publish the
|
|
||||||
// provided payload (the will) to any clients that are subscribed to the provided
|
|
||||||
// topic.
|
|
||||||
func (o *ClientOptions) SetBinaryWill(topic string, payload []byte, qos byte, retained bool) *ClientOptions {
|
|
||||||
o.WillEnabled = true
|
|
||||||
o.WillTopic = topic
|
|
||||||
o.WillPayload = payload
|
|
||||||
o.WillQos = qos
|
|
||||||
o.WillRetained = retained
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetDefaultPublishHandler sets the MessageHandler that will be called when a message
|
|
||||||
// is received that does not match any known subscriptions.
|
|
||||||
func (o *ClientOptions) SetDefaultPublishHandler(defaultHandler MessageHandler) *ClientOptions {
|
|
||||||
o.DefaultPublishHandler = defaultHandler
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetOnConnectHandler sets the function to be called when the client is connected. Both
|
|
||||||
// at initial connection time and upon automatic reconnect.
|
|
||||||
func (o *ClientOptions) SetOnConnectHandler(onConn OnConnectHandler) *ClientOptions {
|
|
||||||
o.OnConnect = onConn
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetConnectionLostHandler will set the OnConnectionLost callback to be executed
|
|
||||||
// in the case where the client unexpectedly loses connection with the MQTT broker.
|
|
||||||
func (o *ClientOptions) SetConnectionLostHandler(onLost ConnectionLostHandler) *ClientOptions {
|
|
||||||
o.OnConnectionLost = onLost
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetWriteTimeout puts a limit on how long a mqtt publish should block until it unblocks with a
|
|
||||||
// timeout error. A duration of 0 never times out. Default 30 seconds
|
|
||||||
func (o *ClientOptions) SetWriteTimeout(t time.Duration) *ClientOptions {
|
|
||||||
o.WriteTimeout = t
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetConnectTimeout limits how long the client will wait when trying to open a connection
|
|
||||||
// to an MQTT server before timeing out and erroring the attempt. A duration of 0 never times out.
|
|
||||||
// Default 30 seconds. Currently only operational on TCP/TLS connections.
|
|
||||||
func (o *ClientOptions) SetConnectTimeout(t time.Duration) *ClientOptions {
|
|
||||||
o.ConnectTimeout = t
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetMaxReconnectInterval sets the maximum time that will be waited between reconnection attempts
|
|
||||||
// when connection is lost
|
|
||||||
func (o *ClientOptions) SetMaxReconnectInterval(t time.Duration) *ClientOptions {
|
|
||||||
o.MaxReconnectInterval = t
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetAutoReconnect sets whether the automatic reconnection logic should be used
|
|
||||||
// when the connection is lost, even if disabled the ConnectionLostHandler is still
|
|
||||||
// called
|
|
||||||
func (o *ClientOptions) SetAutoReconnect(a bool) *ClientOptions {
|
|
||||||
o.AutoReconnect = a
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetMessageChannelDepth sets the size of the internal queue that holds messages while the
|
|
||||||
// client is temporairily offline, allowing the application to publish when the client is
|
|
||||||
// reconnecting. This setting is only valid if AutoReconnect is set to true, it is otherwise
|
|
||||||
// ignored.
|
|
||||||
func (o *ClientOptions) SetMessageChannelDepth(s uint) *ClientOptions {
|
|
||||||
o.MessageChannelDepth = s
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetHTTPHeaders sets the additional HTTP headers that will be sent in the WebSocket
|
|
||||||
// opening handshake.
|
|
||||||
func (o *ClientOptions) SetHTTPHeaders(h http.Header) *ClientOptions {
|
|
||||||
o.HTTPHeaders = h
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
149
vendor/github.com/eclipse/paho.mqtt.golang/options_reader.go
generated
vendored
149
vendor/github.com/eclipse/paho.mqtt.golang/options_reader.go
generated
vendored
@@ -1,149 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2013 IBM Corp.
|
|
||||||
*
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Seth Hoenig
|
|
||||||
* Allan Stockdill-Mander
|
|
||||||
* Mike Robertson
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mqtt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ClientOptionsReader provides an interface for reading ClientOptions after the client has been initialized.
|
|
||||||
type ClientOptionsReader struct {
|
|
||||||
options *ClientOptions
|
|
||||||
}
|
|
||||||
|
|
||||||
//Servers returns a slice of the servers defined in the clientoptions
|
|
||||||
func (r *ClientOptionsReader) Servers() []*url.URL {
|
|
||||||
s := make([]*url.URL, len(r.options.Servers))
|
|
||||||
|
|
||||||
for i, u := range r.options.Servers {
|
|
||||||
nu := *u
|
|
||||||
s[i] = &nu
|
|
||||||
}
|
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
//ResumeSubs returns true if resuming stored (un)sub is enabled
|
|
||||||
func (r *ClientOptionsReader) ResumeSubs() bool {
|
|
||||||
s := r.options.ResumeSubs
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
//ClientID returns the set client id
|
|
||||||
func (r *ClientOptionsReader) ClientID() string {
|
|
||||||
s := r.options.ClientID
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
//Username returns the set username
|
|
||||||
func (r *ClientOptionsReader) Username() string {
|
|
||||||
s := r.options.Username
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
//Password returns the set password
|
|
||||||
func (r *ClientOptionsReader) Password() string {
|
|
||||||
s := r.options.Password
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
//CleanSession returns whether Cleansession is set
|
|
||||||
func (r *ClientOptionsReader) CleanSession() bool {
|
|
||||||
s := r.options.CleanSession
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) Order() bool {
|
|
||||||
s := r.options.Order
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) WillEnabled() bool {
|
|
||||||
s := r.options.WillEnabled
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) WillTopic() string {
|
|
||||||
s := r.options.WillTopic
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) WillPayload() []byte {
|
|
||||||
s := r.options.WillPayload
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) WillQos() byte {
|
|
||||||
s := r.options.WillQos
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) WillRetained() bool {
|
|
||||||
s := r.options.WillRetained
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) ProtocolVersion() uint {
|
|
||||||
s := r.options.ProtocolVersion
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) TLSConfig() *tls.Config {
|
|
||||||
s := r.options.TLSConfig
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) KeepAlive() time.Duration {
|
|
||||||
s := time.Duration(r.options.KeepAlive * int64(time.Second))
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) PingTimeout() time.Duration {
|
|
||||||
s := r.options.PingTimeout
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) ConnectTimeout() time.Duration {
|
|
||||||
s := r.options.ConnectTimeout
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) MaxReconnectInterval() time.Duration {
|
|
||||||
s := r.options.MaxReconnectInterval
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) AutoReconnect() bool {
|
|
||||||
s := r.options.AutoReconnect
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) WriteTimeout() time.Duration {
|
|
||||||
s := r.options.WriteTimeout
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) MessageChannelDepth() uint {
|
|
||||||
s := r.options.MessageChannelDepth
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClientOptionsReader) HTTPHeaders() http.Header {
|
|
||||||
h := r.options.HTTPHeaders
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
55
vendor/github.com/eclipse/paho.mqtt.golang/packets/connack.go
generated
vendored
55
vendor/github.com/eclipse/paho.mqtt.golang/packets/connack.go
generated
vendored
@@ -1,55 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//ConnackPacket is an internal representation of the fields of the
|
|
||||||
//Connack MQTT packet
|
|
||||||
type ConnackPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
SessionPresent bool
|
|
||||||
ReturnCode byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ca *ConnackPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", ca.FixedHeader)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("sessionpresent: %t returncode: %d", ca.SessionPresent, ca.ReturnCode)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ca *ConnackPacket) Write(w io.Writer) error {
|
|
||||||
var body bytes.Buffer
|
|
||||||
var err error
|
|
||||||
|
|
||||||
body.WriteByte(boolToByte(ca.SessionPresent))
|
|
||||||
body.WriteByte(ca.ReturnCode)
|
|
||||||
ca.FixedHeader.RemainingLength = 2
|
|
||||||
packet := ca.FixedHeader.pack()
|
|
||||||
packet.Write(body.Bytes())
|
|
||||||
_, err = packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (ca *ConnackPacket) Unpack(b io.Reader) error {
|
|
||||||
flags, err := decodeByte(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ca.SessionPresent = 1&flags > 0
|
|
||||||
ca.ReturnCode, err = decodeByte(b)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (ca *ConnackPacket) Details() Details {
|
|
||||||
return Details{Qos: 0, MessageID: 0}
|
|
||||||
}
|
|
||||||
154
vendor/github.com/eclipse/paho.mqtt.golang/packets/connect.go
generated
vendored
154
vendor/github.com/eclipse/paho.mqtt.golang/packets/connect.go
generated
vendored
@@ -1,154 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//ConnectPacket is an internal representation of the fields of the
|
|
||||||
//Connect MQTT packet
|
|
||||||
type ConnectPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
ProtocolName string
|
|
||||||
ProtocolVersion byte
|
|
||||||
CleanSession bool
|
|
||||||
WillFlag bool
|
|
||||||
WillQos byte
|
|
||||||
WillRetain bool
|
|
||||||
UsernameFlag bool
|
|
||||||
PasswordFlag bool
|
|
||||||
ReservedBit byte
|
|
||||||
Keepalive uint16
|
|
||||||
|
|
||||||
ClientIdentifier string
|
|
||||||
WillTopic string
|
|
||||||
WillMessage []byte
|
|
||||||
Username string
|
|
||||||
Password []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ConnectPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", c.FixedHeader)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("protocolversion: %d protocolname: %s cleansession: %t willflag: %t WillQos: %d WillRetain: %t Usernameflag: %t Passwordflag: %t keepalive: %d clientId: %s willtopic: %s willmessage: %s Username: %s Password: %s", c.ProtocolVersion, c.ProtocolName, c.CleanSession, c.WillFlag, c.WillQos, c.WillRetain, c.UsernameFlag, c.PasswordFlag, c.Keepalive, c.ClientIdentifier, c.WillTopic, c.WillMessage, c.Username, c.Password)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ConnectPacket) Write(w io.Writer) error {
|
|
||||||
var body bytes.Buffer
|
|
||||||
var err error
|
|
||||||
|
|
||||||
body.Write(encodeString(c.ProtocolName))
|
|
||||||
body.WriteByte(c.ProtocolVersion)
|
|
||||||
body.WriteByte(boolToByte(c.CleanSession)<<1 | boolToByte(c.WillFlag)<<2 | c.WillQos<<3 | boolToByte(c.WillRetain)<<5 | boolToByte(c.PasswordFlag)<<6 | boolToByte(c.UsernameFlag)<<7)
|
|
||||||
body.Write(encodeUint16(c.Keepalive))
|
|
||||||
body.Write(encodeString(c.ClientIdentifier))
|
|
||||||
if c.WillFlag {
|
|
||||||
body.Write(encodeString(c.WillTopic))
|
|
||||||
body.Write(encodeBytes(c.WillMessage))
|
|
||||||
}
|
|
||||||
if c.UsernameFlag {
|
|
||||||
body.Write(encodeString(c.Username))
|
|
||||||
}
|
|
||||||
if c.PasswordFlag {
|
|
||||||
body.Write(encodeBytes(c.Password))
|
|
||||||
}
|
|
||||||
c.FixedHeader.RemainingLength = body.Len()
|
|
||||||
packet := c.FixedHeader.pack()
|
|
||||||
packet.Write(body.Bytes())
|
|
||||||
_, err = packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (c *ConnectPacket) Unpack(b io.Reader) error {
|
|
||||||
var err error
|
|
||||||
c.ProtocolName, err = decodeString(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c.ProtocolVersion, err = decodeByte(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
options, err := decodeByte(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c.ReservedBit = 1 & options
|
|
||||||
c.CleanSession = 1&(options>>1) > 0
|
|
||||||
c.WillFlag = 1&(options>>2) > 0
|
|
||||||
c.WillQos = 3 & (options >> 3)
|
|
||||||
c.WillRetain = 1&(options>>5) > 0
|
|
||||||
c.PasswordFlag = 1&(options>>6) > 0
|
|
||||||
c.UsernameFlag = 1&(options>>7) > 0
|
|
||||||
c.Keepalive, err = decodeUint16(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c.ClientIdentifier, err = decodeString(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if c.WillFlag {
|
|
||||||
c.WillTopic, err = decodeString(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c.WillMessage, err = decodeBytes(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if c.UsernameFlag {
|
|
||||||
c.Username, err = decodeString(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if c.PasswordFlag {
|
|
||||||
c.Password, err = decodeBytes(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Validate performs validation of the fields of a Connect packet
|
|
||||||
func (c *ConnectPacket) Validate() byte {
|
|
||||||
if c.PasswordFlag && !c.UsernameFlag {
|
|
||||||
return ErrRefusedBadUsernameOrPassword
|
|
||||||
}
|
|
||||||
if c.ReservedBit != 0 {
|
|
||||||
//Bad reserved bit
|
|
||||||
return ErrProtocolViolation
|
|
||||||
}
|
|
||||||
if (c.ProtocolName == "MQIsdp" && c.ProtocolVersion != 3) || (c.ProtocolName == "MQTT" && c.ProtocolVersion != 4) {
|
|
||||||
//Mismatched or unsupported protocol version
|
|
||||||
return ErrRefusedBadProtocolVersion
|
|
||||||
}
|
|
||||||
if c.ProtocolName != "MQIsdp" && c.ProtocolName != "MQTT" {
|
|
||||||
//Bad protocol name
|
|
||||||
return ErrProtocolViolation
|
|
||||||
}
|
|
||||||
if len(c.ClientIdentifier) > 65535 || len(c.Username) > 65535 || len(c.Password) > 65535 {
|
|
||||||
//Bad size field
|
|
||||||
return ErrProtocolViolation
|
|
||||||
}
|
|
||||||
if len(c.ClientIdentifier) == 0 && !c.CleanSession {
|
|
||||||
//Bad client identifier
|
|
||||||
return ErrRefusedIDRejected
|
|
||||||
}
|
|
||||||
return Accepted
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (c *ConnectPacket) Details() Details {
|
|
||||||
return Details{Qos: 0, MessageID: 0}
|
|
||||||
}
|
|
||||||
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/disconnect.go
generated
vendored
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/disconnect.go
generated
vendored
@@ -1,36 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//DisconnectPacket is an internal representation of the fields of the
|
|
||||||
//Disconnect MQTT packet
|
|
||||||
type DisconnectPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DisconnectPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", d.FixedHeader)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DisconnectPacket) Write(w io.Writer) error {
|
|
||||||
packet := d.FixedHeader.pack()
|
|
||||||
_, err := packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (d *DisconnectPacket) Unpack(b io.Reader) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (d *DisconnectPacket) Details() Details {
|
|
||||||
return Details{Qos: 0, MessageID: 0}
|
|
||||||
}
|
|
||||||
346
vendor/github.com/eclipse/paho.mqtt.golang/packets/packets.go
generated
vendored
346
vendor/github.com/eclipse/paho.mqtt.golang/packets/packets.go
generated
vendored
@@ -1,346 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//ControlPacket defines the interface for structs intended to hold
|
|
||||||
//decoded MQTT packets, either from being read or before being
|
|
||||||
//written
|
|
||||||
type ControlPacket interface {
|
|
||||||
Write(io.Writer) error
|
|
||||||
Unpack(io.Reader) error
|
|
||||||
String() string
|
|
||||||
Details() Details
|
|
||||||
}
|
|
||||||
|
|
||||||
//PacketNames maps the constants for each of the MQTT packet types
|
|
||||||
//to a string representation of their name.
|
|
||||||
var PacketNames = map[uint8]string{
|
|
||||||
1: "CONNECT",
|
|
||||||
2: "CONNACK",
|
|
||||||
3: "PUBLISH",
|
|
||||||
4: "PUBACK",
|
|
||||||
5: "PUBREC",
|
|
||||||
6: "PUBREL",
|
|
||||||
7: "PUBCOMP",
|
|
||||||
8: "SUBSCRIBE",
|
|
||||||
9: "SUBACK",
|
|
||||||
10: "UNSUBSCRIBE",
|
|
||||||
11: "UNSUBACK",
|
|
||||||
12: "PINGREQ",
|
|
||||||
13: "PINGRESP",
|
|
||||||
14: "DISCONNECT",
|
|
||||||
}
|
|
||||||
|
|
||||||
//Below are the constants assigned to each of the MQTT packet types
|
|
||||||
const (
|
|
||||||
Connect = 1
|
|
||||||
Connack = 2
|
|
||||||
Publish = 3
|
|
||||||
Puback = 4
|
|
||||||
Pubrec = 5
|
|
||||||
Pubrel = 6
|
|
||||||
Pubcomp = 7
|
|
||||||
Subscribe = 8
|
|
||||||
Suback = 9
|
|
||||||
Unsubscribe = 10
|
|
||||||
Unsuback = 11
|
|
||||||
Pingreq = 12
|
|
||||||
Pingresp = 13
|
|
||||||
Disconnect = 14
|
|
||||||
)
|
|
||||||
|
|
||||||
//Below are the const definitions for error codes returned by
|
|
||||||
//Connect()
|
|
||||||
const (
|
|
||||||
Accepted = 0x00
|
|
||||||
ErrRefusedBadProtocolVersion = 0x01
|
|
||||||
ErrRefusedIDRejected = 0x02
|
|
||||||
ErrRefusedServerUnavailable = 0x03
|
|
||||||
ErrRefusedBadUsernameOrPassword = 0x04
|
|
||||||
ErrRefusedNotAuthorised = 0x05
|
|
||||||
ErrNetworkError = 0xFE
|
|
||||||
ErrProtocolViolation = 0xFF
|
|
||||||
)
|
|
||||||
|
|
||||||
//ConnackReturnCodes is a map of the error codes constants for Connect()
|
|
||||||
//to a string representation of the error
|
|
||||||
var ConnackReturnCodes = map[uint8]string{
|
|
||||||
0: "Connection Accepted",
|
|
||||||
1: "Connection Refused: Bad Protocol Version",
|
|
||||||
2: "Connection Refused: Client Identifier Rejected",
|
|
||||||
3: "Connection Refused: Server Unavailable",
|
|
||||||
4: "Connection Refused: Username or Password in unknown format",
|
|
||||||
5: "Connection Refused: Not Authorised",
|
|
||||||
254: "Connection Error",
|
|
||||||
255: "Connection Refused: Protocol Violation",
|
|
||||||
}
|
|
||||||
|
|
||||||
//ConnErrors is a map of the errors codes constants for Connect()
|
|
||||||
//to a Go error
|
|
||||||
var ConnErrors = map[byte]error{
|
|
||||||
Accepted: nil,
|
|
||||||
ErrRefusedBadProtocolVersion: errors.New("Unnacceptable protocol version"),
|
|
||||||
ErrRefusedIDRejected: errors.New("Identifier rejected"),
|
|
||||||
ErrRefusedServerUnavailable: errors.New("Server Unavailable"),
|
|
||||||
ErrRefusedBadUsernameOrPassword: errors.New("Bad user name or password"),
|
|
||||||
ErrRefusedNotAuthorised: errors.New("Not Authorized"),
|
|
||||||
ErrNetworkError: errors.New("Network Error"),
|
|
||||||
ErrProtocolViolation: errors.New("Protocol Violation"),
|
|
||||||
}
|
|
||||||
|
|
||||||
//ReadPacket takes an instance of an io.Reader (such as net.Conn) and attempts
|
|
||||||
//to read an MQTT packet from the stream. It returns a ControlPacket
|
|
||||||
//representing the decoded MQTT packet and an error. One of these returns will
|
|
||||||
//always be nil, a nil ControlPacket indicating an error occurred.
|
|
||||||
func ReadPacket(r io.Reader) (ControlPacket, error) {
|
|
||||||
var fh FixedHeader
|
|
||||||
b := make([]byte, 1)
|
|
||||||
|
|
||||||
_, err := io.ReadFull(r, b)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = fh.unpack(b[0], r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cp, err := NewControlPacketWithHeader(fh)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
packetBytes := make([]byte, fh.RemainingLength)
|
|
||||||
n, err := io.ReadFull(r, packetBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if n != fh.RemainingLength {
|
|
||||||
return nil, errors.New("Failed to read expected data")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cp.Unpack(bytes.NewBuffer(packetBytes))
|
|
||||||
return cp, err
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewControlPacket is used to create a new ControlPacket of the type specified
|
|
||||||
//by packetType, this is usually done by reference to the packet type constants
|
|
||||||
//defined in packets.go. The newly created ControlPacket is empty and a pointer
|
|
||||||
//is returned.
|
|
||||||
func NewControlPacket(packetType byte) ControlPacket {
|
|
||||||
switch packetType {
|
|
||||||
case Connect:
|
|
||||||
return &ConnectPacket{FixedHeader: FixedHeader{MessageType: Connect}}
|
|
||||||
case Connack:
|
|
||||||
return &ConnackPacket{FixedHeader: FixedHeader{MessageType: Connack}}
|
|
||||||
case Disconnect:
|
|
||||||
return &DisconnectPacket{FixedHeader: FixedHeader{MessageType: Disconnect}}
|
|
||||||
case Publish:
|
|
||||||
return &PublishPacket{FixedHeader: FixedHeader{MessageType: Publish}}
|
|
||||||
case Puback:
|
|
||||||
return &PubackPacket{FixedHeader: FixedHeader{MessageType: Puback}}
|
|
||||||
case Pubrec:
|
|
||||||
return &PubrecPacket{FixedHeader: FixedHeader{MessageType: Pubrec}}
|
|
||||||
case Pubrel:
|
|
||||||
return &PubrelPacket{FixedHeader: FixedHeader{MessageType: Pubrel, Qos: 1}}
|
|
||||||
case Pubcomp:
|
|
||||||
return &PubcompPacket{FixedHeader: FixedHeader{MessageType: Pubcomp}}
|
|
||||||
case Subscribe:
|
|
||||||
return &SubscribePacket{FixedHeader: FixedHeader{MessageType: Subscribe, Qos: 1}}
|
|
||||||
case Suback:
|
|
||||||
return &SubackPacket{FixedHeader: FixedHeader{MessageType: Suback}}
|
|
||||||
case Unsubscribe:
|
|
||||||
return &UnsubscribePacket{FixedHeader: FixedHeader{MessageType: Unsubscribe, Qos: 1}}
|
|
||||||
case Unsuback:
|
|
||||||
return &UnsubackPacket{FixedHeader: FixedHeader{MessageType: Unsuback}}
|
|
||||||
case Pingreq:
|
|
||||||
return &PingreqPacket{FixedHeader: FixedHeader{MessageType: Pingreq}}
|
|
||||||
case Pingresp:
|
|
||||||
return &PingrespPacket{FixedHeader: FixedHeader{MessageType: Pingresp}}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewControlPacketWithHeader is used to create a new ControlPacket of the type
|
|
||||||
//specified within the FixedHeader that is passed to the function.
|
|
||||||
//The newly created ControlPacket is empty and a pointer is returned.
|
|
||||||
func NewControlPacketWithHeader(fh FixedHeader) (ControlPacket, error) {
|
|
||||||
switch fh.MessageType {
|
|
||||||
case Connect:
|
|
||||||
return &ConnectPacket{FixedHeader: fh}, nil
|
|
||||||
case Connack:
|
|
||||||
return &ConnackPacket{FixedHeader: fh}, nil
|
|
||||||
case Disconnect:
|
|
||||||
return &DisconnectPacket{FixedHeader: fh}, nil
|
|
||||||
case Publish:
|
|
||||||
return &PublishPacket{FixedHeader: fh}, nil
|
|
||||||
case Puback:
|
|
||||||
return &PubackPacket{FixedHeader: fh}, nil
|
|
||||||
case Pubrec:
|
|
||||||
return &PubrecPacket{FixedHeader: fh}, nil
|
|
||||||
case Pubrel:
|
|
||||||
return &PubrelPacket{FixedHeader: fh}, nil
|
|
||||||
case Pubcomp:
|
|
||||||
return &PubcompPacket{FixedHeader: fh}, nil
|
|
||||||
case Subscribe:
|
|
||||||
return &SubscribePacket{FixedHeader: fh}, nil
|
|
||||||
case Suback:
|
|
||||||
return &SubackPacket{FixedHeader: fh}, nil
|
|
||||||
case Unsubscribe:
|
|
||||||
return &UnsubscribePacket{FixedHeader: fh}, nil
|
|
||||||
case Unsuback:
|
|
||||||
return &UnsubackPacket{FixedHeader: fh}, nil
|
|
||||||
case Pingreq:
|
|
||||||
return &PingreqPacket{FixedHeader: fh}, nil
|
|
||||||
case Pingresp:
|
|
||||||
return &PingrespPacket{FixedHeader: fh}, nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("unsupported packet type 0x%x", fh.MessageType)
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details struct returned by the Details() function called on
|
|
||||||
//ControlPackets to present details of the Qos and MessageID
|
|
||||||
//of the ControlPacket
|
|
||||||
type Details struct {
|
|
||||||
Qos byte
|
|
||||||
MessageID uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
//FixedHeader is a struct to hold the decoded information from
|
|
||||||
//the fixed header of an MQTT ControlPacket
|
|
||||||
type FixedHeader struct {
|
|
||||||
MessageType byte
|
|
||||||
Dup bool
|
|
||||||
Qos byte
|
|
||||||
Retain bool
|
|
||||||
RemainingLength int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fh FixedHeader) String() string {
|
|
||||||
return fmt.Sprintf("%s: dup: %t qos: %d retain: %t rLength: %d", PacketNames[fh.MessageType], fh.Dup, fh.Qos, fh.Retain, fh.RemainingLength)
|
|
||||||
}
|
|
||||||
|
|
||||||
func boolToByte(b bool) byte {
|
|
||||||
switch b {
|
|
||||||
case true:
|
|
||||||
return 1
|
|
||||||
default:
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fh *FixedHeader) pack() bytes.Buffer {
|
|
||||||
var header bytes.Buffer
|
|
||||||
header.WriteByte(fh.MessageType<<4 | boolToByte(fh.Dup)<<3 | fh.Qos<<1 | boolToByte(fh.Retain))
|
|
||||||
header.Write(encodeLength(fh.RemainingLength))
|
|
||||||
return header
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fh *FixedHeader) unpack(typeAndFlags byte, r io.Reader) error {
|
|
||||||
fh.MessageType = typeAndFlags >> 4
|
|
||||||
fh.Dup = (typeAndFlags>>3)&0x01 > 0
|
|
||||||
fh.Qos = (typeAndFlags >> 1) & 0x03
|
|
||||||
fh.Retain = typeAndFlags&0x01 > 0
|
|
||||||
|
|
||||||
var err error
|
|
||||||
fh.RemainingLength, err = decodeLength(r)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeByte(b io.Reader) (byte, error) {
|
|
||||||
num := make([]byte, 1)
|
|
||||||
_, err := b.Read(num)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return num[0], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeUint16(b io.Reader) (uint16, error) {
|
|
||||||
num := make([]byte, 2)
|
|
||||||
_, err := b.Read(num)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return binary.BigEndian.Uint16(num), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeUint16(num uint16) []byte {
|
|
||||||
bytes := make([]byte, 2)
|
|
||||||
binary.BigEndian.PutUint16(bytes, num)
|
|
||||||
return bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeString(field string) []byte {
|
|
||||||
return encodeBytes([]byte(field))
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeString(b io.Reader) (string, error) {
|
|
||||||
buf, err := decodeBytes(b)
|
|
||||||
return string(buf), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeBytes(b io.Reader) ([]byte, error) {
|
|
||||||
fieldLength, err := decodeUint16(b)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
field := make([]byte, fieldLength)
|
|
||||||
_, err = b.Read(field)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return field, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeBytes(field []byte) []byte {
|
|
||||||
fieldLength := make([]byte, 2)
|
|
||||||
binary.BigEndian.PutUint16(fieldLength, uint16(len(field)))
|
|
||||||
return append(fieldLength, field...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeLength(length int) []byte {
|
|
||||||
var encLength []byte
|
|
||||||
for {
|
|
||||||
digit := byte(length % 128)
|
|
||||||
length /= 128
|
|
||||||
if length > 0 {
|
|
||||||
digit |= 0x80
|
|
||||||
}
|
|
||||||
encLength = append(encLength, digit)
|
|
||||||
if length == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return encLength
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeLength(r io.Reader) (int, error) {
|
|
||||||
var rLength uint32
|
|
||||||
var multiplier uint32
|
|
||||||
b := make([]byte, 1)
|
|
||||||
for multiplier < 27 { //fix: Infinite '(digit & 128) == 1' will cause the dead loop
|
|
||||||
_, err := io.ReadFull(r, b)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
digit := b[0]
|
|
||||||
rLength |= uint32(digit&127) << multiplier
|
|
||||||
if (digit & 128) == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
multiplier += 7
|
|
||||||
}
|
|
||||||
return int(rLength), nil
|
|
||||||
}
|
|
||||||
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/pingreq.go
generated
vendored
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/pingreq.go
generated
vendored
@@ -1,36 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//PingreqPacket is an internal representation of the fields of the
|
|
||||||
//Pingreq MQTT packet
|
|
||||||
type PingreqPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pr *PingreqPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", pr.FixedHeader)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pr *PingreqPacket) Write(w io.Writer) error {
|
|
||||||
packet := pr.FixedHeader.pack()
|
|
||||||
_, err := packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (pr *PingreqPacket) Unpack(b io.Reader) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (pr *PingreqPacket) Details() Details {
|
|
||||||
return Details{Qos: 0, MessageID: 0}
|
|
||||||
}
|
|
||||||
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/pingresp.go
generated
vendored
36
vendor/github.com/eclipse/paho.mqtt.golang/packets/pingresp.go
generated
vendored
@@ -1,36 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//PingrespPacket is an internal representation of the fields of the
|
|
||||||
//Pingresp MQTT packet
|
|
||||||
type PingrespPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pr *PingrespPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", pr.FixedHeader)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pr *PingrespPacket) Write(w io.Writer) error {
|
|
||||||
packet := pr.FixedHeader.pack()
|
|
||||||
_, err := packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (pr *PingrespPacket) Unpack(b io.Reader) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (pr *PingrespPacket) Details() Details {
|
|
||||||
return Details{Qos: 0, MessageID: 0}
|
|
||||||
}
|
|
||||||
45
vendor/github.com/eclipse/paho.mqtt.golang/packets/puback.go
generated
vendored
45
vendor/github.com/eclipse/paho.mqtt.golang/packets/puback.go
generated
vendored
@@ -1,45 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//PubackPacket is an internal representation of the fields of the
|
|
||||||
//Puback MQTT packet
|
|
||||||
type PubackPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
MessageID uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pa *PubackPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", pa.FixedHeader)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("MessageID: %d", pa.MessageID)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pa *PubackPacket) Write(w io.Writer) error {
|
|
||||||
var err error
|
|
||||||
pa.FixedHeader.RemainingLength = 2
|
|
||||||
packet := pa.FixedHeader.pack()
|
|
||||||
packet.Write(encodeUint16(pa.MessageID))
|
|
||||||
_, err = packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (pa *PubackPacket) Unpack(b io.Reader) error {
|
|
||||||
var err error
|
|
||||||
pa.MessageID, err = decodeUint16(b)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (pa *PubackPacket) Details() Details {
|
|
||||||
return Details{Qos: pa.Qos, MessageID: pa.MessageID}
|
|
||||||
}
|
|
||||||
45
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubcomp.go
generated
vendored
45
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubcomp.go
generated
vendored
@@ -1,45 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//PubcompPacket is an internal representation of the fields of the
|
|
||||||
//Pubcomp MQTT packet
|
|
||||||
type PubcompPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
MessageID uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pc *PubcompPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", pc.FixedHeader)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("MessageID: %d", pc.MessageID)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pc *PubcompPacket) Write(w io.Writer) error {
|
|
||||||
var err error
|
|
||||||
pc.FixedHeader.RemainingLength = 2
|
|
||||||
packet := pc.FixedHeader.pack()
|
|
||||||
packet.Write(encodeUint16(pc.MessageID))
|
|
||||||
_, err = packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (pc *PubcompPacket) Unpack(b io.Reader) error {
|
|
||||||
var err error
|
|
||||||
pc.MessageID, err = decodeUint16(b)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (pc *PubcompPacket) Details() Details {
|
|
||||||
return Details{Qos: pc.Qos, MessageID: pc.MessageID}
|
|
||||||
}
|
|
||||||
88
vendor/github.com/eclipse/paho.mqtt.golang/packets/publish.go
generated
vendored
88
vendor/github.com/eclipse/paho.mqtt.golang/packets/publish.go
generated
vendored
@@ -1,88 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//PublishPacket is an internal representation of the fields of the
|
|
||||||
//Publish MQTT packet
|
|
||||||
type PublishPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
TopicName string
|
|
||||||
MessageID uint16
|
|
||||||
Payload []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PublishPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", p.FixedHeader)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("topicName: %s MessageID: %d", p.TopicName, p.MessageID)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("payload: %s", string(p.Payload))
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PublishPacket) Write(w io.Writer) error {
|
|
||||||
var body bytes.Buffer
|
|
||||||
var err error
|
|
||||||
|
|
||||||
body.Write(encodeString(p.TopicName))
|
|
||||||
if p.Qos > 0 {
|
|
||||||
body.Write(encodeUint16(p.MessageID))
|
|
||||||
}
|
|
||||||
p.FixedHeader.RemainingLength = body.Len() + len(p.Payload)
|
|
||||||
packet := p.FixedHeader.pack()
|
|
||||||
packet.Write(body.Bytes())
|
|
||||||
packet.Write(p.Payload)
|
|
||||||
_, err = w.Write(packet.Bytes())
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (p *PublishPacket) Unpack(b io.Reader) error {
|
|
||||||
var payloadLength = p.FixedHeader.RemainingLength
|
|
||||||
var err error
|
|
||||||
p.TopicName, err = decodeString(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.Qos > 0 {
|
|
||||||
p.MessageID, err = decodeUint16(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
payloadLength -= len(p.TopicName) + 4
|
|
||||||
} else {
|
|
||||||
payloadLength -= len(p.TopicName) + 2
|
|
||||||
}
|
|
||||||
if payloadLength < 0 {
|
|
||||||
return fmt.Errorf("Error unpacking publish, payload length < 0")
|
|
||||||
}
|
|
||||||
p.Payload = make([]byte, payloadLength)
|
|
||||||
_, err = b.Read(p.Payload)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Copy creates a new PublishPacket with the same topic and payload
|
|
||||||
//but an empty fixed header, useful for when you want to deliver
|
|
||||||
//a message with different properties such as Qos but the same
|
|
||||||
//content
|
|
||||||
func (p *PublishPacket) Copy() *PublishPacket {
|
|
||||||
newP := NewControlPacket(Publish).(*PublishPacket)
|
|
||||||
newP.TopicName = p.TopicName
|
|
||||||
newP.Payload = p.Payload
|
|
||||||
|
|
||||||
return newP
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (p *PublishPacket) Details() Details {
|
|
||||||
return Details{Qos: p.Qos, MessageID: p.MessageID}
|
|
||||||
}
|
|
||||||
45
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubrec.go
generated
vendored
45
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubrec.go
generated
vendored
@@ -1,45 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//PubrecPacket is an internal representation of the fields of the
|
|
||||||
//Pubrec MQTT packet
|
|
||||||
type PubrecPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
MessageID uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pr *PubrecPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", pr.FixedHeader)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("MessageID: %d", pr.MessageID)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pr *PubrecPacket) Write(w io.Writer) error {
|
|
||||||
var err error
|
|
||||||
pr.FixedHeader.RemainingLength = 2
|
|
||||||
packet := pr.FixedHeader.pack()
|
|
||||||
packet.Write(encodeUint16(pr.MessageID))
|
|
||||||
_, err = packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (pr *PubrecPacket) Unpack(b io.Reader) error {
|
|
||||||
var err error
|
|
||||||
pr.MessageID, err = decodeUint16(b)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (pr *PubrecPacket) Details() Details {
|
|
||||||
return Details{Qos: pr.Qos, MessageID: pr.MessageID}
|
|
||||||
}
|
|
||||||
45
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubrel.go
generated
vendored
45
vendor/github.com/eclipse/paho.mqtt.golang/packets/pubrel.go
generated
vendored
@@ -1,45 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//PubrelPacket is an internal representation of the fields of the
|
|
||||||
//Pubrel MQTT packet
|
|
||||||
type PubrelPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
MessageID uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pr *PubrelPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", pr.FixedHeader)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("MessageID: %d", pr.MessageID)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pr *PubrelPacket) Write(w io.Writer) error {
|
|
||||||
var err error
|
|
||||||
pr.FixedHeader.RemainingLength = 2
|
|
||||||
packet := pr.FixedHeader.pack()
|
|
||||||
packet.Write(encodeUint16(pr.MessageID))
|
|
||||||
_, err = packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (pr *PubrelPacket) Unpack(b io.Reader) error {
|
|
||||||
var err error
|
|
||||||
pr.MessageID, err = decodeUint16(b)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (pr *PubrelPacket) Details() Details {
|
|
||||||
return Details{Qos: pr.Qos, MessageID: pr.MessageID}
|
|
||||||
}
|
|
||||||
60
vendor/github.com/eclipse/paho.mqtt.golang/packets/suback.go
generated
vendored
60
vendor/github.com/eclipse/paho.mqtt.golang/packets/suback.go
generated
vendored
@@ -1,60 +0,0 @@
|
|||||||
package packets
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//SubackPacket is an internal representation of the fields of the
|
|
||||||
//Suback MQTT packet
|
|
||||||
type SubackPacket struct {
|
|
||||||
FixedHeader
|
|
||||||
MessageID uint16
|
|
||||||
ReturnCodes []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sa *SubackPacket) String() string {
|
|
||||||
str := fmt.Sprintf("%s", sa.FixedHeader)
|
|
||||||
str += " "
|
|
||||||
str += fmt.Sprintf("MessageID: %d", sa.MessageID)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sa *SubackPacket) Write(w io.Writer) error {
|
|
||||||
var body bytes.Buffer
|
|
||||||
var err error
|
|
||||||
body.Write(encodeUint16(sa.MessageID))
|
|
||||||
body.Write(sa.ReturnCodes)
|
|
||||||
sa.FixedHeader.RemainingLength = body.Len()
|
|
||||||
packet := sa.FixedHeader.pack()
|
|
||||||
packet.Write(body.Bytes())
|
|
||||||
_, err = packet.WriteTo(w)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unpack decodes the details of a ControlPacket after the fixed
|
|
||||||
//header has been read
|
|
||||||
func (sa *SubackPacket) Unpack(b io.Reader) error {
|
|
||||||
var qosBuffer bytes.Buffer
|
|
||||||
var err error
|
|
||||||
sa.MessageID, err = decodeUint16(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = qosBuffer.ReadFrom(b)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sa.ReturnCodes = qosBuffer.Bytes()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Details returns a Details struct containing the Qos and
|
|
||||||
//MessageID of this ControlPacket
|
|
||||||
func (sa *SubackPacket) Details() Details {
|
|
||||||
return Details{Qos: 0, MessageID: sa.MessageID}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user