Compare commits
74 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 078b8dc490 | |||
| 14aaa8a0ed | |||
| 1f4d59d84e | |||
| 3643a479ab | |||
| 625805fe96 | |||
| 4b6f7d408e | |||
| 9034ef7c6b | |||
| 9939f724c4 | |||
| 8dce33f7b1 | |||
| 563009c44d | |||
| d4a8a9f8c9 | |||
| 7fd9af3288 | |||
| 4508559bfd | |||
| 0cdf8a4bed | |||
| 2af6c1887f | |||
| 3a3f95acdf | |||
| d318c584da | |||
| c8c617c10a | |||
| 365414f951 | |||
| 9b04d06560 | |||
| 23e9195e70 | |||
| cd68fbd7f3 | |||
| 205a041758 | |||
| 62597f70ee | |||
| 32bb141244 | |||
| f28c68438a | |||
| aa90e9eb26 | |||
| 553709ce8d | |||
| 2ded0d36b1 | |||
| a885eacc70 | |||
| 9e63401db3 | |||
| 2f14e70721 | |||
| 614d14e399 | |||
| c08ddfd810 | |||
| 4bdb82b1bc | |||
| b4d302caf6 | |||
| 4b2694ee0d | |||
| 4c36144b0b | |||
| e88dea40fb | |||
| 23b9cfe8a3 | |||
| 518fe74e96 | |||
| 27aabdceba | |||
| c019d7523b | |||
| 03c3c6b22f | |||
| 69d1027f01 | |||
| 873df67d1f | |||
| a9ef386883 | |||
| 24cfda82d7 | |||
| 655af5c446 | |||
| b363a20a9d | |||
| f5d326124d | |||
| cb8fb2c0bc | |||
| 38119435f1 | |||
| 034a69c12f | |||
| 70006a3d7b | |||
| 8aada58d64 | |||
| 5d231207cd | |||
| 584d9426e6 | |||
| 7a772a5458 | |||
| 079c733b60 | |||
| e24a8e9088 | |||
| 0b5d777077 | |||
| 75327286ef | |||
| 099b0cd849 | |||
| c9c00e0072 | |||
| b2ffb2062a | |||
| 2e8c825fff | |||
| 7b870950d1 | |||
| 3a877c41a4 | |||
| 04fb390bee | |||
| 50b17d3266 | |||
| 763d408405 | |||
| fbb7cd9bc1 | |||
| f1b7f70313 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
/itctl
|
/itctl
|
||||||
/itd
|
/itd
|
||||||
/itgui
|
/itgui
|
||||||
|
/version.txt
|
||||||
8
Makefile
8
Makefile
@@ -3,13 +3,14 @@ BIN_PREFIX = $(DESTDIR)$(PREFIX)/bin
|
|||||||
SERVICE_PREFIX = $(DESTDIR)$(PREFIX)/lib/systemd/user
|
SERVICE_PREFIX = $(DESTDIR)$(PREFIX)/lib/systemd/user
|
||||||
CFG_PREFIX = $(DESTDIR)/etc
|
CFG_PREFIX = $(DESTDIR)/etc
|
||||||
|
|
||||||
all:
|
all: version
|
||||||
go build $(GOFLAGS)
|
go build $(GOFLAGS)
|
||||||
go build ./cmd/itctl $(GOFLAGS)
|
go build ./cmd/itctl $(GOFLAGS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f itctl
|
rm -f itctl
|
||||||
rm -f itd
|
rm -f itd
|
||||||
|
printf "unknown" > version.txt
|
||||||
|
|
||||||
install:
|
install:
|
||||||
install -Dm755 ./itd $(BIN_PREFIX)/itd
|
install -Dm755 ./itd $(BIN_PREFIX)/itd
|
||||||
@@ -23,4 +24,7 @@ uninstall:
|
|||||||
rm $(SERVICE_PREFIX)/itd.service
|
rm $(SERVICE_PREFIX)/itd.service
|
||||||
rm $(CFG_PREFIX)/itd.toml
|
rm $(CFG_PREFIX)/itd.toml
|
||||||
|
|
||||||
.PHONY: all clean install uninstall
|
version:
|
||||||
|
printf "r%s.%s" "$(shell git rev-list --count HEAD)" "$(shell git rev-parse --short HEAD)" > version.txt
|
||||||
|
|
||||||
|
.PHONY: all clean install uninstall version
|
||||||
15
README.md
15
README.md
@@ -55,11 +55,12 @@ Since the PineTime does not have enough space to store all unicode glyphs, it on
|
|||||||
- Lithuanian
|
- Lithuanian
|
||||||
- Estonian
|
- Estonian
|
||||||
- Icelandic
|
- Icelandic
|
||||||
- Czeck
|
- Czech
|
||||||
- French
|
- French
|
||||||
- Armenian
|
- Armenian
|
||||||
- Korean
|
- Korean
|
||||||
- Chinese
|
- Chinese
|
||||||
|
- Romanian
|
||||||
- Emoji
|
- Emoji
|
||||||
|
|
||||||
Place the desired map names in an array as `notifs.translit.use`. They will be evaluated in order. You can also put custom transliterations in `notifs.translit.custom`. These take priority over any other maps. The `notifs.translit` config section should look like this:
|
Place the desired map names in an array as `notifs.translit.use`. They will be evaluated in order. You can also put custom transliterations in `notifs.translit.custom`. These take priority over any other maps. The `notifs.translit` config section should look like this:
|
||||||
@@ -177,7 +178,7 @@ To cross compile, simply set the go environment variables. For example, for Pine
|
|||||||
make GOOS=linux GOARCH=arm64
|
make GOOS=linux GOARCH=arm64
|
||||||
```
|
```
|
||||||
|
|
||||||
This will compile `itd` and `itctl` for Linux aarch64 which is what runs on the PinePhone. This daemon only runs on Linux due to the library's dependencies (`dbus`, `bluez`, and `playerctl` specifically).
|
This will compile `itd` and `itctl` for Linux aarch64 which is what runs on the PinePhone. This daemon only runs on Linux due to the library's dependencies (`dbus`, and `bluez` specifically).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -185,4 +186,12 @@ This will compile `itd` and `itctl` for Linux aarch64 which is what runs on the
|
|||||||
|
|
||||||
This daemon places a config file at `/etc/itd.toml`. This is the global config. `itd` will also look for a config at `~/.config/itd.toml`.
|
This daemon places a config file at `/etc/itd.toml`. This is the global config. `itd` will also look for a config at `~/.config/itd.toml`.
|
||||||
|
|
||||||
Most of the time, the daemon does not need to be restarted for config changes to take effect.
|
Most of the time, the daemon does not need to be restarted for config changes to take effect.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Attribution
|
||||||
|
|
||||||
|
Location data from OpenStreetMap Nominatim, © [OpenStreetMap](https://www.openstreetmap.org/copyright) contributors
|
||||||
|
|
||||||
|
Weather data from the [Norwegian Meteorological Institute](https://www.met.no/en)
|
||||||
117
api/api.go
Normal file
117
api/api.go
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/smallnest/rpcx/client"
|
||||||
|
"github.com/smallnest/rpcx/protocol"
|
||||||
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
|
"go.arsenm.dev/infinitime"
|
||||||
|
)
|
||||||
|
|
||||||
|
const DefaultAddr = "/tmp/itd/socket"
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
itdClient client.XClient
|
||||||
|
itdCh chan *protocol.Message
|
||||||
|
fsClient client.XClient
|
||||||
|
fsCh chan *protocol.Message
|
||||||
|
srvVals map[string]chan interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(sockPath string) (*Client, error) {
|
||||||
|
d, err := client.NewPeer2PeerDiscovery("unix@"+sockPath, "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
out := &Client{}
|
||||||
|
|
||||||
|
out.itdCh = make(chan *protocol.Message, 5)
|
||||||
|
out.itdClient = client.NewBidirectionalXClient(
|
||||||
|
"ITD",
|
||||||
|
client.Failtry,
|
||||||
|
client.RandomSelect,
|
||||||
|
d,
|
||||||
|
client.DefaultOption,
|
||||||
|
out.itdCh,
|
||||||
|
)
|
||||||
|
|
||||||
|
out.fsCh = make(chan *protocol.Message, 5)
|
||||||
|
out.fsClient = client.NewBidirectionalXClient(
|
||||||
|
"FS",
|
||||||
|
client.Failtry,
|
||||||
|
client.RandomSelect,
|
||||||
|
d,
|
||||||
|
client.DefaultOption,
|
||||||
|
out.fsCh,
|
||||||
|
)
|
||||||
|
|
||||||
|
out.srvVals = map[string]chan interface{}{}
|
||||||
|
|
||||||
|
go out.handleMessages(out.itdCh)
|
||||||
|
go out.handleMessages(out.fsCh)
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) handleMessages(msgCh chan *protocol.Message) {
|
||||||
|
for msg := range msgCh {
|
||||||
|
_, ok := c.srvVals[msg.ServicePath]
|
||||||
|
if !ok {
|
||||||
|
c.srvVals[msg.ServicePath] = make(chan interface{}, 5)
|
||||||
|
}
|
||||||
|
|
||||||
|
//fmt.Printf("%+v\n", msg)
|
||||||
|
|
||||||
|
ch := c.srvVals[msg.ServicePath]
|
||||||
|
|
||||||
|
switch msg.ServiceMethod {
|
||||||
|
case "FSProgress":
|
||||||
|
var progress FSTransferProgress
|
||||||
|
msgpack.Unmarshal(msg.Payload, &progress)
|
||||||
|
ch <- progress
|
||||||
|
case "DFUProgress":
|
||||||
|
var progress infinitime.DFUProgress
|
||||||
|
msgpack.Unmarshal(msg.Payload, &progress)
|
||||||
|
ch <- progress
|
||||||
|
case "MotionSample":
|
||||||
|
var motionVals infinitime.MotionValues
|
||||||
|
msgpack.Unmarshal(msg.Payload, &motionVals)
|
||||||
|
ch <- motionVals
|
||||||
|
case "Done":
|
||||||
|
close(c.srvVals[msg.ServicePath])
|
||||||
|
delete(c.srvVals, msg.ServicePath)
|
||||||
|
default:
|
||||||
|
var value interface{}
|
||||||
|
msgpack.Unmarshal(msg.Payload, &value)
|
||||||
|
ch <- value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) done(id string) error {
|
||||||
|
return c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Done",
|
||||||
|
id,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
err := c.itdClient.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.fsClient.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
close(c.itdCh)
|
||||||
|
close(c.fsCh)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
133
api/client.go
133
api/client.go
@@ -1,133 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
"go.arsenm.dev/infinitime"
|
|
||||||
"go.arsenm.dev/itd/internal/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Default socket address
|
|
||||||
const DefaultAddr = "/tmp/itd/socket"
|
|
||||||
|
|
||||||
// Client is the socket API client
|
|
||||||
type Client struct {
|
|
||||||
conn net.Conn
|
|
||||||
respCh chan types.Response
|
|
||||||
heartRateCh chan types.Response
|
|
||||||
battLevelCh chan types.Response
|
|
||||||
stepCountCh chan types.Response
|
|
||||||
motionCh chan types.Response
|
|
||||||
dfuProgressCh chan types.Response
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a new client and sets it up
|
|
||||||
func New(addr string) (*Client, error) {
|
|
||||||
conn, err := net.Dial("unix", addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
out := &Client{
|
|
||||||
conn: conn,
|
|
||||||
respCh: make(chan types.Response, 5),
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
scanner := bufio.NewScanner(conn)
|
|
||||||
for scanner.Scan() {
|
|
||||||
var res types.Response
|
|
||||||
err = json.Unmarshal(scanner.Bytes(), &res)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
out.handleResp(res)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Close() error {
|
|
||||||
err := c.conn.Close()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
close(c.respCh)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// request sends a request to itd and waits for and returns the response
|
|
||||||
func (c *Client) request(req types.Request) (types.Response, error) {
|
|
||||||
// Encode request into connection
|
|
||||||
err := json.NewEncoder(c.conn).Encode(req)
|
|
||||||
if err != nil {
|
|
||||||
return types.Response{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res := <-c.respCh
|
|
||||||
|
|
||||||
if res.Error {
|
|
||||||
return res, errors.New(res.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// requestNoRes sends a request to itd and does not wait for the response
|
|
||||||
func (c *Client) requestNoRes(req types.Request) error {
|
|
||||||
// Encode request into connection
|
|
||||||
err := json.NewEncoder(c.conn).Encode(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleResp handles the received response as needed
|
|
||||||
func (c *Client) handleResp(res types.Response) error {
|
|
||||||
switch res.Type {
|
|
||||||
case types.ReqTypeWatchHeartRate:
|
|
||||||
c.heartRateCh <- res
|
|
||||||
case types.ReqTypeWatchBattLevel:
|
|
||||||
c.battLevelCh <- res
|
|
||||||
case types.ReqTypeWatchStepCount:
|
|
||||||
c.stepCountCh <- res
|
|
||||||
case types.ReqTypeWatchMotion:
|
|
||||||
c.motionCh <- res
|
|
||||||
case types.ReqTypeFwUpgrade:
|
|
||||||
c.dfuProgressCh <- res
|
|
||||||
default:
|
|
||||||
c.respCh <- res
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeUint8(val interface{}) uint8 {
|
|
||||||
return uint8(val.(float64))
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeUint32(val interface{}) uint32 {
|
|
||||||
return uint32(val.(float64))
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeMotion(val interface{}) (infinitime.MotionValues, error) {
|
|
||||||
out := infinitime.MotionValues{}
|
|
||||||
err := mapstructure.Decode(val, &out)
|
|
||||||
if err != nil {
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeDFUProgress(val interface{}) (DFUProgress, error) {
|
|
||||||
out := DFUProgress{}
|
|
||||||
err := mapstructure.Decode(val, &out)
|
|
||||||
if err != nil {
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
40
api/firmware.go
Normal file
40
api/firmware.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go.arsenm.dev/infinitime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) FirmwareUpgrade(upgType UpgradeType, files ...string) (chan infinitime.DFUProgress, error) {
|
||||||
|
var id string
|
||||||
|
err := c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"FirmwareUpgrade",
|
||||||
|
FwUpgradeData{
|
||||||
|
Type: upgType,
|
||||||
|
Files: files,
|
||||||
|
},
|
||||||
|
&id,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
progressCh := make(chan infinitime.DFUProgress, 5)
|
||||||
|
go func() {
|
||||||
|
srvValCh, ok := c.srvVals[id]
|
||||||
|
for !ok {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
srvValCh, ok = c.srvVals[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
for val := range srvValCh {
|
||||||
|
progressCh <- val.(infinitime.DFUProgress)
|
||||||
|
}
|
||||||
|
close(progressCh)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return progressCh, nil
|
||||||
|
}
|
||||||
101
api/fs.go
Normal file
101
api/fs.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) Remove(paths ...string) error {
|
||||||
|
return c.fsClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Remove",
|
||||||
|
paths,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Rename(old, new string) error {
|
||||||
|
return c.fsClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Remove",
|
||||||
|
[2]string{old, new},
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Mkdir(paths ...string) error {
|
||||||
|
return c.fsClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Mkdir",
|
||||||
|
paths,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ReadDir(dir string) (out []FileInfo, err error) {
|
||||||
|
err = c.fsClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"ReadDir",
|
||||||
|
dir,
|
||||||
|
&out,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Upload(dst, src string) (chan FSTransferProgress, error) {
|
||||||
|
var id string
|
||||||
|
err := c.fsClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Upload",
|
||||||
|
[2]string{dst, src},
|
||||||
|
&id,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
progressCh := make(chan FSTransferProgress, 5)
|
||||||
|
go func() {
|
||||||
|
srvValCh, ok := c.srvVals[id]
|
||||||
|
for !ok {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
srvValCh, ok = c.srvVals[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
for val := range srvValCh {
|
||||||
|
progressCh <- val.(FSTransferProgress)
|
||||||
|
}
|
||||||
|
close(progressCh)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return progressCh, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Download(dst, src string) (chan FSTransferProgress, error) {
|
||||||
|
var id string
|
||||||
|
err := c.fsClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Download",
|
||||||
|
[2]string{dst, src},
|
||||||
|
&id,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
progressCh := make(chan FSTransferProgress, 5)
|
||||||
|
go func() {
|
||||||
|
srvValCh, ok := c.srvVals[id]
|
||||||
|
for !ok {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
srvValCh, ok = c.srvVals[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
for val := range srvValCh {
|
||||||
|
progressCh <- val.(FSTransferProgress)
|
||||||
|
}
|
||||||
|
close(progressCh)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return progressCh, nil
|
||||||
|
}
|
||||||
67
api/get.go
Normal file
67
api/get.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"go.arsenm.dev/infinitime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) HeartRate() (out uint8, err error) {
|
||||||
|
err = c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"HeartRate",
|
||||||
|
nil,
|
||||||
|
&out,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) BatteryLevel() (out uint8, err error) {
|
||||||
|
err = c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"BatteryLevel",
|
||||||
|
nil,
|
||||||
|
&out,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Motion() (out infinitime.MotionValues, err error) {
|
||||||
|
err = c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Motion",
|
||||||
|
nil,
|
||||||
|
&out,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) StepCount() (out uint32, err error) {
|
||||||
|
err = c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"StepCount",
|
||||||
|
nil,
|
||||||
|
&out,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Version() (out string, err error) {
|
||||||
|
err = c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Version",
|
||||||
|
nil,
|
||||||
|
&out,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Address() (out string, err error) {
|
||||||
|
err = c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"Address",
|
||||||
|
nil,
|
||||||
|
&out,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
209
api/info.go
209
api/info.go
@@ -1,209 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
"go.arsenm.dev/infinitime"
|
|
||||||
"go.arsenm.dev/itd/internal/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Address gets the bluetooth address of the connected device
|
|
||||||
func (c *Client) Address() (string, error) {
|
|
||||||
res, err := c.request(types.Request{
|
|
||||||
Type: types.ReqTypeBtAddress,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.Value.(string), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Version gets the firmware version of the connected device
|
|
||||||
func (c *Client) Version() (string, error) {
|
|
||||||
res, err := c.request(types.Request{
|
|
||||||
Type: types.ReqTypeFwVersion,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.Value.(string), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BatteryLevel gets the battery level of the connected device
|
|
||||||
func (c *Client) BatteryLevel() (uint8, error) {
|
|
||||||
res, err := c.request(types.Request{
|
|
||||||
Type: types.ReqTypeBattLevel,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return uint8(res.Value.(float64)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WatchBatteryLevel returns a channel which will contain
|
|
||||||
// new battery level values as they update. Do not use after
|
|
||||||
// calling cancellation function
|
|
||||||
func (c *Client) WatchBatteryLevel() (<-chan uint8, func(), error) {
|
|
||||||
c.battLevelCh = make(chan types.Response, 2)
|
|
||||||
err := c.requestNoRes(types.Request{
|
|
||||||
Type: types.ReqTypeWatchBattLevel,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
res := <-c.battLevelCh
|
|
||||||
done, cancel := c.cancelFn(res.ID, c.battLevelCh)
|
|
||||||
out := make(chan uint8, 2)
|
|
||||||
go func() {
|
|
||||||
for res := range c.battLevelCh {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
out <- decodeUint8(res.Value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return out, cancel, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HeartRate gets the heart rate from the connected device
|
|
||||||
func (c *Client) HeartRate() (uint8, error) {
|
|
||||||
res, err := c.request(types.Request{
|
|
||||||
Type: types.ReqTypeHeartRate,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return decodeUint8(res.Value), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WatchHeartRate returns a channel which will contain
|
|
||||||
// new heart rate values as they update. Do not use after
|
|
||||||
// calling cancellation function
|
|
||||||
func (c *Client) WatchHeartRate() (<-chan uint8, func(), error) {
|
|
||||||
c.heartRateCh = make(chan types.Response, 2)
|
|
||||||
err := c.requestNoRes(types.Request{
|
|
||||||
Type: types.ReqTypeWatchHeartRate,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
res := <-c.heartRateCh
|
|
||||||
done, cancel := c.cancelFn(res.ID, c.heartRateCh)
|
|
||||||
out := make(chan uint8, 2)
|
|
||||||
go func() {
|
|
||||||
for res := range c.heartRateCh {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
out <- decodeUint8(res.Value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return out, cancel, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// cancelFn generates a cancellation function for the given
|
|
||||||
// request type and channel
|
|
||||||
func (c *Client) cancelFn(reqID string, ch chan types.Response) (chan struct{}, func()) {
|
|
||||||
done := make(chan struct{}, 1)
|
|
||||||
return done, func() {
|
|
||||||
done <- struct{}{}
|
|
||||||
close(ch)
|
|
||||||
c.requestNoRes(types.Request{
|
|
||||||
Type: types.ReqTypeCancel,
|
|
||||||
Data: reqID,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// StepCount gets the step count from the connected device
|
|
||||||
func (c *Client) StepCount() (uint32, error) {
|
|
||||||
res, err := c.request(types.Request{
|
|
||||||
Type: types.ReqTypeStepCount,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return uint32(res.Value.(float64)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WatchStepCount returns a channel which will contain
|
|
||||||
// new step count values as they update. Do not use after
|
|
||||||
// calling cancellation function
|
|
||||||
func (c *Client) WatchStepCount() (<-chan uint32, func(), error) {
|
|
||||||
c.stepCountCh = make(chan types.Response, 2)
|
|
||||||
err := c.requestNoRes(types.Request{
|
|
||||||
Type: types.ReqTypeWatchStepCount,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
res := <-c.stepCountCh
|
|
||||||
done, cancel := c.cancelFn(res.ID, c.stepCountCh)
|
|
||||||
out := make(chan uint32, 2)
|
|
||||||
go func() {
|
|
||||||
for res := range c.stepCountCh {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
out <- decodeUint32(res.Value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return out, cancel, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Motion gets the motion values from the connected device
|
|
||||||
func (c *Client) Motion() (infinitime.MotionValues, error) {
|
|
||||||
out := infinitime.MotionValues{}
|
|
||||||
res, err := c.request(types.Request{
|
|
||||||
Type: types.ReqTypeMotion,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
err = mapstructure.Decode(res.Value, &out)
|
|
||||||
if err != nil {
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WatchMotion returns a channel which will contain
|
|
||||||
// new motion values as they update. Do not use after
|
|
||||||
// calling cancellation function
|
|
||||||
func (c *Client) WatchMotion() (<-chan infinitime.MotionValues, func(), error) {
|
|
||||||
c.motionCh = make(chan types.Response, 5)
|
|
||||||
err := c.requestNoRes(types.Request{
|
|
||||||
Type: types.ReqTypeWatchMotion,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
res := <-c.motionCh
|
|
||||||
done, cancel := c.cancelFn(res.ID, c.motionCh)
|
|
||||||
out := make(chan infinitime.MotionValues, 5)
|
|
||||||
go func() {
|
|
||||||
for res := range c.motionCh {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
motion, err := decodeMotion(res.Value)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
out <- motion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return out, cancel, nil
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,17 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import "go.arsenm.dev/itd/internal/types"
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
func (c *Client) Notify(title string, body string) error {
|
func (c *Client) Notify(title, body string) error {
|
||||||
_, err := c.request(types.Request{
|
return c.itdClient.Call(
|
||||||
Type: types.ReqTypeNotify,
|
context.Background(),
|
||||||
Data: types.ReqDataNotify{
|
"Notify",
|
||||||
|
NotifyData{
|
||||||
Title: title,
|
Title: title,
|
||||||
Body: body,
|
Body: body,
|
||||||
},
|
},
|
||||||
})
|
nil,
|
||||||
return err
|
)
|
||||||
}
|
}
|
||||||
15
api/set.go
Normal file
15
api/set.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) SetTime(t time.Time) error {
|
||||||
|
return c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"SetTime",
|
||||||
|
t,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
}
|
||||||
33
api/time.go
33
api/time.go
@@ -1,33 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.arsenm.dev/itd/internal/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SetTime sets the given time on the connected device
|
|
||||||
func (c *Client) SetTime(t time.Time) error {
|
|
||||||
_, err := c.request(types.Request{
|
|
||||||
Type: types.ReqTypeSetTime,
|
|
||||||
Data: t.Format(time.RFC3339),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetTimeNow sets the time on the connected device to
|
|
||||||
// the current time. This is more accurate than
|
|
||||||
// SetTime(time.Now()) due to RFC3339 formatting
|
|
||||||
func (c *Client) SetTimeNow() error {
|
|
||||||
_, err := c.request(types.Request{
|
|
||||||
Type: types.ReqTypeSetTime,
|
|
||||||
Data: "now",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
96
api/types.go
Normal file
96
api/types.go
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpgradeType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
UpgradeTypeArchive UpgradeType = iota
|
||||||
|
UpgradeTypeFiles
|
||||||
|
)
|
||||||
|
|
||||||
|
type FSData struct {
|
||||||
|
Files []string
|
||||||
|
Data string
|
||||||
|
}
|
||||||
|
|
||||||
|
type FwUpgradeData struct {
|
||||||
|
Type UpgradeType
|
||||||
|
Files []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type NotifyData struct {
|
||||||
|
Title string
|
||||||
|
Body string
|
||||||
|
}
|
||||||
|
|
||||||
|
type FSTransferProgress struct {
|
||||||
|
Total uint32
|
||||||
|
Sent uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileInfo struct {
|
||||||
|
Name string
|
||||||
|
Size int64
|
||||||
|
IsDir bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fi FileInfo) String() string {
|
||||||
|
var isDirChar rune
|
||||||
|
if fi.IsDir {
|
||||||
|
isDirChar = 'd'
|
||||||
|
} else {
|
||||||
|
isDirChar = '-'
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get human-readable value for file size
|
||||||
|
val, unit := bytesHuman(fi.Size)
|
||||||
|
prec := 0
|
||||||
|
// If value is less than 10, set precision to 1
|
||||||
|
if val < 10 {
|
||||||
|
prec = 1
|
||||||
|
}
|
||||||
|
// Convert float to string
|
||||||
|
valStr := strconv.FormatFloat(val, 'f', prec, 64)
|
||||||
|
|
||||||
|
// Return string formatted like so:
|
||||||
|
// - 10 kB file
|
||||||
|
// or:
|
||||||
|
// d 0 B .
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"%c %3s %-2s %s",
|
||||||
|
isDirChar,
|
||||||
|
valStr,
|
||||||
|
unit,
|
||||||
|
fi.Name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// bytesHuman returns a human-readable string for
|
||||||
|
// the amount of bytes inputted.
|
||||||
|
func bytesHuman(b int64) (float64, string) {
|
||||||
|
const unit = 1000
|
||||||
|
// Set possible units prefixes (PineTime flash is 4MB)
|
||||||
|
units := [2]rune{'k', 'M'}
|
||||||
|
// If amount of bytes is less than smallest unit
|
||||||
|
if b < unit {
|
||||||
|
// Return unchanged with unit "B"
|
||||||
|
return float64(b), "B"
|
||||||
|
}
|
||||||
|
|
||||||
|
div, exp := int64(unit), 0
|
||||||
|
// Get decimal values and unit prefix index
|
||||||
|
for n := b / unit; n >= unit; n /= unit {
|
||||||
|
div *= unit
|
||||||
|
exp++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create string for full unit
|
||||||
|
unitStr := string([]rune{units[exp], 'B'})
|
||||||
|
|
||||||
|
// Return decimal with unit string
|
||||||
|
return float64(b) / float64(div), unitStr
|
||||||
|
}
|
||||||
12
api/update.go
Normal file
12
api/update.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
func (c *Client) WeatherUpdate() error {
|
||||||
|
return c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"WeatherUpdate",
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"go.arsenm.dev/itd/internal/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DFUProgress stores the progress of a DFU upfate
|
|
||||||
type DFUProgress types.DFUProgress
|
|
||||||
|
|
||||||
// UpgradeType indicates the type of upgrade to be performed
|
|
||||||
type UpgradeType uint8
|
|
||||||
|
|
||||||
// Type of DFU upgrade
|
|
||||||
const (
|
|
||||||
UpgradeTypeArchive UpgradeType = iota
|
|
||||||
UpgradeTypeFiles
|
|
||||||
)
|
|
||||||
|
|
||||||
// FirmwareUpgrade initiates a DFU update and returns the progress channel
|
|
||||||
func (c *Client) FirmwareUpgrade(upgType UpgradeType, files ...string) (<-chan DFUProgress, error) {
|
|
||||||
err := json.NewEncoder(c.conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeFwUpgrade,
|
|
||||||
Data: types.ReqDataFwUpgrade{
|
|
||||||
Type: int(upgType),
|
|
||||||
Files: files,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.dfuProgressCh = make(chan types.Response, 5)
|
|
||||||
|
|
||||||
out := make(chan DFUProgress, 5)
|
|
||||||
go func() {
|
|
||||||
for res := range c.dfuProgressCh {
|
|
||||||
progress, err := decodeDFUProgress(res.Value)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
out <- progress
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
144
api/watch.go
Normal file
144
api/watch.go
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go.arsenm.dev/infinitime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) WatchHeartRate() (<-chan uint8, func(), error) {
|
||||||
|
var id string
|
||||||
|
err := c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"WatchHeartRate",
|
||||||
|
nil,
|
||||||
|
&id,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
outCh := make(chan uint8, 2)
|
||||||
|
go func() {
|
||||||
|
srvValCh, ok := c.srvVals[id]
|
||||||
|
for !ok {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
srvValCh, ok = c.srvVals[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
for val := range srvValCh {
|
||||||
|
outCh <- val.(uint8)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
doneFn := func() {
|
||||||
|
c.done(id)
|
||||||
|
close(c.srvVals[id])
|
||||||
|
delete(c.srvVals, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return outCh, doneFn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) WatchBatteryLevel() (<-chan uint8, func(), error) {
|
||||||
|
var id string
|
||||||
|
err := c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"WatchBatteryLevel",
|
||||||
|
nil,
|
||||||
|
&id,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
outCh := make(chan uint8, 2)
|
||||||
|
go func() {
|
||||||
|
srvValCh, ok := c.srvVals[id]
|
||||||
|
for !ok {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
srvValCh, ok = c.srvVals[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
for val := range srvValCh {
|
||||||
|
outCh <- val.(uint8)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
doneFn := func() {
|
||||||
|
c.done(id)
|
||||||
|
close(c.srvVals[id])
|
||||||
|
delete(c.srvVals, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return outCh, doneFn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) WatchStepCount() (<-chan uint32, func(), error) {
|
||||||
|
var id string
|
||||||
|
err := c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"WatchStepCount",
|
||||||
|
nil,
|
||||||
|
&id,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
outCh := make(chan uint32, 2)
|
||||||
|
go func() {
|
||||||
|
srvValCh, ok := c.srvVals[id]
|
||||||
|
for !ok {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
srvValCh, ok = c.srvVals[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
for val := range srvValCh {
|
||||||
|
outCh <- val.(uint32)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
doneFn := func() {
|
||||||
|
c.done(id)
|
||||||
|
close(c.srvVals[id])
|
||||||
|
delete(c.srvVals, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return outCh, doneFn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) WatchMotion() (<-chan infinitime.MotionValues, func(), error) {
|
||||||
|
var id string
|
||||||
|
err := c.itdClient.Call(
|
||||||
|
context.Background(),
|
||||||
|
"WatchMotion",
|
||||||
|
nil,
|
||||||
|
&id,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
outCh := make(chan infinitime.MotionValues, 2)
|
||||||
|
go func() {
|
||||||
|
srvValCh, ok := c.srvVals[id]
|
||||||
|
for !ok {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
srvValCh, ok = c.srvVals[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
for val := range srvValCh {
|
||||||
|
outCh <- val.(infinitime.MotionValues)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
doneFn := func() {
|
||||||
|
c.done(id)
|
||||||
|
close(c.srvVals[id])
|
||||||
|
delete(c.srvVals, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return outCh, doneFn, nil
|
||||||
|
}
|
||||||
224
calls.go
224
calls.go
@@ -1,85 +1,90 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"sync"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"os/exec"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"github.com/godbus/dbus/v5"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"go.arsenm.dev/infinitime"
|
"gitea.arsenm.dev/Arsen6331/infinitime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initCallNotifs(dev *infinitime.Device) error {
|
func initCallNotifs(dev *infinitime.Device) error {
|
||||||
// Define rule to filter dbus messages
|
// Connect to system bus. This connection is for method calls.
|
||||||
rule := "type='signal',sender='org.freedesktop.ModemManager1',interface='org.freedesktop.ModemManager1.Modem.Voice',member='CallAdded'"
|
conn, err := newSystemBusConn()
|
||||||
|
|
||||||
// Use dbus-monitor command with profiling output as a workaround
|
|
||||||
// because go-bluetooth seems to monopolize the system bus connection
|
|
||||||
// which makes monitoring show only bluez-related messages.
|
|
||||||
cmd := exec.Command("dbus-monitor", "--system", "--profile", rule)
|
|
||||||
// Get command output pipe
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Run command asynchronously
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new scanner for command output
|
// Check if modem manager interface exists
|
||||||
scanner := bufio.NewScanner(stdout)
|
exists, err := modemManagerExists(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it does not exist, stop function
|
||||||
|
if !exists {
|
||||||
|
conn.Close()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to system bus. This connection is for monitoring.
|
||||||
|
monitorConn, err := newSystemBusConn()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add match for new calls to monitor connection
|
||||||
|
err = monitorConn.AddMatchSignal(
|
||||||
|
dbus.WithMatchSender("org.freedesktop.ModemManager1"),
|
||||||
|
dbus.WithMatchInterface("org.freedesktop.ModemManager1.Modem.Voice"),
|
||||||
|
dbus.WithMatchMember("CallAdded"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create channel to receive calls
|
||||||
|
callCh := make(chan *dbus.Message, 5)
|
||||||
|
// Notify channel upon received message
|
||||||
|
monitorConn.Eavesdrop(callCh)
|
||||||
|
|
||||||
|
var respHandlerOnce sync.Once
|
||||||
|
var callObj dbus.BusObject
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// For each line in output
|
// For every message received
|
||||||
for scanner.Scan() {
|
for event := range callCh {
|
||||||
// Get line as string
|
// Get path to call object
|
||||||
text := scanner.Text()
|
callPath := event.Body[0].(dbus.ObjectPath)
|
||||||
|
// Get call object
|
||||||
|
callObj = conn.Object("org.freedesktop.ModemManager1", callPath)
|
||||||
|
|
||||||
// If line starts with "#", it is part of
|
// Get phone number from call object using method call connection
|
||||||
// the field format, skip it.
|
phoneNum, err := getPhoneNum(conn, callObj)
|
||||||
if strings.HasPrefix(text, "#") {
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error getting phone number")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split line into fields. The order is as follows:
|
// Send call notification to InfiniTime
|
||||||
// type timestamp serial sender destination path interface member
|
resCh, err := dev.NotifyCall(phoneNum)
|
||||||
fields := strings.Fields(text)
|
if err != nil {
|
||||||
// Field 7 is Member. Make sure it is "CallAdded".
|
continue
|
||||||
if fields[7] == "CallAdded" {
|
}
|
||||||
// Get Modem ID from modem path
|
|
||||||
modemID := parseModemID(fields[5])
|
go respHandlerOnce.Do(func() {
|
||||||
// Get call ID of current call
|
// Wait for PineTime response
|
||||||
callID, err := getCurrentCallID(modemID)
|
for res := range resCh {
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// Get phone number of current call
|
|
||||||
phoneNum, err := getPhoneNum(callID)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// Send call notification to PineTime
|
|
||||||
resCh, err := dev.NotifyCall(phoneNum)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
// Wait for PineTime response
|
|
||||||
res := <-resCh
|
|
||||||
switch res {
|
switch res {
|
||||||
case infinitime.CallStatusAccepted:
|
case infinitime.CallStatusAccepted:
|
||||||
// Attempt to accept call
|
// Attempt to accept call
|
||||||
err = acceptCall(callID)
|
err = acceptCall(conn, callObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn().Err(err).Msg("Error accepting call")
|
log.Warn().Err(err).Msg("Error accepting call")
|
||||||
}
|
}
|
||||||
case infinitime.CallStatusDeclined:
|
case infinitime.CallStatusDeclined:
|
||||||
// Attempt to decline call
|
// Attempt to decline call
|
||||||
err = declineCall(callID)
|
err = declineCall(conn, callObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn().Err(err).Msg("Error declining call")
|
log.Warn().Err(err).Msg("Error declining call")
|
||||||
}
|
}
|
||||||
@@ -87,90 +92,51 @@ func initCallNotifs(dev *infinitime.Device) error {
|
|||||||
// Warn about unimplemented muting
|
// Warn about unimplemented muting
|
||||||
log.Warn().Msg("Muting calls is not implemented")
|
log.Warn().Msg("Muting calls is not implemented")
|
||||||
}
|
}
|
||||||
}()
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
log.Info().Msg("Relaying calls to InfiniTime")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseModemID(modemPath string) int {
|
func modemManagerExists(conn *dbus.Conn) (bool, error) {
|
||||||
// Split path by "/"
|
var names []string
|
||||||
splitPath := strings.Split(modemPath, "/")
|
err := conn.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&names)
|
||||||
// Get last element and convert to integer
|
if err != nil {
|
||||||
id, _ := strconv.Atoi(splitPath[len(splitPath)-1])
|
return false, err
|
||||||
return id
|
}
|
||||||
|
return strSlcContains(names, "org.freedesktop.ModemManager1"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCurrentCallID(modemID int) (int, error) {
|
// getPhoneNum gets a phone number from a call object using a DBus connection
|
||||||
// Create mmcli command
|
func getPhoneNum(conn *dbus.Conn, callObj dbus.BusObject) (string, error) {
|
||||||
cmd := exec.Command("mmcli", "--voice-list-calls", "-m", fmt.Sprint(modemID), "-J")
|
var out string
|
||||||
// Run command and get output
|
// Get number property on DBus object and store return value in out
|
||||||
data, err := cmd.Output()
|
err := callObj.StoreProperty("org.freedesktop.ModemManager1.Call.Number", &out)
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
var calls map[string][]string
|
|
||||||
// Decode JSON from command output
|
|
||||||
err = json.Unmarshal(data, &calls)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
// Get first call in output
|
|
||||||
firstCall := calls["modem.voice.call"][0]
|
|
||||||
// Split path by "/"
|
|
||||||
splitCall := strings.Split(firstCall, "/")
|
|
||||||
// Return last element converted to integer
|
|
||||||
return strconv.Atoi(splitCall[len(splitCall)-1])
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPhoneNum(callID int) (string, error) {
|
|
||||||
// Create dbus-send command
|
|
||||||
cmd := exec.Command("dbus-send",
|
|
||||||
"--dest=org.freedesktop.ModemManager1",
|
|
||||||
"--system",
|
|
||||||
"--print-reply=literal",
|
|
||||||
"--type=method_call",
|
|
||||||
fmt.Sprintf("/org/freedesktop/ModemManager1/Call/%d", callID),
|
|
||||||
"org.freedesktop.DBus.Properties.Get",
|
|
||||||
"string:org.freedesktop.ModemManager1.Call",
|
|
||||||
"string:Number",
|
|
||||||
)
|
|
||||||
// Run command and get output
|
|
||||||
numData, err := cmd.Output()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// Split output into fields
|
return out, nil
|
||||||
num := strings.Fields(string(numData))
|
|
||||||
// Return last field
|
|
||||||
return num[len(num)-1], nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func acceptCall(callID int) error {
|
// getPhoneNum accepts a call using a DBus connection
|
||||||
// Create dbus-send command
|
func acceptCall(conn *dbus.Conn, callObj dbus.BusObject) error {
|
||||||
cmd := exec.Command("dbus-send",
|
// Call Accept() method on DBus object
|
||||||
"--dest=org.freedesktop.ModemManager1",
|
call := callObj.Call("org.freedesktop.ModemManager1.Call.Accept", 0)
|
||||||
"--print-reply",
|
if call.Err != nil {
|
||||||
"--system",
|
return call.Err
|
||||||
"--type=method_call",
|
}
|
||||||
fmt.Sprintf("/org/freedesktop/ModemManager1/Call/%d", callID),
|
return nil
|
||||||
"org.freedesktop.ModemManager1.Call.Accept",
|
|
||||||
)
|
|
||||||
// Run command and return errpr
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func declineCall(callID int) error {
|
// getPhoneNum declines a call using a DBus connection
|
||||||
// Create dbus-send command
|
func declineCall(conn *dbus.Conn, callObj dbus.BusObject) error {
|
||||||
cmd := exec.Command("dbus-send",
|
// Call Hangup() method on DBus object
|
||||||
"--dest=org.freedesktop.ModemManager1",
|
call := callObj.Call("org.freedesktop.ModemManager1.Call.Hangup", 0)
|
||||||
"--print-reply",
|
if call.Err != nil {
|
||||||
"--system",
|
return call.Err
|
||||||
"--type=method_call",
|
}
|
||||||
fmt.Sprintf("/org/freedesktop/ModemManager1/Call/%d", callID),
|
return nil
|
||||||
"org.freedesktop.ModemManager1.Call.Hangup",
|
|
||||||
)
|
|
||||||
// Run command and return errpr
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
}
|
||||||
|
|||||||
79
cmd/itctl/firmware.go
Normal file
79
cmd/itctl/firmware.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/cheggaaa/pb/v3"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"go.arsenm.dev/itd/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func fwUpgrade(c *cli.Context) error {
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
var upgType api.UpgradeType
|
||||||
|
var files []string
|
||||||
|
// Get relevant data struct
|
||||||
|
if c.String("archive") != "" {
|
||||||
|
// Get archive data struct
|
||||||
|
upgType = api.UpgradeTypeArchive
|
||||||
|
files = []string{c.String("archive")}
|
||||||
|
} else if c.String("init-packet") != "" && c.String("firmware") != "" {
|
||||||
|
// Get files data struct
|
||||||
|
upgType = api.UpgradeTypeFiles
|
||||||
|
files = []string{c.String("init-packet"), c.String("firmware")}
|
||||||
|
} else {
|
||||||
|
return cli.Exit("Upgrade command requires either archive or init packet and firmware.", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
progress, err := client.FirmwareUpgrade(upgType, abs(files)...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create progress bar template
|
||||||
|
barTmpl := `{{counters . }} B {{bar . "|" "-" (cycle .) " " "|"}} {{percent . }} {{rtime . "%s"}}`
|
||||||
|
// Start full bar at 0 total
|
||||||
|
bar := pb.ProgressBarTemplate(barTmpl).Start(0)
|
||||||
|
// Create new scanner of connection
|
||||||
|
for event := range progress {
|
||||||
|
// Set total bytes in progress bar
|
||||||
|
bar.SetTotal(event.Total)
|
||||||
|
// Set amount of bytes received in progress bar
|
||||||
|
bar.SetCurrent(int64(event.Received))
|
||||||
|
// If transfer finished, break
|
||||||
|
if int64(event.Sent) == event.Total {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Finish progress bar
|
||||||
|
bar.Finish()
|
||||||
|
|
||||||
|
fmt.Printf("Transferred %d B in %s.\n", bar.Total(), time.Since(start))
|
||||||
|
fmt.Println("Remember to validate the new firmware in the InfiniTime settings.")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fwVersion(c *cli.Context) error {
|
||||||
|
version, err := client.Version()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(version)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func abs(paths []string) []string {
|
||||||
|
for index, path := range paths {
|
||||||
|
newPath, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
paths[index] = newPath
|
||||||
|
}
|
||||||
|
return paths
|
||||||
|
}
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package firmware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"go.arsenm.dev/itd/cmd/itctl/root"
|
|
||||||
)
|
|
||||||
|
|
||||||
// firmwareCmd represents the firmware command
|
|
||||||
var firmwareCmd = &cobra.Command{
|
|
||||||
Use: "firmware",
|
|
||||||
Short: "Manage InfiniTime firmware",
|
|
||||||
Aliases: []string{"fw"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
root.RootCmd.AddCommand(firmwareCmd)
|
|
||||||
}
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package firmware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/cheggaaa/pb/v3"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
"go.arsenm.dev/itd/internal/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DFUProgress struct {
|
|
||||||
Received int64 `mapstructure:"recvd"`
|
|
||||||
Total int64 `mapstructure:"total"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// upgradeCmd represents the upgrade command
|
|
||||||
var upgradeCmd = &cobra.Command{
|
|
||||||
Use: "upgrade",
|
|
||||||
Short: "Upgrade InfiniTime firmware using files or archive",
|
|
||||||
Aliases: []string{"upg"},
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
var upgType api.UpgradeType
|
|
||||||
var files []string
|
|
||||||
// Get relevant data struct
|
|
||||||
if viper.GetString("archive") != "" {
|
|
||||||
// Get archive data struct
|
|
||||||
upgType = types.UpgradeTypeArchive
|
|
||||||
files = []string{viper.GetString("archive")}
|
|
||||||
} else if viper.GetString("initPkt") != "" && viper.GetString("firmware") != "" {
|
|
||||||
// Get files data struct
|
|
||||||
upgType = types.UpgradeTypeFiles
|
|
||||||
files = []string{viper.GetString("initPkt"), viper.GetString("firmware")}
|
|
||||||
} else {
|
|
||||||
cmd.Usage()
|
|
||||||
log.Warn().Msg("Upgrade command requires either archive or init packet and firmware.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
progress, err := client.FirmwareUpgrade(upgType, files...)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error initiating DFU")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create progress bar template
|
|
||||||
barTmpl := `{{counters . }} B {{bar . "|" "-" (cycle .) " " "|"}} {{percent . }} {{rtime . "%s"}}`
|
|
||||||
// Start full bar at 0 total
|
|
||||||
bar := pb.ProgressBarTemplate(barTmpl).Start(0)
|
|
||||||
// Create new scanner of connection
|
|
||||||
for event := range progress {
|
|
||||||
// Set total bytes in progress bar
|
|
||||||
bar.SetTotal(event.Total)
|
|
||||||
// Set amount of bytes received in progress bar
|
|
||||||
bar.SetCurrent(event.Received)
|
|
||||||
// If transfer finished, break
|
|
||||||
if event.Sent == event.Total {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Finish progress bar
|
|
||||||
bar.Finish()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
firmwareCmd.AddCommand(upgradeCmd)
|
|
||||||
|
|
||||||
// Register flags
|
|
||||||
upgradeCmd.Flags().StringP("archive", "a", "", "Path to firmware archive")
|
|
||||||
upgradeCmd.Flags().StringP("init-pkt", "i", "", "Path to init packet (.dat file)")
|
|
||||||
upgradeCmd.Flags().StringP("firmware", "f", "", "Path to firmware image (.bin file)")
|
|
||||||
|
|
||||||
// Bind flags to viper keys
|
|
||||||
viper.BindPFlag("archive", upgradeCmd.Flags().Lookup("archive"))
|
|
||||||
viper.BindPFlag("initPkt", upgradeCmd.Flags().Lookup("init-pkt"))
|
|
||||||
viper.BindPFlag("firmware", upgradeCmd.Flags().Lookup("firmware"))
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package firmware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// versionCmd represents the version command
|
|
||||||
var versionCmd = &cobra.Command{
|
|
||||||
Use: "version",
|
|
||||||
Aliases: []string{"ver"},
|
|
||||||
Short: "Get firmware version of InfiniTime",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
version, err := client.Version()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting firmware version")
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(version)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
firmwareCmd.AddCommand(versionCmd)
|
|
||||||
}
|
|
||||||
165
cmd/itctl/fs.go
Normal file
165
cmd/itctl/fs.go
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/cheggaaa/pb/v3"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func fsList(c *cli.Context) error {
|
||||||
|
dirPath := "/"
|
||||||
|
if c.Args().Len() > 0 {
|
||||||
|
dirPath = c.Args().Get(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
listing, err := client.ReadDir(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range listing {
|
||||||
|
fmt.Println(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fsMkdir(c *cli.Context) error {
|
||||||
|
if c.Args().Len() < 1 {
|
||||||
|
return cli.Exit("Command mkdir requires one or more arguments", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := client.Mkdir(c.Args().Slice()...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fsMove(c *cli.Context) error {
|
||||||
|
if c.Args().Len() != 2 {
|
||||||
|
return cli.Exit("Command move requires two arguments", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := client.Rename(c.Args().Get(0), c.Args().Get(1))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fsRead(c *cli.Context) error {
|
||||||
|
if c.Args().Len() != 2 {
|
||||||
|
return cli.Exit("Command read requires two arguments", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
var tmpFile *os.File
|
||||||
|
var path string
|
||||||
|
var err error
|
||||||
|
if c.Args().Get(1) == "-" {
|
||||||
|
tmpFile, err = ioutil.TempFile("/tmp", "itctl.*")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
path = tmpFile.Name()
|
||||||
|
} else {
|
||||||
|
path, err = filepath.Abs(c.Args().Get(1))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
progress, err := client.Download(path, c.Args().Get(0))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create progress bar template
|
||||||
|
barTmpl := `{{counters . }} B {{bar . "|" "-" (cycle .) " " "|"}} {{percent . }} {{rtime . "%s"}}`
|
||||||
|
// Start full bar at 0 total
|
||||||
|
bar := pb.ProgressBarTemplate(barTmpl).Start(0)
|
||||||
|
// Get progress events
|
||||||
|
for event := range progress {
|
||||||
|
// Set total bytes in progress bar
|
||||||
|
bar.SetTotal(int64(event.Total))
|
||||||
|
// Set amount of bytes sent in progress bar
|
||||||
|
bar.SetCurrent(int64(event.Sent))
|
||||||
|
}
|
||||||
|
bar.Finish()
|
||||||
|
|
||||||
|
if c.Args().Get(1) == "-" {
|
||||||
|
io.Copy(os.Stdout, tmpFile)
|
||||||
|
os.Stdout.WriteString("\n")
|
||||||
|
os.Stdout.Sync()
|
||||||
|
tmpFile.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fsRemove(c *cli.Context) error {
|
||||||
|
if c.Args().Len() < 1 {
|
||||||
|
return cli.Exit("Command remove requires one or more arguments", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := client.Remove(c.Args().Slice()...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fsWrite(c *cli.Context) error {
|
||||||
|
if c.Args().Len() != 2 {
|
||||||
|
return cli.Exit("Command write requires two arguments", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
var tmpFile *os.File
|
||||||
|
var path string
|
||||||
|
var err error
|
||||||
|
if c.Args().Get(0) == "-" {
|
||||||
|
tmpFile, err = ioutil.TempFile("/tmp", "itctl.*")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
path = tmpFile.Name()
|
||||||
|
} else {
|
||||||
|
path, err = filepath.Abs(c.Args().Get(0))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.Args().Get(0) == "-" {
|
||||||
|
io.Copy(tmpFile, os.Stdin)
|
||||||
|
defer tmpFile.Close()
|
||||||
|
defer os.Remove(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
progress, err := client.Upload(c.Args().Get(1), path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create progress bar template
|
||||||
|
barTmpl := `{{counters . }} B {{bar . "|" "-" (cycle .) " " "|"}} {{percent . }} {{rtime . "%s"}}`
|
||||||
|
// Start full bar at 0 total
|
||||||
|
bar := pb.ProgressBarTemplate(barTmpl).Start(0)
|
||||||
|
// Get progress events
|
||||||
|
for event := range progress {
|
||||||
|
// Set total bytes in progress bar
|
||||||
|
bar.SetTotal(int64(event.Total))
|
||||||
|
// Set amount of bytes sent in progress bar
|
||||||
|
bar.SetCurrent(int64(event.Sent))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
71
cmd/itctl/get.go
Normal file
71
cmd/itctl/get.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getAddress(c *cli.Context) error {
|
||||||
|
address, err := client.Address()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(address)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBattery(c *cli.Context) error {
|
||||||
|
battLevel, err := client.BatteryLevel()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print returned percentage
|
||||||
|
fmt.Printf("%d%%\n", battLevel)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHeart(c *cli.Context) error {
|
||||||
|
heartRate, err := client.HeartRate()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print returned BPM
|
||||||
|
fmt.Printf("%d BPM\n", heartRate)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMotion(c *cli.Context) error {
|
||||||
|
motionVals, err := client.Motion()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.Bool("shell") {
|
||||||
|
fmt.Printf(
|
||||||
|
"X=%d\nY=%d\nZ=%d\n",
|
||||||
|
motionVals.X,
|
||||||
|
motionVals.Y,
|
||||||
|
motionVals.Z,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return json.NewEncoder(os.Stdout).Encode(motionVals)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSteps(c *cli.Context) error {
|
||||||
|
stepCount, err := client.StepCount()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print returned BPM
|
||||||
|
fmt.Printf("%d Steps\n", stepCount)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package get
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// addressCmd represents the address command
|
|
||||||
var addressCmd = &cobra.Command{
|
|
||||||
Use: "address",
|
|
||||||
Aliases: []string{"addr"},
|
|
||||||
Short: "Get InfiniTime's bluetooth address",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
address, err := client.Address()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting bluetooth address")
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(address)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
getCmd.AddCommand(addressCmd)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
|
||||||
// and all subcommands, e.g.:
|
|
||||||
// addressCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
|
||||||
// is called directly, e.g.:
|
|
||||||
// addressCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package get
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// batteryCmd represents the batt command
|
|
||||||
var batteryCmd = &cobra.Command{
|
|
||||||
Use: "battery",
|
|
||||||
Aliases: []string{"batt"},
|
|
||||||
Short: "Get battery level from InfiniTime",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
battLevel, err := client.BatteryLevel()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting battery level")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print returned percentage
|
|
||||||
fmt.Printf("%d%%\n", battLevel)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
getCmd.AddCommand(batteryCmd)
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package get
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"go.arsenm.dev/itd/cmd/itctl/root"
|
|
||||||
)
|
|
||||||
|
|
||||||
// getCmd represents the get command
|
|
||||||
var getCmd = &cobra.Command{
|
|
||||||
Use: "get",
|
|
||||||
Short: "Get information from InfiniTime",
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
root.RootCmd.AddCommand(getCmd)
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package get
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// heartCmd represents the heart command
|
|
||||||
var heartCmd = &cobra.Command{
|
|
||||||
Use: "heart",
|
|
||||||
Short: "Get heart rate from InfiniTime",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
heartRate, err := client.HeartRate()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting heart rate")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print returned BPM
|
|
||||||
fmt.Printf("%d BPM\n", heartRate)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
getCmd.AddCommand(heartCmd)
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package get
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// steps.goCmd represents the steps.go command
|
|
||||||
var motionCmd = &cobra.Command{
|
|
||||||
Use: "motion",
|
|
||||||
Short: "Get motion values from InfiniTime",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
motionVals, err := client.Motion()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting motion values")
|
|
||||||
}
|
|
||||||
|
|
||||||
if viper.GetBool("shell") {
|
|
||||||
fmt.Printf(
|
|
||||||
"X=%d\nY=%d\nZ=%d",
|
|
||||||
motionVals.X,
|
|
||||||
motionVals.Y,
|
|
||||||
motionVals.Z,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("%+v\n", motionVals)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
getCmd.AddCommand(motionCmd)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
|
||||||
// and all subcommands, e.g.:
|
|
||||||
// steps.goCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
|
||||||
// is called directly, e.g.:
|
|
||||||
motionCmd.Flags().BoolP("shell", "s", false, "Output data in shell-compatible format")
|
|
||||||
viper.BindPFlag("shell", motionCmd.Flags().Lookup("shell"))
|
|
||||||
}
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package get
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// steps.goCmd represents the steps.go command
|
|
||||||
var stepsCmd = &cobra.Command{
|
|
||||||
Use: "steps",
|
|
||||||
Short: "Get step count from InfiniTime",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
stepCount, err := client.StepCount()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting step count")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print returned BPM
|
|
||||||
fmt.Printf("%d Steps\n", stepCount)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
getCmd.AddCommand(stepsCmd)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
|
||||||
// and all subcommands, e.g.:
|
|
||||||
// steps.goCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
|
||||||
// is called directly, e.g.:
|
|
||||||
// steps.goCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
|
||||||
@@ -1,41 +1,257 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "go.arsenm.dev/itd/cmd/itctl/firmware"
|
|
||||||
_ "go.arsenm.dev/itd/cmd/itctl/get"
|
|
||||||
_ "go.arsenm.dev/itd/cmd/itctl/notify"
|
|
||||||
"go.arsenm.dev/itd/cmd/itctl/root"
|
|
||||||
_ "go.arsenm.dev/itd/cmd/itctl/set"
|
|
||||||
_ "go.arsenm.dev/itd/cmd/itctl/watch"
|
|
||||||
|
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"go.arsenm.dev/itd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
var client *api.Client
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
root.Execute()
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
|
||||||
|
app := cli.App{
|
||||||
|
Name: "itctl",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "socket-path",
|
||||||
|
Aliases: []string{"s"},
|
||||||
|
Value: api.DefaultAddr,
|
||||||
|
Usage: "Path to itd socket",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Commands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "filesystem",
|
||||||
|
Aliases: []string{"fs"},
|
||||||
|
Usage: "Perform filesystem operations on the PineTime",
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "list",
|
||||||
|
ArgsUsage: "[dir]",
|
||||||
|
Aliases: []string{"ls"},
|
||||||
|
Usage: "List a directory",
|
||||||
|
Action: fsList,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "mkdir",
|
||||||
|
ArgsUsage: "<paths...>",
|
||||||
|
Usage: "Create new directories",
|
||||||
|
Action: fsMkdir,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "move",
|
||||||
|
ArgsUsage: "<old> <new>",
|
||||||
|
Aliases: []string{"mv"},
|
||||||
|
Usage: "Move a file or directory",
|
||||||
|
Action: fsMove,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "read",
|
||||||
|
ArgsUsage: `<remote path> <local path>`,
|
||||||
|
Usage: "Read a file from InfiniTime.",
|
||||||
|
Description: `Read is used to read files from InfiniTime's filesystem. A "-" can be used to signify stdout`,
|
||||||
|
Action: fsRead,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "remove",
|
||||||
|
ArgsUsage: "<paths...>",
|
||||||
|
Aliases: []string{"rm"},
|
||||||
|
Usage: "Remove a file from InfiniTime",
|
||||||
|
Action: fsRemove,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "write",
|
||||||
|
ArgsUsage: `<local path> <remote path>`,
|
||||||
|
Usage: "Write a file to InfiniTime",
|
||||||
|
Description: `Write is used to write files to InfiniTime's filesystem. A "-" can be used to signify stdin`,
|
||||||
|
Action: fsWrite,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "firmware",
|
||||||
|
Aliases: []string{"fw"},
|
||||||
|
Usage: "Manage InfiniTime firmware",
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.PathFlag{
|
||||||
|
Name: "init-packet",
|
||||||
|
Aliases: []string{"i"},
|
||||||
|
Usage: "Path to init packet (.dat file)",
|
||||||
|
},
|
||||||
|
&cli.PathFlag{
|
||||||
|
Name: "firmware",
|
||||||
|
Aliases: []string{"f"},
|
||||||
|
Usage: "Path to firmware image (.bin file)",
|
||||||
|
},
|
||||||
|
&cli.PathFlag{
|
||||||
|
Name: "archive",
|
||||||
|
Aliases: []string{"a"},
|
||||||
|
Usage: "Path to firmware archive (.zip file)",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Name: "upgrade",
|
||||||
|
Aliases: []string{"upg"},
|
||||||
|
Usage: "Upgrade InfiniTime firmware using files or archive",
|
||||||
|
Action: fwUpgrade,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "version",
|
||||||
|
Aliases: []string{"ver"},
|
||||||
|
Usage: "Get firmware version of InfiniTime",
|
||||||
|
Action: fwVersion,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "get",
|
||||||
|
Usage: "Get information from InfiniTime",
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "address",
|
||||||
|
Aliases: []string{"addr"},
|
||||||
|
Usage: "Get InfiniTime's bluetooth address",
|
||||||
|
Action: getAddress,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "battery",
|
||||||
|
Aliases: []string{"batt"},
|
||||||
|
Usage: "Get InfiniTime's battery percentage",
|
||||||
|
Action: getBattery,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "heart",
|
||||||
|
Usage: "Get heart rate from InfiniTime",
|
||||||
|
Action: getHeart,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{Name: "shell"},
|
||||||
|
},
|
||||||
|
Name: "motion",
|
||||||
|
Usage: "Get motion values from InfiniTime",
|
||||||
|
Action: getMotion,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "steps",
|
||||||
|
Usage: "Get step count from InfiniTime",
|
||||||
|
Action: getSteps,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "notify",
|
||||||
|
Usage: "Send notification to InfiniTime",
|
||||||
|
Action: notify,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "set",
|
||||||
|
Usage: "Set information on InfiniTime",
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "time",
|
||||||
|
ArgsUsage: `<ISO8601|"now">`,
|
||||||
|
Usage: "Set InfiniTime's clock to specified time",
|
||||||
|
Action: setTime,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "update",
|
||||||
|
Usage: "Update information on InfiniTime",
|
||||||
|
Aliases: []string{"upd"},
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "weather",
|
||||||
|
Usage: "Force an immediate update of weather data",
|
||||||
|
Action: updateWeather,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "watch",
|
||||||
|
Usage: "Watch a value for changes",
|
||||||
|
Subcommands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{Name: "json"},
|
||||||
|
&cli.BoolFlag{Name: "shell"},
|
||||||
|
},
|
||||||
|
Name: "heart",
|
||||||
|
Usage: "Watch heart rate value for changes",
|
||||||
|
Action: watchHeart,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{Name: "json"},
|
||||||
|
&cli.BoolFlag{Name: "shell"},
|
||||||
|
},
|
||||||
|
Name: "steps",
|
||||||
|
Usage: "Watch step count value for changes",
|
||||||
|
Action: watchStepCount,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{Name: "json"},
|
||||||
|
&cli.BoolFlag{Name: "shell"},
|
||||||
|
},
|
||||||
|
Name: "motion",
|
||||||
|
Usage: "Watch motion coordinates for changes",
|
||||||
|
Action: watchMotion,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{Name: "json"},
|
||||||
|
&cli.BoolFlag{Name: "shell"},
|
||||||
|
},
|
||||||
|
Name: "battery",
|
||||||
|
Aliases: []string{"batt"},
|
||||||
|
Usage: "Watch battery level value for changes",
|
||||||
|
Action: watchBattLevel,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Before: func(c *cli.Context) error {
|
||||||
|
newClient, err := api.New(c.String("socket-path"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client = newClient
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
After: func(*cli.Context) error {
|
||||||
|
if client != nil {
|
||||||
|
client.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.Run(os.Args)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("Error while running app")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func catchSignal(fn func()) {
|
||||||
|
sigCh := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(
|
||||||
|
sigCh,
|
||||||
|
syscall.SIGINT,
|
||||||
|
syscall.SIGTERM,
|
||||||
|
)
|
||||||
|
go func() {
|
||||||
|
<-sigCh
|
||||||
|
fn()
|
||||||
|
os.Exit(0)
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
17
cmd/itctl/notify.go
Normal file
17
cmd/itctl/notify.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/urfave/cli/v2"
|
||||||
|
|
||||||
|
func notify(c *cli.Context) error {
|
||||||
|
// Ensure required arguments
|
||||||
|
if c.Args().Len() != 2 {
|
||||||
|
return cli.Exit("Command notify requires two arguments", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := client.Notify(c.Args().Get(0), c.Args().Get(1))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package notify
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
"go.arsenm.dev/itd/cmd/itctl/root"
|
|
||||||
)
|
|
||||||
|
|
||||||
// notifyCmd represents the notify command
|
|
||||||
var notifyCmd = &cobra.Command{
|
|
||||||
Use: "notify <title> <body>",
|
|
||||||
Short: "Send notification to InfiniTime",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
// Ensure required arguments
|
|
||||||
if len(args) != 2 {
|
|
||||||
cmd.Usage()
|
|
||||||
log.Fatal().Msg("Command notify requires two arguments")
|
|
||||||
}
|
|
||||||
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
err := client.Notify(args[0], args[1])
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error sending notification")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
root.RootCmd.AddCommand(notifyCmd)
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package root
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/abiosoft/ishell"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RootCmd represents the base command when called without any subcommands
|
|
||||||
var RootCmd = &cobra.Command{
|
|
||||||
Use: "itctl",
|
|
||||||
Short: "Control the itd daemon for InfiniTime smartwatches",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
|
|
||||||
// Create new shell
|
|
||||||
sh := ishell.New()
|
|
||||||
sh.SetPrompt("itctl> ")
|
|
||||||
|
|
||||||
// For every command in cobra
|
|
||||||
for _, subCmd := range cmd.Commands() {
|
|
||||||
// Add top level command to ishell
|
|
||||||
sh.AddCmd(&ishell.Cmd{
|
|
||||||
Name: subCmd.Name(),
|
|
||||||
Help: subCmd.Short,
|
|
||||||
Aliases: subCmd.Aliases,
|
|
||||||
LongHelp: subCmd.Long,
|
|
||||||
Func: func(ctx *ishell.Context) {
|
|
||||||
// Append name and arguments of command
|
|
||||||
args := append([]string{ctx.Cmd.Name}, ctx.Args...)
|
|
||||||
// Set root command arguments
|
|
||||||
cmd.SetArgs(args)
|
|
||||||
// Execute root command with new arguments
|
|
||||||
cmd.Execute()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start shell
|
|
||||||
sh.Run()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
||||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
||||||
func Execute() {
|
|
||||||
client, err := api.New(viper.GetString("sockPath"))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error connecting to socket. Is itd running?")
|
|
||||||
}
|
|
||||||
defer client.Close()
|
|
||||||
viper.Set("client", client)
|
|
||||||
RootCmd.CompletionOptions.DisableDefaultCmd = true
|
|
||||||
cobra.CheckErr(RootCmd.Execute())
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// Register flag for socket path
|
|
||||||
RootCmd.Flags().StringP("socket-path", "s", api.DefaultAddr, "Path to itd socket")
|
|
||||||
|
|
||||||
// Bind flag and environment variable to viper key
|
|
||||||
viper.BindPFlag("sockPath", RootCmd.Flags().Lookup("socket-path"))
|
|
||||||
viper.BindEnv("sockPath", "ITCTL_SOCKET_PATH")
|
|
||||||
|
|
||||||
// Set default value for socket path
|
|
||||||
viper.SetDefault("sockPath", api.DefaultAddr)
|
|
||||||
}
|
|
||||||
24
cmd/itctl/set.go
Normal file
24
cmd/itctl/set.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setTime(c *cli.Context) error {
|
||||||
|
// Ensure required arguments
|
||||||
|
if c.Args().Len() < 1 {
|
||||||
|
return cli.Exit("Command time requires one argument", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.Args().Get(0) == "now" {
|
||||||
|
return client.SetTime(time.Now())
|
||||||
|
} else {
|
||||||
|
parsed, err := time.Parse(time.RFC3339, c.Args().Get(0))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return client.SetTime(parsed)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package set
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"go.arsenm.dev/itd/cmd/itctl/root"
|
|
||||||
)
|
|
||||||
|
|
||||||
// setCmd represents the set command
|
|
||||||
var setCmd = &cobra.Command{
|
|
||||||
Use: "set",
|
|
||||||
Short: "Set information on InfiniTime",
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
root.RootCmd.AddCommand(setCmd)
|
|
||||||
}
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package set
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/internal/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
// timeCmd represents the time command
|
|
||||||
var timeCmd = &cobra.Command{
|
|
||||||
Use: `time <ISO8601|"now">`,
|
|
||||||
Short: "Set InfiniTime's clock to specified time",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
// Ensure required arguments
|
|
||||||
if len(args) != 1 {
|
|
||||||
cmd.Usage()
|
|
||||||
log.Warn().Msg("Command time requires one argument")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to itd UNIX socket
|
|
||||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Encode request into connection
|
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeSetTime,
|
|
||||||
Data: args[0],
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read one line from connetion
|
|
||||||
line, _, err := bufio.NewReader(conn).ReadLine()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error reading line from connection")
|
|
||||||
}
|
|
||||||
|
|
||||||
var res types.Response
|
|
||||||
// Decode line into response
|
|
||||||
err = json.Unmarshal(line, &res)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error decoding JSON data")
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Error {
|
|
||||||
log.Fatal().Msg(res.Message)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
setCmd.AddCommand(timeCmd)
|
|
||||||
}
|
|
||||||
7
cmd/itctl/update.go
Normal file
7
cmd/itctl/update.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/urfave/cli/v2"
|
||||||
|
|
||||||
|
func updateWeather(c *cli.Context) error {
|
||||||
|
return client.WeatherUpdate()
|
||||||
|
}
|
||||||
104
cmd/itctl/watch.go
Normal file
104
cmd/itctl/watch.go
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func watchHeart(c *cli.Context) error {
|
||||||
|
heartCh, cancel, err := client.WatchHeartRate()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
catchSignal(cancel)
|
||||||
|
|
||||||
|
for heartRate := range heartCh {
|
||||||
|
if c.Bool("json") {
|
||||||
|
json.NewEncoder(os.Stdout).Encode(
|
||||||
|
map[string]uint8{"heartRate": heartRate},
|
||||||
|
)
|
||||||
|
} else if c.Bool("shell") {
|
||||||
|
fmt.Printf("HEART_RATE=%d\n", heartRate)
|
||||||
|
} else {
|
||||||
|
fmt.Println(heartRate, "BPM")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func watchBattLevel(c *cli.Context) error {
|
||||||
|
battLevelCh, cancel, err := client.WatchBatteryLevel()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
catchSignal(cancel)
|
||||||
|
|
||||||
|
for battLevel := range battLevelCh {
|
||||||
|
if c.Bool("json") {
|
||||||
|
json.NewEncoder(os.Stdout).Encode(
|
||||||
|
map[string]uint8{"battLevel": battLevel},
|
||||||
|
)
|
||||||
|
} else if c.Bool("shell") {
|
||||||
|
fmt.Printf("BATTERY_LEVEL=%d\n", battLevel)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%d%%\n", battLevel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func watchStepCount(c *cli.Context) error {
|
||||||
|
stepCountCh, cancel, err := client.WatchStepCount()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
catchSignal(cancel)
|
||||||
|
|
||||||
|
for stepCount := range stepCountCh {
|
||||||
|
if c.Bool("json") {
|
||||||
|
json.NewEncoder(os.Stdout).Encode(
|
||||||
|
map[string]uint32{"stepCount": stepCount},
|
||||||
|
)
|
||||||
|
} else if c.Bool("shell") {
|
||||||
|
fmt.Printf("STEP_COUNT=%d\n", stepCount)
|
||||||
|
} else {
|
||||||
|
fmt.Println(stepCount, "Steps")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func watchMotion(c *cli.Context) error {
|
||||||
|
motionCh, cancel, err := client.WatchMotion()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
catchSignal(cancel)
|
||||||
|
|
||||||
|
for motionVals := range motionCh {
|
||||||
|
if c.Bool("json") {
|
||||||
|
json.NewEncoder(os.Stdout).Encode(motionVals)
|
||||||
|
} else if c.Bool("shell") {
|
||||||
|
fmt.Printf(
|
||||||
|
"X=%d\nY=%d\nZ=%d\n",
|
||||||
|
motionVals.X,
|
||||||
|
motionVals.Y,
|
||||||
|
motionVals.Z,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
fmt.Println(motionVals)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package watch
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// heartCmd represents the address command
|
|
||||||
var batteryCmd = &cobra.Command{
|
|
||||||
Use: "battery",
|
|
||||||
Aliases: []string{"batt"},
|
|
||||||
Short: "Watch InfiniTime's battery level for changes",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
battLevelCh, cancel, err := client.WatchBatteryLevel()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting battery level channel")
|
|
||||||
}
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
signalCh := make(chan os.Signal, 1)
|
|
||||||
go func() {
|
|
||||||
<-signalCh
|
|
||||||
cancel()
|
|
||||||
os.Exit(0)
|
|
||||||
}()
|
|
||||||
signal.Notify(signalCh,
|
|
||||||
syscall.SIGINT,
|
|
||||||
syscall.SIGTERM,
|
|
||||||
)
|
|
||||||
|
|
||||||
for battlevel := range battLevelCh {
|
|
||||||
fmt.Printf("%d%%\n", battlevel)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
watchCmd.AddCommand(batteryCmd)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
|
||||||
// and all subcommands, e.g.:
|
|
||||||
// addressCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
|
||||||
// is called directly, e.g.:
|
|
||||||
// addressCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package watch
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// heartCmd represents the address command
|
|
||||||
var heartCmd = &cobra.Command{
|
|
||||||
Use: "heart",
|
|
||||||
Short: "Watch InfiniTime's heart rate for changes",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
heartRateCh, cancel, err := client.WatchHeartRate()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting heart rate channel")
|
|
||||||
}
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
signalCh := make(chan os.Signal, 1)
|
|
||||||
go func() {
|
|
||||||
<-signalCh
|
|
||||||
cancel()
|
|
||||||
os.Exit(0)
|
|
||||||
}()
|
|
||||||
signal.Notify(signalCh,
|
|
||||||
syscall.SIGINT,
|
|
||||||
syscall.SIGTERM,
|
|
||||||
)
|
|
||||||
|
|
||||||
for heartRate := range heartRateCh {
|
|
||||||
fmt.Println(heartRate, "BPM")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
watchCmd.AddCommand(heartCmd)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
|
||||||
// and all subcommands, e.g.:
|
|
||||||
// addressCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
|
||||||
// is called directly, e.g.:
|
|
||||||
// addressCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package watch
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// heartCmd represents the address command
|
|
||||||
var motionCmd = &cobra.Command{
|
|
||||||
Use: "motion",
|
|
||||||
Short: "Watch InfiniTime's motion values for changes",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
motionValCh, cancel, err := client.WatchMotion()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting motion value channel")
|
|
||||||
}
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
signalCh := make(chan os.Signal, 1)
|
|
||||||
go func() {
|
|
||||||
<-signalCh
|
|
||||||
cancel()
|
|
||||||
os.Exit(0)
|
|
||||||
}()
|
|
||||||
signal.Notify(signalCh,
|
|
||||||
syscall.SIGINT,
|
|
||||||
syscall.SIGTERM,
|
|
||||||
)
|
|
||||||
|
|
||||||
for motionVals := range motionValCh {
|
|
||||||
if viper.GetBool("shell") {
|
|
||||||
fmt.Printf(
|
|
||||||
"X=%d\nY=%d\nZ=%d\n",
|
|
||||||
motionVals.X,
|
|
||||||
motionVals.Y,
|
|
||||||
motionVals.Z,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("%+v\n", motionVals)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
watchCmd.AddCommand(motionCmd)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
|
||||||
// and all subcommands, e.g.:
|
|
||||||
// addressCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
|
||||||
// is called directly, e.g.:
|
|
||||||
// addressCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
motionCmd.Flags().BoolP("shell", "s", false, "Output data in shell-compatible format")
|
|
||||||
viper.BindPFlag("shell", motionCmd.Flags().Lookup("shell"))
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package watch
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/itd/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// heartCmd represents the address command
|
|
||||||
var stepsCmd = &cobra.Command{
|
|
||||||
Use: "steps",
|
|
||||||
Short: "Watch InfiniTime's step count for changes",
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
client := viper.Get("client").(*api.Client)
|
|
||||||
|
|
||||||
stepCountCh, cancel, err := client.WatchStepCount()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error getting step count channel")
|
|
||||||
}
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
signalCh := make(chan os.Signal, 1)
|
|
||||||
go func() {
|
|
||||||
<-signalCh
|
|
||||||
cancel()
|
|
||||||
os.Exit(0)
|
|
||||||
}()
|
|
||||||
signal.Notify(signalCh,
|
|
||||||
syscall.SIGINT,
|
|
||||||
syscall.SIGTERM,
|
|
||||||
)
|
|
||||||
|
|
||||||
for stepCount := range stepCountCh {
|
|
||||||
fmt.Println(stepCount, "Steps")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
watchCmd.AddCommand(stepsCmd)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
|
||||||
// and all subcommands, e.g.:
|
|
||||||
// addressCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
|
||||||
// is called directly, e.g.:
|
|
||||||
// addressCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
||||||
* Copyright (C) 2021 Arsen Musayelyan
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package watch
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"go.arsenm.dev/itd/cmd/itctl/root"
|
|
||||||
)
|
|
||||||
|
|
||||||
// watchCmd represents the watch command
|
|
||||||
var watchCmd = &cobra.Command{
|
|
||||||
Use: "watch",
|
|
||||||
Short: "Watch values from InfiniTime for changes",
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
root.RootCmd.AddCommand(watchCmd)
|
|
||||||
}
|
|
||||||
@@ -49,7 +49,7 @@ func timeTab(parent fyne.Window, client *api.Client) *fyne.Container {
|
|||||||
func setTime(client *api.Client, current bool, t ...time.Time) error {
|
func setTime(client *api.Client, current bool, t ...time.Time) error {
|
||||||
var err error
|
var err error
|
||||||
if current {
|
if current {
|
||||||
err = client.SetTimeNow()
|
err = client.SetTime(time.Now())
|
||||||
} else {
|
} else {
|
||||||
err = client.SetTime(t[0])
|
err = client.SetTime(t[0])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"fyne.io/fyne/v2/storage"
|
"fyne.io/fyne/v2/storage"
|
||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/widget"
|
||||||
"go.arsenm.dev/itd/api"
|
"go.arsenm.dev/itd/api"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func upgradeTab(parent fyne.Window, client *api.Client) *fyne.Container {
|
func upgradeTab(parent fyne.Window, client *api.Client) *fyne.Container {
|
||||||
@@ -119,10 +118,10 @@ func upgradeTab(parent fyne.Window, client *api.Client) *fyne.Container {
|
|||||||
// Get appropriate upgrade type and file paths
|
// Get appropriate upgrade type and file paths
|
||||||
switch upgradeTypeSelect.Selected {
|
switch upgradeTypeSelect.Selected {
|
||||||
case "Archive":
|
case "Archive":
|
||||||
fwUpgType = types.UpgradeTypeArchive
|
fwUpgType = api.UpgradeTypeArchive
|
||||||
files = append(files, archivePath)
|
files = append(files, archivePath)
|
||||||
case "Files":
|
case "Files":
|
||||||
fwUpgType = types.UpgradeTypeFiles
|
fwUpgType = api.UpgradeTypeFiles
|
||||||
files = append(files, initPktPath, firmwarePath)
|
files = append(files, initPktPath, firmwarePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,8 +133,6 @@ func upgradeTab(parent fyne.Window, client *api.Client) *fyne.Container {
|
|||||||
|
|
||||||
// Show progress dialog
|
// Show progress dialog
|
||||||
progressDlg.Show()
|
progressDlg.Show()
|
||||||
// Hide progress dialog after completion
|
|
||||||
defer progressDlg.Hide()
|
|
||||||
|
|
||||||
for event := range progress {
|
for event := range progress {
|
||||||
// Set label text to received / total B
|
// Set label text to received / total B
|
||||||
@@ -146,10 +143,28 @@ func upgradeTab(parent fyne.Window, client *api.Client) *fyne.Container {
|
|||||||
// Refresh progress bar
|
// Refresh progress bar
|
||||||
progressBar.Refresh()
|
progressBar.Refresh()
|
||||||
// If transfer finished, break
|
// If transfer finished, break
|
||||||
if event.Sent == event.Total {
|
if int64(event.Sent) == event.Total {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hide progress dialog after completion
|
||||||
|
progressDlg.Hide()
|
||||||
|
|
||||||
|
// Reset screen to default
|
||||||
|
upgradeTypeSelect.SetSelectedIndex(0)
|
||||||
|
firmwareBtn.SetText("Select firmware (.bin)")
|
||||||
|
initPktBtn.SetText("Select init packet (.dat)")
|
||||||
|
archiveBtn.SetText("Select archive (.zip)")
|
||||||
|
firmwarePath = ""
|
||||||
|
initPktPath = ""
|
||||||
|
archivePath = ""
|
||||||
|
|
||||||
|
dialog.NewInformation(
|
||||||
|
"Upgrade Complete",
|
||||||
|
"The firmware was transferred successfully.\nRemember to validate the firmware in InfiniTime settings.",
|
||||||
|
parent,
|
||||||
|
).Show()
|
||||||
})
|
})
|
||||||
|
|
||||||
// Return container containing all elements
|
// Return container containing all elements
|
||||||
|
|||||||
76
config.go
76
config.go
@@ -2,46 +2,78 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/knadh/koanf/parsers/toml"
|
||||||
|
"github.com/knadh/koanf/providers/confmap"
|
||||||
|
"github.com/knadh/koanf/providers/env"
|
||||||
|
"github.com/knadh/koanf/providers/file"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Set up logger
|
// Set up logger
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
|
||||||
// Set config settings
|
// Get user's configuration directory
|
||||||
|
cfgDir, err := os.UserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set config defaults
|
||||||
setCfgDefaults()
|
setCfgDefaults()
|
||||||
viper.AddConfigPath("$HOME/.config")
|
|
||||||
viper.AddConfigPath("/etc")
|
// Load config files
|
||||||
viper.SetConfigName("itd")
|
etcProvider := file.Provider("/etc/itd.toml")
|
||||||
viper.SetConfigType("toml")
|
cfgProvider := file.Provider(filepath.Join(cfgDir, "itd.toml"))
|
||||||
viper.WatchConfig()
|
k.Load(etcProvider, toml.Parser())
|
||||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
k.Load(cfgProvider, toml.Parser())
|
||||||
viper.SetEnvPrefix("itd")
|
|
||||||
// Ignore error because defaults set
|
// Watch configs for changes
|
||||||
viper.ReadInConfig()
|
cfgWatch(etcProvider)
|
||||||
viper.AutomaticEnv()
|
cfgWatch(cfgProvider)
|
||||||
|
|
||||||
|
// Load envireonment variables
|
||||||
|
k.Load(env.Provider("ITD_", "_", func(s string) string {
|
||||||
|
return strings.ToLower(strings.TrimPrefix(s, "ITD_"))
|
||||||
|
}), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cfgWatch(provider *file.File) {
|
||||||
|
// Watch for changes and reload when detected
|
||||||
|
provider.Watch(func(_ interface{}, err error) {
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
k.Load(provider, toml.Parser())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func setCfgDefaults() {
|
func setCfgDefaults() {
|
||||||
viper.SetDefault("cfg.version", 2)
|
k.Load(confmap.Provider(map[string]interface{}{
|
||||||
|
"socket.path": "/tmp/itd/socket",
|
||||||
|
|
||||||
viper.SetDefault("socket.path", "/tmp/itd/socket")
|
"conn.reconnect": true,
|
||||||
|
|
||||||
viper.SetDefault("conn.reconnect", true)
|
"conn.whitelist.enabled": false,
|
||||||
|
"conn.whitelist.devices": []string{},
|
||||||
|
|
||||||
viper.SetDefault("on.connect.notify", true)
|
"on.connect.notify": true,
|
||||||
|
|
||||||
viper.SetDefault("on.reconnect.notify", true)
|
"on.reconnect.notify": true,
|
||||||
viper.SetDefault("on.reconnect.setTime", true)
|
"on.reconnect.setTime": true,
|
||||||
|
|
||||||
viper.SetDefault("notifs.ignore.sender", []string{})
|
"notifs.translit.use": []string{"eASCII"},
|
||||||
viper.SetDefault("notifs.ignore.summary", []string{"InfiniTime"})
|
"notifs.translit.custom": []string{},
|
||||||
viper.SetDefault("notifs.ignore.body", []string{})
|
|
||||||
|
|
||||||
viper.SetDefault("music.vol.interval", 5)
|
"notifs.ignore.sender": []string{},
|
||||||
|
"notifs.ignore.summary": []string{"InfiniTime"},
|
||||||
|
"notifs.ignore.body": []string{},
|
||||||
|
|
||||||
|
"music.vol.interval": 5,
|
||||||
|
}, "."), nil)
|
||||||
}
|
}
|
||||||
|
|||||||
37
dbus.go
Normal file
37
dbus.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/godbus/dbus/v5"
|
||||||
|
|
||||||
|
func newSystemBusConn() (*dbus.Conn, error) {
|
||||||
|
// Connect to dbus session bus
|
||||||
|
conn, err := dbus.SystemBusPrivate()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = conn.Auth(nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = conn.Hello()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSessionBusConn() (*dbus.Conn, error) {
|
||||||
|
// Connect to dbus session bus
|
||||||
|
conn, err := dbus.SessionBusPrivate()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = conn.Auth(nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = conn.Hello()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
143
go.mod
143
go.mod
@@ -1,33 +1,122 @@
|
|||||||
module go.arsenm.dev/itd
|
module gitea.arsenm.dev/cpyarger/itd
|
||||||
|
|
||||||
go 1.16
|
go 1.17
|
||||||
|
|
||||||
require (
|
require (
|
||||||
fyne.io/fyne/v2 v2.1.0
|
fyne.io/fyne/v2 v2.1.2
|
||||||
github.com/VividCortex/ewma v1.2.0 // indirect
|
|
||||||
github.com/abiosoft/ishell v2.0.0+incompatible
|
|
||||||
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect
|
|
||||||
github.com/cheggaaa/pb/v3 v3.0.8
|
github.com/cheggaaa/pb/v3 v3.0.8
|
||||||
github.com/fatih/color v1.13.0 // indirect
|
github.com/gen2brain/dlgs v0.0.0-20211108104213-bade24837f0b
|
||||||
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
|
github.com/godbus/dbus/v5 v5.0.6
|
||||||
github.com/go-gl/gl v0.0.0-20210905235341-f7a045908259 // indirect
|
github.com/google/uuid v1.3.0
|
||||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be // indirect
|
github.com/knadh/koanf v1.4.0
|
||||||
github.com/godbus/dbus/v5 v5.0.5
|
github.com/mattn/go-isatty v0.0.14
|
||||||
github.com/google/uuid v1.1.2
|
github.com/mozillazg/go-pinyin v0.19.0
|
||||||
github.com/mattn/go-colorable v0.1.11 // indirect
|
github.com/rs/zerolog v1.26.1
|
||||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
github.com/smallnest/rpcx v1.7.4
|
||||||
github.com/mitchellh/mapstructure v1.4.2
|
github.com/urfave/cli/v2 v2.3.0
|
||||||
github.com/mozillazg/go-pinyin v0.18.0
|
github.com/vmihailenco/msgpack/v5 v5.3.5
|
||||||
github.com/rs/zerolog v1.25.0
|
gitea.arsenm.dev/Arsen6331/infinitime v0.0.0-20220424030849-6c3f1b14c948
|
||||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
|
||||||
github.com/spf13/cobra v1.2.1
|
|
||||||
github.com/spf13/viper v1.9.0
|
|
||||||
github.com/srwiley/oksvg v0.0.0-20210519022825-9fc0c575d5fe // indirect
|
|
||||||
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
|
|
||||||
github.com/yuin/goldmark v1.4.1 // indirect
|
|
||||||
go.arsenm.dev/infinitime v0.0.0-20211023042633-53aa6f8a0c72
|
|
||||||
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
|
|
||||||
golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0 // indirect
|
|
||||||
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
|
|
||||||
golang.org/x/text v0.3.7
|
golang.org/x/text v0.3.7
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/VividCortex/ewma v1.2.0 // indirect
|
||||||
|
github.com/akutz/memconn v0.1.0 // indirect
|
||||||
|
github.com/apache/thrift v0.16.0 // indirect
|
||||||
|
github.com/armon/go-metrics v0.3.10 // indirect
|
||||||
|
github.com/cenk/backoff v2.2.1+incompatible // indirect
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||||
|
github.com/cheekybits/genny v1.0.0 // indirect
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
github.com/edwingeng/doublejump v0.0.0-20210724020454-c82f1bcb3280 // indirect
|
||||||
|
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
|
||||||
|
github.com/fatih/color v1.13.0 // indirect
|
||||||
|
github.com/fatih/structs v1.1.0 // indirect
|
||||||
|
github.com/fredbi/uri v0.0.0-20181227131451-3dcfdacbaaf3 // indirect
|
||||||
|
github.com/fsnotify/fsnotify v1.5.1 // indirect
|
||||||
|
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
|
||||||
|
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect
|
||||||
|
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211204153444-caad923f49f4 // indirect
|
||||||
|
github.com/go-logr/logr v1.2.3 // indirect
|
||||||
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
|
github.com/go-ping/ping v0.0.0-20211130115550-779d1e919534 // indirect
|
||||||
|
github.com/go-redis/redis/v8 v8.11.5 // indirect
|
||||||
|
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
|
||||||
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
|
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff // indirect
|
||||||
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
|
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
|
||||||
|
github.com/grandcat/zeroconf v1.0.0 // indirect
|
||||||
|
github.com/hashicorp/consul/api v1.12.0 // indirect
|
||||||
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
|
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||||
|
github.com/hashicorp/go-hclog v1.2.0 // indirect
|
||||||
|
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
|
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
||||||
|
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||||
|
github.com/hashicorp/serf v0.9.7 // indirect
|
||||||
|
github.com/juju/ratelimit v1.0.1 // indirect
|
||||||
|
github.com/julienschmidt/httprouter v1.3.0 // indirect
|
||||||
|
github.com/kavu/go_reuseport v1.5.0 // indirect
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
|
||||||
|
github.com/klauspost/reedsolomon v1.9.16 // indirect
|
||||||
|
github.com/lucas-clemente/quic-go v0.27.0 // indirect
|
||||||
|
github.com/marten-seemann/qtls-go1-16 v0.1.5 // indirect
|
||||||
|
github.com/marten-seemann/qtls-go1-17 v0.1.1 // indirect
|
||||||
|
github.com/marten-seemann/qtls-go1-18 v0.1.1 // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||||
|
github.com/miekg/dns v1.1.48 // indirect
|
||||||
|
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
|
github.com/mitchellh/mapstructure v1.4.3 // indirect
|
||||||
|
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||||
|
github.com/muka/go-bluetooth v0.0.0-20220219050759-674a63b8741a // indirect
|
||||||
|
github.com/nxadm/tail v1.4.8 // indirect
|
||||||
|
github.com/onsi/ginkgo v1.16.5 // indirect
|
||||||
|
github.com/pelletier/go-toml v1.9.4 // indirect
|
||||||
|
github.com/philhofer/fwd v1.1.1 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
|
github.com/rpcxio/libkv v0.5.1-0.20210420120011-1fceaedca8a5 // indirect
|
||||||
|
github.com/rs/cors v1.8.2 // indirect
|
||||||
|
github.com/rubyist/circuitbreaker v2.2.1+incompatible // indirect
|
||||||
|
github.com/russross/blackfriday/v2 v2.0.1 // indirect
|
||||||
|
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414 // indirect
|
||||||
|
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||||
|
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||||
|
github.com/smallnest/quick v0.0.0-20220103065406-780def6371e6 // indirect
|
||||||
|
github.com/soheilhy/cmux v0.1.5 // indirect
|
||||||
|
github.com/srwiley/oksvg v0.0.0-20211120171407-1837d6608d8c // indirect
|
||||||
|
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
|
||||||
|
github.com/stretchr/testify v1.7.1 // indirect
|
||||||
|
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
|
||||||
|
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b // indirect
|
||||||
|
github.com/tinylib/msgp v1.1.6 // indirect
|
||||||
|
github.com/tjfoc/gmsm v1.4.1 // indirect
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
|
github.com/valyala/fastrand v1.1.0 // indirect
|
||||||
|
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||||
|
github.com/x448/float16 v0.8.4 // indirect
|
||||||
|
github.com/xtaci/kcp-go v5.4.20+incompatible // indirect
|
||||||
|
github.com/yuin/goldmark v1.4.4 // indirect
|
||||||
|
go.opentelemetry.io/otel v1.6.3 // indirect
|
||||||
|
go.opentelemetry.io/otel/trace v1.6.3 // indirect
|
||||||
|
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 // indirect
|
||||||
|
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
|
||||||
|
golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3 // indirect
|
||||||
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
|
||||||
|
golang.org/x/tools v0.1.10 // indirect
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
|
google.golang.org/protobuf v1.28.0 // indirect
|
||||||
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
package types
|
|
||||||
|
|
||||||
const (
|
|
||||||
ReqTypeHeartRate = iota
|
|
||||||
ReqTypeBattLevel
|
|
||||||
ReqTypeFwVersion
|
|
||||||
ReqTypeFwUpgrade
|
|
||||||
ReqTypeBtAddress
|
|
||||||
ReqTypeNotify
|
|
||||||
ReqTypeSetTime
|
|
||||||
ReqTypeWatchHeartRate
|
|
||||||
ReqTypeWatchBattLevel
|
|
||||||
ReqTypeMotion
|
|
||||||
ReqTypeWatchMotion
|
|
||||||
ReqTypeStepCount
|
|
||||||
ReqTypeWatchStepCount
|
|
||||||
ReqTypeCancel
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
UpgradeTypeArchive = iota
|
|
||||||
UpgradeTypeFiles
|
|
||||||
)
|
|
||||||
|
|
||||||
type ReqDataFwUpgrade struct {
|
|
||||||
Type int
|
|
||||||
Files []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Response struct {
|
|
||||||
Type int `json:"type"`
|
|
||||||
Value interface{} `json:"value,omitempty"`
|
|
||||||
Message string `json:"msg,omitempty"`
|
|
||||||
ID string `json:"id,omitempty"`
|
|
||||||
Error bool `json:"error"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Request struct {
|
|
||||||
Type int `json:"type"`
|
|
||||||
Data interface{} `json:"data,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ReqDataNotify struct {
|
|
||||||
Title string
|
|
||||||
Body string
|
|
||||||
}
|
|
||||||
|
|
||||||
type DFUProgress struct {
|
|
||||||
Received int64 `mapstructure:"recvd"`
|
|
||||||
Total int64 `mapstructure:"total"`
|
|
||||||
Sent int64 `mapstructure:"sent"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type MotionValues struct {
|
|
||||||
X int16
|
|
||||||
Y int16
|
|
||||||
Z int16
|
|
||||||
}
|
|
||||||
14
itd.toml
14
itd.toml
@@ -1,13 +1,13 @@
|
|||||||
# This is temporary, it is to show a notice
|
|
||||||
# to people still using the old config
|
|
||||||
cfg.version = 2
|
|
||||||
|
|
||||||
[socket]
|
[socket]
|
||||||
path = "/tmp/itd/socket"
|
path = "/tmp/itd/socket"
|
||||||
|
|
||||||
[conn]
|
[conn]
|
||||||
reconnect = true
|
reconnect = true
|
||||||
|
|
||||||
|
[conn.whitelist]
|
||||||
|
enabled = false
|
||||||
|
devices = []
|
||||||
|
|
||||||
[on.connect]
|
[on.connect]
|
||||||
notify = true
|
notify = true
|
||||||
|
|
||||||
@@ -26,3 +26,9 @@ cfg.version = 2
|
|||||||
[music]
|
[music]
|
||||||
vol.interval = 5
|
vol.interval = 5
|
||||||
|
|
||||||
|
[weather]
|
||||||
|
enabled = true
|
||||||
|
location = "Los Angeles, CA"
|
||||||
|
|
||||||
|
[logging]
|
||||||
|
level = "info"
|
||||||
103
main.go
103
main.go
@@ -19,50 +19,91 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gen2brain/dlgs"
|
||||||
|
"github.com/knadh/koanf"
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/viper"
|
"gitea.arsenm.dev/cpyarger/itd"
|
||||||
"go.arsenm.dev/infinitime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var firmwareUpdating = false
|
var k = koanf.New(".")
|
||||||
|
|
||||||
|
//go:embed version.txt
|
||||||
|
var version string
|
||||||
|
|
||||||
|
var (
|
||||||
|
firmwareUpdating = false
|
||||||
|
// The FS must be updated when the watch is reconnected
|
||||||
|
updateFS = false
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if viper.GetInt("cfg.version") != 2 {
|
showVer := flag.Bool("version", false, "Show version number and exit")
|
||||||
log.Fatal().Msg("Please update your config to the newest format, only v2 configs supported.")
|
flag.Parse()
|
||||||
|
// If version requested, print and exit
|
||||||
|
if *showVer {
|
||||||
|
fmt.Println(version)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
level, err := zerolog.ParseLevel(k.String("logging.level"))
|
||||||
|
if err != nil || level == zerolog.NoLevel {
|
||||||
|
level = zerolog.InfoLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize infinitime library
|
||||||
|
infinitime.Init()
|
||||||
// Cleanly exit after function
|
// Cleanly exit after function
|
||||||
defer infinitime.Exit()
|
defer infinitime.Exit()
|
||||||
|
|
||||||
|
// Create infinitime options struct
|
||||||
|
opts := &infinitime.Options{
|
||||||
|
AttemptReconnect: k.Bool("conn.reconnect"),
|
||||||
|
WhitelistEnabled: k.Bool("conn.whitelist.enabled"),
|
||||||
|
Whitelist: k.Strings("conn.whitelist.devices"),
|
||||||
|
OnReqPasskey: onReqPasskey,
|
||||||
|
Logger: log.Logger,
|
||||||
|
LogLevel: level,
|
||||||
|
}
|
||||||
|
|
||||||
// Connect to InfiniTime with default options
|
// Connect to InfiniTime with default options
|
||||||
dev, err := infinitime.Connect(&infinitime.Options{
|
dev, err := infinitime.Connect(opts)
|
||||||
AttemptReconnect: viper.GetBool("conn.reconnect"),
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error connecting to InfiniTime")
|
log.Fatal().Err(err).Msg("Error connecting to InfiniTime")
|
||||||
}
|
}
|
||||||
|
|
||||||
// When InfiniTime reconnects
|
// When InfiniTime reconnects
|
||||||
dev.OnReconnect(func() {
|
opts.OnReconnect = func() {
|
||||||
if viper.GetBool("on.reconnect.setTime") {
|
if k.Bool("on.reconnect.setTime") {
|
||||||
// Set time to current time
|
// Set time to current time
|
||||||
err = dev.SetTime(time.Now())
|
err = dev.SetTime(time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error setting current time on connected InfiniTime")
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If config specifies to notify on reconnect
|
// If config specifies to notify on reconnect
|
||||||
if viper.GetBool("on.reconnect.notify") {
|
if k.Bool("on.reconnect.notify") {
|
||||||
// Send notification to InfiniTime
|
// Send notification to InfiniTime
|
||||||
err = dev.Notify("itd", "Successfully reconnected")
|
err = dev.Notify("itd", "Successfully reconnected")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error sending notification to InfiniTime")
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
// FS must be updated on reconnect
|
||||||
|
updateFS = true
|
||||||
|
// Resend weather on reconnect
|
||||||
|
sendWeatherCh <- struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
// Get firmware version
|
// Get firmware version
|
||||||
ver, err := dev.Version()
|
ver, err := dev.Version()
|
||||||
@@ -74,7 +115,7 @@ func main() {
|
|||||||
log.Info().Str("version", ver).Msg("Connected to InfiniTime")
|
log.Info().Str("version", ver).Msg("Connected to InfiniTime")
|
||||||
|
|
||||||
// If config specifies to notify on connect
|
// If config specifies to notify on connect
|
||||||
if viper.GetBool("on.connect.notify") {
|
if k.Bool("on.connect.notify") {
|
||||||
// Send notification to InfiniTime
|
// Send notification to InfiniTime
|
||||||
err = dev.Notify("itd", "Successfully connected")
|
err = dev.Notify("itd", "Successfully connected")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -97,7 +138,7 @@ func main() {
|
|||||||
// Start control socket
|
// Start control socket
|
||||||
err = initCallNotifs(dev)
|
err = initCallNotifs(dev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error starting socket")
|
log.Error().Err(err).Msg("Error initializing call notifications")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize notification relay
|
// Initialize notification relay
|
||||||
@@ -106,6 +147,12 @@ func main() {
|
|||||||
log.Error().Err(err).Msg("Error initializing notification relay")
|
log.Error().Err(err).Msg("Error initializing notification relay")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initializa weather
|
||||||
|
err = initWeather(dev)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error initializing weather")
|
||||||
|
}
|
||||||
|
|
||||||
// Start control socket
|
// Start control socket
|
||||||
err = startSocket(dev)
|
err = startSocket(dev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -115,3 +162,25 @@ func main() {
|
|||||||
// Block forever
|
// Block forever
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func onReqPasskey() (uint32, error) {
|
||||||
|
var out uint32
|
||||||
|
if isatty.IsTerminal(os.Stdin.Fd()) {
|
||||||
|
fmt.Print("Passkey: ")
|
||||||
|
_, err := fmt.Scanln(&out)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
passkey, ok, err := dlgs.Entry("Pairing", "Enter the passkey displayed on your watch.", "")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
passkeyInt, err := strconv.Atoi(passkey)
|
||||||
|
return uint32(passkeyInt), err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|||||||
55
music.go
55
music.go
@@ -20,51 +20,32 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
"go.arsenm.dev/infinitime/pkg/player"
|
"go.arsenm.dev/infinitime/pkg/player"
|
||||||
|
"go.arsenm.dev/itd/translit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initMusicCtrl(dev *infinitime.Device) error {
|
func initMusicCtrl(dev *infinitime.Device) error {
|
||||||
// On player status change, set status
|
player.Init()
|
||||||
err := player.Status(func(newStatus bool) {
|
|
||||||
if !firmwareUpdating {
|
|
||||||
dev.Music.SetStatus(newStatus)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// On player title change, set track
|
maps := k.Strings("notifs.translit.use")
|
||||||
err = player.Metadata("title", func(newTitle string) {
|
translit.Transliterators["custom"] = translit.Map(k.Strings("notifs.translit.custom"))
|
||||||
if !firmwareUpdating {
|
|
||||||
dev.Music.SetTrack(newTitle)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// On player album change, set album
|
player.OnChange(func(ct player.ChangeType, val string) {
|
||||||
err = player.Metadata("album", func(newAlbum string) {
|
newVal := translit.Transliterate(val, maps...)
|
||||||
if !firmwareUpdating {
|
if !firmwareUpdating {
|
||||||
dev.Music.SetAlbum(newAlbum)
|
switch ct {
|
||||||
|
case player.ChangeTypeStatus:
|
||||||
|
dev.Music.SetStatus(val == "Playing")
|
||||||
|
case player.ChangeTypeTitle:
|
||||||
|
dev.Music.SetTrack(newVal)
|
||||||
|
case player.ChangeTypeAlbum:
|
||||||
|
dev.Music.SetAlbum(newVal)
|
||||||
|
case player.ChangeTypeArtist:
|
||||||
|
dev.Music.SetArtist(newVal)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// On player artist change, set artist
|
|
||||||
err = player.Metadata("artist", func(newArtist string) {
|
|
||||||
if !firmwareUpdating {
|
|
||||||
dev.Music.SetArtist(newArtist)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Watch for music events
|
// Watch for music events
|
||||||
musicEvtCh, err := dev.Music.WatchEvents()
|
musicEvtCh, err := dev.Music.WatchEvents()
|
||||||
@@ -85,9 +66,9 @@ func initMusicCtrl(dev *infinitime.Device) error {
|
|||||||
case infinitime.MusicEventPrev:
|
case infinitime.MusicEventPrev:
|
||||||
player.Prev()
|
player.Prev()
|
||||||
case infinitime.MusicEventVolUp:
|
case infinitime.MusicEventVolUp:
|
||||||
player.VolUp(viper.GetUint("music.vol.interval"))
|
player.VolUp(uint(k.Int("music.vol.interval")))
|
||||||
case infinitime.MusicEventVolDown:
|
case infinitime.MusicEventVolDown:
|
||||||
player.VolDown(viper.GetUint("music.vol.interval"))
|
player.VolDown(uint(k.Int("music.vol.interval")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
13
notifs.go
13
notifs.go
@@ -23,14 +23,13 @@ import (
|
|||||||
|
|
||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
"go.arsenm.dev/itd/translit"
|
"go.arsenm.dev/itd/translit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initNotifRelay(dev *infinitime.Device) error {
|
func initNotifRelay(dev *infinitime.Device) error {
|
||||||
// Connect to dbus session bus
|
// Connect to dbus session bus
|
||||||
bus, err := dbus.SessionBus()
|
bus, err := newSessionBusConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -72,8 +71,8 @@ func initNotifRelay(dev *infinitime.Device) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
maps := viper.GetStringSlice("notifs.translit.use")
|
maps := k.Strings("notifs.translit.use")
|
||||||
translit.Transliterators["custom"] = translit.Map(viper.GetStringSlice("notifs.translit.custom"))
|
translit.Transliterators["custom"] = translit.Map(k.Strings("notifs.translit.custom"))
|
||||||
sender = translit.Transliterate(sender, maps...)
|
sender = translit.Transliterate(sender, maps...)
|
||||||
summary = translit.Transliterate(summary, maps...)
|
summary = translit.Transliterate(summary, maps...)
|
||||||
body = translit.Transliterate(body, maps...)
|
body = translit.Transliterate(body, maps...)
|
||||||
@@ -97,9 +96,9 @@ func initNotifRelay(dev *infinitime.Device) error {
|
|||||||
|
|
||||||
// ignored checks whether any fields were ignored in the config
|
// ignored checks whether any fields were ignored in the config
|
||||||
func ignored(sender, summary, body string) bool {
|
func ignored(sender, summary, body string) bool {
|
||||||
ignoreSender := viper.GetStringSlice("notifs.ignore.sender")
|
ignoreSender := k.Strings("notifs.ignore.sender")
|
||||||
ignoreSummary := viper.GetStringSlice("notifs.ignore.summary")
|
ignoreSummary := k.Strings("notifs.ignore.summary")
|
||||||
ignoreBody := viper.GetStringSlice("notifs.ignore.body")
|
ignoreBody := k.Strings("notifs.ignore.body")
|
||||||
return strSlcContains(ignoreSender, sender) ||
|
return strSlcContains(ignoreSender, sender) ||
|
||||||
strSlcContains(ignoreSummary, summary) ||
|
strSlcContains(ignoreSummary, summary) ||
|
||||||
strSlcContains(ignoreBody, body)
|
strSlcContains(ignoreBody, body)
|
||||||
|
|||||||
@@ -301,7 +301,7 @@ var Transliterators = map[string]Transliterator{
|
|||||||
"Ð", "D",
|
"Ð", "D",
|
||||||
"ð", "d",
|
"ð", "d",
|
||||||
},
|
},
|
||||||
"Czeck": Map{
|
"Czech": Map{
|
||||||
"ř", "r",
|
"ř", "r",
|
||||||
"ě", "e",
|
"ě", "e",
|
||||||
"ý", "y",
|
"ý", "y",
|
||||||
@@ -327,11 +327,40 @@ var Transliterators = map[string]Transliterator{
|
|||||||
"ÿ", "y",
|
"ÿ", "y",
|
||||||
"ç", "c",
|
"ç", "c",
|
||||||
},
|
},
|
||||||
|
"Romanian": Map{
|
||||||
|
"ă", "a",
|
||||||
|
"Ă", "A",
|
||||||
|
"â", "a",
|
||||||
|
"Â", "A",
|
||||||
|
"î", "i",
|
||||||
|
"Î", "I",
|
||||||
|
"ș", "s",
|
||||||
|
"Ș", "S",
|
||||||
|
"ț", "t",
|
||||||
|
"Ț", "T",
|
||||||
|
"ş", "s",
|
||||||
|
"Ş", "S",
|
||||||
|
"ţ", "t",
|
||||||
|
"Ţ", "T",
|
||||||
|
"„", "\"",
|
||||||
|
"”", "\"",
|
||||||
|
},
|
||||||
"Emoji": Map{
|
"Emoji": Map{
|
||||||
"😂", ":')",
|
"😂", "XD",
|
||||||
|
"🤣", "XD",
|
||||||
"😊", ":)",
|
"😊", ":)",
|
||||||
"😃", ":)",
|
"☺️", ":)",
|
||||||
|
"😌", ":)",
|
||||||
|
"😃", ":D",
|
||||||
|
"😁", ":D",
|
||||||
|
"😋", ":P",
|
||||||
|
"😛", ":P",
|
||||||
|
"😜", ";P",
|
||||||
|
"🙃", "(:",
|
||||||
|
"😎", "8)",
|
||||||
|
"😶", ":#",
|
||||||
"😩", "-_-",
|
"😩", "-_-",
|
||||||
|
"😕", ":(",
|
||||||
"😏", ":‑J",
|
"😏", ":‑J",
|
||||||
"💜", "<3",
|
"💜", "<3",
|
||||||
"💖", "<3",
|
"💖", "<3",
|
||||||
@@ -343,12 +372,37 @@ var Transliterators = map[string]Transliterator{
|
|||||||
"💓", "<3",
|
"💓", "<3",
|
||||||
"💚", "<3",
|
"💚", "<3",
|
||||||
"💙", "<3",
|
"💙", "<3",
|
||||||
|
"💟", "<3",
|
||||||
|
"❣️", "<3!",
|
||||||
"💔", "</3",
|
"💔", "</3",
|
||||||
"😱", "D:",
|
"😱", "D:",
|
||||||
"😮", ":O",
|
"😮", ":O",
|
||||||
"😝", ":P",
|
"😯", ":O",
|
||||||
"😍", ":x",
|
"😝", "xP",
|
||||||
"😢", ":(",
|
"🤔", "',:-|",
|
||||||
|
"😔", ":|",
|
||||||
|
"😍", ":*",
|
||||||
|
"😘", ":*",
|
||||||
|
"😚", ":*",
|
||||||
|
"😙", ":*",
|
||||||
|
"👍", ":thumbsup:",
|
||||||
|
"👌", ":ok_hand:",
|
||||||
|
"🤞", ":crossed_fingers:",
|
||||||
|
"✌️", ":victory_hand:",
|
||||||
|
"🌄", ":sunrise_over_mountains:",
|
||||||
|
"🌞", ":sun_with_face:",
|
||||||
|
"🤗", ":hugging_face:",
|
||||||
|
"🌻", ":sunflower:",
|
||||||
|
"🥱", ":yawning_face:",
|
||||||
|
"🙄", ":face_with_rolling_eyes:",
|
||||||
|
"🔫", ":gun:",
|
||||||
|
"🥔", ":potato:",
|
||||||
|
"😬", ":E",
|
||||||
|
"✨", "***",
|
||||||
|
"🌌", "***",
|
||||||
|
"💀", "8-X",
|
||||||
|
"😅", "':D",
|
||||||
|
"😢", ":'(",
|
||||||
"💯", ":100:",
|
"💯", ":100:",
|
||||||
"🔥", ":fire:",
|
"🔥", ":fire:",
|
||||||
"😉", ";)",
|
"😉", ";)",
|
||||||
|
|||||||
1
version.txt
Normal file
1
version.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
unknown
|
||||||
277
weather.go
Normal file
277
weather.go
Normal file
@@ -0,0 +1,277 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"go.arsenm.dev/infinitime"
|
||||||
|
"go.arsenm.dev/infinitime/weather"
|
||||||
|
)
|
||||||
|
|
||||||
|
// METResponse represents a response from
|
||||||
|
// the MET Norway API
|
||||||
|
type METResponse struct {
|
||||||
|
Properties struct {
|
||||||
|
Timeseries []struct {
|
||||||
|
Time time.Time
|
||||||
|
Data METData
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// METData represents data in a METResponse
|
||||||
|
type METData struct {
|
||||||
|
Instant struct {
|
||||||
|
Details struct {
|
||||||
|
AirPressure float32 `json:"air_pressure_at_sea_level"`
|
||||||
|
AirTemperature float32 `json:"air_temperature"`
|
||||||
|
DewPoint float32 `json:"dew_point_temperature"`
|
||||||
|
CloudAreaFraction float32 `json:"cloud_area_fraction"`
|
||||||
|
FogAreaFraction float32 `json:"fog_area_fraction"`
|
||||||
|
RelativeHumidity float32 `json:"relative_humidity"`
|
||||||
|
UVIndex float32 `json:"ultraviolet_index_clear_sky"`
|
||||||
|
WindDirection float32 `json:"wind_from_direction"`
|
||||||
|
WindSpeed float32 `json:"wind_speed"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NextHour struct {
|
||||||
|
Summary struct {
|
||||||
|
SymbolCode string `json:"symbol_code"`
|
||||||
|
}
|
||||||
|
Details struct {
|
||||||
|
PrecipitationAmount float32 `json:"precipitation_amount"`
|
||||||
|
}
|
||||||
|
} `json:"next_1_hours"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSMData represents lat/long data from
|
||||||
|
// OpenStreetMap Nominatim
|
||||||
|
type OSMData []struct {
|
||||||
|
Lat string `json:"lat"`
|
||||||
|
Lon string `json:"lon"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var sendWeatherCh = make(chan struct{}, 1)
|
||||||
|
|
||||||
|
func initWeather(dev *infinitime.Device) error {
|
||||||
|
if !k.Bool("weather.enabled") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get location based on string in config
|
||||||
|
lat, lon, err := getLocation(k.String("weather.location"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
timer := time.NewTimer(time.Hour)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
// Attempt to get weather
|
||||||
|
data, err := getWeather(lat, lon)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn().Err(err).Msg("Error getting weather data")
|
||||||
|
// Wait 15 minutes before retrying
|
||||||
|
time.Sleep(15 * time.Minute)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get current data
|
||||||
|
current := data.Properties.Timeseries[0]
|
||||||
|
currentData := current.Data.Instant.Details
|
||||||
|
|
||||||
|
// Add temperature event
|
||||||
|
err = dev.AddWeatherEvent(weather.TemperatureEvent{
|
||||||
|
TimelineHeader: weather.NewHeader(
|
||||||
|
weather.EventTypeTemperature,
|
||||||
|
time.Hour,
|
||||||
|
),
|
||||||
|
Temperature: int16(round(currentData.AirTemperature * 100)),
|
||||||
|
DewPoint: int16(round(currentData.DewPoint)),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error adding temperature event")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add precipitation event
|
||||||
|
err = dev.AddWeatherEvent(weather.PrecipitationEvent{
|
||||||
|
TimelineHeader: weather.NewHeader(
|
||||||
|
weather.EventTypePrecipitation,
|
||||||
|
time.Hour,
|
||||||
|
),
|
||||||
|
Type: parseSymbol(current.Data.NextHour.Summary.SymbolCode),
|
||||||
|
Amount: uint8(round(current.Data.NextHour.Details.PrecipitationAmount)),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error adding precipitation event")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add wind event
|
||||||
|
err = dev.AddWeatherEvent(weather.WindEvent{
|
||||||
|
TimelineHeader: weather.NewHeader(
|
||||||
|
weather.EventTypeWind,
|
||||||
|
time.Hour,
|
||||||
|
),
|
||||||
|
SpeedMin: uint8(round(currentData.WindSpeed)),
|
||||||
|
SpeedMax: uint8(round(currentData.WindSpeed)),
|
||||||
|
DirectionMin: uint8(round(currentData.WindDirection)),
|
||||||
|
DirectionMax: uint8(round(currentData.WindDirection)),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error adding wind event")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add cloud event
|
||||||
|
err = dev.AddWeatherEvent(weather.CloudsEvent{
|
||||||
|
TimelineHeader: weather.NewHeader(
|
||||||
|
weather.EventTypeClouds,
|
||||||
|
time.Hour,
|
||||||
|
),
|
||||||
|
Amount: uint8(round(currentData.CloudAreaFraction)),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error adding clouds event")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add humidity event
|
||||||
|
err = dev.AddWeatherEvent(weather.HumidityEvent{
|
||||||
|
TimelineHeader: weather.NewHeader(
|
||||||
|
weather.EventTypeHumidity,
|
||||||
|
time.Hour,
|
||||||
|
),
|
||||||
|
Humidity: uint8(round(currentData.RelativeHumidity)),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error adding humidity event")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add pressure event
|
||||||
|
err = dev.AddWeatherEvent(weather.PressureEvent{
|
||||||
|
TimelineHeader: weather.NewHeader(
|
||||||
|
weather.EventTypePressure,
|
||||||
|
time.Hour,
|
||||||
|
),
|
||||||
|
Pressure: int16(round(currentData.AirPressure)),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error adding pressure event")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset timer to 1 hour
|
||||||
|
timer.Stop()
|
||||||
|
timer.Reset(time.Hour)
|
||||||
|
|
||||||
|
// Wait for timer to fire or manual update signal
|
||||||
|
select {
|
||||||
|
case <-timer.C:
|
||||||
|
case <-sendWeatherCh:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getLocation returns the latitude and longitude
|
||||||
|
// given a location
|
||||||
|
func getLocation(loc string) (lat, lon float64, err error) {
|
||||||
|
// Create request URL and perform GET request
|
||||||
|
reqURL := fmt.Sprintf("https://nominatim.openstreetmap.org/search.php?q=%s&format=jsonv2", url.QueryEscape(loc))
|
||||||
|
res, err := http.Get(reqURL)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode JSON from response into OSMData
|
||||||
|
data := OSMData{}
|
||||||
|
err = json.NewDecoder(res.Body).Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// If no data points
|
||||||
|
if len(data) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get first data point
|
||||||
|
out := data[0]
|
||||||
|
|
||||||
|
// Attempt to parse latitude
|
||||||
|
lat, err = strconv.ParseFloat(out.Lat, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Attempt to parse longitude
|
||||||
|
lon, err = strconv.ParseFloat(out.Lon, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// getWeather gets weather data given a latitude and longitude
|
||||||
|
func getWeather(lat, lon float64) (*METResponse, error) {
|
||||||
|
// Create new GET request
|
||||||
|
req, err := http.NewRequest(
|
||||||
|
http.MethodGet,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=%.2f&lon=%.2f",
|
||||||
|
lat,
|
||||||
|
lon,
|
||||||
|
),
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set identifying user agent as per NMI requirements
|
||||||
|
req.Header.Set("User-Agent", fmt.Sprintf("ITD/%s gitea.arsenm.dev/Arsen6331/itd", version))
|
||||||
|
|
||||||
|
// Perform request
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode JSON from response to METResponse struct
|
||||||
|
out := &METResponse{}
|
||||||
|
err = json.NewDecoder(res.Body).Decode(out)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseSymbol determines what type of precipitation a symbol code
|
||||||
|
// codes for.
|
||||||
|
func parseSymbol(symCode string) weather.PrecipitationType {
|
||||||
|
switch {
|
||||||
|
case strings.Contains(symCode, "lightrain"):
|
||||||
|
return weather.PrecipitationTypeRain
|
||||||
|
case strings.Contains(symCode, "rain"):
|
||||||
|
return weather.PrecipitationTypeRain
|
||||||
|
case strings.Contains(symCode, "snow"):
|
||||||
|
return weather.PrecipitationTypeSnow
|
||||||
|
case strings.Contains(symCode, "sleet"):
|
||||||
|
return weather.PrecipitationTypeSleet
|
||||||
|
case strings.Contains(symCode, "snow"):
|
||||||
|
return weather.PrecipitationTypeSnow
|
||||||
|
default:
|
||||||
|
return weather.PrecipitationTypeNone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// round rounds 32-bit floats to 32-bit integers
|
||||||
|
func round(f float32) int32 {
|
||||||
|
return int32(math.Round(float64(f)))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user