Functioning shell for controlling lights :^)

This commit is contained in:
kayos@tcp.direct 2022-07-08 06:26:51 -07:00
parent e6874ed3b8
commit cd0c68e8a2
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
3 changed files with 77 additions and 30 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt"
"os"
"strings"
"sync"
"time"
cli "git.tcp.direct/Mirrors/go-prompt"
@ -132,7 +131,7 @@ func cmdScan(br *lights.Bridge, args []string) error {
if err != nil {
return err
}
for resp, _ := range r.Success {
for resp := range r.Success {
log.Info().Msg(resp)
}
var count = 0
@ -171,20 +170,20 @@ func StartCLI() {
var hist []string
processBridges(lights.Lucifer.Bridges)
comphead := 0
compmu := &sync.Mutex{}
cleanSlate := func(b *cli.Buffer) {
compmu.Lock()
defer compmu.Lock()
sugs := completer(*b.Document())
if comphead > len(sugs)-2 {
comphead = 0
return
}
comphead++
b.CursorLeft(len(b.Document().TextBeforeCursor()))
b.InsertText(sugs[comphead].Text, true, true)
}
/* comphead := 0
compmu := &sync.Mutex{}
cleanSlate := func(b *cli.Buffer) {
compmu.Lock()
defer compmu.Lock()
sugs := completer(*b.Document())
if comphead > len(sugs)-2 {
comphead = 0
return
}
comphead++
b.CursorLeft(len(b.Document().TextBeforeCursor()))
b.InsertText(sugs[comphead].Text, true, true)
}*/
p := cli.New(
executor,
@ -205,16 +204,16 @@ func StartCLI() {
return fmt.Sprintf("ziggs[%s] %s ", sel, bulb), true
}),
cli.OptionTitle("ziggs"),
cli.OptionAddKeyBind(cli.KeyBind{
Key: cli.Tab,
Fn: cleanSlate,
}),
cli.OptionCompletionOnDown(),
cli.OptionAddKeyBind(cli.KeyBind{
Key: cli.Down,
Fn: cleanSlate,
}),
)
/* cli.OptionAddKeyBind(cli.KeyBind{
Key: cli.Tab,
Fn: cleanSlate,
}),
*/cli.OptionCompletionOnDown(),
/* cli.OptionAddKeyBind(cli.KeyBind{
Key: cli.Down,
Fn: cleanSlate,
}),
*/)
p.Run()
}

View File

@ -1,12 +1,59 @@
package interactive
import (
"errors"
"strconv"
cli "git.tcp.direct/Mirrors/go-prompt"
"github.com/amimof/huego"
"git.tcp.direct/kayos/ziggs/lights"
)
func cmdLights(br *lights.Bridge, args []string) error {
if len(br.HueLights) <= 0 {
return errors.New("no lights found")
}
for _, l := range br.HueLights {
log.Info().Str("caller", l.Name).
Int("ID", l.ID).Str("type", l.ProductName).
Str("model", l.ModelID).Bool("on", l.IsOn()).Msgf("%v", l.State)
}
return nil
}
func cmdGroups(br *lights.Bridge, args []string) error {
var groupmap = make(map[string]*huego.Group)
gs, err := br.Bridge.GetGroups()
if err != nil {
return err
}
for _, g := range gs {
groupmap[g.Name] = &g
}
if len(args) > 0 {
switch {
case args[1] == "+":
return groupmap[args[0]].Bri(groupmap[args[0]].State.Bri + 25)
case args[1] == "-":
return groupmap[args[0]].Bri(groupmap[args[0]].State.Bri - 25)
default:
newBrightness, numErr := strconv.Atoi(args[1])
if numErr != nil {
return numErr
}
return groupmap[args[0]].Bri(uint8(newBrightness))
}
}
if len(gs) == 0 {
return errors.New("no lights found")
}
for _, g := range gs {
log.Info().Str("caller", g.Name).Str("type", g.Type).Int("ID", g.ID).
Str("class", g.Class).Bool("on", g.IsOn()).Msgf("%v", g.GroupState)
}
return nil
}
@ -15,6 +62,7 @@ type reactor func(bridge *lights.Bridge, args []string) error
var bridgeCMD = map[string]reactor{
"scan": cmdScan,
"lights": cmdLights,
"groups": cmdGroups,
}
const use = "use"

View File

@ -13,16 +13,16 @@ import (
var (
saveTermios unix.Termios
saveTermiosFD int
saveTermiosOnce sync.Once
saveTermiosOnce = &sync.Once{}
)
func getOriginalTermios(fd int) (unix.Termios, error) {
var err error
saveTermiosOnce.Do(func() {
saveTermiosFD = fd
var saveTermiosPtr *unix.Termios
termios.Tcgetattr(uintptr(fd), saveTermiosPtr)
saveTermios = *saveTermiosPtr
var saveTermiosPtr unix.Termios
err = termios.Tcgetattr(uintptr(fd), &saveTermiosPtr)
saveTermios = saveTermiosPtr
})
return saveTermios, err
}