Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Jul 13, 2024
1 parent f1aa674 commit 0f63c34
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
13 changes: 7 additions & 6 deletions base/operator.go → base/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ import (
"github.com/god-jason/bucket/pkg/calc"
"github.com/god-jason/bucket/types"
"strings"
"time"
)

type Executor struct {
Point string `json:"point,omitempty"`
Value any `json:"value,omitempty"`
Delay int64 `json:"delay,omitempty"` //ms
Point string `json:"point,omitempty"`
Value any `json:"value,omitempty"`
Delay time.Duration `json:"delay,omitempty"` //延迟 ms

_value gval.Evaluable
}

type Operator struct {
type Action struct {
Name string `json:"name"`
Label string `json:"label"`
Parameters []*types.SmartField `json:"parameters,omitempty"`
Return []*types.SmartField `json:"return,omitempty"`
Executors []*Executor `json:"executors,omitempty"`
}

func (a *Operator) Init() (err error) {
func (a *Action) Init() (err error) {
for _, e := range a.Executors {
if str, ok := e.Value.(string); ok {
if expr, has := strings.CutPrefix(str, "="); has {
Expand All @@ -38,7 +39,7 @@ func (a *Operator) Init() (err error) {
return nil
}

func (a *Operator) GetExecutors(args any) (es []*Executor, err error) {
func (a *Action) GetExecutors(args any) (es []*Executor, err error) {
for _, e := range a.Executors {
ee := *e
if e._value != nil {
Expand Down
26 changes: 16 additions & 10 deletions device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ type Device struct {

adapter protocol.Adapter

operators map[string]*base.Operator
actions map[string]*base.Action
}

func (d *Device) Open() error {
operators, err := product.LoadConfig[[]*base.Operator](d.ProductId, "operators")
operators, err := product.LoadConfig[[]*base.Action](d.ProductId, "actions")
if err != nil {
return err
}

d.operators = make(map[string]*base.Operator)
d.actions = make(map[string]*base.Action)
for _, op := range *operators {
d.operators[op.Name] = op
d.actions[op.Name] = op
err = op.Init()
if err != nil {
return err
Expand Down Expand Up @@ -103,20 +103,26 @@ func (d *Device) SetAdapter(adapter protocol.Adapter) {
}

func (d *Device) Action(name string, values map[string]any) (map[string]any, error) {
oper := d.operators[name]
if oper == nil {
action := d.actions[name]
if action == nil {
return nil, exception.New("找不到操作")
}

executors, err := oper.GetExecutors(values)
executors, err := action.GetExecutors(values)
if err != nil {
return nil, err
}

for _, executor := range executors {
err = d.Write(executor.Point, executor.Value)
if err != nil {
return nil, err
if executor.Delay > 0 {
time.AfterFunc(executor.Delay*time.Millisecond, func() {
_ = d.Write(executor.Point, executor.Value)
})
} else {
err = d.Write(executor.Point, executor.Value)
if err != nil {
return nil, err
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion product/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Product struct {
Keywords []string `json:"keywords,omitempty"` //关键字
Created time.Time `json:"created" xorm:"created"`

operators []*base.Operator
operators []*base.Action
}

func (p *Product) Init() error {
Expand Down

0 comments on commit 0f63c34

Please sign in to comment.