-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommandopenclose.go
More file actions
executable file
·59 lines (52 loc) · 2.19 KB
/
commandopenclose.go
File metadata and controls
executable file
·59 lines (52 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gbridge
import (
"github.com/pborges/gbridge/proto"
)
// OpenCloseCommand defines how a function should handle this specific trait
type OpenCloseCommand func(ctx Context, openPercent float64) proto.DeviceError
// Execute checks if the arguments from the Intent Request are correct and passes them to a user-defined type safe execute handler
func (t OpenCloseCommand) Execute(ctx Context, args map[string]interface{}) proto.CommandResponse {
res := proto.CommandResponse{
ErrorCode: proto.ErrorCodeProtocolError,
}
// validate if our EXECUTE Request contains the "openPercent" object so the handler can actually handle something
if argOpenPercent, ok := args["openPercent"]; ok {
if openPercent, ok := argOpenPercent.(float64); ok {
res.ErrorCode = t(ctx, openPercent)
} else {
res.ErrorCode = proto.ErrorCodeNotSupported
}
}
return res
}
// Name returns the intent for this Trait
func (t OpenCloseCommand) Name() string {
return "action.devices.commands.OpenClose"
}
// DirectionalOpenCloseCommand defines how a function should handle this specific trait
type DirectionalOpenCloseCommand func(ctx Context, openPercent float64, openDirection OpenCloseTraitDirection) proto.DeviceError
// Execute validates the request and calls the user defined handler for the device trait
func (t DirectionalOpenCloseCommand) Execute(ctx Context, args map[string]interface{}) proto.CommandResponse {
res := proto.CommandResponse{
ErrorCode: proto.ErrorCodeProtocolError,
}
// validate if our EXECUTE Request contains the "openPercent" object so the handler can actually handle something
openDirection := OpenCloseTraitDirectionNone
if argOpenPercent, ok := args["openPercent"]; ok {
if openPercent, ok := argOpenPercent.(float64); ok {
// if openDirection is specified, set it, otherwise lets use none
if argDir, ok := args["openDirection"]; ok {
if dir, ok := argDir.(string); ok {
openDirection = OpenCloseTraitDirection(dir)
}
}
res.ErrorCode = t(ctx, openPercent, openDirection)
}
res.ErrorCode = proto.ErrorCodeNotSupported
}
return res
}
// Name returns the intent for this Trait
func (t DirectionalOpenCloseCommand) Name() string {
return "action.devices.commands.OpenClose"
}