-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5feddea
commit 286e910
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |