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"
|
||||
)
|
||||
|
||||
const VERSION = "v16"
|
||||
const VERSION = "v15"
|
||||
|
||||
var (
|
||||
logger *slog.Logger
|
||||
@ -137,7 +137,6 @@ 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)
|
||||
@ -673,38 +672,6 @@ 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,7 +7,6 @@ import (
|
||||
"github.com/lib/pq"
|
||||
|
||||
// Standard
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
@ -21,13 +20,6 @@ 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{}
|
||||
|
||||
@ -145,34 +137,15 @@ func (t *Trigger) Update() (err error) { // {{{
|
||||
}
|
||||
jsonDatapoints, _ := json.Marshal(t.Datapoints)
|
||||
if t.ID == 0 {
|
||||
var row *sql.Row
|
||||
row = service.Db.Conn.QueryRow(`
|
||||
_, err = service.Db.Conn.Exec(`
|
||||
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,29 +3,6 @@
|
||||
{{ 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">
|
||||
@ -34,7 +11,7 @@
|
||||
<div class="section">
|
||||
<div class="create">
|
||||
<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 class="triggers">
|
||||
|
Loading…
Reference in New Issue
Block a user