Better trigger create
This commit is contained in:
parent
4c622561e3
commit
37bf692984
33
main.go
33
main.go
@ -137,6 +137,7 @@ func main() { // {{{
|
|||||||
service.Register("/datapoint/values/{id}", false, false, pageDatapointValues)
|
service.Register("/datapoint/values/{id}", false, false, pageDatapointValues)
|
||||||
|
|
||||||
service.Register("/triggers", false, false, pageTriggers)
|
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}", 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/addDatapoint/{id}/{datapointName}", false, false, pageTriggerDatapointAdd)
|
||||||
@ -672,6 +673,38 @@ func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
|
|||||||
|
|
||||||
page.Render(w)
|
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) { // {{{
|
func pageTriggerEdit(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)
|
||||||
|
29
trigger.go
29
trigger.go
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
|
|
||||||
// Standard
|
// Standard
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@ -20,6 +21,13 @@ type Trigger struct {
|
|||||||
Datapoints []string
|
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) { // {{{
|
func TriggersRetrieve() (areas []Area, err error) { // {{{
|
||||||
areas = []Area{}
|
areas = []Area{}
|
||||||
|
|
||||||
@ -137,15 +145,34 @@ func (t *Trigger) Update() (err error) { // {{{
|
|||||||
}
|
}
|
||||||
jsonDatapoints, _ := json.Marshal(t.Datapoints)
|
jsonDatapoints, _ := json.Marshal(t.Datapoints)
|
||||||
if t.ID == 0 {
|
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)
|
INSERT INTO "trigger"(name, section_id, expression, datapoints)
|
||||||
VALUES($1, $2, $3, $4)
|
VALUES($1, $2, $3, $4)
|
||||||
|
RETURNING id
|
||||||
`,
|
`,
|
||||||
t.Name,
|
t.Name,
|
||||||
t.SectionID,
|
t.SectionID,
|
||||||
t.Expression,
|
t.Expression,
|
||||||
jsonDatapoints,
|
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 {
|
} else {
|
||||||
_, err = service.Db.Conn.Exec(`
|
_, err = service.Db.Conn.Exec(`
|
||||||
UPDATE "trigger"
|
UPDATE "trigger"
|
||||||
|
@ -3,6 +3,29 @@
|
|||||||
{{ block "page_label" . }}{{end}}
|
{{ block "page_label" . }}{{end}}
|
||||||
{{ $version := .VERSION }}
|
{{ $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">
|
<div id="areas">
|
||||||
{{ range .Data.Areas }}
|
{{ range .Data.Areas }}
|
||||||
<div class="area">
|
<div class="area">
|
||||||
@ -11,7 +34,7 @@
|
|||||||
<div class="section">
|
<div class="section">
|
||||||
<div class="create">
|
<div class="create">
|
||||||
<div class="name">{{ .Name }}</div>
|
<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>
|
||||||
|
|
||||||
<div class="triggers">
|
<div class="triggers">
|
||||||
|
Loading…
Reference in New Issue
Block a user