-
Notifications
You must be signed in to change notification settings - Fork 12
/
feature_remote.go
93 lines (77 loc) · 2.46 KB
/
feature_remote.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package spine
import (
"fmt"
"time"
"github.com/enbility/eebus-go/logging"
"github.com/enbility/eebus-go/spine/model"
"github.com/enbility/eebus-go/util"
"github.com/rickb777/date/period"
)
const defaultMaxResponseDelay = time.Duration(time.Second * 10)
type FeatureRemoteImpl struct {
*FeatureImpl
entity *EntityRemoteImpl
functionDataMap map[model.FunctionType]FunctionData
maxResponseDelay *time.Duration
}
func NewFeatureRemoteImpl(id uint, entity *EntityRemoteImpl, ftype model.FeatureTypeType, role model.RoleType) *FeatureRemoteImpl {
res := &FeatureRemoteImpl{
FeatureImpl: NewFeatureImpl(
featureAddressType(id, entity.Address()),
ftype,
role),
entity: entity,
functionDataMap: make(map[model.FunctionType]FunctionData),
}
for _, fd := range CreateFunctionData[FunctionData](ftype) {
res.functionDataMap[fd.Function()] = fd
}
res.operations = make(map[model.FunctionType]*Operations)
return res
}
func (r *FeatureRemoteImpl) Data(function model.FunctionType) any {
return r.functionData(function).DataAny()
}
func (r *FeatureRemoteImpl) UpdateData(function model.FunctionType, data any, filterPartial *model.FilterType, filterDelete *model.FilterType) {
r.functionData(function).UpdateDataAny(data, filterPartial, filterDelete)
// TODO: fire event
}
func (r *FeatureRemoteImpl) Sender() Sender {
return r.Device().Sender()
}
func (r *FeatureRemoteImpl) Device() *DeviceRemoteImpl {
return r.entity.Device()
}
func (r *FeatureRemoteImpl) Entity() *EntityRemoteImpl {
return r.entity
}
func (r *FeatureRemoteImpl) SetOperations(functions []model.FunctionPropertyType) {
r.operations = make(map[model.FunctionType]*Operations)
for _, sf := range functions {
r.operations[*sf.Function] = NewOperations(sf.PossibleOperations.Read != nil, sf.PossibleOperations.Write != nil)
}
}
func (r *FeatureRemoteImpl) SetMaxResponseDelay(delay *model.MaxResponseDelayType) {
if delay == nil {
return
}
p, err := period.Parse(string(*delay))
if err != nil {
r.maxResponseDelay = util.Ptr(p.DurationApprox())
} else {
logging.Log.Debug(err)
}
}
func (r *FeatureRemoteImpl) MaxResponseDelayDuration() time.Duration {
if r.maxResponseDelay != nil {
return *r.maxResponseDelay
}
return defaultMaxResponseDelay
}
func (r *FeatureRemoteImpl) functionData(function model.FunctionType) FunctionData {
fd, found := r.functionDataMap[function]
if !found {
panic(fmt.Errorf("Data was not found for function '%s'", function))
}
return fd
}