-
Notifications
You must be signed in to change notification settings - Fork 6
/
configurator.go
130 lines (104 loc) · 3.85 KB
/
configurator.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
129
130
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"
// This interface is used to configure the API by setting various options,
// and the controller device name. When configuration is finished, call the Run method
// with an EventLoop function.
//
// To obtain a reference to this interface, call BuildAPI().
//
// The result of the Run function can be passed as a parameter to Os.Exit().
//
//
type Configurator interface {
//Configure the logger implementation.
SetLogger(Logger) Configurator
//Configure the synchronous notification callback.
SetNotificationCallback(NotificationCallback) Configurator
//Configure the synchronous events callback
SetEventsCallback(eventCallback EventCallback) Configurator
//Configure the synchronous events callback
SetDeviceFactory(deviceFactory DeviceFactory) Configurator
//Configure the event loop function
SetEventLoop(EventLoop) Configurator
// Add an integer option.
AddIntOption(option string, value int) Configurator
// Add a boolean option.
AddBoolOption(option string, value bool) Configurator
// Add a string option.
AddStringOption(option string, value string, append bool) Configurator
// Set the device name used by the driver.
SetDeviceName(device string) Configurator
// Run the event loop forever
Run() int
}
// configure the C++ Options object with an integer value
func (a *api) AddIntOption(option string, value int) Configurator {
var cOption *C.char = C.CString(option)
//defer C.free(unsafe.Pointer(cOption))
C.addIntOption(cOption, C.int(value))
return a
}
// configure the C++ Options object with a boolean value
func (a *api) AddBoolOption(option string, value bool) Configurator {
var cOption *C.char = C.CString(option)
//defer C.free(unsafe.Pointer(cOption))
C.addBoolOption(cOption, C._Bool(value))
return a
}
// configure the C++ Options object with a string value
func (a *api) AddStringOption(option string, value string, append bool) Configurator {
var cOption *C.char = C.CString(option)
//defer C.free(unsafe.Pointer(cOption))
C.addStringOption(cOption, C.CString(value), C._Bool(append))
return a
}
// set the device name
func (a *api) SetDeviceName(device string) Configurator {
if device != "" {
a.device = device
}
return a
}
// set the logger
func (a *api) SetLogger(logger Logger) Configurator {
a.logger = logger
return a
}
// Clients of the API should provide Configuration.Run() with an implementation of this type to
// handle the notifications generated by the API.
//
// The implementor can return a non-zero code to indicate that the process should exit now. 0 means
// that the loop can be restarted, if required.
//
type EventLoop func(API) int
// set the event loop
func (a *api) SetEventLoop(loop EventLoop) Configurator {
a.loop = loop
return a
}
// A type of function that can receive notifications from the OpenZWave library when they occur.
//
// This callback is processed synchronously. This means that the implementor:
// MUST NOT block; MUST NOT hand the reference to the notification to a goroutine which lives beyond
// the callback call; MUST NOT store the reference to the notification in a structure that lives beyond
// the duration of the callback call.
type NotificationCallback func(API, Notification)
// set the synchronous call back
func (a *api) SetNotificationCallback(callback NotificationCallback) Configurator {
a.callback = callback
return a
}
type EventCallback func(API, Event)
// set the synchronous call back
func (a *api) SetEventsCallback(eventCallback EventCallback) Configurator {
a.eventCallback = eventCallback
return a
}
// set the device factory
func (a *api) SetDeviceFactory(deviceFactory DeviceFactory) Configurator {
a.deviceFactory = deviceFactory
return a
}