-
Notifications
You must be signed in to change notification settings - Fork 6
/
notification.go
128 lines (112 loc) · 3.16 KB
/
notification.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package openzwave
// #cgo LDFLAGS: -lopenzwave -Lgo/src/github.com/ninjasphere/go-openzwave/openzwave
// #cgo CPPFLAGS: -Iopenzwave/cpp/src/platform -Iopenzwave/cpp/src -Iopenzwave/cpp/src/value_classes
//
// #include "api.h"
import "C"
import (
"fmt"
"github.com/ninjasphere/go-openzwave/CODE"
"github.com/ninjasphere/go-openzwave/NT"
)
// The type of notifications received from the API
type Notification interface {
GetNode() Node
GetNotificationType() *NT.Enum
}
// The type of notifications received via the API's Notifications() channel.
type notification struct {
cRef *C.Notification
node *node // should be freed by the receiver, iff it is not null
value *value // should be freed by the receiver, iff it is not null
}
// Converts the notification into a string representation.
func (n *notification) String() string {
return fmt.Sprintf(
"Notification["+
"notificationType=%v/%v, "+
"node=%v, "+
"value=%v]",
NT.ToEnum(int(n.cRef.notificationType)),
CODE.ToEnum(int(n.cRef.notificationCode)),
n.GetNode(),
n.GetValue())
}
func (n *notification) free() {
C.freeNotification(n.cRef)
if n.node != nil {
n.node.free()
}
if n.value != nil {
n.value.free()
}
}
func (n *notification) GetValue() Value {
return n.value
}
func (n *notification) GetNode() Node {
return n.node
}
func (n *notification) GetNotificationType() *NT.Enum {
return NT.ToEnum(int(n.cRef.notificationType))
}
func newGoNotification(cRef *C.Notification) *notification {
result := ¬ification{cRef, newGoNode(cRef.node), newGoValue(cRef.value)}
// transfer ownership of C structure to the go object
result.cRef.value = nil
result.cRef.node = nil
return result
}
//
// Swap the cRef of the receiver's node with cRef of the specified node.
//
// The intent is to swap the *C.Node reachable from 'existing' with the *C.Node
// reachable from the receiver so that existing gets the fresh object.
//
// If there is no existing object then we steal the whole go object from the
// receiver.
//
func (n *notification) swapNodeImpl(existing *node) *node {
if existing != nil {
// then swap the cRef pointers
swap := n.node.cRef
n.node.cRef = existing.cRef
existing.cRef = swap
} else {
existing = n.node
n.node = nil
}
return existing
}
//
// Swap the cRef of the receiver's node with cRef of the specified node.
//
// The intent is to update an existing go representation of a node with
// the latest *C.Value from the notification and then recycle
// the old *C.Value by attaching it to the notification where it will then
// be freed.
//
func (n *notification) swapValueImpl(existing *value) *value {
if existing != nil {
// then swap the cRef pointers
swap := n.value.cRef
n.value.cRef = existing.cRef
existing.cRef = swap
} else {
existing = n.value
n.value = nil
}
return existing
}
//
// called for unexpected notifications.
//
func unexpected(api API, notification Notification) {
api.Logger().Warningf("unexpected notification received %v]\n", notification)
}
//
// called for expected notifications that are not handled
//
func unhandled(api API, notification Notification) {
// api.Logger().Debugf("unhandled notification received %v]\n", notification)
}