1
0

inital commit

This commit is contained in:
2023-10-15 19:12:06 +02:00
commit 873ecb87db
7 changed files with 137 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
GoMqttIR_*

33
Makefile Normal file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/make
.DEFAULT_GOAL := list
list: #Shows this list
@echo The following options are available
@grep '^[^#[:space:]].*:' Makefile | grep -v := | grep \# | sed -e "s/:[^|]*#/: /g"
all: prepair amd64_build arm64_build arm_build #Build for all targets (excludes native)
native: prepair native_build #Build for "Native" target
amd64: prepair amd64_build #Build for amd64 target
arm64: prepair arm64_build #Build for arm64 target
arm: prepair arm_build #Build for arm target
prepair:
go get
go install
native_build:
go build -o GoMqttIR_Native
amd64_build:
env GOARCH=amd64 go build -o GoMqttIR_amd64
arm64_build:
env GOARCH=arm64 go build -o GoMqttIR_arm64
arm_build:
env GOARCH=arm go build -o GoMqttIR_arm
clean: # Run cleanup
go clean
rm -f GoMqttIR_Native GoMqttIR_amd64 GoMqttIR_arm64 GoMqttIR_arm

3
Readme.md Normal file
View File

@@ -0,0 +1,3 @@
# GoMqttIR
an ir-ctl command wrapper to bridge mqtt with an ir blaster written in golang

8
go.mod Normal file
View File

@@ -0,0 +1,8 @@
module brammp.nl/GoMqttIR
go 1.21.1
require github.com/eclipse/paho.mqtt.golang v1.4.3
require github.com/gorilla/websocket v1.5.0
require golang.org/x/sync v0.4.0
require golang.org/x/net v0.17.0

8
go.sum Normal file
View File

@@ -0,0 +1,8 @@
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=

73
main.go Normal file
View File

@@ -0,0 +1,73 @@
package main
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"time"
"os/exec"
"os"
"flag"
)
//mqtt Event listener
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
RecievedPayload := string(msg.Payload())
cmd := exec.Command("ir-ctl","-S" + RecievedPayload)
out, err := cmd.Output()
if err != nil {
fmt.Println("could not run command: ", err)
}
fmt.Println("Output: ", string(out))
}
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
fmt.Println("Connected")
}
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
fmt.Printf("Connect lost: %v", err)
}
func main() {
//Set variables
var DeviceName string
var broker = "192.168.1.4"
var port = 1883
var CoreTopic = "KodiIR"
//Get the hostname as sane default for mqtt topic/device id
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
//innput flag -DeviceName
flag.StringVar(&DeviceName, "DeviceName", hostname, "The name that will be used as the topic an mqtt DeviceID")
flag.Parse()
//Set Mqtt Client options
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port))
opts.SetClientID(DeviceName)
opts.SetDefaultPublishHandler(messagePubHandler)
opts.SetWill(CoreTopic + "/" + DeviceName + "/status", "Offline", 0, true)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
//Subscribe to command toptic
client.Subscribe(CoreTopic + "/" + DeviceName + "/cmd", 1, nil).Wait()
//Publish connect msg
client.Publish(CoreTopic + "/" + DeviceName + "/status", 0, true, "Online")
//Print status
fmt.Printf("Mqtt subscribed")
//Loop so the program keeps running
for {
time.Sleep(time.Hour)
}
//Publish disconnect msg
client.Publish(CoreTopic + "/" + DeviceName + "/status", 0, true, "Offline")
client.Disconnect(250)
}

11
mqttir.service Normal file
View File

@@ -0,0 +1,11 @@
[Unit]
Description=IR over MQTT controller
After=multi-user.target
[Service]
Type=simple
Restart=always
ExecStart=/opt/GoMqttIR_<ARCH> <DeviceName>
[Install]
WantedBy=multi-user.target