Skip to content

Commit

Permalink
add ir control
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilGruber committed Jul 1, 2024
1 parent 5feddea commit 286e910
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions devices/ir-control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package devices

import (
"encoding/json"
"github.com/PhilGruber/dimmy/core"
mqtt "github.com/eclipse/paho.mqtt.golang"
"log"
)

type IRControl struct {
Device

commands map[string]string
nextRequest *IrControlMessage
}

type IrControlMessage struct {
IrCode string `json:"ir_code_to_send"`
}

func makeIrcontrol(config core.DeviceConfig) IRControl {
i := IRControl{}
i.Emoji = "📡"
i.setBaseConfig(config)

i.Type = "IRControl"
i.commands = *config.Options.Commands

log.Printf("IRControl device %s created with commands: %s\n", i.Name, i.GetCommands())

return i
}

func NewIrControl(config core.DeviceConfig) *IRControl {
i := makeIrcontrol(config)
return &i
}

func (i *IRControl) ProcessRequest(request core.SwitchRequest) {
log.Printf("Processing request for device %s: %v\n", i.Name, request.Value)
command, ok := i.commands[request.Value]
if !ok {
log.Printf("Device %s does not support command %s. Please define this in config file.\n", i.Name, request.Command)
return
}
req := IrControlMessage{IrCode: command}
i.nextRequest = &req
}

func (i *IRControl) PublishValue(mqtt mqtt.Client) {
if i.nextRequest == nil {
return
}
s, _ := json.Marshal(i.nextRequest)
mqtt.Publish(i.MqttTopic, 0, false, s)
i.nextRequest = nil
}

func (i *IRControl) GetMax() int {
return 1
}
func (i *IRControl) GetMin() int {
return 1
}

func (i *IRControl) UpdateValue() (float64, bool) {
return 0, i.nextRequest != nil
}

func (i *IRControl) GetCommands() []string {
var commands []string
for k, _ := range i.commands {
commands = append(commands, k)
}
return commands
}

0 comments on commit 286e910

Please sign in to comment.