Compare commits
2 Commits
4c622561e3
...
a120100305
Author | SHA1 | Date | |
---|---|---|---|
|
a120100305 | ||
|
37bf692984 |
35
main.go
35
main.go
@ -27,7 +27,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const VERSION = "v15"
|
||||
const VERSION = "v16"
|
||||
|
||||
var (
|
||||
logger *slog.Logger
|
||||
@ -137,6 +137,7 @@ func main() { // {{{
|
||||
service.Register("/datapoint/values/{id}", false, false, pageDatapointValues)
|
||||
|
||||
service.Register("/triggers", false, false, pageTriggers)
|
||||
service.Register("/trigger/create/{sectionID}/{name}", false, false, triggerCreate)
|
||||
service.Register("/trigger/edit/{id}", false, false, pageTriggerEdit)
|
||||
service.Register("/trigger/edit/{id}/{sectionID}", false, false, pageTriggerEdit)
|
||||
service.Register("/trigger/addDatapoint/{id}/{datapointName}", false, false, pageTriggerDatapointAdd)
|
||||
@ -672,6 +673,38 @@ func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
|
||||
|
||||
page.Render(w)
|
||||
} // }}}
|
||||
func triggerCreate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
|
||||
name := r.PathValue("name")
|
||||
sectionIDStr := r.PathValue("sectionID")
|
||||
sectionID, err := strconv.Atoi(sectionIDStr)
|
||||
if err != nil {
|
||||
httpError(w, werr.Wrap(err).WithData(sectionIDStr).Log())
|
||||
return
|
||||
}
|
||||
|
||||
t, err := TriggerCreate(sectionID, name)
|
||||
if err != nil {
|
||||
httpError(w, werr.Wrap(err).WithData(struct {
|
||||
SectionID int
|
||||
Name string
|
||||
}{
|
||||
sectionID,
|
||||
name,
|
||||
}).Log())
|
||||
return
|
||||
}
|
||||
|
||||
resp := struct {
|
||||
OK bool
|
||||
Trigger Trigger
|
||||
}{
|
||||
true,
|
||||
t,
|
||||
}
|
||||
j, _ := json.Marshal(resp)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
|
||||
idStr := r.PathValue("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
|
29
trigger.go
29
trigger.go
@ -7,6 +7,7 @@ import (
|
||||
"github.com/lib/pq"
|
||||
|
||||
// Standard
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
@ -20,6 +21,13 @@ type Trigger struct {
|
||||
Datapoints []string
|
||||
}
|
||||
|
||||
func TriggerCreate(sectionID int, name string) (t Trigger, err error) { // {{{
|
||||
t.SectionID = sectionID
|
||||
t.Name = name
|
||||
t.Expression = "false"
|
||||
err = t.Update()
|
||||
return
|
||||
} // }}}
|
||||
func TriggersRetrieve() (areas []Area, err error) { // {{{
|
||||
areas = []Area{}
|
||||
|
||||
@ -137,15 +145,34 @@ func (t *Trigger) Update() (err error) { // {{{
|
||||
}
|
||||
jsonDatapoints, _ := json.Marshal(t.Datapoints)
|
||||
if t.ID == 0 {
|
||||
_, err = service.Db.Conn.Exec(`
|
||||
var row *sql.Row
|
||||
row = service.Db.Conn.QueryRow(`
|
||||
INSERT INTO "trigger"(name, section_id, expression, datapoints)
|
||||
VALUES($1, $2, $3, $4)
|
||||
RETURNING id
|
||||
`,
|
||||
t.Name,
|
||||
t.SectionID,
|
||||
t.Expression,
|
||||
jsonDatapoints,
|
||||
)
|
||||
err = row.Scan(&t.ID)
|
||||
if err != nil {
|
||||
err = we.Wrap(err).WithData(
|
||||
struct {
|
||||
SectionID int
|
||||
Name string
|
||||
Expression string
|
||||
JsonDataPoints []byte
|
||||
}{
|
||||
t.SectionID,
|
||||
t.Name,
|
||||
t.Expression,
|
||||
jsonDatapoints,
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err = service.Db.Conn.Exec(`
|
||||
UPDATE "trigger"
|
||||
|
@ -3,6 +3,29 @@
|
||||
{{ block "page_label" . }}{{end}}
|
||||
{{ $version := .VERSION }}
|
||||
|
||||
<script type="text/javascript">
|
||||
function createTrigger(sectionID) {
|
||||
let name = prompt("Trigger name")
|
||||
if (name === null)
|
||||
return
|
||||
|
||||
if (name.trim() == "") {
|
||||
alert('A name is required')
|
||||
return
|
||||
}
|
||||
|
||||
fetch(`/trigger/create/${sectionID}/${encodeURIComponent(name)}`)
|
||||
.then(data=>data.json())
|
||||
.then(json=>{
|
||||
if (!json.OK) {
|
||||
alert(json.Error)
|
||||
return
|
||||
}
|
||||
location.href = `/trigger/edit/${json.Trigger.ID}`
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<div id="areas">
|
||||
{{ range .Data.Areas }}
|
||||
<div class="area">
|
||||
@ -11,7 +34,7 @@
|
||||
<div class="section">
|
||||
<div class="create">
|
||||
<div class="name">{{ .Name }}</div>
|
||||
<div class="new"><a href="/trigger/edit/0/{{ .ID }}">+</a></div>
|
||||
<div class="new"><a onclick="createTrigger({{ .ID }})">+</a></div>
|
||||
</div>
|
||||
|
||||
<div class="triggers">
|
||||
|
Loading…
Reference in New Issue
Block a user