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 (
|
2022-05-11 20:24:12 +00:00
|
|
|
"context"
|
2022-02-22 00:18:52 +00:00
|
|
|
_ "embed"
|
2022-02-22 16:43:29 +00:00
|
|
|
"flag"
|
2021-12-17 05:32:06 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-05-11 20:24:12 +00:00
|
|
|
"os/signal"
|
2021-12-17 05:32:06 +00:00
|
|
|
"strconv"
|
2022-05-11 20:24:12 +00:00
|
|
|
"syscall"
|
2021-08-21 08:19:49 +00:00
|
|
|
"time"
|
|
|
|
|
2021-12-17 05:32:06 +00:00
|
|
|
"github.com/gen2brain/dlgs"
|
2022-02-22 00:18:52 +00:00
|
|
|
"github.com/knadh/koanf"
|
2021-12-17 05:32:06 +00:00
|
|
|
"github.com/mattn/go-isatty"
|
2022-04-16 11:28:53 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-08-21 08:19:49 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.arsenm.dev/infinitime"
|
|
|
|
)
|
|
|
|
|
2022-02-21 19:20:02 +00:00
|
|
|
var k = koanf.New(".")
|
|
|
|
|
2021-11-26 04:35:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-24 03:20:13 +00:00
|
|
|
level, err := zerolog.ParseLevel(k.String("logging.level"))
|
|
|
|
if err != nil || level == zerolog.NoLevel {
|
|
|
|
level = zerolog.InfoLevel
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:43:29 +00:00
|
|
|
// Initialize infinitime library
|
2022-05-03 03:17:38 +00:00
|
|
|
infinitime.Init(k.String("bluetooth.adapter"))
|
2021-08-21 08:19:49 +00:00
|
|
|
// Cleanly exit after function
|
|
|
|
defer infinitime.Exit()
|
|
|
|
|
2022-02-21 10:47:48 +00:00
|
|
|
// Create infinitime options struct
|
|
|
|
opts := &infinitime.Options{
|
2022-02-21 19:20:02 +00:00
|
|
|
AttemptReconnect: k.Bool("conn.reconnect"),
|
|
|
|
WhitelistEnabled: k.Bool("conn.whitelist.enabled"),
|
|
|
|
Whitelist: k.Strings("conn.whitelist.devices"),
|
2021-12-17 05:32:06 +00:00
|
|
|
OnReqPasskey: onReqPasskey,
|
2022-04-16 11:28:53 +00:00
|
|
|
Logger: log.Logger,
|
2022-04-24 03:20:13 +00:00
|
|
|
LogLevel: level,
|
2022-02-21 10:47:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:24:12 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
go func() {
|
|
|
|
<-sigCh
|
|
|
|
cancel()
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
signal.Notify(
|
|
|
|
sigCh,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
)
|
|
|
|
|
2022-02-21 10:47:48 +00:00
|
|
|
// Connect to InfiniTime with default options
|
2022-05-11 20:24:12 +00:00
|
|
|
dev, err := infinitime.Connect(ctx, opts)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
2021-12-17 05:32:06 +00:00
|
|
|
log.Fatal().Err(err).Msg("Error connecting to InfiniTime")
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When InfiniTime reconnects
|
2022-02-21 10:47:48 +00:00
|
|
|
opts.OnReconnect = func() {
|
2022-02-21 19:20:02 +00:00
|
|
|
if k.Bool("on.reconnect.setTime") {
|
2021-08-24 15:33:41 +00:00
|
|
|
// Set time to current time
|
|
|
|
err = dev.SetTime(time.Now())
|
|
|
|
if err != nil {
|
2022-02-21 10:47:48 +00:00
|
|
|
return
|
2021-08-24 15:33:41 +00:00
|
|
|
}
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If config specifies to notify on reconnect
|
2022-02-21 19:20:02 +00:00
|
|
|
if k.Bool("on.reconnect.notify") {
|
2021-08-21 08:19:49 +00:00
|
|
|
// Send notification to InfiniTime
|
|
|
|
err = dev.Notify("itd", "Successfully reconnected")
|
|
|
|
if err != nil {
|
2022-02-21 10:47:48 +00:00
|
|
|
return
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-26 04:35:03 +00:00
|
|
|
|
2022-02-22 00:18:52 +00:00
|
|
|
// FS must be updated on reconnect
|
2021-11-26 04:35:03 +00:00
|
|
|
updateFS = true
|
2022-02-22 00:18:52 +00:00
|
|
|
// Resend weather on reconnect
|
|
|
|
sendWeatherCh <- struct{}{}
|
2022-02-21 10:47:48 +00:00
|
|
|
}
|
2021-08-21 08:19:49 +00:00
|
|
|
|
|
|
|
// Get firmware version
|
|
|
|
ver, err := dev.Version()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error getting firmware version")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log connection
|
|
|
|
log.Info().Str("version", ver).Msg("Connected to InfiniTime")
|
|
|
|
|
|
|
|
// 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().Err(err).Msg("Error sending notification to InfiniTime")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set time to current time
|
|
|
|
err = dev.SetTime(time.Now())
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error setting current time on connected InfiniTime")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize music controls
|
|
|
|
err = initMusicCtrl(dev)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error initializing music control")
|
|
|
|
}
|
|
|
|
|
2021-10-15 07:25:34 +00:00
|
|
|
// Start control socket
|
2022-05-11 20:24:12 +00:00
|
|
|
err = initCallNotifs(ctx, dev)
|
2021-10-15 07:25:34 +00:00
|
|
|
if err != nil {
|
2021-12-17 08:31:05 +00:00
|
|
|
log.Error().Err(err).Msg("Error initializing call notifications")
|
2021-10-15 07:25:34 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 08:19:49 +00:00
|
|
|
// Initialize notification relay
|
2022-05-11 20:24:12 +00:00
|
|
|
err = initNotifRelay(ctx, dev)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error initializing notification relay")
|
|
|
|
}
|
|
|
|
|
2022-02-22 00:27:04 +00:00
|
|
|
// Initializa weather
|
2022-05-11 20:24:12 +00:00
|
|
|
err = initWeather(ctx, dev)
|
2022-02-22 00:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error initializing weather")
|
|
|
|
}
|
2022-02-22 00:18:52 +00:00
|
|
|
|
2022-05-11 01:03:37 +00:00
|
|
|
// Initialize metrics collection
|
2022-05-11 20:24:12 +00:00
|
|
|
err = initMetrics(ctx, dev)
|
2022-05-11 01:03:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error intializing metrics collection")
|
|
|
|
}
|
|
|
|
|
2022-11-07 20:22:14 +00:00
|
|
|
// Initialize metrics collection
|
|
|
|
err = initPureMaps(ctx, dev)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error intializing puremaps integration")
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:19:49 +00:00
|
|
|
// Start control socket
|
2022-05-13 00:14:34 +00:00
|
|
|
err = startSocket(ctx, dev)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error starting socket")
|
|
|
|
}
|
|
|
|
// Block forever
|
|
|
|
select {}
|
|
|
|
}
|
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
|
2022-02-21 10:47:48 +00:00
|
|
|
}
|