-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
165 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,50 @@ | ||
package base | ||
|
||
import ( | ||
"context" | ||
"github.com/PaesslerAG/gval" | ||
"github.com/god-jason/bucket/pkg/calc" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Action struct { | ||
ProductId string `json:"product_id,omitempty" bson:"product_id"` | ||
DeviceId string `json:"device_id,omitempty" bson:"device_id"` | ||
Name string `json:"action"` | ||
Name string `json:"name,omitempty"` | ||
Parameters map[string]any `json:"parameters,omitempty"` | ||
Delay time.Duration `json:"delay,omitempty"` //延迟 ms | ||
|
||
_parameters map[string]gval.Evaluable | ||
} | ||
|
||
func (a *Action) Init() (err error) { | ||
a._parameters = make(map[string]gval.Evaluable) | ||
for key, value := range a.Parameters { | ||
if val, ok := value.(string); ok { | ||
if expr, has := strings.CutPrefix(val, "="); has { | ||
a._parameters[key], err = calc.New(expr) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (a *Action) Evaluate(args any) (values map[string]any, err error) { | ||
values = make(map[string]any) | ||
for key, value := range a.Parameters { | ||
values[key] = value | ||
} | ||
for key, value := range a._parameters { | ||
if value != nil { | ||
values[key], err = value(context.Background(), args) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package product | ||
|
||
// Property 属性 | ||
type Property struct { | ||
Name string `json:"name,omitempty"` //变量名称 | ||
Label string `json:"label,omitempty"` //显示名称 | ||
Unit string `json:"unit,omitempty"` //单位 | ||
Type string `json:"type,omitempty"` //bool string number array object | ||
Default any `json:"default,omitempty"` //默认值 | ||
Writable bool `json:"writable,omitempty"` //是否可写 | ||
Remember bool `json:"remember,omitempty"` //是否保存历史 | ||
Aggregators []*Aggregator `json:"aggregators,omitempty"` //聚合计算 | ||
|
||
//Children *Property | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.