Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Jul 13, 2024
1 parent 9cb0afd commit 9dfe8a5
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 78 deletions.
2 changes: 1 addition & 1 deletion action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Action struct {
DeviceId primitive.ObjectID `json:"device_id,omitempty" bson:"device_id"`
ProjectId primitive.ObjectID `json:"project_id,omitempty" bson:"project_id"`
SpaceId primitive.ObjectID `json:"space_id,omitempty" bson:"space_id"`
Name string `json:"action"`
Name string `json:"name,omitempty"`
Parameters map[string]any `json:"parameters,omitempty"`
Result string `json:"result,omitempty"`
Return map[string]any `json:"return,omitempty"`
Expand Down
44 changes: 43 additions & 1 deletion base/action.go
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
}
4 changes: 2 additions & 2 deletions device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (d *Device) PatchValues(values map[string]any) {
p := d.product.GetProperty(k)
if p != nil {
//保存历史
if p.Historical {
if p.Remember {
his[k] = v
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func (d *Device) Action(name string, values map[string]any) (map[string]any, err
//向网关发送写指令
if d.gatewayClient != nil && !d.gatewayClient.Closed() {
payload := PayloadActionDown{Id: id, Name: name, Parameters: values}
err := publishDirectly(d.gatewayClient, "down/device/"+d.Id+"/name", &payload)
err := publishDirectly(d.gatewayClient, "down/device/"+d.Id+"/action", &payload)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions device/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (d *Device) HandleMqtt(typ string, cl *mqtt.Client, payload []byte) {
d.gatewayClient = cl

switch typ {
case "property":
case "values":
var values map[string]any
err := json.Unmarshal(payload, &values)
Expand Down
37 changes: 13 additions & 24 deletions product/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ type Aggregator struct {
As string `json:"as,omitempty"`
}

// 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"` //是否可写
Historical bool `json:"historical,omitempty"` //是否保存历史
Aggregators []*Aggregator `json:"aggregators,omitempty"` //聚合计算

//Children *Property
}

// Alarm 报警器
type Alarm struct {
Title string
Expand All @@ -35,21 +21,24 @@ type Alarm struct {
}

type Product struct {
Id string `json:"_id,omitempty"`
Name string `json:"name,omitempty"` //名称
Type string `json:"type,omitempty"` //泛类型,比如:电表,水表
Id string `json:"_id,omitempty"`
Name string `json:"name,omitempty"` //名称
Type string `json:"type,omitempty"` //泛类型,比如:电表,水表

Properties []*Property `json:"properties,omitempty"`

properties map[string]*Property
}

func (p *Product) GetProperty(k string) *Property {
if p.properties == nil {
//创建索引
p.properties = make(map[string]*Property)
for _, a := range p.Properties {
p.properties[a.Name] = a
}
func (p *Product) Init() error {
p.properties = make(map[string]*Property)
for _, a := range p.Properties {
p.properties[a.Name] = a
}

return nil
}

func (p *Product) GetProperty(k string) *Property {
return p.properties[k]
}
15 changes: 15 additions & 0 deletions product/property.go
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
}
70 changes: 45 additions & 25 deletions scene/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func (s *Scene) Open() error {
// }
//}

for _, a := range s.Actions {
err := a.Init()
if err != nil {
return err
}
}

return s.Condition.Init()
}

Expand Down Expand Up @@ -159,22 +166,47 @@ func (s *Scene) OnDeviceValuesChange(product, device string, values map[string]a
s.last = ret
}

func (s *Scene) execute(id string, name string, params map[string]any) {
func (s *Scene) execute(id string, action *base.Action) error {
dev := device.Get(id)
if dev != nil {
_ = pool.Insert(func() {
_, err := dev.Action(name, params)
args, err := action.Evaluate(dev.Values())
if err != nil {
return err
}
//等待
if action.Delay > 0 {
time.AfterFunc(action.Delay*time.Millisecond, func() {
_, err = dev.Action(action.Name, args)
if err != nil {
log.Error(err)
}
})
} else {
_, err = dev.Action(action.Name, args)
if err != nil {
log.Error(err)
return err
}
})
}

return nil
}
return exception.New("找不到设备")
}

func (s *Scene) executeParallel(id string, action *base.Action) {
_ = pool.Insert(func() {
err := s.execute(id, action)
if err != nil {
log.Error(err)
return
}
})
}

func (s *Scene) ExecuteIgnoreError() {
for _, a := range s.Actions {
if a.DeviceId != "" {
s.execute(a.DeviceId, a.Name, a.Parameters)
s.executeParallel(a.DeviceId, a)
} else if a.ProductId != "" {
if s.deviceContainer != nil {
ids, err := s.deviceContainer.Devices(a.ProductId)
Expand All @@ -183,14 +215,12 @@ func (s *Scene) ExecuteIgnoreError() {
continue
}
for _, d := range ids {
s.execute(d, a.Name, a.Parameters)
s.executeParallel(d, a)
}
} else {
log.Error("需要指定产品ID")
//error
}
} else {
//error
log.Error("无效的动作")
}
}
Expand All @@ -199,14 +229,9 @@ func (s *Scene) ExecuteIgnoreError() {
func (s *Scene) Execute() error {
for _, a := range s.Actions {
if a.DeviceId != "" {
dev := device.Get(a.DeviceId)
if dev != nil {
_, err := dev.Action(a.Name, a.Parameters)
if err != nil {
return err
}
} else {
return exception.New("设备找不到")
err := s.execute(a.DeviceId, a)
if err != nil {
return err
}
} else if a.ProductId != "" {
if s.deviceContainer != nil {
Expand All @@ -215,14 +240,9 @@ func (s *Scene) Execute() error {
return err
}
for _, d := range ids {
dev := device.Get(d)
if dev != nil {
_, err := dev.Action(a.Name, a.Parameters)
if err != nil {
return err
}
} else {
return exception.New("设备找不到")
err = s.execute(d, a)
if err != nil {
return err
}
}
} else {
Expand Down
Loading

0 comments on commit 9dfe8a5

Please sign in to comment.