Skip to content

Commit ed2d5b3

Browse files
initial
1 parent 543247c commit ed2d5b3

File tree

8 files changed

+611
-0
lines changed

8 files changed

+611
-0
lines changed

.github/workflows/go.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
build:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
- name: Setup Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: '1.23.8'
24+
- name: Update
25+
run: sudo apt-get update
26+
- name: Install system dependencies
27+
run: sudo apt-get install -y libudev-dev
28+
- name: Install dependencies
29+
run: go get .
30+
- name: Build
31+
run: go build -v ./...

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
openlinkhub_tray

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module openlinkhub_tray
2+
3+
go 1.23.8
4+
5+
require github.com/godbus/dbus/v5 v5.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
2+
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=

main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"net"
8+
"openlinkhub_tray/src/common"
9+
"openlinkhub_tray/src/controller"
10+
)
11+
12+
func main() {
13+
ip := flag.String("ip", "127.0.0.1", "IP address of the OpenLinkHub service")
14+
port := flag.Int("port", 27003, "Port number of the OpenLinkHub service")
15+
flag.Parse()
16+
17+
// Crash it
18+
if net.ParseIP(*ip) == nil {
19+
log.Fatalf("Invalid IP address: %s", *ip)
20+
}
21+
22+
// Format backend address
23+
addr := fmt.Sprintf("%s:%d", *ip, *port)
24+
25+
// Store it
26+
common.BackendAddr = addr
27+
28+
// Run
29+
controller.Init()
30+
}

src/common/common.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package common
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
"strconv"
10+
"time"
11+
)
12+
13+
var BackendAddr string
14+
15+
type Response struct {
16+
Code int `json:"code"`
17+
Status int `json:"status"`
18+
Data Data `json:"data"`
19+
}
20+
21+
type Data struct {
22+
CpuTemp string `json:"cpu_temp"`
23+
GpuTemp string `json:"gpu_temp"`
24+
Battery map[string]BatteryDetails `json:"battery"`
25+
}
26+
27+
type BatteryDetails struct {
28+
Device string `json:"Device"`
29+
Level int `json:"Level"`
30+
DeviceType int `json:"DeviceType"`
31+
}
32+
33+
// LoadDataFromBackend will load systray data from backend service
34+
func LoadDataFromBackend() (*Response, error) {
35+
url := fmt.Sprintf("http://%s/api/systray", BackendAddr)
36+
client := http.Client{Timeout: 5 * time.Second}
37+
38+
resp, err := client.Get(url)
39+
if err != nil {
40+
return nil, err
41+
}
42+
defer func(Body io.ReadCloser) {
43+
err := Body.Close()
44+
if err != nil {
45+
46+
}
47+
}(resp.Body)
48+
49+
if resp.StatusCode != http.StatusOK {
50+
return nil, errors.New("Non-200 response code: " + strconv.Itoa(resp.StatusCode))
51+
}
52+
53+
body, err := io.ReadAll(resp.Body)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
var response Response
59+
err = json.Unmarshal(body, &response)
60+
if err != nil {
61+
return nil, err
62+
}
63+
return &response, nil
64+
}

src/controller/controller.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package controller
2+
3+
import (
4+
"fmt"
5+
"openlinkhub_tray/src/common"
6+
"openlinkhub_tray/src/systray"
7+
"sync"
8+
"time"
9+
)
10+
11+
var (
12+
menuMutex sync.Mutex
13+
queueTime = 60
14+
)
15+
16+
// processMenu will process any menu adding / changes
17+
func processMenu() {
18+
menuMutex.Lock()
19+
defer menuMutex.Unlock()
20+
response, err := common.LoadDataFromBackend()
21+
if err == nil {
22+
systray.SyncBatteryToMenu(response)
23+
} else {
24+
fmt.Println("Failed to load data from backend. Error:", err)
25+
}
26+
}
27+
28+
func Init() {
29+
ready := make(chan struct{})
30+
go func() {
31+
systray.Init(ready)
32+
}()
33+
34+
<-ready // Wait for systray to be ready
35+
36+
go func() {
37+
ticker := time.NewTicker(time.Duration(queueTime) * time.Second)
38+
defer ticker.Stop()
39+
40+
processMenu()
41+
for range ticker.C {
42+
processMenu()
43+
}
44+
}()
45+
select {}
46+
}

0 commit comments

Comments
 (0)