forked from brutella/hap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotification.go
72 lines (61 loc) · 1.76 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
package hap
import (
"github.com/LUJUNQUAN/hap/accessory"
"github.com/LUJUNQUAN/hap/characteristic"
"github.com/LUJUNQUAN/hap/log"
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
func sendNotification(a *accessory.A, c *characteristic.C, req *http.Request) error {
pl := struct {
Cs []characteristicData `json:"characteristics"`
}{
Cs: []characteristicData{
characteristicData{
Aid: a.Id,
Iid: c.Id,
Value: &characteristic.V{c.Val},
},
},
}
plb, err := json.Marshal(pl)
if err != nil {
return err
}
body := bytes.NewBuffer(plb)
// Use http.Response to send the notification as an http message.
resp := new(http.Response)
resp.Status = "200 OK"
resp.StatusCode = http.StatusOK
resp.ProtoMajor = 1
resp.ProtoMinor = 0
resp.Body = ioutil.NopCloser(body)
resp.ContentLength = int64(body.Len())
resp.Header = map[string][]string{}
resp.Header.Set("Content-Type", HTTPContentTypeHAPJson)
// Will be ignored unfortunately and won't be fixed https://github.com/golang/go/issues/9304
// Make sure to call FixProtocolSpecifier() instead
// resp.Proto = "EVENT/1.0"
// Set protocol of message to "EVENT/1.0".
var buffer = new(bytes.Buffer)
resp.Write(buffer)
b, err := ioutil.ReadAll(buffer)
b = []byte(strings.Replace(string(b), "HTTP/1.0", "EVENT/1.0", 1))
for _, conn := range conns() {
if req != nil && req.RemoteAddr == conn.RemoteAddr().String() {
// Don't send notification to the client
// who updated the value.
log.Debug.Printf("skip notification for %s\n", conn.RemoteAddr())
continue
}
// Check which connection has events enabled.
if c.HasEventsEnabled(conn.RemoteAddr().String()) {
log.Debug.Printf("send event to %s:\n%s\n", conn.RemoteAddr(), string(b))
conn.Write(b)
}
}
return nil
}