Skip to content

Commit 6c59f41

Browse files
committed
Upgrade GitHub Actions and implement element functionality
- Implement JSON response for API state in serve command - Add GetKey method to Element and modify VolumeElement to include name in state response
1 parent df5b0af commit 6c59f41

4 files changed

Lines changed: 43 additions & 14 deletions

File tree

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ jobs:
1515

1616
steps:
1717
- name: Checkout repository
18-
uses: actions/checkout@v4
18+
uses: actions/checkout@v6
1919

2020
- name: Set up Go
21-
uses: actions/setup-go@v4
21+
uses: actions/setup-go@v6
2222
with:
2323
go-version: '1.26.4'
2424

2525
- name: Cache Go modules
26-
uses: actions/cache@v4
26+
uses: actions/cache@v5
2727
with:
2828
path: |
2929
~/.cache/go-build

cmd/serve/serve.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package serve
66
// create a web server and register all http Handlers and Functions
77

88
import (
9+
"encoding/json"
910
"errors"
1011
"flag"
1112
"fmt"
@@ -15,6 +16,8 @@ import (
1516

1617
"github.com/HomeDing/goding/internal/global"
1718
"github.com/HomeDing/goding/internal/http/verbose"
19+
20+
"github.com/HomeDing/goding/internal/elements"
1821
)
1922

2023
// serve command parameters
@@ -60,15 +63,22 @@ func Run() error {
6063
var fileServer http.Handler = http.FileServer(http.Dir(global.WebFolder))
6164

6265
// TODO: enable embedding the files from /web into the binary using the embed package
63-
6466
mux.Handle("/", fileServer)
6567

6668
// mux.HandleFunc("GET /api/state", api.HandleStatus)
6769

6870
mux.Handle("GET /api", http.NotFoundHandler())
6971

7072
mux.HandleFunc("/api/state/", func(w http.ResponseWriter, r *http.Request) {
71-
fmt.Fprint(w, "got path\n")
73+
var v = elements.NewVolumeElement("main")
74+
75+
var stateMap = map[string]map[string]string{}
76+
77+
w.Header().Set("Content-Type", "application/json")
78+
79+
stateMap[v.GetKey()] = v.State()
80+
81+
json.NewEncoder(w).Encode(stateMap)
7282
})
7383

7484
mux.HandleFunc("/task/{id}/", func(w http.ResponseWriter, r *http.Request) {

internal/elements/element.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package elements
22

3+
// import (
4+
// "maps"
5+
// )
6+
37
type Element struct {
4-
Type string // Type of the element, e.g. "volume", "light", etc.
5-
Id string // Unique identifier for the element, e.g. "speaker1", "lamp2", etc.
6-
key string // key is a unique identifier for the element, e.g. "volume/speaker1"
8+
Type string // Type of the element, e.g. "volume", "light", etc.
9+
Id string // Unique identifier for the element, e.g. "speaker1", "lamp2", etc.
10+
key string // key is a unique identifier for the element, e.g. "volume/speaker1"
711
config map[string]string // config holds the configuration for the element, e.g. {"min": "0", "max": "100"}
812
values map[string]string // values holds the current state of the element, e.g. {"level": "50"}
913
}
@@ -14,7 +18,6 @@ func NewElement(elementType string, elementId string) Element {
1418
element := Element{
1519
Id: elementId,
1620
Type: elementType,
17-
key: elementType + "/" + elementId,
1821
config: map[string]string{},
1922
values: map[string]string{},
2023
}
@@ -27,6 +30,10 @@ func (e *Element) Init(id string) bool {
2730
return true
2831
}
2932

33+
func (e *Element) GetKey() string {
34+
return e.Type + "/" + e.Id
35+
}
36+
3037
func (e Element) Set(key, value string) bool {
3138
return false
3239
}
@@ -36,5 +43,9 @@ func (e Element) Loop() bool {
3643
}
3744

3845
func (e Element) State() map[string]string {
39-
return (map[string]string{})
46+
// var res map[string]string
47+
// maps.Copy(res, e.config)
48+
// maps.Copy(res, e.values)
49+
// return (res)
50+
return (e.values)
4051
}

internal/elements/volume.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package elements
22

3+
import (
4+
"maps"
5+
)
6+
37
type VolumeElement struct {
48
Element
59
name string
610
}
711

8-
func NewVolumeElement(elementType string, elementId string) VolumeElement {
9-
var e Element = NewElement(elementType, elementId)
12+
func NewVolumeElement(elementId string) VolumeElement {
13+
var e Element = NewElement("volume", elementId)
1014
var v VolumeElement = VolumeElement{Element: e, name: "Volume"}
1115

1216
v.config["min"] = "0"
@@ -15,7 +19,6 @@ func NewVolumeElement(elementType string, elementId string) VolumeElement {
1519
return v
1620
}
1721

18-
1922
func (e VolumeElement) Set(key, value string) bool {
2023

2124
var oldValue string
@@ -36,5 +39,10 @@ func (e VolumeElement) Loop() bool {
3639
}
3740

3841
func (e VolumeElement) State() map[string]string {
39-
return e.Element.State()
42+
var res = map[string]string{}
43+
44+
maps.Copy(res, e.Element.State())
45+
res["name"] = e.name
46+
47+
return res
4048
}

0 commit comments

Comments
 (0)