itd/main.go

228 lines
5.1 KiB
Go
Raw Permalink Normal View History

2021-08-21 08:19:49 +00:00
/*
* 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
import (
"context"
2022-02-22 00:18:52 +00:00
_ "embed"
2022-02-22 16:43:29 +00:00
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
2021-08-21 08:19:49 +00:00
"time"
"github.com/gen2brain/dlgs"
2022-02-22 00:18:52 +00:00
"github.com/knadh/koanf"
"github.com/mattn/go-isatty"
"go.elara.ws/itd/infinitime"
2023-04-21 02:54:58 +00:00
"go.elara.ws/logger"
"go.elara.ws/logger/log"
2021-08-21 08:19:49 +00:00
)
2022-02-21 19:20:02 +00:00
var k = koanf.New(".")
var (
firmwareUpdating = false
// The FS must be updated when the watch is reconnected
updateFS = false
)
2021-08-21 08:19:49 +00:00
func main() {
2022-02-22 16:43:29 +00:00
showVer := flag.Bool("version", false, "Show version number and exit")
flag.Parse()
// If version requested, print and exit
if *showVer {
fmt.Println(version)
return
}
level, err := logger.ParseLogLevel(k.String("logging.level"))
if err != nil {
level = logger.LogLevelInfo
2022-04-24 03:20:13 +00:00
}
log.Logger.SetLevel(level)
2021-08-21 08:19:49 +00:00
// Create infinitime options struct
opts := infinitime.Options{
OnReconnect: func(dev *infinitime.Device) {
if k.Bool("on.reconnect.setTime") {
// Set time to current time
err = dev.SetTime(time.Now())
if err != nil {
return
}
}
// If config specifies to notify on reconnect
if k.Bool("on.reconnect.notify") {
// Send notification to InfiniTime
err = dev.Notify("itd", "Successfully reconnected")
if err != nil {
return
}
}
// FS must be updated on reconnect
updateFS = true
// Resend weather on reconnect
sendWeatherCh <- struct{}{}
},
}
ctx := context.Background()
// Connect to InfiniTime with default options
dev, err := infinitime.Connect(opts)
2021-08-21 08:19:49 +00:00
if err != nil {
log.Fatal("Error connecting to InfiniTime").Err(err).Send()
2021-08-21 08:19:49 +00:00
}
// Get firmware version
ver, err := dev.Version()
if err != nil {
log.Error("Error getting firmware version").Err(err).Send()
2021-08-21 08:19:49 +00:00
}
// Log connection
log.Info("Connected to InfiniTime").Str("version", ver).Send()
2021-08-21 08:19:49 +00:00
// If config specifies to notify on connect
2022-02-21 19:20:02 +00:00
if k.Bool("on.connect.notify") {
2021-08-21 08:19:49 +00:00
// Send notification to InfiniTime
err = dev.Notify("itd", "Successfully connected")
if err != nil {
log.Error("Error sending notification to InfiniTime").Err(err).Send()
2021-08-21 08:19:49 +00:00
}
}
// Set time to current time
err = dev.SetTime(time.Now())
if err != nil {
log.Error("Error setting current time on connected InfiniTime").Err(err).Send()
2021-08-21 08:19:49 +00:00
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
sig := <-sigCh
log.Warn("Signal received, shutting down").Stringer("signal", sig).Send()
cancel()
}()
wg := WaitGroup{&sync.WaitGroup{}}
2021-08-21 08:19:49 +00:00
// Initialize music controls
err = initMusicCtrl(ctx, wg, dev)
2021-08-21 08:19:49 +00:00
if err != nil {
log.Error("Error initializing music control").Err(err).Send()
2021-08-21 08:19:49 +00:00
}
// Start control socket
err = initCallNotifs(ctx, wg, dev)
if err != nil {
log.Error("Error initializing call notifications").Err(err).Send()
}
2021-08-21 08:19:49 +00:00
// Initialize notification relay
err = initNotifRelay(ctx, wg, dev)
2021-08-21 08:19:49 +00:00
if err != nil {
log.Error("Error initializing notification relay").Err(err).Send()
2021-08-21 08:19:49 +00:00
}
2022-02-22 00:27:04 +00:00
// Initializa weather
err = initWeather(ctx, wg, dev)
2022-02-22 00:27:04 +00:00
if err != nil {
log.Error("Error initializing weather").Err(err).Send()
2022-02-22 00:27:04 +00:00
}
2022-02-22 00:18:52 +00:00
2022-05-11 01:03:37 +00:00
// Initialize metrics collection
err = initMetrics(ctx, wg, dev)
2022-05-11 01:03:37 +00:00
if err != nil {
log.Error("Error intializing metrics collection").Err(err).Send()
2022-05-11 01:03:37 +00:00
}
// Initialize puremaps integration
err = initPureMaps(ctx, wg, dev)
2022-11-07 20:22:14 +00:00
if err != nil {
log.Error("Error intializing puremaps integration").Err(err).Send()
2022-11-07 20:22:14 +00:00
}
// Start fuse socket
if k.Bool("fuse.enabled") {
err = startFUSE(ctx, wg, dev)
if err != nil {
log.Error("Error starting fuse socket").Err(err).Send()
}
}
2021-08-21 08:19:49 +00:00
// Start control socket
err = startSocket(ctx, wg, dev)
2021-08-21 08:19:49 +00:00
if err != nil {
log.Error("Error starting socket").Err(err).Send()
2021-08-21 08:19:49 +00:00
}
wg.Wait()
}
type x struct {
n int
*sync.WaitGroup
}
func (xy *x) Add(i int) {
xy.n += i
xy.WaitGroup.Add(i)
fmt.Println("add: counter:", xy.n)
}
func (xy *x) Done() {
xy.n -= 1
xy.WaitGroup.Done()
fmt.Println("done: counter:", xy.n)
2021-08-21 08:19:49 +00:00
}
2021-12-17 08:31:05 +00:00
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
}