-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththing.go
70 lines (61 loc) · 1.89 KB
/
thing.go
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
60
61
62
63
64
65
66
67
68
69
70
package gateway
const (
// PropertyTypeInt represents number type of a property
PropertyTypeInt = 0
// PropertyTypeString represents string type of a property
PropertyTypeString = 1
// PropertyTypeBool represents boolean type of a property
PropertyTypeBool = 2
// PropertyTypeColor represents color type of a property
PropertyTypeColor = 3
)
// Instance represents a thing instance can be registered to DevIoT
type Instance interface {
Init(thing *Thing)
}
// Property represents a property model in DevIoT
type Property struct {
Name string `json:"name"`
Type int `json:"type"`
Value interface{} `json:"value,omitempty"`
Range []interface{} `json:"range,omitempty"`
Unit string `json:"unit,omitempty"`
Description string `json:"description,omitempty"`
}
// Action represents a action model in DevIoT
type Action struct {
Name string `json:"name,omitempty"`
Parameters []Property `json:"parameters,omitempty"`
}
// Thing represents a thing model in DevIoT
type Thing struct {
Id string `json:"id"`
Name string `json:"name"`
Kind string `json:"kind,omitempty"`
Actions []Action `json:"actions,omitempty"`
Properties []Property `json:"properties,omitempty"`
}
// AddParameter add a parameter to action model
func (a *Action) AddParameter(p Property) *Action {
a.Parameters = append(a.Parameters, p)
return a
}
// AddAction add an action to thing model
func (t *Thing) AddAction(a Action) *Thing {
t.Actions = append(t.Actions, a)
return t
}
// AddProperty add a property to thing model
func (t *Thing) AddProperty(p Property) *Thing {
t.Properties = append(t.Properties, p)
return t
}
// FindAction find action definition by name
func (t *Thing) FindAction(name string) (*Action, bool) {
for _, a := range t.Actions {
if a.Name == name {
return &a, true
}
}
return nil, false
}