-
Notifications
You must be signed in to change notification settings - Fork 5
/
bridge.go
228 lines (166 loc) · 4.54 KB
/
bridge.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/service"
"github.com/tarm/serial"
"github.com/urfave/cli"
"log"
"os"
"time"
)
type MessageType byte
const (
// checking the availability of the desk
TypeAliveRequest MessageType = 0x01
TypeAliveResponse MessageType = 0x02
// setting the height of the desk
TypeSetHeightRequest MessageType = 0x03
// querying the height of the desk
TypeGetHeightRequest MessageType = 0x04
TypeGetHeightResponse MessageType = 0x05
// stopping the desk
TypeStopRequest MessageType = 0x06
// TODO: to be implemented
TypeGetStatusRequest MessageType = 0x07
TypeGetStatusResponse MessageType = 0x08
// moving the desk
TypeMoveUpRequest MessageType = 0x0A
TypeMoveDownRequest MessageType = 0x0B
// the desk notifying about a height change
TypeUpdateHeightEvent MessageType = 0x0C
)
type Message struct {
Type MessageType
Value byte
}
func receiver(c chan<- Message, p *serial.Port) {
message := make([]byte, 3)
buf := make([]byte, 1)
for {
// shift bytes to left
message[0] = message[1]
message[1] = message[2]
// read new byte
_, err := p.Read(buf)
if err != nil {
log.Fatal(err)
}
// append new byte
message[2] = buf[0]
// checksum
if message[0]+message[1] != message[2] {
continue
}
c <- Message{Type: MessageType(message[0]), Value: message[1]}
}
}
func sender(c <-chan Message, p *serial.Port) {
message := make([]byte, 3)
for {
// get a message
m := <-c
log.Println("Message to send", m)
// fill the message buffer
message[0] = byte(m.Type)
message[1] = m.Value
// calculate the checksum
message[2] = message[0] + message[1]
log.Println("Buffer to send", message)
// write the buffer
_, err := p.Write(message)
if err != nil {
log.Fatal(err)
}
}
}
func heightToPercentage(height int) int {
return (height - 68) / 50
}
func heightPercentageToCentimeters(percentage int) int {
factor := float64(percentage) / 100.0
return int(68.0 + 50.0*factor)
}
func setInitialDeskPosition(outgoing chan<- Message, incoming <-chan Message, service *service.Window) {
log.Println("Setting initial desk position.")
time.Sleep(2000 * time.Millisecond)
outgoing <- Message{Type: TypeGetHeightRequest}
hi := <-incoming
log.Println("height is", hi.Value)
percentage := heightToPercentage(int(hi.Value))
service.TargetPosition.SetValue(percentage)
service.CurrentPosition.SetValue(percentage)
}
func startServer(dataPath string) {
info := accessory.Info{
Name: "Office Desk",
Manufacturer: "David Knezic",
SerialNumber: "3214-3232-32",
Model: "A",
}
log.Println("Creating accessory and service...")
// sadly, window is the closest thing to a desk in HomeKit
acc := accessory.New(info, accessory.TypeWindow)
service := service.NewWindow()
log.Println("Opening serial console...")
c := &serial.Config{
Name: "/dev/ttyAMA0",
Baud: 9600,
}
s, err := serial.OpenPort(c)
if err != nil {
log.Println("Failed opening serial console.")
log.Fatal(err)
}
log.Println("Starting desk utility...")
var incoming chan Message = make(chan Message)
var outgoing chan Message = make(chan Message)
go receiver(incoming, s)
go sender(outgoing, s)
service.TargetPosition.OnValueRemoteUpdate(func(position int) {
log.Println("Setting desk to", position, "percent height")
height := heightPercentageToCentimeters(position)
log.Println("This corresponds to", height, "cm height")
outgoing <- Message{Type: MessageType(TypeSetHeightRequest), Value: byte(height)}
service.CurrentPosition.SetValue(position)
})
log.Println("Adding service to accessory...")
acc.AddService(service.Service)
log.Println("Will store data to", dataPath)
config := hc.Config{
Pin: "32191123",
StoragePath: dataPath,
}
log.Println("Starting home control...")
t, err := hc.NewIPTransport(config, acc)
if err != nil {
log.Println("Failed creating home control.")
log.Fatal(err)
}
log.Println("Created home control.")
hc.OnTermination(func() {
t.Stop()
})
go setInitialDeskPosition(outgoing, incoming, service)
t.Start()
}
func main() {
app := cli.NewApp()
app.Name = "desk"
app.Usage = "HomeKit bridge for height-adjustable desks"
app.Version = "1.0.0"
log.Println("Starting desk utility")
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "data",
Value: "./desk",
Usage: "Save HomeKit data to `PATH`",
EnvVar: "DESK_DATA_PATH",
},
}
app.Action = func(c *cli.Context) error {
startServer(c.String("data"))
return nil
}
app.Run(os.Args)
}