Skip to content

Commit

Permalink
Merge pull request #83 from CameronRP/fix-set-time
Browse files Browse the repository at this point in the history
Update clock page
  • Loading branch information
CameronRP authored Nov 4, 2019
2 parents 8d4b00e + 9991df6 commit 5e5959a
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 319 deletions.
23 changes: 0 additions & 23 deletions a_managementinterface-packr.go

This file was deleted.

98 changes: 98 additions & 0 deletions api/clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
management-interface - Web based management of Raspberry Pis over WiFi
Copyright (C) 2019, The Cacophony Project
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 api

import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"strings"
"time"

"github.com/TheCacophonyProject/rtc-utils/rtc"
)

const (
timeFormat = "2006-01-02T15:04:05Z07:00"
dateCmdFormat = "+%Y-%m-%dT%H:%M:%S%:z"
)

type clockInfo struct {
RTCTimeUTC string
RTCTimeLocal string
SystemTime string
LowRTCBattery bool
RTCIntegrity bool
NTPSynced bool
}

func (api *ManagementAPI) GetClock(w http.ResponseWriter, r *http.Request) {
out, err := exec.Command("date", dateCmdFormat).CombinedOutput()
if err != nil {
serverError(&w, err)
return
}
systemTime, err := time.Parse(timeFormat, strings.TrimSpace(string(out)))
if err != nil {
serverError(&w, err)
return
}
ntpSynced, err := rtc.IsNTPSynced()
if err != nil {
serverError(&w, err)
return
}
rtcState, err := rtc.State(1)
if err != nil {
serverError(&w, err)
return
}

b, err := json.Marshal(&clockInfo{
RTCTimeUTC: rtcState.Time.UTC().Format(timeFormat),
RTCTimeLocal: rtcState.Time.Local().Format(timeFormat),
SystemTime: systemTime.Format(timeFormat),
LowRTCBattery: rtcState.LowBattery,
RTCIntegrity: rtcState.ClockIntegrity,
NTPSynced: ntpSynced,
})
if err != nil {
serverError(&w, err)
return
}
w.Write(b)
}

func (api *ManagementAPI) PostClock(w http.ResponseWriter, r *http.Request) {
date, err := time.Parse(timeFormat, r.FormValue("date"))
if err != nil {
badRequest(&w, err)
return
}
cmd := exec.Command("date", dateCmdFormat, "--utc", fmt.Sprintf("--set=%s", date.Format(timeFormat)))
_, err = cmd.CombinedOutput()
if err != nil {
serverError(&w, err)
return
}
if err := rtc.Write(1); err != nil {
serverError(&w, err)
}
}
175 changes: 0 additions & 175 deletions clock.go

This file was deleted.

36 changes: 0 additions & 36 deletions cmd/managementd/a_main-packr.go

This file was deleted.

7 changes: 4 additions & 3 deletions cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
router.HandleFunc("/speaker/status", managementinterface.SpeakerStatusHandler).Methods("GET")
router.HandleFunc("/disk-memory", managementinterface.DiskMemoryHandler).Methods("GET")
router.HandleFunc("/location", managementinterface.GenLocationHandler(config.config)).Methods("GET") // Form to view and/or set location manually.
router.HandleFunc("/clock", managementinterface.TimeHandler).Methods("GET", "POST") // Form to view and/or adjust time settings.
router.HandleFunc("/clock", managementinterface.TimeHandler).Methods("GET") // Form to view and/or adjust time settings.
router.HandleFunc("/about", managementinterface.AboutHandlerGen(config.config)).Methods("GET")

router.HandleFunc("/advanced", managementinterface.AdvancedMenuHandler).Methods("GET")
Expand All @@ -92,8 +92,9 @@ func main() {
apiRouter.HandleFunc("/reboot", apiObj.Reboot).Methods("POST")
apiRouter.HandleFunc("/config", apiObj.GetConfig).Methods("GET")
apiRouter.HandleFunc("/clear-config-section", apiObj.ClearConfigSection).Methods("POST")
apiRouter.HandleFunc("/location", apiObj.SetLocation).Methods("POST") // Set location via a POST request.
apiRouter.HandleFunc("/clock", managementinterface.APITimeHandler).Methods("POST") // Set times via a POST request.
apiRouter.HandleFunc("/location", apiObj.SetLocation).Methods("POST") // Set location via a POST request.
apiRouter.HandleFunc("/clock", apiObj.GetClock).Methods("GET")
apiRouter.HandleFunc("/clock", apiObj.PostClock).Methods("POST")
apiRouter.Use(basicAuth)

listenAddr := fmt.Sprintf(":%d", config.Port)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.12
require (
github.com/TheCacophonyProject/go-api v0.0.0-20190923033957-174cea2ac81c
github.com/TheCacophonyProject/go-config v0.0.0-20191003001220-bff132a03491
github.com/TheCacophonyProject/rtc-utils v1.2.0
github.com/gobuffalo/packr v1.30.1
github.com/godbus/dbus v4.1.0+incompatible
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2
gopkg.in/yaml.v2 v2.2.2
)
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ github.com/TheCacophonyProject/go-api v0.0.0-20190923033957-174cea2ac81c h1:oe4a
github.com/TheCacophonyProject/go-api v0.0.0-20190923033957-174cea2ac81c/go.mod h1:FfMpa4cFhNXQ9tuKG18HO6yLExezcJhzjUjBOFocrQw=
github.com/TheCacophonyProject/go-config v0.0.0-20190922224052-7c2a21bc6b88 h1:b3CpL57RUjdlgRmm54qNj37tLEwmJzNDvxOXX7GHYBw=
github.com/TheCacophonyProject/go-config v0.0.0-20190922224052-7c2a21bc6b88/go.mod h1:gPUJLVu408NRz9/P3BrsxzOzLc+KJLrv+jVdDw3RI0Y=
github.com/TheCacophonyProject/go-config v0.0.0-20191001063829-bf05ee481a80 h1:wmj+QLUR90c/DwQq6aUCWL0Ca2ly2kQCayfHPVT7YZQ=
github.com/TheCacophonyProject/go-config v0.0.0-20191001063829-bf05ee481a80/go.mod h1:QCgT+KCrz1CmLVpeeOMl5L8/X1QvWwpsLzR7afTmEJc=
github.com/TheCacophonyProject/go-config v0.0.0-20191002030903-2dd35609b718 h1:yuTvOqpOmhnfiUers0C919RLwFAY22th5cdo+7z0rBI=
github.com/TheCacophonyProject/go-config v0.0.0-20191002030903-2dd35609b718/go.mod h1:WuesghaKfkLjOSR07Aaw3el/KPOSB8rRsL3pg5RMr8o=
github.com/TheCacophonyProject/go-config v0.0.0-20191003001220-bff132a03491 h1:QFWP26EWnV7mDUr6LTLzL7wvRNOpcMgETAGAA+njkUg=
github.com/TheCacophonyProject/go-config v0.0.0-20191003001220-bff132a03491/go.mod h1:WuesghaKfkLjOSR07Aaw3el/KPOSB8rRsL3pg5RMr8o=
github.com/TheCacophonyProject/rtc-utils v1.2.0 h1:570sPJE/s0b21NrP9VVeSI/gBh/dLK+G5Ne/ZFwkNv0=
github.com/TheCacophonyProject/rtc-utils v1.2.0/go.mod h1:uV1SIy93TLZrrBcqDczUNFUz2G/Pk6pZNUdTRglmANU=
github.com/TheCacophonyProject/window v0.0.0-20190821235241-ab92c2ee24b6/go.mod h1:Vww417iimOb0s46Ndsm8U/vYtwc0dZUet4uW8QzBo4M=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alexflint/go-arg v1.1.0/go.mod h1:3Rj4baqzWaGGmZA2+bVTV8zQOZEjBQAPBnL5xLT+ftY=
github.com/alexflint/go-arg v1.2.0/go.mod h1:3Rj4baqzWaGGmZA2+bVTV8zQOZEjBQAPBnL5xLT+ftY=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -126,6 +125,7 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
Expand Down Expand Up @@ -187,6 +187,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEha
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -220,3 +221,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
periph.io/x/periph v3.6.2+incompatible h1:B9vqhYVuhKtr6bXua8N9GeBEvD7yanczCvE0wU2LEqw=
periph.io/x/periph v3.6.2+incompatible/go.mod h1:EWr+FCIU2dBWz5/wSWeiIUJTriYv9v2j2ENBmgYyy7Y=
Loading

0 comments on commit 5e5959a

Please sign in to comment.