Skip to content

Commit

Permalink
add lint, unit and build to make script and travis
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Hartmann <chris@lollyrock.com>
  • Loading branch information
chris-rock committed Dec 30, 2017
1 parent d89af5f commit a5e2d3d
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,3 +1,3 @@
FRITZBox 7490
vendor
trials
dist
27 changes: 27 additions & 0 deletions .travis.yml
@@ -0,0 +1,27 @@
language: go
branches:
only:
- master

sudo: required

go:
- 1.9

install:
- make install/golint

matrix:
include:
- script: make $TASK
env: TASK="build/linux-arm"
- script: make $TASK
env: TASK="build/linux-amd64"
- script: make $TASK
env: TASK="build/macos-amd64"
- script: make $TASK
env: TASK="test/lint"
- script: make $TASK
env: TASK="test/unit"
- script: make $TASK
env: TASK="test/vet"
35 changes: 35 additions & 0 deletions Makefile
@@ -0,0 +1,35 @@
#!/bin/bash

build/linux-arm:
@GOOS=linux GOARCH=arm go build -o dist/linux-arm/hkfritz main.go

build/linux-amd64:
@GOOS=linux GOARCH=amd64 go build -o dist/linux-amd64/hkfritz main.go

build/macos-amd64:
@GOOS=darwin GOARCH=amd64 go build -o dist/macos-amd64/hkfritz main.go

run:
@go run main.go serve

test/unit :
@go test -v -i $(shell go list ./... | grep -v '/vendor/')
@go test -v $(shell go list ./... | grep -v '/vendor/')

test/vet:
@go vet $(shell go list ./... | grep -v '/vendor/')

test/fmt:
@go fmt $(shell go list ./... | grep -v '/vendor/')

test/lint:
@for package in $(shell go list ./... | grep -v '/vendor/' | grep -v '/api' | grep -v '/server/internal'); do \
golint -set_exit_status $$package $$i || exit 1; \
done

install/golint:
@go get -u github.com/golang/lint/golint

install/goreleaser:
@go get github.com/goreleaser/goreleaser

11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -3,14 +3,15 @@
This project adds the missing feature of a HomeKit Bridge to all Fritz!Box smart home devices.

Note: This is a hobby project and was stated by a few questions:
- How does homekit work?

- How does HomeKit work?
- What is required to run a secure environment for smart home devices?
- Can everything on raspberry pi 1
- How can I manage my devices with Siri
- Can everything run on RaspberryPi 1
- How can I manage my smart home devices with Siri

## Features

- no need for exposing Fritz!Box to internet
- no need for exposing Fritz!Box to public internet
- easy setup with HomeKit setup codes

## Tested Devices
Expand Down Expand Up @@ -49,5 +50,5 @@ This project is built ontop of the great libraries:

## Author

- Christopoh Hartmann
- Christoph Hartmann

6 changes: 3 additions & 3 deletions cmd/configure.go
Expand Up @@ -38,7 +38,7 @@ var configureCmd = &cobra.Command{
// initialize config
cfg := &homekit.Config{
FritzBox: &homekit.FritzBoxConfig{},
HomeKit: &homekit.HomeKitConfig{},
HomeKit: &homekit.HKConfig{},
}

validateURL := func(input string) error {
Expand All @@ -59,7 +59,7 @@ var configureCmd = &cobra.Command{
fmt.Printf("Prompt failed %v\n", err)
return
}
cfg.FritzBox.Url = fbURL
cfg.FritzBox.URL = fbURL

prompt = promptui.Prompt{
Label: "Self-signed certificate",
Expand Down Expand Up @@ -94,7 +94,7 @@ var configureCmd = &cobra.Command{
// generate PIN and Setup Code
cfg.HomeKit.Pin = fmt.Sprintf("%08d", rand.Intn(100000000))
// generate setup id
cfg.HomeKit.SetupId = randStringBytesRmndr(4)
cfg.HomeKit.SetupID = randStringBytesRmndr(4)

d, err := yaml.Marshal(cfg)
if err != nil {
Expand Down
12 changes: 4 additions & 8 deletions cmd/root.go
Expand Up @@ -15,14 +15,10 @@ var verbose bool

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "boxkit",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Use: "hkfritz",
Short: "HomeKit bridge for Fritz!Box",
Long: `It is a service that bridges Fritz!Box smart home devices with
Apple's HomeKit`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand Down
8 changes: 3 additions & 5 deletions cmd/serve.go
Expand Up @@ -14,14 +14,14 @@ var serveCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
setLogging()

hk := &homekit.HomeKitConfig{
hk := &homekit.HKConfig{
Pin: viper.GetString("homekit.pin"),
SetupId: viper.GetString("homekit.setupid"),
SetupID: viper.GetString("homekit.setupid"),
}
fb := &homekit.FritzBoxConfig{
Username: viper.GetString("fritzbox.username"),
Password: viper.GetString("fritzbox.password"),
Url: viper.GetString("fritzbox.url"),
URL: viper.GetString("fritzbox.url"),
}

config := &homekit.Config{
Expand All @@ -37,8 +37,6 @@ var serveCmd = &cobra.Command{
},
}

var servce_hk_pin string

func init() {
rootCmd.AddCommand(serveCmd)
}
16 changes: 8 additions & 8 deletions cmd/setupcode.go
Expand Up @@ -3,8 +3,7 @@ package cmd
import (
"fmt"

"github.com/brutella/hc/accessory"
"github.com/chris-rock/homekit-fritz/setupcode"
"github.com/chris-rock/homekit-fritz/homekit"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -15,13 +14,14 @@ var setupcodeCmd = &cobra.Command{
Short: "Print HomeKit SetupCode",
Long: `This command prints the setup code that can be used within the Home app`,
Run: func(cmd *cobra.Command, args []string) {
pin := viper.GetString("homekit.pin")
setupid := viper.GetString("homekit.setupid")
xhmuri := setupcode.GenXhmUri(uint(accessory.TypeBridge), 0, pin, setupid)
qrcode := setupcode.GenCliQRCode(xhmuri)
hk := &homekit.HKConfig{
Pin: viper.GetString("homekit.pin"),
SetupID: viper.GetString("homekit.setupid"),
}

fmt.Println("HomeKit setup qr code:")
fmt.Println(qrcode)
fmt.Printf("HomeKit setup code: %s\n", pin)
homekit.Qrcode(hk)
fmt.Printf("HomeKit setup code: %s\n", hk.Pin)
},
}

Expand Down
1 change: 1 addition & 0 deletions homekit/bridge.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/sirupsen/logrus"
)

// CreateBridge reads the Fritz!Box information and creates a HomeKit Bridge device
func CreateBridge(fbConfig *FritzBoxConfig) (*accessory.Accessory, error) {

fburl := fbConfig.GetFritzBoxURL()
Expand Down
3 changes: 3 additions & 0 deletions homekit/devices.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/sirupsen/logrus"
)

// NewHKSwitch creates a new switch for Fritz!DECT 200 devices
func NewHKSwitch(info accessory.Info, h fritz.HomeAuto, fdevice fritz.Device) (*accessory.Accessory, error) {
logrus.Debugf("create new HomeKit outlet device %v", info)

Expand Down Expand Up @@ -36,6 +37,7 @@ func NewHKSwitch(info accessory.Info, h fritz.HomeAuto, fdevice fritz.Device) (*
return acc.Accessory, nil
}

// NewHKThermostat creates a new thermostat for Fritz!DECT 30x devices
func NewHKThermostat(info accessory.Info, h fritz.HomeAuto, fdevice fritz.Device) (*accessory.Accessory, error) {
logrus.Debugf("create new HomeKit thermostat device %v", info)

Expand Down Expand Up @@ -64,6 +66,7 @@ func NewHKThermostat(info accessory.Info, h fritz.HomeAuto, fdevice fritz.Device
return acc.Accessory, nil
}

// NewHomeKitDevice will create a HomeKit device based on the information about the Fritz!Device
func NewHomeKitDevice(h fritz.HomeAuto, fdevice fritz.Device) (*accessory.Accessory, error) {
logrus.Infof("register %s", fdevice.Name)

Expand Down
2 changes: 2 additions & 0 deletions homekit/fritz.go
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/sirupsen/logrus"
)

// ListHKDevices reads all smart home devices from Fritz!Box and maps them
// to HomeKit devices
func ListHKDevices(fbConfig *FritzBoxConfig) ([]*accessory.Accessory, error) {

h := fritz.NewHomeAuto(
Expand Down
24 changes: 14 additions & 10 deletions homekit/homekit.go
Expand Up @@ -10,39 +10,43 @@ import (
"github.com/sirupsen/logrus"
)

// Configuration
type HomeKitConfig struct {
// HKConfig contains the configuration for the HomeKit service
type HKConfig struct {
Pin string `yaml:"pin"`
SetupId string `yaml:"setupid"`
SetupID string `yaml:"setupid"`
}

// FritzBoxConfig contains the confiratioin to access the Fritz!Box API
type FritzBoxConfig struct {
Url string `yaml:"url"`
URL string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Insecure bool `yaml:"insecure"`
}

// GetFritzBoxURL parses the url and return its object
func (fbc *FritzBoxConfig) GetFritzBoxURL() *url.URL {
url, err := url.Parse(fbc.Url)
url, err := url.Parse(fbc.URL)
if err != nil {
return nil
}
return url
}

// Config combines the Fritz!Box and the HomeKit configuration
type Config struct {
HomeKit *HomeKitConfig `yaml:"homekit"`
HomeKit *HKConfig `yaml:"homekit"`
FritzBox *FritzBoxConfig `yaml:"fritzbox"`
}

func Qrcode(hk *HomeKitConfig) {
xhmuri := setupcode.GenXhmUri(uint(accessory.TypeBridge), 0, hk.Pin, hk.SetupId)
// Qrcode prints out the setup code based on the configuration
func Qrcode(hk *HKConfig) {
xhmuri := setupcode.GenXhmURI(uint(accessory.TypeBridge), 0, hk.Pin, hk.SetupID)
qrcode := setupcode.GenCliQRCode(xhmuri)
fmt.Println(qrcode)
}

// Service Implementation
// Start is the HomeKit service that runs when you start `hkfritz serve`
func Start(config *Config) {

// create fritzbox gateway
Expand All @@ -52,7 +56,7 @@ func Start(config *Config) {
hkDevices, err := ListHKDevices(config.FritzBox)

// configure homekit service
hcconfig := hc.Config{Pin: config.HomeKit.Pin, SetupId: config.HomeKit.SetupId}
hcconfig := hc.Config{Pin: config.HomeKit.Pin, SetupId: config.HomeKit.SetupID}

// create fritzbox as bridge device with all attached home kit devices
t, err := hc.NewIPTransport(hcconfig, fbBridge, hkDevices...)
Expand Down
7 changes: 4 additions & 3 deletions setupcode/setupcode.go
Expand Up @@ -9,11 +9,11 @@ import (
qrcode "github.com/skip2/go-qrcode"
)

// GenXhmUri generates the xhm uri for that includes the pin and the setupid
// GenXhmURI generates the xhm uri for that includes the pin and the setupid
// References are:
// - https://github.com/nfarina/homebridge/issues/1588#issuecomment-341158722
// - https://github.com/KhaosT/HAP-NodeJS/blob/67032e75b9f5f74993ad932c849d5bbb1937a097/lib/Accessory.js#L363
func GenXhmUri(category uint, hapType uint, pin string, setupId string) string {
func GenXhmURI(category uint, hapType uint, pin string, setupID string) string {

prefix := "X-HM://00"
var payload uint64
Expand All @@ -35,10 +35,11 @@ func GenXhmUri(category uint, hapType uint, pin string, setupId string) string {
// covert to base 36
s36 := strings.ToUpper(strconv.FormatUint(payload, 36))

content := prefix + s36 + setupId
content := prefix + s36 + setupID
return content
}

// GenCliQRCode takes a xhm uri string and returns the qr code as string
func GenCliQRCode(xhm string) string {
var q *qrcode.QRCode
q, err := qrcode.New(xhm, qrcode.Highest)
Expand Down
4 changes: 2 additions & 2 deletions setupcode/setupcode_test.go
Expand Up @@ -6,8 +6,8 @@ func TestGenxhmuri(t *testing.T) {

expected := "X-HM://0023ISYWYFRIT"

result := genxhmuri(2, 0, "03145154", "FRIT")
result := GenXhmURI(2, 0, "03145154", "FRIT")
if result != expected {
t.Errorf("genxhmuri was incorrect, got: %d, want: %d.", result, expected)
t.Errorf("genxhmuri was incorrect, got: %s, want: %s.", result, expected)
}
}

0 comments on commit a5e2d3d

Please sign in to comment.