Wip problem

This commit is contained in:
Magnus Åhall 2024-04-30 20:10:05 +02:00
parent 965e2daeb3
commit ffe0c3def2
3 changed files with 61 additions and 22 deletions

View File

@ -53,13 +53,13 @@ func (dp DatapointValue) Value() any { // {{{
return nil return nil
} // }}} } // }}}
func (dp DatapointValue) FormattedTime() string { func (dp DatapointValue) FormattedTime() string {// {{{
if dp.ValueDateTime.Valid { if dp.ValueDateTime.Valid {
return dp.ValueDateTime.Time.Format("2006-01-02 15:04:05") return dp.ValueDateTime.Time.Format("2006-01-02 15:04:05")
} }
return "invalid time" return "invalid time"
} }// }}}
func (dp Datapoint) Update() (err error) { func (dp Datapoint) Update() (err error) {// {{{
name := strings.TrimSpace(dp.Name) name := strings.TrimSpace(dp.Name)
if name == "" { if name == "" {
err = errors.New("Name can't be empty") err = errors.New("Name can't be empty")
@ -82,7 +82,7 @@ func (dp Datapoint) Update() (err error) {
} }
return return
} }// }}}
func DatapointAdd[T any](name string, value T) (err error) { // {{{ func DatapointAdd[T any](name string, value T) (err error) { // {{{
row := service.Db.Conn.QueryRow(`SELECT id, datatype FROM datapoint WHERE name=$1`, name) row := service.Db.Conn.QueryRow(`SELECT id, datatype FROM datapoint WHERE name=$1`, name)

19
main.go
View File

@ -170,6 +170,13 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
return return
} }
var triggers []Trigger
triggers, err = TriggersRetrieveByDatapoint(dpoint)
for _, trigger := range triggers {
trigger.Run()
}
j, _ := json.Marshal(struct{ OK bool }{true}) j, _ := json.Marshal(struct{ OK bool }{true})
w.Write(j) w.Write(j)
} // }}} } // }}}
@ -436,23 +443,13 @@ func pageTriggerRun(w http.ResponseWriter, r *http.Request, _ *session.T) { // {
expr, _ := io.ReadAll(r.Body) expr, _ := io.ReadAll(r.Body)
trigger.Expression = string(expr) 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 { resp := struct {
OK bool OK bool
Output any Output any
}{ }{
OK: true, OK: true,
} }
resp.Output, err = trigger.Run(datapoints) resp.Output, err = trigger.Run()
if err != nil { if err != nil {
we.Wrap(err).Log() we.Wrap(err).Log()
httpError(w, err) httpError(w, err)

View File

@ -4,6 +4,7 @@ import (
// External // External
we "git.gibonuddevalla.se/go/wrappederror" we "git.gibonuddevalla.se/go/wrappederror"
"github.com/expr-lang/expr" "github.com/expr-lang/expr"
"github.com/jmoiron/sqlx"
// Standard // Standard
"encoding/json" "encoding/json"
@ -73,6 +74,37 @@ func TriggersRetrieve() (areas []Area, err error) { // {{{
return 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) { // {{{ func TriggerRetrieve(id int) (trigger Trigger, err error) { // {{{
row := service.Db.Conn.QueryRow(`SELECT to_jsonb(t.*) FROM "trigger" t WHERE id=$1`, id) row := service.Db.Conn.QueryRow(`SELECT to_jsonb(t.*) FROM "trigger" t WHERE id=$1`, id)
var jsonData []byte var jsonData []byte
@ -85,7 +117,7 @@ func TriggerRetrieve(id int) (trigger Trigger, err error) { // {{{
err = json.Unmarshal(jsonData, &trigger) err = json.Unmarshal(jsonData, &trigger)
return return
} // }}} } // }}}
func (t *Trigger) Validate() (ok bool, err error) { func (t *Trigger) Validate() (ok bool, err error) { // {{{
if strings.TrimSpace(t.Name) == "" { if strings.TrimSpace(t.Name) == "" {
err = fmt.Errorf("Name can't be empty") err = fmt.Errorf("Name can't be empty")
return return
@ -97,8 +129,8 @@ func (t *Trigger) Validate() (ok bool, err error) {
} }
return true, nil return true, nil
} } // }}}
func (t *Trigger) Update() (err error) { func (t *Trigger) Update() (err error) { // {{{
var ok bool var ok bool
if ok, err = t.Validate(); !ok { if ok, err = t.Validate(); !ok {
return return
@ -121,9 +153,20 @@ func (t *Trigger) Update() (err error) {
err = we.Wrap(err) err = we.Wrap(err)
} }
return 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) env := make(map[string]any)
for dpName, dp := range datapoints { for dpName, dp := range datapoints {
env[dpName] = dp.LastDatapointValue.Value() env[dpName] = dp.LastDatapointValue.Value()
@ -138,6 +181,5 @@ func (t *Trigger) Run(datapoints map[string]Datapoint) (output any, err error) {
if err != nil { if err != nil {
return return
} }
return return
} } // }}}