Wip problem
This commit is contained in:
parent
965e2daeb3
commit
ffe0c3def2
@ -53,13 +53,13 @@ func (dp DatapointValue) Value() any { // {{{
|
||||
|
||||
return nil
|
||||
} // }}}
|
||||
func (dp DatapointValue) FormattedTime() string {
|
||||
func (dp DatapointValue) FormattedTime() string {// {{{
|
||||
if dp.ValueDateTime.Valid {
|
||||
return dp.ValueDateTime.Time.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
return "invalid time"
|
||||
}
|
||||
func (dp Datapoint) Update() (err error) {
|
||||
}// }}}
|
||||
func (dp Datapoint) Update() (err error) {// {{{
|
||||
name := strings.TrimSpace(dp.Name)
|
||||
if name == "" {
|
||||
err = errors.New("Name can't be empty")
|
||||
@ -82,7 +82,7 @@ func (dp Datapoint) Update() (err error) {
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}// }}}
|
||||
|
||||
func DatapointAdd[T any](name string, value T) (err error) { // {{{
|
||||
row := service.Db.Conn.QueryRow(`SELECT id, datatype FROM datapoint WHERE name=$1`, name)
|
||||
|
19
main.go
19
main.go
@ -170,6 +170,13 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
|
||||
return
|
||||
}
|
||||
|
||||
var triggers []Trigger
|
||||
triggers, err = TriggersRetrieveByDatapoint(dpoint)
|
||||
for _, trigger := range triggers {
|
||||
trigger.Run()
|
||||
}
|
||||
|
||||
|
||||
j, _ := json.Marshal(struct{ OK bool }{true})
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
@ -436,23 +443,13 @@ func pageTriggerRun(w http.ResponseWriter, r *http.Request, _ *session.T) { // {
|
||||
expr, _ := io.ReadAll(r.Body)
|
||||
trigger.Expression = string(expr)
|
||||
|
||||
datapoints := make(map[string]Datapoint)
|
||||
for _, dpname := range trigger.Datapoints {
|
||||
dp, err := DatapointRetrieve(0, dpname)
|
||||
if err != nil {
|
||||
httpError(w, we.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
datapoints[dpname] = dp
|
||||
}
|
||||
|
||||
resp := struct {
|
||||
OK bool
|
||||
Output any
|
||||
}{
|
||||
OK: true,
|
||||
}
|
||||
resp.Output, err = trigger.Run(datapoints)
|
||||
resp.Output, err = trigger.Run()
|
||||
if err != nil {
|
||||
we.Wrap(err).Log()
|
||||
httpError(w, err)
|
||||
|
56
trigger.go
56
trigger.go
@ -4,6 +4,7 @@ import (
|
||||
// External
|
||||
we "git.gibonuddevalla.se/go/wrappederror"
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
// Standard
|
||||
"encoding/json"
|
||||
@ -73,6 +74,37 @@ func TriggersRetrieve() (areas []Area, err error) { // {{{
|
||||
|
||||
return
|
||||
} // }}}
|
||||
func TriggersRetrieveByDatapoint(datapointName string) (triggers []Trigger, err error) { // {{{
|
||||
triggers = []Trigger{}
|
||||
var rows *sqlx.Rows
|
||||
|
||||
rows, err = service.Db.Conn.Queryx(`
|
||||
SELECT
|
||||
*
|
||||
FROM public."trigger"
|
||||
WHERE
|
||||
datapoints @> $1
|
||||
`,
|
||||
fmt.Sprintf(`["%s"]`, datapointName),
|
||||
)
|
||||
if err != nil {
|
||||
err = we.Wrap(err).WithData(datapointName)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
trigger := Trigger{}
|
||||
err = rows.StructScan(&trigger)
|
||||
if err != nil {
|
||||
err = we.Wrap(err).WithData(datapointName)
|
||||
return
|
||||
}
|
||||
triggers = append(triggers, trigger)
|
||||
}
|
||||
|
||||
return
|
||||
} // }}}
|
||||
func TriggerRetrieve(id int) (trigger Trigger, err error) { // {{{
|
||||
row := service.Db.Conn.QueryRow(`SELECT to_jsonb(t.*) FROM "trigger" t WHERE id=$1`, id)
|
||||
var jsonData []byte
|
||||
@ -85,7 +117,7 @@ func TriggerRetrieve(id int) (trigger Trigger, err error) { // {{{
|
||||
err = json.Unmarshal(jsonData, &trigger)
|
||||
return
|
||||
} // }}}
|
||||
func (t *Trigger) Validate() (ok bool, err error) {
|
||||
func (t *Trigger) Validate() (ok bool, err error) { // {{{
|
||||
if strings.TrimSpace(t.Name) == "" {
|
||||
err = fmt.Errorf("Name can't be empty")
|
||||
return
|
||||
@ -97,8 +129,8 @@ func (t *Trigger) Validate() (ok bool, err error) {
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
func (t *Trigger) Update() (err error) {
|
||||
} // }}}
|
||||
func (t *Trigger) Update() (err error) { // {{{
|
||||
var ok bool
|
||||
if ok, err = t.Validate(); !ok {
|
||||
return
|
||||
@ -121,9 +153,20 @@ func (t *Trigger) Update() (err error) {
|
||||
err = we.Wrap(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
} // }}}
|
||||
|
||||
func (t *Trigger) Run() (output any, err error) { // {{{
|
||||
datapoints := make(map[string]Datapoint)
|
||||
for _, dpname := range t.Datapoints {
|
||||
var dp Datapoint
|
||||
dp, err = DatapointRetrieve(0, dpname)
|
||||
if err != nil {
|
||||
err = we.Wrap(err)
|
||||
return
|
||||
}
|
||||
datapoints[dpname] = dp
|
||||
}
|
||||
|
||||
func (t *Trigger) Run(datapoints map[string]Datapoint) (output any, err error) {
|
||||
env := make(map[string]any)
|
||||
for dpName, dp := range datapoints {
|
||||
env[dpName] = dp.LastDatapointValue.Value()
|
||||
@ -138,6 +181,5 @@ func (t *Trigger) Run(datapoints map[string]Datapoint) (output any, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
} // }}}
|
||||
|
Loading…
Reference in New Issue
Block a user