Skip to content

Commit

Permalink
Golang (#42)
Browse files Browse the repository at this point in the history
* feat: implemented in go to reduce memory amount

* position commands

* publish positions

* feat: support tilt

* listen to mqtt

* chore: build infrastructure for go

chore: build infrastructure for go

fix(ci): fix the build

* chore(deps): update go to 1.23

* update docu
  • Loading branch information
philipparndt authored Oct 13, 2024
1 parent 5ba74ed commit b832788
Show file tree
Hide file tree
Showing 54 changed files with 916 additions and 15,871 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

60 changes: 21 additions & 39 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,60 @@ jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
permissions:
contents: write

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 20.x
steps:
- uses: actions/checkout@v4

- uses: philipparndt/get-release-number@v2
- uses: philipparndt/get-release-number@v3
id: next
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
releaseType: ${{ github.event.inputs.releaseType }}

- name: Update version
working-directory: app
env:
RELEASE_VERSION: ${{ steps.next.outputs.version }}
run: npm version $RELEASE_VERSION --allow-same-version

- name: Install
working-directory: app
shell: bash
run: npm install
- uses: actions/setup-go@v5
with:
go-version: '1.23.2'

- name: Build
working-directory: app
shell: bash
run: npm run build
run: |
go build .
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build docker container and push
id: docker_build
uses: docker/build-push-action@v4
uses: docker/build-push-action@v6
env:
RELEASE_VERSION: ${{ steps.next.outputs.version }}
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64
context: ./app
file: ./app/Dockerfile
platforms: linux/amd64,linux/arm/v7,linux/arm64
push: true
tags: |
pharndt/eltakomqtt:latest
pharndt/eltakomqtt:${{env.RELEASE_VERSION}}
- name: Create Release
id: create_release
uses: ncipollo/release-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_VERSION: ${{ steps.next.outputs.version }}
- uses: ncipollo/release-action@v1
with:
tag: ${{env.RELEASE_VERSION}}
name: ${{env.RELEASE_VERSION}}
name: ${{ steps.next.outputs.version }}
tag: ${{ steps.next.outputs.version }}
body: |
Docker tag: `pharndt/eltakomqtt:${{env.RELEASE_VERSION}}`
Docker tag: `pharndt/eltakomqtt:${{ steps.next.outputs.version }}`
Changes in this Release
- Dependency update
- ...
draft: false
prerelease: false
34 changes: 16 additions & 18 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup node
uses: actions/setup-node@v3
- uses: actions/setup-go@v5
with:
node-version: 20.x

- name: Install
working-directory: app
run: |
npm install
- name: Lint/Test
working-directory: app
run: |
npm run lint
npm test
go-version: '1.23.2'

- name: Build
working-directory: app
run: |
npm run build
go build .
- name: Build container
run: docker compose build
- name: Build docker container and push
id: docker_build
uses: docker/build-push-action@v6
env:
RELEASE_VERSION: ${{ steps.next.outputs.version }}
with:
context: ./app
file: ./app/Dockerfile
platforms: linux/amd64
push: false
tags: |
pharndt/eltakomqtt:latest
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ config-auth.json
.DS_Store

.idea
*.iml
*.iml

eltako-to-mqtt-gw
12 changes: 12 additions & 0 deletions .run/run.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="run" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="miele-to-mqtt-gw" />
<working_directory value="$PROJECT_DIR$/v2" />
<parameters value="../production/config/config.json" />
<kind value="PACKAGE" />
<package value="github.com/mqtt-home/eltako-to-mqtt-gw" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$/v2/main.go" />
<method v="2" />
</configuration>
</component>
5 changes: 0 additions & 5 deletions Dockerfile

This file was deleted.

20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

[![mqtt-smarthome](https://img.shields.io/badge/mqtt-smarthome-blue.svg)](https://github.com/mqtt-smarthome/mqtt-smarthome)

Convert the Eltako Series 62-IP data to MQTT messages
Convert the Eltako Series 62-IP data to MQTT messages.

Some waring about those devices:
- they are announced to come with Matter support, but they will never get an update support it
- they do not support setting the position in a finer granularity than 1% so tilt is only possible when the blinds
are small enough so that 1% is a small enough step

## Devices

Expand Down Expand Up @@ -60,6 +65,19 @@ Topic: `home/eltako/<device-name>/set`
}
```

### Tilt the blinds

Topic: `home/eltako/<device-name>/set`

```json
{
"action": "tilt",
"position": 50
}
```

This will move the position to 50% and then tilt the blinds.

## Configuration

Example configuration:
Expand Down
47 changes: 0 additions & 47 deletions app/.eslintrc.json

This file was deleted.

4 changes: 0 additions & 4 deletions app/.gitignore

This file was deleted.

26 changes: 26 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Build the application from source
FROM golang:1.23 AS build-stage

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . ./

RUN CGO_ENABLED=0 GOOS=linux go build -o /eltako-to-mqtt-gw

# Run the tests in the container
FROM build-stage AS run-test-stage
RUN go test -v ./...

# Deploy the application binary into a lean image
FROM gcr.io/distroless/base-debian11 AS build-release-stage

WORKDIR /

COPY --from=build-stage /eltako-to-mqtt-gw /eltako-to-mqtt-gw

USER nonroot:nonroot

ENTRYPOINT ["/eltako-to-mqtt-gw", "/var/lib/eltako-to-mqtt-gw/config.json"]
33 changes: 33 additions & 0 deletions app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Makefile for Go application

# Set the Go binary and flags
GO = go
GOFLAGS = -v

# Set the paths
BUILD_DIR = build
CONFIG_DIR = $(PWD)/../production/config

# Set the binary name
BINARY_NAME = eltako-to-mqtt-gw
DOCKER_IMAGE_NAME = pharndt/eltako:latest

.PHONY: build
build:
@echo "Building the application..."
@$(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) .

.PHONY: run
run: build
@echo "Running the application..."
@$(BUILD_DIR)/$(BINARY_NAME) $(CONFIG_DIR)/config.json

.PHONY: docker
docker: build
@echo "Building Docker image..."
@docker build -t $(DOCKER_IMAGE_NAME) .

.PHONY: clean
clean:
@echo "Cleaning up..."
@rm -rf $(BUILD_DIR)
69 changes: 69 additions & 0 deletions app/commands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package commands

import (
"encoding/json"
"fmt"
"strings"
)

type ActionType string

func (a *ActionType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*a = ActionType(strings.ToLower(s))
return nil
}

const (
ActionClose ActionType = "close"
ActionOpen ActionType = "open"
ActionSet ActionType = "set"
ActionCloseAndOpenBlinds ActionType = "closeandopenblinds"
ActionTilt ActionType = "tilt"
)

type Action struct {
Action ActionType `json:"action"`
Position int `json:"position"`
}

func Parse(data []byte) (LLCommand, error) {
var command Action
err := json.Unmarshal(data, &command)

if err == nil {
return command.validate()
}

return LLCommand{}, err
}

func (c *Action) validate() (LLCommand, error) {
llc := LLCommand{}
switch strings.ToLower(string(c.Action)) {
case string(ActionClose):
llc.Action = LLActionSet
llc.Position = 0
case string(ActionOpen):
llc.Action = LLActionSet
llc.Position = 100
case string(ActionSet):
fallthrough
case "":
llc.Action = LLActionSet
llc.Position = c.Position
case string(ActionCloseAndOpenBlinds):
llc.Action = LLActionTilt
llc.Position = 0
case string(ActionTilt):
llc.Action = LLActionTilt
llc.Position = c.Position
default:
return llc, fmt.Errorf("invalid action")
}

return llc, nil
}
Loading

0 comments on commit b832788

Please sign in to comment.