-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
122 lines (107 loc) · 3.58 KB
/
api.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
package openzwave
//
// Provides a facade for the C++ API that exposes just enough of the underlying C++
// API to be useful to implementing the Ninja Zwave driver
//
// The functions in this module are responsible for marshalling to and from the C functions
// declared in api.hpp and api.cpp.
//
//
// The following #cgo directives assumed that 'go' is a symbolic link that references the gopath that contains the current directory, e.g. ../../../..
//
// All 'go' packages that have this package as a dependency should include such a go link and will then inherit the library built in this package.
//
// #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"
// A value-less type that is used to represent signals generated by the API, particularly quit signals used to
// ask an EventLoop to quit.
type api struct {
shareable
loop EventLoop
callback NotificationCallback
eventCallback EventCallback
deviceFactory DeviceFactory
device string
quitEventLoop chan int
shutdownDriver chan int
logger Logger
networks map[uint32]*network
quitDeviceMonitor chan int
}
//
// The API interface is available to implementors of the EventLoop type when the
// Configurator.Run() method is called.
//
type API interface {
// The EventLoop should return from the function when a signal is received on this channel
QuitSignal() chan int
// the API logger
Logger() Logger
// Get a network by HomeID
// Returns network, or nil if no network exists matching the specified HomeID
GetNetwork(homeId uint32) Network
// Shutdown the event loop
Shutdown(exit int)
}
//
// Begin the construction of the API by returning a Configurator
//
// configPath is the name of the directory containing openzwave configuration files;
// userPath is the name of the directory containing user specific openzwave configuration files;
// overrides are command line options (uptto --) that can overide the configuration provided by the previous two options.
//
// For more information about these parameters, refer to the documentation for the C++ OpenZWave::Options class.
//
func BuildAPI(configPath string, userPath string, overrides string) Configurator {
var (
cConfigPath *C.char = C.CString(configPath)
cUserPath *C.char = C.CString(userPath)
cOverrides *C.char = C.CString(overrides)
)
//defer C.free(unsafe.Pointer(cConfigPath))
//defer C.free(unsafe.Pointer(cUserPath))
//defer C.free(unsafe.Pointer(cOverrides)
C.startOptions(cConfigPath, cUserPath, cOverrides)
var r *api
r = &api{
loop: defaultEventLoop,
callback: nil,
eventCallback: defaultEventCallback,
deviceFactory: defaultDeviceFactory,
device: defaultDriverName,
quitEventLoop: make(chan int, 0),
shutdownDriver: make(chan int, 2),
logger: &defaultLogger{},
networks: make(map[uint32]*network),
quitDeviceMonitor: make(chan int, 2)}
r.shareable.init(r)
return r
}
func (a *api) QuitSignal() chan int {
return a.quitEventLoop
}
func (a *api) Logger() Logger {
return a.logger
}
func (a *api) GetNetwork(homeId uint32) Network {
net, ok := a.networks[homeId]
if !ok {
return nil
}
return net
}
func (a *api) getNetwork(homeId uint32) *network {
net, ok := a.networks[homeId]
if !ok {
net = newNetwork(homeId)
a.networks[homeId] = net
}
return net
}
func (a *api) notifyEvent(event Event) {
if a.eventCallback != nil {
a.eventCallback(a, event)
}
}