-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswitch.go
67 lines (57 loc) · 1.52 KB
/
switch.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 wemo
import (
"context"
"fmt"
"log"
"net/http"
"sync"
)
type SwitchService struct {
state bool // false=off, true=on
mu sync.Mutex
name string
port string
uuid string
serial string
onCallback func(ctx context.Context, state bool) bool
offCallback func(ctx context.Context, state bool) bool
}
func ConfigSwitchService(name string, port, uuid, serial string, onCallback, offCallback func(ctx context.Context, state bool) bool) *SwitchService {
return &SwitchService{
state: false,
name: name,
port: port,
uuid: uuid,
serial: serial,
onCallback: onCallback,
offCallback: offCallback,
}
}
func (s *SwitchService) run(ctx context.Context) error {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome to wemo emulator.\nhttps://github.com/atotto/wemo-emulator")
})
mux.HandleFunc("/setup.xml", s.handleSetup)
mux.HandleFunc("/upnp/control/basicevent1", s.handleUpnpControlBasicEvent1)
mux.HandleFunc("/eventservice.xml", handleEventService)
// TODO: shutdown
return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", s.port), mux)
}
func (s *SwitchService) SetState(state bool) {
s.mu.Lock()
s.state = state
s.mu.Unlock()
}
func StartSwitchServices(ctx context.Context, services ...*SwitchService) error {
for _, s := range services {
s := s
go func() {
err := s.run(ctx)
if err != nil {
log.Fatal(err)
}
}()
}
return startUPnPService(ctx, services...)
}