From 96a60483819ce0e9c0b8c4a497fdc8fb7eb1be06 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Sun, 21 Nov 2021 15:12:17 -0600 Subject: [PATCH] updates --- cmd/root.go | 29 +++++-- go.mod | 30 ++++++- go.sum | 16 ++-- internal/macrobutton.go | 187 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 241 insertions(+), 21 deletions(-) create mode 100644 internal/macrobutton.go diff --git a/cmd/root.go b/cmd/root.go index 0b8e751..661e306 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -328,7 +328,7 @@ func root(cmd *cobra.Command, args []string) { t := internal.NewDynamicIconTextButton( ha, sd, - "switch.lamp_near_tv", + "switch.lamp_near_tv_relay", internal.ButtonInfo{ X: 4, Y: 2, @@ -393,7 +393,7 @@ func root(cmd *cobra.Command, args []string) { internal.ServiceInfo{ Domain: "homeassistant", Service: "toggle", - Data: internal.HomeAssistantToggle{EntityID: "switch.lamp_near_tv"}, + Data: internal.HomeAssistantToggle{EntityID: "switch.lamp_near_tv_relay"}, }, ) t.InitializeButton() @@ -401,7 +401,7 @@ func root(cmd *cobra.Command, args []string) { t = internal.NewDynamicIconTextButton( ha, sd, - "light.lamp_near_recliner", + "light.lamp_near_recliner_2", internal.ButtonInfo{ X: 4, Y: 1, @@ -466,7 +466,7 @@ func root(cmd *cobra.Command, args []string) { internal.ServiceInfo{ Domain: "homeassistant", Service: "toggle", - Data: internal.HomeAssistantToggle{EntityID: "light.lamp_near_recliner"}, + Data: internal.HomeAssistantToggle{EntityID: "light.lamp_near_recliner_2"}, }, ) t.InitializeButton() @@ -474,7 +474,7 @@ func root(cmd *cobra.Command, args []string) { t = internal.NewDynamicIconTextButton( ha, sd, - "switch.lamp_near_couch", + "switch.lamp_near_couch_relay", internal.ButtonInfo{ X: 4, Y: 0, @@ -539,7 +539,7 @@ func root(cmd *cobra.Command, args []string) { internal.ServiceInfo{ Domain: "homeassistant", Service: "toggle", - Data: internal.HomeAssistantToggle{EntityID: "switch.lamp_near_couch"}, + Data: internal.HomeAssistantToggle{EntityID: "switch.lamp_near_couch_relay"}, }, ) t.InitializeButton() @@ -547,7 +547,7 @@ func root(cmd *cobra.Command, args []string) { t = internal.NewDynamicIconTextButton( ha, sd, - "switch.lava_lamp", + "switch.wall_plug_switch_3", internal.ButtonInfo{ X: 3, Y: 2, @@ -602,11 +602,24 @@ func root(cmd *cobra.Command, args []string) { internal.ServiceInfo{ Domain: "homeassistant", Service: "toggle", - Data: internal.HomeAssistantToggle{EntityID: "switch.lava_lamp"}, + Data: internal.HomeAssistantToggle{EntityID: "switch.wall_plug_switch_3"}, }, ) t.InitializeButton() + // m := internal.NewMacroButton( + // sd, + // internal.ButtonInfo{X: 3, Y: 0}, + // internal.StaticIconInfo{ + // Icon: "skip-backward", + // FgColor: color.RGBA{150, 150, 150, 255}, + // BgColor: color.RGBA{0, 0, 0, 255}, + // PressedFgColor: color.RGBA{255, 0, 0, 255}, + // PressedBgColor: color.RGBA{0, 0, 0, 255}, + // }, + // ) + // m.InitializeButton() + wg.Add(1) go func() { defer wg.Done() diff --git a/go.mod b/go.mod index 8de30f1..ff3f67f 100644 --- a/go.mod +++ b/go.mod @@ -1,15 +1,41 @@ module git.ocjtech.us/jeff/mystreamdeck -go 1.15 +go 1.17 require ( github.com/cenkalti/backoff/v4 v4.0.2 github.com/go-fonts/dejavu v0.1.0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 - github.com/jcollie/go-homeassistant v0.0.3 + github.com/jcollie/go-homeassistant v0.0.4 github.com/jcollie/go-streamdeck v0.0.2 + github.com/micmonay/keybd_event v1.1.0 github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.7.1 ) + +require ( + github.com/disintegration/imaging v1.6.2 // indirect + github.com/fsnotify/fsnotify v1.4.7 // indirect + github.com/gorilla/websocket v1.4.2 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/karalabe/hid v1.0.1-0.20190806082151-9c14560f9ee8 // indirect + github.com/magiconair/properties v1.8.1 // indirect + github.com/mitchellh/mapstructure v1.1.2 // indirect + github.com/pelletier/go-toml v1.2.0 // indirect + github.com/spf13/afero v1.1.2 // indirect + github.com/spf13/cast v1.3.0 // indirect + github.com/spf13/jwalterweatherman v1.0.0 // indirect + github.com/spf13/pflag v1.0.3 // indirect + github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 // indirect + github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect + github.com/subosito/gotenv v1.2.0 // indirect + golang.org/x/image v0.0.0-20200926103441-2fd4ef34c9a9 // indirect + golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321 // indirect + golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect + golang.org/x/text v0.3.2 // indirect + gopkg.in/ini.v1 v1.51.0 // indirect + gopkg.in/yaml.v2 v2.2.4 // indirect +) diff --git a/go.sum b/go.sum index bb61499..d3a344c 100644 --- a/go.sum +++ b/go.sum @@ -108,13 +108,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/jcollie/go-homeassistant v0.0.3-0.20200926210117-3648ddaefa07 h1:1ayms6ixwYsak8qFDVu8ovNWXuIzpsGPRsETFGSkMNs= -github.com/jcollie/go-homeassistant v0.0.3-0.20200926210117-3648ddaefa07/go.mod h1:q15A/ofWWLuq1/kxkKM/V6smX/2GKHOk3PbqE+wc5lY= -github.com/jcollie/go-homeassistant v0.0.3 h1:MqzitlpiyDTnThJM6Vyq76sch+29xm9ebRW796Vy7K8= -github.com/jcollie/go-homeassistant v0.0.3/go.mod h1:q15A/ofWWLuq1/kxkKM/V6smX/2GKHOk3PbqE+wc5lY= -github.com/jcollie/go-streamdeck v0.0.0-20200926205347-53a83a6effbd h1:SZA9nLLrf67EbwO3TAUXTogMjAvvwYspT/3psZQzHKs= -github.com/jcollie/go-streamdeck v0.0.0-20200926205347-53a83a6effbd/go.mod h1:5ycbGGE24pJwle/oc17hCMNI+vpbbUsAAcW1DoYn7s8= -github.com/jcollie/go-streamdeck v0.0.1/go.mod h1:5ycbGGE24pJwle/oc17hCMNI+vpbbUsAAcW1DoYn7s8= +github.com/jcollie/go-homeassistant v0.0.4 h1:RpMXV8GLkI3QWHyMdh42l97DqSNgVmjzetHT8QxSQDc= +github.com/jcollie/go-homeassistant v0.0.4/go.mod h1:q15A/ofWWLuq1/kxkKM/V6smX/2GKHOk3PbqE+wc5lY= github.com/jcollie/go-streamdeck v0.0.2 h1:FXKdFvAva0YZsMckMY4hkqmtOR6aG1a1HCVKt/sbFTg= github.com/jcollie/go-streamdeck v0.0.2/go.mod h1:5ycbGGE24pJwle/oc17hCMNI+vpbbUsAAcW1DoYn7s8= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= @@ -140,6 +135,8 @@ github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/micmonay/keybd_event v1.1.0 h1:fQzkqiG/Siwji1Ju9NDkIb8FSFFlPU76YbJntrXdtQw= +github.com/micmonay/keybd_event v1.1.0/go.mod h1:QS2Kfz0PbPezFqMPEot+l/cK78/tHLZtZ7AbYUCRKsQ= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -159,7 +156,6 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -235,7 +231,6 @@ golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm0 golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.0.0-20200430140353-33d19683fad8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.0.0-20200926103441-2fd4ef34c9a9 h1:/kXuZhPKOfy9OvsBtn1A0UI/5lkPZICzxggUl1XWTDg= @@ -265,7 +260,6 @@ golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321 h1:lleNcKRbcaC8MqgLwghIkzZ2JBQAb7QQ9MiwRt1BisA= golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= @@ -289,8 +283,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/internal/macrobutton.go b/internal/macrobutton.go new file mode 100644 index 0000000..717924e --- /dev/null +++ b/internal/macrobutton.go @@ -0,0 +1,187 @@ +package internal + +import ( + "encoding/json" + "log" + "runtime" + "time" + + "github.com/jcollie/go-streamdeck" + "github.com/micmonay/keybd_event" +) + +// MacroButton . +type MacroButton struct { + kb keybd_event.KeyBonding + kbc chan rune + sd *streamdeck.StreamDeck + Button ButtonInfo + Icon StaticIconInfo + Service ServiceInfo +} + +// NewMacroButton . +func NewMacroButton( + sd *streamdeck.StreamDeck, + button ButtonInfo, + icon StaticIconInfo, +) *MacroButton { + b := new(MacroButton) + + b.sd = sd + b.Button = button + b.Icon = icon + myButtons = append(myButtons, b) + sd.SetCallback(b.Button.X, b.Button.Y, b) + return b +} + +// InitializeButton . +func (b *MacroButton) InitializeButton() { + b.sd.FillIcon(b.Button.X, b.Button.Y, + streamdeck.IconButton{ + Icon: b.Icon.Icon, + IconColor: b.Icon.FgColor, + BackgroundColor: b.Icon.BgColor, + }) + kb, err := keybd_event.NewKeyBonding() + if err != nil { + panic(err) + } + b.kb = kb + b.kbc = make(chan rune) + go b.HandlePresses() + +} + +// StateChange . +func (b *MacroButton) StateChange(entityID string, newState string) { + // do nothing +} + +// ButtonPressed . +func (b *MacroButton) ButtonPressed(sd *streamdeck.StreamDeck, x int, y int, timestamp time.Time) { + log.Printf("pressed %d %d %s\n", x, y, timestamp) + b.sd.FillIcon(b.Button.X, b.Button.Y, + streamdeck.IconButton{ + Icon: b.Icon.Icon, + IconColor: b.Icon.PressedFgColor, + BackgroundColor: b.Icon.PressedBgColor, + }, + ) +} + +// ButtonReleased . +func (b *MacroButton) ButtonReleased(sd *streamdeck.StreamDeck, x int, y int, timestamp time.Time) { + log.Printf("released %d %d %s\n", x, y, timestamp) + b.sd.FillIcon(b.Button.X, b.Button.Y, + streamdeck.IconButton{ + Icon: b.Icon.Icon, + IconColor: b.Icon.FgColor, + BackgroundColor: b.Icon.BgColor, + }, + ) + + for _, r := range "/kill @e[type=enderman]" { + b.kbc <- r + } +} + +// HandlePresses . +func (b *MacroButton) HandlePresses() { + // For linux, it is very important to wait 2 seconds + if runtime.GOOS == "linux" { + time.Sleep(2 * time.Second) + } + for r := range b.kbc { + switch r { + case ' ': + b.kb.SetKeys(keybd_event.VK_SPACE) + case '=': + b.kb.SetKeys(keybd_event.VK_EQUAL) + case '@': + b.kb.SetKeys(keybd_event.VK_2) + b.kb.HasSHIFT(true) + case '/': + b.kb.SetKeys(keybd_event.VK_SLASH) + case '[': + b.kb.SetKeys(keybd_event.VK_LEFTBRACE) + case ']': + b.kb.SetKeys(keybd_event.VK_RIGHTBRACE) + case 'a': + b.kb.SetKeys(keybd_event.VK_A) + case 'b': + b.kb.SetKeys(keybd_event.VK_B) + case 'c': + b.kb.SetKeys(keybd_event.VK_C) + case 'd': + b.kb.SetKeys(keybd_event.VK_D) + case 'e': + b.kb.SetKeys(keybd_event.VK_E) + case 'f': + b.kb.SetKeys(keybd_event.VK_F) + case 'g': + b.kb.SetKeys(keybd_event.VK_G) + case 'h': + b.kb.SetKeys(keybd_event.VK_H) + case 'i': + b.kb.SetKeys(keybd_event.VK_I) + case 'j': + b.kb.SetKeys(keybd_event.VK_J) + case 'k': + b.kb.SetKeys(keybd_event.VK_K) + case 'l': + b.kb.SetKeys(keybd_event.VK_L) + case 'm': + b.kb.SetKeys(keybd_event.VK_M) + case 'n': + b.kb.SetKeys(keybd_event.VK_N) + case 'o': + b.kb.SetKeys(keybd_event.VK_O) + case 'p': + b.kb.SetKeys(keybd_event.VK_P) + case 'q': + b.kb.SetKeys(keybd_event.VK_Q) + case 'r': + b.kb.SetKeys(keybd_event.VK_R) + case 's': + b.kb.SetKeys(keybd_event.VK_S) + case 't': + b.kb.SetKeys(keybd_event.VK_T) + case 'u': + b.kb.SetKeys(keybd_event.VK_U) + case 'v': + b.kb.SetKeys(keybd_event.VK_V) + case 'w': + b.kb.SetKeys(keybd_event.VK_W) + case 'x': + b.kb.SetKeys(keybd_event.VK_X) + case 'y': + b.kb.SetKeys(keybd_event.VK_Y) + case 'z': + b.kb.SetKeys(keybd_event.VK_Z) + } + err := b.kb.Press() + if err != nil { + log.Printf("error sending keyboard event: %+v", err) + } + time.Sleep(10 * time.Millisecond) + err = b.kb.Release() + if err != nil { + log.Printf("error sending keyboard event: %+v", err) + } + b.kb.Clear() + time.Sleep(30 * time.Millisecond) + } +} + +// HandleResult . +func (b *MacroButton) HandleResult(id uint64, success bool, result json.RawMessage) { + log.Printf("result: %v %s\n", success, string(result)) + +} + +// HandleClose . +func (b *MacroButton) HandleClose(id uint64) { + log.Printf("home assistant closed") +}