Skip to content

Commit

Permalink
Merge pull request #102 from CameronRP/toggle-auto-update
Browse files Browse the repository at this point in the history
Toggle auto update
  • Loading branch information
CameronRP authored May 24, 2021
2 parents 78b83be + 45adea2 commit 9f9c699
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
31 changes: 30 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
cptvGlob = "*.cptv"
failedUploadsFolder = "failed-uploads"
rebootDelay = time.Second * 5
apiVersion = 6
apiVersion = 7
)

type ManagementAPI struct {
Expand Down Expand Up @@ -492,6 +492,35 @@ func (api *ManagementAPI) GetSaltUpdateState(w http.ResponseWriter, r *http.Requ
json.NewEncoder(w).Encode(state)
}

//GetSaltAutoUpdate will check if salt auto update is enabled
func (api *ManagementAPI) GetSaltAutoUpdate(w http.ResponseWriter, r *http.Request) {
autoUpdate, err := saltrequester.IsAutoUpdateOn()
if err != nil {
log.Printf("error getting salt auto update state: %v", err)
serverError(&w, errors.New("failed to get salt auto update state"))
return
}
json.NewEncoder(w).Encode(map[string]interface{}{"autoUpdate": autoUpdate})
}

//PostSaltAutoUpdate will set if auto update is enabled or not
func (api *ManagementAPI) PostSaltAutoUpdate(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
parseFormErrorResponse(&w, err)
return
}
autoUpdateStr := strings.ToLower(r.Form.Get("autoUpdate"))
if autoUpdateStr != "true" && autoUpdateStr != "false" {
parseFormErrorResponse(&w, errors.New("invalid value for autoUpdate"))
return
}
autoUpdate := strings.ToLower(autoUpdateStr) == "true"
if err := saltrequester.SetAutoUpdate(autoUpdate); err != nil {
log.Printf("error setting auto update: %v", err)
serverError(&w, errors.New("failed to set auto update"))
}
}

func (api *ManagementAPI) GetServiceLogs(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
parseFormErrorResponse(&w, err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func main() {
apiRouter.HandleFunc("/check-salt-connection", apiObj.CheckSaltConnection).Methods("GET")
apiRouter.HandleFunc("/salt-update", apiObj.StartSaltUpdate).Methods("POST")
apiRouter.HandleFunc("/salt-update", apiObj.GetSaltUpdateState).Methods("GET")
apiRouter.HandleFunc("/auto-update", apiObj.GetSaltAutoUpdate).Methods("GET")
apiRouter.HandleFunc("/auto-update", apiObj.PostSaltAutoUpdate).Methods("POST")
apiRouter.HandleFunc("/audiobait", apiObj.GetAudiobait).Methods("GET")
apiRouter.HandleFunc("/play-test-sound", apiObj.PlayTestSound).Methods("POST")
apiRouter.HandleFunc("/play-audiobait-sound", apiObj.PlayAudiobaitSound).Methods("POST")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/TheCacophonyProject/go-config v1.6.2
github.com/TheCacophonyProject/lepton3 v0.0.0-20200909032119-e2b2b778a8ee
github.com/TheCacophonyProject/rtc-utils v1.2.0
github.com/TheCacophonyProject/salt-updater v0.3.0
github.com/TheCacophonyProject/salt-updater v0.4.0
github.com/gobuffalo/packr v1.30.1
github.com/godbus/dbus v4.1.0+incompatible
github.com/gorilla/context v1.1.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ github.com/TheCacophonyProject/periph v2.1.1-0.20200615222341-6834cd5be8c1+incom
github.com/TheCacophonyProject/periph v2.1.1-0.20200615222341-6834cd5be8c1+incompatible/go.mod h1:K1wa07sGXKzV0G9p+4R069mZk4GOpqKW8GU6/k+BrIo=
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/salt-updater v0.3.0 h1:+osvpE4BzHadZETmmWDkS+CDhtdL2oGsqwLGLLnsdkc=
github.com/TheCacophonyProject/salt-updater v0.3.0/go.mod h1:fVlMqJZpvT8HeT4GcmQlvMq8+AtvF4NZ/LuEVSoA5E8=
github.com/TheCacophonyProject/salt-updater v0.4.0 h1:2RCgdI4wr/ZoeWOvfGlZ/Epgz3GTC+Vc7/wS6EI90c0=
github.com/TheCacophonyProject/salt-updater v0.4.0/go.mod h1:fVlMqJZpvT8HeT4GcmQlvMq8+AtvF4NZ/LuEVSoA5E8=
github.com/TheCacophonyProject/window v0.0.0-20190821235241-ab92c2ee24b6 h1:aLER2Au+/pJe9FzTW7lmQJIOLO8bgio5i/Uzx/EGEmE=
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=
Expand Down
10 changes: 9 additions & 1 deletion html/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ <h3>Device<br></h3>
</div>
<button id="check-salt-button" type="button" onclick="checkSaltConnection()" class="btn btn-primary">Check Salt Connection</button>
<button id="salt-update-button" type="button" onclick="runSaltUpdate()" class="btn btn-primary">Run Salt Update</button>

<label>AutoUpdate:</label>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary" id="auto-update-on" onclick="setAutoUpdate(true);">
<input type="radio" name="auto-update"> ON
</label>
<label class="btn btn-secondary" id="auto-update-off" onclick="setAutoUpdate(false);">
<input type="radio" name="auto-update" > OFF
</label>
</div>

<div class="container pt-5 pb-4 pl-0">
<h3>Installed Packages<br></h3>
Expand Down
36 changes: 35 additions & 1 deletion static/js/about.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
function checkSaltConnection() {
authHeaders = new Headers()
authHeaders.append("Authorization", "Basic YWRtaW46ZmVhdGhlcnM=")

window.onload = async function() {
readAutoUpdate()
}

async function setAutoUpdate(autoUpdate) {
console.log("set auto update", autoUpdate)
var res = await fetch("/api/auto-update", {
method: "POST",
headers: authHeaders,
body: new URLSearchParams({'autoUpdate': autoUpdate})
})
if (!res.ok) {
alert("failed to update auto update state")
}
await readAutoUpdate()
}

async function readAutoUpdate() {
var res = await fetch("/api/auto-update", {headers: authHeaders})
if (res.ok) {
resJson = await res.json()
if (resJson.autoUpdate == true) {
document.getElementById("auto-update-on").classList.add("active")
document.getElementById("auto-update-off").classList.remove("active")
} else {
document.getElementById("auto-update-on").classList.remove("active")
document.getElementById("auto-update-off").classList.add("active")
}
}
}

function checkSaltConnection() {
$("#check-salt-button").attr('disabled', true);
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "/api/check-salt-connection", true);
Expand Down

0 comments on commit 9f9c699

Please sign in to comment.