summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2019-01-11 04:23:56 +0000
committerMike Crute <mike@crute.us>2019-01-11 04:23:56 +0000
commitd87c500edebab14345c5524bf0ed6e026615c12b (patch)
treee38e412b3ea63f790526477f97906e015a67fe35
parent769043aa828524c14116023597755959ce8697b7 (diff)
downloadmfi_homekit-d87c500edebab14345c5524bf0ed6e026615c12b.tar.bz2
mfi_homekit-d87c500edebab14345c5524bf0ed6e026615c12b.tar.xz
mfi_homekit-d87c500edebab14345c5524bf0ed6e026615c12b.zip
Cleanup and add todos
-rw-r--r--Makefile4
-rw-r--r--main.go37
2 files changed, 14 insertions, 27 deletions
diff --git a/Makefile b/Makefile
index 99f8680..bdce54d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,7 @@
1.PHONY: all
2all:
3 CGO_ENABLED=0 go build -o mfi_homekit main.go
4
1.PHONY: docker 5.PHONY: docker
2docker: 6docker:
3 cp mfi_homekit docker/mfi_homekit; \ 7 cp mfi_homekit docker/mfi_homekit; \
diff --git a/main.go b/main.go
index 5545b65..8926c4d 100644
--- a/main.go
+++ b/main.go
@@ -4,18 +4,18 @@ import (
4 "encoding/json" 4 "encoding/json"
5 "errors" 5 "errors"
6 "fmt" 6 "fmt"
7 "io/ioutil"
8 "net"
9 "time"
10
7 "github.com/brutella/hc" 11 "github.com/brutella/hc"
8 "github.com/brutella/hc/accessory" 12 "github.com/brutella/hc/accessory"
9 "github.com/brutella/hc/characteristic" 13 "github.com/brutella/hc/characteristic"
10 "golang.org/x/crypto/ssh" 14 "golang.org/x/crypto/ssh"
11 "io/ioutil"
12 "net"
13 "time"
14) 15)
15 16
16const ( 17const (
17 CMD_PATH = "/var/etc/persistent/power_control.sh" 18 CMD_PATH = "/var/etc/persistent/power_control.sh"
18 NO_BLINK_CMD = "/usr/bin/sh -c 'echo 0 > /proc/led/freq && echo 1 > /proc/led/status'"
19) 19)
20 20
21type Device struct { 21type Device struct {
@@ -173,15 +173,6 @@ func (d *Device) Toggle(n string, on bool) ([]DeviceOutput, error) {
173 } 173 }
174} 174}
175 175
176func (d *Device) DisableBlink() error {
177 _, err := d.runCommand(NO_BLINK_CMD)
178 if err != nil {
179 return err
180 } else {
181 return nil
182 }
183}
184
185func (d *Device) GetReport() ([]DeviceOutput, error) { 176func (d *Device) GetReport() ([]DeviceOutput, error) {
186 out, err := d.runCommand(fmt.Sprintf("%s report", CMD_PATH)) 177 out, err := d.runCommand(fmt.Sprintf("%s report", CMD_PATH))
187 if err != nil { 178 if err != nil {
@@ -249,18 +240,6 @@ func GatherReports(devs []*Device) {
249 } 240 }
250} 241}
251 242
252func StopBlinking(devs []*Device) {
253 t := time.NewTicker(10 * time.Second)
254 defer t.Stop()
255
256 for {
257 <-t.C
258 for _, d := range devs {
259 d.DisableBlink()
260 }
261 }
262}
263
264func main() { 243func main() {
265 devs, err := LoadAppConfig("config.json") 244 devs, err := LoadAppConfig("config.json")
266 if err != nil { 245 if err != nil {
@@ -271,9 +250,13 @@ func main() {
271 reg := []*accessory.Accessory{} 250 reg := []*accessory.Accessory{}
272 accs := make(map[*characteristic.Characteristic]*Output, 10) 251 accs := make(map[*characteristic.Characteristic]*Output, 10)
273 252
253 // TODO: Do this in a goroutine and add retries for devices so one device
254 // doesn't block booting the whole controller
274 for _, k := range devs { 255 for _, k := range devs {
275 fmt.Printf("Connecting to %s\n", k.Name) 256 fmt.Printf("Connecting to %s\n", k.Name)
276 257
258 // TODO: Upgrade or install controller script when first connecting
259
277 if err = k.Connect(); err != nil { 260 if err = k.Connect(); err != nil {
278 panic(err) 261 panic(err)
279 } 262 }
@@ -283,6 +266,7 @@ func main() {
283 panic(err) 266 panic(err)
284 } 267 }
285 268
269 // TODO: Allow the homekit app to provide device and output names
286 for i, d := range k.Devices { 270 for i, d := range k.Devices {
287 // Unnamed outputs are unused 271 // Unnamed outputs are unused
288 if d == "" { 272 if d == "" {
@@ -318,6 +302,5 @@ func main() {
318 302
319 // Gathering reports also keeps the SSH connections alive 303 // Gathering reports also keeps the SSH connections alive
320 go GatherReports(devs) 304 go GatherReports(devs)
321 go StopBlinking(devs)
322 t.Start() 305 t.Start()
323} 306}