Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tessro committed May 4, 2021
0 parents commit 5df2e23
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
picoleaf
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright 2021 Paul Rosania

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Picoleaf

Picoleaf is a tiny CLI tool for controlling Nanoleaf.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/paulrosania/picoleaf

go 1.16

require gopkg.in/ini.v1 v1.62.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
267 changes: 267 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/user"
"path/filepath"

"gopkg.in/ini.v1"
)

const defaultConfigFile = ".picoleafrc"

var verbose = flag.Bool("v", false, "Verbose")

// Client is a Nanoleaf REST API client.
type Client struct {
Host string
Token string

client http.Client
}

// Get performs a GET request.
func (c Client) Get(path string) string {
if *verbose {
fmt.Println("\nGET", path)
}

url := c.Endpoint(path)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}

req.Header.Set("Accept", "application/json")

res, err := c.client.Do(req)
if err != nil {
log.Fatal(err)
}

if res.Body != nil {
defer res.Body.Close()
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}

if *verbose {
fmt.Println("<===", string(body))
}
return string(body)
}

// Put performs a PUT request.
func (c Client) Put(path string, body []byte) {
if *verbose {
fmt.Println("PUT", path)
fmt.Println("===>", string(body))
}

url := c.Endpoint(path)
req, err := http.NewRequest(http.MethodPut, url, nil)
if err != nil {
log.Fatal(err)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")

req.Body = ioutil.NopCloser(bytes.NewReader(body))

res, err := c.client.Do(req)
if err != nil {
log.Fatal(err)
}

if res.Body != nil {
defer res.Body.Close()
}
}

// Endpoint returns the full URL for an API endpoint.
func (c Client) Endpoint(path string) string {
return fmt.Sprintf("http://%s/api/v1/%s/%s", c.Host, c.Token, path)
}

// ListEffects returns an array of effect names.
func (c Client) ListEffects() ([]string, error) {
body := c.Get("effects/effectsList")
var list []string
err := json.Unmarshal([]byte(body), &list)
return list, err
}

// SelectEffect activates the specified effect.
func (c Client) SelectEffect(name string) error {
req := EffectsSelectRequest{
Select: name,
}
bytes, err := json.Marshal(req)
if err != nil {
return err
}

c.Put("effects/select", bytes)
return nil
}

// BrightnessProperty represents the brightness of the Nanoleaf.
type BrightnessProperty struct {
Value int16 `json:"value"`
Duration int16 `json:"duration,omitempty"`
}

// ColorTemperatureProperty represents the color temperature of the Nanoleaf.
type ColorTemperatureProperty struct {
Value int16 `json:"value"`
}

// HueProperty represents the hue of the Nanoleaf.
type HueProperty struct {
Value int16 `json:"value"`
}

// OnProperty represents the power state of the Nanoleaf.
type OnProperty struct {
Value bool `json:"value"`
}

// SaturationProperty represents the saturation of the Nanoleaf.
type SaturationProperty struct {
Value int16 `json:"value"`
}

// State represents a Nanoleaf state.
type State struct {
On *OnProperty `json:"on,omitempty"`
Brightness *BrightnessProperty `json:"brightness,omitempty"`
ColorTemperature *ColorTemperatureProperty `json:"ct,omitempty"`
Hue *HueProperty `json:"hue,omitempty"`
Saturation *SaturationProperty `json:"sat,omitempty"`
}

// EffectsSelectRequest represents a JSON PUT body for `effects/select`.
type EffectsSelectRequest struct {
Select string `json:"select"`
}

func main() {
flag.Parse()

usr, err := user.Current()
if err != nil {
fmt.Printf("Failed to fetch current user: %v", err)
os.Exit(1)
}
dir := usr.HomeDir
configFilePath := filepath.Join(dir, defaultConfigFile)

cfg, err := ini.Load(configFilePath)
if err != nil {
fmt.Printf("Failed to read file: %v", err)
os.Exit(1)
}

client := Client{
Host: cfg.Section("").Key("host").String(),
Token: cfg.Section("").Key("access_token").String(),
}

if *verbose {
fmt.Printf("Host: %s\n\n", client.Host)
}

if flag.NArg() > 0 {
cmd := flag.Arg(0)
switch cmd {
case "off":
state := State{
On: &OnProperty{false},
}
bytes, err := json.Marshal(state)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v", err)
os.Exit(1)
}
client.Put("state", bytes)
case "on":
state := State{
On: &OnProperty{true},
}
bytes, err := json.Marshal(state)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v", err)
os.Exit(1)
}
client.Put("state", bytes)
case "white":
state := State{
ColorTemperature: &ColorTemperatureProperty{6500},
}
bytes, err := json.Marshal(state)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v", err)
os.Exit(1)
}
client.Put("state", bytes)
case "red":
state := State{
Brightness: &BrightnessProperty{60, 0},
Hue: &HueProperty{0},
Saturation: &SaturationProperty{100},
}
bytes, err := json.Marshal(state)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v", err)
os.Exit(1)
}
client.Put("state", bytes)
case "effect":
doEffectCommand(client, flag.Args()[1:])
}
}
}

func doEffectCommand(client Client, args []string) {
if len(args) < 1 {
fmt.Println("usage: picoleaf effect list")
fmt.Println(" picoleaf effect select <name>")
os.Exit(1)
}

command := args[0]
switch command {
case "list":
list, err := client.ListEffects()
if err != nil {
fmt.Printf("Failed retrieve effects list: %v", err)
os.Exit(1)
}
for _, name := range list {
fmt.Println(name)
}
case "select":
if len(args) != 2 {
fmt.Println("usage: picoleaf effect select <name>")
os.Exit(1)
}

name := args[1]
err := client.SelectEffect(name)
if err != nil {
fmt.Printf("Failed to select effect: %v", err)
os.Exit(1)
}
}
}

0 comments on commit 5df2e23

Please sign in to comment.