-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgoopenzwave.go
67 lines (55 loc) · 1.48 KB
/
goopenzwave.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
package goopenzwave
// #cgo pkg-config: libopenzwave
// #include "gzw_manager.h"
// #include "gzw_notification.h"
// #include <stdlib.h>
import "C"
import (
"fmt"
)
var (
started bool
notificationHandler NotificationHandler
)
// NotificationHandler defines the format for a function that will handle new
// Notification's as they arrive.
type NotificationHandler func(notification *Notification)
// Start will create a new OpenZWave Manager, starting the library execution.
// The OpenZWave Options must be created and locked before calling this. See the
// Options struct. Also pass a NotificationHandler function to this as
// notifications will also be started.
func Start(handler NotificationHandler) error {
// Only start once.
if started {
return fmt.Errorf("already started")
}
// Create the manager.
err := createManager()
if err != nil {
return fmt.Errorf("failed to create manager: %s", err)
}
// Start notifications.
err = startNotifications()
if err != nil {
return err
}
// Set the notification handler.
notificationHandler = handler
return nil
}
// Stop will stop notifications and destroy the manager. Do this just before you
// quit your app. Don't forget to destroy the Options object after calling this.
func Stop() error {
// Check we have started.
if started {
return fmt.Errorf("already started")
}
// Stop notifications.
err := stopNotifications()
if err != nil {
return err
}
// Destroy the manager.
destroyManager()
return nil
}