In-page addition of datapoints to triggers

This commit is contained in:
Magnus Åhall 2024-05-30 14:33:43 +02:00
parent 68abb894a6
commit adab2ab67d
2 changed files with 49 additions and 5 deletions

34
main.go
View File

@ -21,6 +21,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"slices"
"sort" "sort"
"strconv" "strconv"
"time" "time"
@ -138,6 +139,7 @@ func main() { // {{{
service.Register("/triggers", false, false, pageTriggers) service.Register("/triggers", false, false, pageTriggers)
service.Register("/trigger/edit/{id}", false, false, pageTriggerEdit) service.Register("/trigger/edit/{id}", false, false, pageTriggerEdit)
service.Register("/trigger/edit/{id}/{sectionID}", false, false, pageTriggerEdit) service.Register("/trigger/edit/{id}/{sectionID}", false, false, pageTriggerEdit)
service.Register("/trigger/addDatapoint/{id}/{datapointName}", false, false, pageTriggerDatapointAdd)
service.Register("/trigger/update/{id}", false, false, pageTriggerUpdate) service.Register("/trigger/update/{id}", false, false, pageTriggerUpdate)
service.Register("/trigger/run/{id}", false, false, pageTriggerRun) service.Register("/trigger/run/{id}", false, false, pageTriggerRun)
@ -725,6 +727,38 @@ func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { //
page.Render(w) page.Render(w)
} // }}} } // }}}
func pageTriggerDatapointAdd(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
triggerID := r.PathValue("id")
dpName := r.PathValue("datapointName")
id, err := strconv.Atoi(triggerID)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
var trigger Trigger
if id > 0 {
trigger, err = TriggerRetrieve(id)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
}
if !slices.Contains(trigger.Datapoints, dpName) {
trigger.Datapoints = append(trigger.Datapoints, dpName)
}
err = trigger.Update()
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
j, _ := json.Marshal(struct{ OK bool }{OK: true})
w.Header().Add("Content-Type", "application/json")
w.Write(j)
} // }}}
func pageTriggerUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ func pageTriggerUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
idStr := r.PathValue("id") idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr) id, err := strconv.Atoi(idStr)

View File

@ -66,7 +66,7 @@ export class UI {
}) })
.catch(err => alert(err)) .catch(err => alert(err))
}//}}} }//}}}
chooseDatapoint() {//{{{ async chooseDatapoint() {//{{{
const dlg = document.getElementById('dlg-datapoints') const dlg = document.getElementById('dlg-datapoints')
const datapoint = document.getElementById('datapoint').value const datapoint = document.getElementById('datapoint').value
const dp = this.datapoints.find(dp => dp.Name == datapoint) const dp = this.datapoints.find(dp => dp.Name == datapoint)
@ -77,8 +77,10 @@ export class UI {
} }
this.trigger.addDatapoint(dp) this.trigger.addDatapoint(dp)
.then(() => {
dlg.close() dlg.close()
this.render() this.render()
})
}//}}} }//}}}
deleteDatapoint(name) {//{{{ deleteDatapoint(name) {//{{{
if (!confirm(`Delete ${name}?`)) { if (!confirm(`Delete ${name}?`)) {
@ -147,7 +149,15 @@ export class Trigger {
}) })
.catch(err => alert(err)) .catch(err => alert(err))
}//}}} }//}}}
addDatapoint(dp) {//{{{ async addDatapoint(dp) {//{{{
return fetch(`/trigger/addDatapoint/${this.id}/${dp.Name}`)
.then(data => data.json())
.then(json => {
if (!json.OK) {
alert(json.Error)
return
}
this.datapoints[dp.Name] = dp this.datapoints[dp.Name] = dp
})
}//}}} }//}}}
} }