Compare commits
No commits in common. "a1201003055d9aeff30af3722caa45380ea5701c" and "4c622561e39903c3825a87b380e684b9670572f2" have entirely different histories.
a120100305
...
4c622561e3
35
main.go
35
main.go
@ -27,7 +27,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "v16"
|
const VERSION = "v15"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
@ -137,7 +137,6 @@ 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)
|
||||||
@ -673,38 +672,6 @@ 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,7 +7,6 @@ import (
|
|||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
|
|
||||||
// Standard
|
// Standard
|
||||||
"database/sql"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@ -21,13 +20,6 @@ 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{}
|
||||||
|
|
||||||
@ -145,34 +137,15 @@ func (t *Trigger) Update() (err error) { // {{{
|
|||||||
}
|
}
|
||||||
jsonDatapoints, _ := json.Marshal(t.Datapoints)
|
jsonDatapoints, _ := json.Marshal(t.Datapoints)
|
||||||
if t.ID == 0 {
|
if t.ID == 0 {
|
||||||
var row *sql.Row
|
_, err = service.Db.Conn.Exec(`
|
||||||
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,29 +3,6 @@
|
|||||||
{{ 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">
|
||||||
@ -34,7 +11,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 onclick="createTrigger({{ .ID }})">+</a></div>
|
<div class="new"><a href="/trigger/edit/0/{{ .ID }}">+</a></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="triggers">
|
<div class="triggers">
|
||||||
|
Loading…
Reference in New Issue
Block a user