itd/cmd/itgui/info.go

150 lines
3.2 KiB
Go
Raw Permalink Normal View History

2021-08-26 04:18:24 +00:00
package main
import (
"bufio"
"errors"
"fmt"
"image/color"
"net"
"encoding/json"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"go.arsenm.dev/itd/internal/types"
)
func infoTab(parent fyne.Window) *fyne.Container {
infoLayout := container.NewVBox(
// Add rectangle for a bit of padding
canvas.NewRectangle(color.Transparent),
)
2021-08-26 15:47:17 +00:00
// Create label for heart rate
2021-08-26 04:18:24 +00:00
heartRateLbl := newText("0 BPM", 24)
2021-08-26 15:47:17 +00:00
// Creae container to store heart rate section
2021-08-26 04:18:24 +00:00
heartRate := container.NewVBox(
newText("Heart Rate", 12),
heartRateLbl,
canvas.NewLine(theme.ShadowColor()),
)
infoLayout.Add(heartRate)
2021-08-26 15:47:17 +00:00
// Watch for heart rate updates
2021-08-26 04:18:24 +00:00
go watch(types.ReqTypeWatchHeartRate, func(data interface{}) {
2021-08-26 15:47:17 +00:00
// Change text of heart rate label
2021-08-26 04:18:24 +00:00
heartRateLbl.Text = fmt.Sprintf("%d BPM", int(data.(float64)))
2021-08-26 15:47:17 +00:00
// Refresh label
2021-08-26 04:18:24 +00:00
heartRateLbl.Refresh()
}, parent)
2021-08-26 15:47:17 +00:00
// Create label for battery level
2021-08-26 04:18:24 +00:00
battLevelLbl := newText("0%", 24)
2021-08-26 15:47:17 +00:00
// Create container to store battery level section
2021-08-26 04:18:24 +00:00
battLevel := container.NewVBox(
newText("Battery Level", 12),
battLevelLbl,
canvas.NewLine(theme.ShadowColor()),
)
infoLayout.Add(battLevel)
2021-08-26 15:47:17 +00:00
// Watch for changes in battery level
2021-08-26 04:18:24 +00:00
go watch(types.ReqTypeWatchBattLevel, func(data interface{}) {
battLevelLbl.Text = fmt.Sprintf("%d%%", int(data.(float64)))
battLevelLbl.Refresh()
}, parent)
fwVerString, err := get(types.ReqTypeFwVersion)
if err != nil {
2021-08-27 15:47:24 +00:00
guiErr(err, "Error getting firmware string", true, parent)
2021-08-26 04:18:24 +00:00
}
fwVer := container.NewVBox(
newText("Firmware Version", 12),
newText(fwVerString.(string), 24),
canvas.NewLine(theme.ShadowColor()),
)
infoLayout.Add(fwVer)
btAddrString, err := get(types.ReqTypeBtAddress)
if err != nil {
panic(err)
}
btAddr := container.NewVBox(
newText("Bluetooth Address", 12),
newText(btAddrString.(string), 24),
canvas.NewLine(theme.ShadowColor()),
)
infoLayout.Add(btAddr)
return infoLayout
}
func watch(req int, onRecv func(data interface{}), parent fyne.Window) error {
conn, err := net.Dial("unix", SockPath)
if err != nil {
return err
}
defer conn.Close()
err = json.NewEncoder(conn).Encode(types.Request{
Type: req,
})
if err != nil {
return err
}
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
res, err := getResp(scanner.Bytes())
if err != nil {
2021-08-27 15:47:24 +00:00
guiErr(err, "Error getting response from connection", false, parent)
2021-08-26 04:18:24 +00:00
continue
}
onRecv(res.Value)
}
return nil
}
func get(req int) (interface{}, error) {
conn, err := net.Dial("unix", SockPath)
if err != nil {
return nil, err
}
defer conn.Close()
err = json.NewEncoder(conn).Encode(types.Request{
Type: req,
})
if err != nil {
return nil, err
}
line, _, err := bufio.NewReader(conn).ReadLine()
if err != nil {
return nil, err
}
res, err := getResp(line)
if err != nil {
return nil, err
}
return res.Value, nil
}
func getResp(line []byte) (*types.Response, error) {
var res types.Response
err := json.Unmarshal(line, &res)
if err != nil {
return nil, err
}
if res.Error {
return nil, errors.New(res.Message)
}
return &res, nil
}
func newText(t string, size float32) *canvas.Text {
text := canvas.NewText(t, theme.ForegroundColor())
text.TextSize = size
return text
}