From 9e63401db3e760b852f34d5065b6fd4f60163943 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Wed, 23 Feb 2022 21:22:03 -0800 Subject: [PATCH] Add update weather command to itctl --- api/weather.go | 17 +++++++++++++++ cmd/itctl/main.go | 1 + cmd/itctl/update/time.go | 44 ++++++++++++++++++++++++++++++++++++++ cmd/itctl/update/update.go | 35 ++++++++++++++++++++++++++++++ internal/types/types.go | 1 + socket.go | 4 ++++ weather.go | 7 ++++-- 7 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 api/weather.go create mode 100644 cmd/itctl/update/time.go create mode 100644 cmd/itctl/update/update.go diff --git a/api/weather.go b/api/weather.go new file mode 100644 index 0000000..5d937c2 --- /dev/null +++ b/api/weather.go @@ -0,0 +1,17 @@ +package api + +import ( + "go.arsenm.dev/itd/internal/types" +) + +// UpdateWeather sends the update weather signal, +// immediately sending current weather data +func (c *Client) UpdateWeather() error { + _, err := c.request(types.Request{ + Type: types.ReqTypeWeatherUpdate, + }) + if err != nil { + return err + } + return nil +} diff --git a/cmd/itctl/main.go b/cmd/itctl/main.go index cc88f7d..500cffd 100644 --- a/cmd/itctl/main.go +++ b/cmd/itctl/main.go @@ -26,6 +26,7 @@ import ( _ "go.arsenm.dev/itd/cmd/itctl/set" _ "go.arsenm.dev/itd/cmd/itctl/watch" _ "go.arsenm.dev/itd/cmd/itctl/filesystem" + _ "go.arsenm.dev/itd/cmd/itctl/update" "os" diff --git a/cmd/itctl/update/time.go b/cmd/itctl/update/time.go new file mode 100644 index 0000000..e8cda34 --- /dev/null +++ b/cmd/itctl/update/time.go @@ -0,0 +1,44 @@ +/* + * 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 . + */ + +package update + +import ( + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.arsenm.dev/itd/api" +) + +// weatherCmd represents the time command +var weatherCmd = &cobra.Command{ + Use: `weather`, + Short: "Force an immediate update of weather data", + Run: func(cmd *cobra.Command, args []string) { + client := viper.Get("client").(*api.Client) + + err := client.UpdateWeather() + if err != nil { + log.Fatal().Err(err).Msg("Error updating weather") + } + }, +} + +func init() { + updateCmd.AddCommand(weatherCmd) +} diff --git a/cmd/itctl/update/update.go b/cmd/itctl/update/update.go new file mode 100644 index 0000000..2159545 --- /dev/null +++ b/cmd/itctl/update/update.go @@ -0,0 +1,35 @@ +/* + * 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 . + */ + +package update + +import ( + "github.com/spf13/cobra" + "go.arsenm.dev/itd/cmd/itctl/root" +) + +// updateCmd represents the update command +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Update information on InfiniTime", + Aliases: []string{"upd"}, +} + +func init() { + root.RootCmd.AddCommand(updateCmd) +} diff --git a/internal/types/types.go b/internal/types/types.go index 725fdcf..13c8507 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -21,6 +21,7 @@ const ( ReqTypeWatchStepCount ReqTypeCancel ReqTypeFS + ReqTypeWeatherUpdate ) const ( diff --git a/socket.go b/socket.go index 6f461f7..94f1d14 100644 --- a/socket.go +++ b/socket.go @@ -642,6 +642,10 @@ func handleConnection(conn net.Conn, dev *infinitime.Device, fs *blefs.FS) { }, }) } + case types.ReqTypeWeatherUpdate: + // Send weather update signal + sendWeatherCh <- struct{}{} + json.NewEncoder(conn).Encode(types.Response{Type: req.Type}) case types.ReqTypeCancel: if req.Data == nil { connErr(conn, req.Type, nil, "No data provided. Cancel request requires request ID string as data.") diff --git a/weather.go b/weather.go index f3e18d9..0f11e82 100644 --- a/weather.go +++ b/weather.go @@ -75,6 +75,7 @@ func initWeather(dev *infinitime.Device) error { go func() { for { + fmt.Println("weather update") // Attempt to get weather data, err := getWeather(lat, lon) if err != nil { @@ -165,12 +166,14 @@ func initWeather(dev *infinitime.Device) error { 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: - timer.Stop() case <-sendWeatherCh: - timer.Stop() } } }()